Socket
Socket
Sign inDemoInstall

level-ttl

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

level-ttl - npm Package Compare versions

Comparing version 3.0.2 to 3.0.3

runner.js

2

package.json

@@ -11,3 +11,3 @@ {

],
"version": "3.0.2",
"version": "3.0.3",
"homepage": "https://github.com/rvagg/node-level-ttl",

@@ -14,0 +14,0 @@ "authors": [

@@ -32,3 +32,3 @@ const tape = require('tape')

return t.fail(err)
callback(null, arr)
callback(arr)
}))

@@ -57,10 +57,14 @@ }

}
for (var i = 0; i < length; ++i) {
batch.push({ type: 'put', key: randomize(), value: randomize() })
}
return batch
}
function verifyIn (delay, createReadStream, t, cb, opts) {
setTimeout(function () {
db2arr(createReadStream, t, cb, opts)
}, delay)
}
test('single ttl entry', function (t, db) {

@@ -75,12 +79,5 @@ t.throws(db.put.bind(db), { name: 'WriteError', message: 'put() requires key and value arguments' })

t.notOk(err, 'no error')
var base = Date.now() // *should* be able to catch it to the ms
db.put('bar', 'barvalue', { ttl: 100 }, function (err) {
t.notOk(err, 'no error')
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
var ts = base + 100
// allow 1ms leeway
if (arr[3] && arr[3].value != String(ts))
ts++
db2arr(createReadStream, t, function (arr) {
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')

@@ -90,12 +87,8 @@ contains(t, arr, '!ttl!bar', /\d{13}/)

contains(t, arr, 'foo', 'foovalue')
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
t.deepEqual(arr, [
{ key: 'foo', value: 'foovalue' }
])
t.end()
})
}, 150)
verifyIn(150, createReadStream, t, function (arr) {
t.deepEqual(arr, [
{ key: 'foo', value: 'foovalue' }
])
t.end()
})
})

@@ -107,25 +100,23 @@ })

test('multiple ttl entries with put', function (t, db, createReadStream) {
var expect = function (delay, keys) {
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
t.equal(arr.length, 1 + keys * 3, 'correct number of entries in db')
contains(t, arr, 'afoo', 'foovalue')
if (keys >= 1) {
contains(t, arr, 'bar1', 'barvalue1')
contains(t, arr, /^!ttl!x!\d{13}!bar1$/, 'bar1')
contains(t, arr, '!ttl!bar1', /^\d{13}$/)
}
if (keys >= 2) {
contains(t, arr, 'bar2', 'barvalue2')
contains(t, arr, /^!ttl!x!\d{13}!bar2$/, 'bar2')
contains(t, arr, '!ttl!bar2', /^\d{13}$/)
}
if (keys >= 3) {
contains(t, arr, 'bar3', 'barvalue3')
contains(t, arr, /^!ttl!x!\d{13}!bar3$/, 'bar3')
contains(t, arr, '!ttl!bar3', /^\d{13}$/)
}
})
}, delay)
var expect = function (delay, keys, cb) {
verifyIn(delay, createReadStream, t, function (arr) {
t.equal(arr.length, 1 + keys * 3, 'correct number of entries in db')
contains(t, arr, 'afoo', 'foovalue')
if (keys >= 1) {
contains(t, arr, 'bar1', 'barvalue1')
contains(t, arr, /^!ttl!x!\d{13}!bar1$/, 'bar1')
contains(t, arr, '!ttl!bar1', /^\d{13}$/)
}
if (keys >= 2) {
contains(t, arr, 'bar2', 'barvalue2')
contains(t, arr, /^!ttl!x!\d{13}!bar2$/, 'bar2')
contains(t, arr, '!ttl!bar2', /^\d{13}$/)
}
if (keys >= 3) {
contains(t, arr, 'bar3', 'barvalue3')
contains(t, arr, /^!ttl!x!\d{13}!bar3$/, 'bar3')
contains(t, arr, '!ttl!bar3', /^\d{13}$/)
}
cb && cb()
})
}

@@ -139,38 +130,34 @@

expect(25, 3)
expect(150, 2)
expect(300, 1)
expect(450, 0)
setTimeout(t.end.bind(t), 600)
expect(200, 2)
expect(350, 1)
expect(500, 0, t.end.bind(t))
})
test('multiple ttl entries with batch-put', function (t, db, createReadStream) {
var expect = function (delay, keys) {
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
t.equal(arr.length, 1 + keys * 3, 'correct number of entries in db')
contains(t, arr, 'afoo', 'foovalue')
if (keys >= 1) {
contains(t, arr, 'bar1', 'barvalue1')
contains(t, arr, /^!ttl!x!\d{13}!bar1$/, 'bar1')
contains(t, arr, '!ttl!bar1', /^\d{13}$/)
}
if (keys >= 2) {
contains(t, arr, 'bar2', 'barvalue2')
contains(t, arr, /^!ttl!x!\d{13}!bar2$/, 'bar2')
contains(t, arr, '!ttl!bar2', /^\d{13}$/)
}
if (keys >= 3) {
contains(t, arr, 'bar3', 'barvalue3')
contains(t, arr, /^!ttl!x!\d{13}!bar3$/, 'bar3')
contains(t, arr, '!ttl!bar3', /^\d{13}$/)
}
if (keys >= 3) {
contains(t, arr, 'bar4', 'barvalue4')
contains(t, arr, /^!ttl!x!\d{13}!bar4$/, 'bar4')
contains(t, arr, '!ttl!bar4', /^\d{13}$/)
}
})
}, delay)
var expect = function (delay, keys, cb) {
verifyIn(delay, createReadStream, t, function (arr) {
t.equal(arr.length, 1 + keys * 3, 'correct number of entries in db')
contains(t, arr, 'afoo', 'foovalue')
if (keys >= 1) {
contains(t, arr, 'bar1', 'barvalue1')
contains(t, arr, /^!ttl!x!\d{13}!bar1$/, 'bar1')
contains(t, arr, '!ttl!bar1', /^\d{13}$/)
}
if (keys >= 2) {
contains(t, arr, 'bar2', 'barvalue2')
contains(t, arr, /^!ttl!x!\d{13}!bar2$/, 'bar2')
contains(t, arr, '!ttl!bar2', /^\d{13}$/)
}
if (keys >= 3) {
contains(t, arr, 'bar3', 'barvalue3')
contains(t, arr, /^!ttl!x!\d{13}!bar3$/, 'bar3')
contains(t, arr, '!ttl!bar3', /^\d{13}$/)
}
if (keys >= 3) {
contains(t, arr, 'bar4', 'barvalue4')
contains(t, arr, /^!ttl!x!\d{13}!bar4$/, 'bar4')
contains(t, arr, '!ttl!bar4', /^\d{13}$/)
}
cb && cb()
})
}

@@ -188,62 +175,31 @@

expect(20, 4)
setTimeout(t.end.bind(t), 275)
expect(20, 4, t.end.bind(t))
})
test('prolong entry life with additional put', function (t, db, createReadStream) {
var putBar = function () {
db.put('bar', 'barvalue', { ttl: 250 })
return Date.now()
}
, verify = function (base, delay) {
var retest = function (delay, cb) {
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
var ts = base + 37
, i = 0
// allow +/- 3ms leeway, allow for processing speed and Node timer inaccuracy
for (; i < 6 && arr[3] && arr[3].value; i++) {
if (arr[3] && arr[3].value == String(ts))
break
ts++
}
db.put('bar', 'barvalue', { ttl: 250 })
verifyIn(50, createReadStream, t, function (arr) {
contains(t, arr, 'foo', 'foovalue')
contains(t, arr, 'bar', 'barvalue')
contains(t, arr, 'foo', 'foovalue')
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')
contains(t, arr, '!ttl!bar', /\d{13}/)
cb && cb()
})
}, delay)
}
, retest = function (delay) {
setTimeout(function () {
var base = putBar()
verify(base, 50)
}, delay)
}
, i
db.put('foo', 'foovalue')
for (i = 0; i < 200; i += 20)
for (i = 0; i < 180; i += 20)
retest(i)
setTimeout(t.end.bind(t), 300)
retest(180, t.end.bind(t))
})
test('prolong entry life with ttl(key, ttl)', function (t, db, createReadStream) {
var ttlBar = function () {
db.ttl('bar', 250)
return Date.now()
}
, verify = function (base, delay) {
var retest = function (delay, cb) {
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
var ts = base + 37
, i = 0
// allow +/- 3ms leeway, allow for processing speed and Node timer inaccuracy
for (; i < 6 && arr[3] && arr[3].value; i++) {
if (arr[3] && arr[3].value == String(ts))
break
ts++
}
db.ttl('bar', 250)
verifyIn(25, createReadStream, t, function (arr) {
contains(t, arr, 'bar', 'barvalue')

@@ -253,11 +209,6 @@ contains(t, arr, 'foo', 'foovalue')

contains(t, arr, '!ttl!bar', /\d{13}/)
cb && cb()
})
}, delay)
}
, retest = function (delay) {
setTimeout(function () {
var base = ttlBar()
verify(base, 25)
}, delay)
}
, i

@@ -267,87 +218,52 @@

db.put('bar', 'barvalue')
for (i = 0; i < 200; i += 20)
for (i = 0; i < 180; i += 20)
retest(i)
setTimeout(t.end.bind(t), 300)
retest(180, t.end.bind(t))
})
test('del removes both key and its ttl meta data', function (t, db, createReadStream) {
var verify = function (base, delay) {
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
if (base == -1) {
// test complete deletion
t.deepEqual(arr, [
{ key: 'foo', value: 'foovalue' }
])
} else {
var ts = base + 197
, i = 0
// allow +/- 3ms leeway, allow for processing speed and Node timer inaccuracy
for (; i < 6 && arr[3] && arr[3].value; i++) {
if (arr[3] && arr[3].value == String(ts))
break
ts++
}
contains(t, arr, 'foo', 'foovalue')
contains(t, arr, 'bar', 'barvalue')
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')
contains(t, arr, '!ttl!bar', /\d{13}/)
}
})
}, delay)
}
, base
db.put('foo', 'foovalue')
base = Date.now()
db.put('bar', 'barvalue', { ttl: 250 })
verify(base, 150)
verifyIn(150, createReadStream, t, function (arr) {
contains(t, arr, 'foo', 'foovalue')
contains(t, arr, 'bar', 'barvalue')
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')
contains(t, arr, '!ttl!bar', /\d{13}/)
})
setTimeout(function () {
db.del('bar')
}, 250)
// should not exist at all by 70
verify(-1, 350)
setTimeout(t.end.bind(t), 550)
verifyIn(350, createReadStream, t, function (arr) {
t.deepEqual(arr, [
{ key: 'foo', value: 'foovalue' }
])
t.end()
})
})
test('del removes both key and its ttl meta data (value encoding)', function (t, db, createReadStream) {
var verify = function (base, delay) {
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
if (base == -1) {
// test complete deletion
t.deepEqual(arr, [
{ key: 'foo', value: '{"v":"foovalue"}' }
])
} else {
var ts = base + 197
, i = 0
// allow +/- 10ms leeway, allow for processing speed and Node timer inaccuracy
for (; i < 10 && arr[3] && arr[3].value; i++) {
if (arr[3] && arr[3].value == String(ts))
break
ts++
}
contains(t, arr, 'foo', '{"v":"foovalue"}')
contains(t, arr, 'bar', '{"v":"barvalue"}')
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')
contains(t, arr, '!ttl!bar', /\d{13}/)
}
}, { valueEncoding: 'utf8' })
}, delay)
}
, base
db.put( 'foo', { v: 'foovalue' })
base = Date.now()
db.put('bar', { v: 'barvalue' }, { ttl: 250 })
verify(base, 50)
verifyIn(50, createReadStream, t, function (arr) {
contains(t, arr, 'foo', '{"v":"foovalue"}')
contains(t, arr, 'bar', '{"v":"barvalue"}')
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')
contains(t, arr, '!ttl!bar', /\d{13}/)
}, { valueEncoding: 'utf8' })
setTimeout(function () {
db.del('bar')
}, 175)
// should not exist at all by 70
verify(-1, 350)
setTimeout(t.end.bind(t), 550)
verifyIn(350, createReadStream, t, function (arr) {
t.deepEqual(arr, [
{ key: 'foo', value: '{"v":"foovalue"}' }
])
t.end()
}, { valueEncoding: 'utf8' })
}, { keyEncoding: 'utf8', valueEncoding: 'json' })

@@ -383,4 +299,3 @@

db.get('foo', function (err, value) {
t.ok(err, 'got error')
t.ok(err.notFound, 'not found error')
t.ok(err && err.notFound, 'not found error')
t.notOk(value, 'no value')

@@ -406,3 +321,3 @@ })

test('single put with default ttl set', function (t, db, createReadStream) {
db.put('foo', 'bar1', function(err) {
db.put('foo', 'foovalue', function(err) {
t.ok(!err, 'no error')

@@ -413,3 +328,3 @@

t.notOk(err, 'no error')
t.equal('bar1', value)
t.equal('foovalue', value)
})

@@ -420,14 +335,13 @@ }, 50)

db.get('foo', function (err, value) {
t.ok(err, 'got error')
t.ok(err.notFound, 'not found error')
t.ok(err && err.notFound, 'not found error')
t.notOk(value, 'no value')
t.end()
})
}, 125)
}, 175)
})
setTimeout(t.end.bind(t), 175)
}, { defaultTTL: 75 } )
test('single put with overridden ttl set', function (t, db, createReadStream) {
db.put('foo', 'bar1', { ttl: 99 }, function(err) {
db.put('foo', 'foovalue', { ttl: 99 }, function(err) {
t.ok(!err, 'no error')

@@ -437,3 +351,3 @@ setTimeout(function () {

t.notOk(err, 'no error')
t.equal('bar1', value)
t.equal('foovalue', value)
})

@@ -444,10 +358,9 @@ }, 50)

db.get('foo', function (err, value) {
t.ok(err, 'got error')
t.ok(err.notFound, 'not found error')
t.ok(err && err.notFound, 'not found error')
t.notOk(value, 'no value')
t.end()
})
}, 125)
}, 200)
})
setTimeout(t.end.bind(t), 175)
}, { defaultTTL: 75 } )

@@ -457,4 +370,4 @@

db.batch([
{ type: 'put', key: 'foo', value: 'bar1' },
{ type: 'put', key: 'bar', value: 'foo1' }
{ type: 'put', key: 'foo', value: 'foovalue' },
{ type: 'put', key: 'bar', value: 'barvalue' }
], function(err) {

@@ -465,6 +378,6 @@ t.ok(!err, 'no error')

t.notOk(err, 'no error')
t.equal('bar1', value)
t.equal('foovalue', value)
db.get('bar', function(err, value) {
t.notOk(err, 'no error')
t.equal('foo1', value)
t.equal('barvalue', value)
})

@@ -476,15 +389,13 @@ })

db.get('foo', function (err, value) {
t.ok(err, 'got error')
t.ok(err.notFound, 'not found error')
t.ok(err && err.notFound, 'not found error')
t.notOk(value, 'no value')
db.get('bar', function(err, value) {
t.ok(err, 'no error')
t.ok(err.notFound, 'not found error')
t.ok(err && err.notFound, 'not found error')
t.notOk(value, 'no value')
t.end()
})
})
}, 125)
}, 175)
})
setTimeout(t.end.bind(t), 175)
}, { defaultTTL: 75 })

@@ -494,4 +405,4 @@

db.batch([
{ type: 'put', key: 'foo', value: 'bar1' },
{ type: 'put', key: 'bar', value: 'foo1' }
{ type: 'put', key: 'foo', value: 'foovalue' },
{ type: 'put', key: 'bar', value: 'barvalue' }
], { ttl: 99 }, function(err) {

@@ -501,6 +412,6 @@ setTimeout(function () {

t.notOk(err, 'no error')
t.equal('bar1', value)
t.equal('foovalue', value)
db.get('bar', function(err, value) {
t.notOk(err, 'no error')
t.equal('foo1', value)
t.equal('barvalue', value)
})

@@ -512,15 +423,13 @@ })

db.get('foo', function (err, value) {
t.ok(err, 'got error')
t.ok(err.notFound, 'not found error')
t.ok(err && err.notFound, 'not found error')
t.notOk(value, 'no value')
db.get('bar', function(err, value) {
t.ok(err, 'no error')
t.ok(err.notFound, 'not found error')
t.ok(err && err.notFound, 'not found error')
t.notOk(value, 'no value')
t.end()
})
})
}, 125)
}, 200)
})
setTimeout(t.end.bind(t), 175)
}, { defaultTTL: 75 })

@@ -545,4 +454,3 @@

t.ok(!err, 'no error')
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
db2arr(createReadStream, t, function (arr) {
batch.forEach(function (item) {

@@ -564,10 +472,7 @@ contains(t, arr, '!meta!' + item.key, /\d{13}/)

t.ok(!err, 'no error')
setTimeout(function () {
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
t.equal(arr.length, 0, 'should be empty array')
t.end()
})
}, 150)
verifyIn(200, createReadStream, t, function (arr) {
t.equal(arr.length, 0, 'should be empty array')
t.end()
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc