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.1 to 3.0.2

4

package.json

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

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

@@ -37,3 +37,5 @@ "authors": [

"faucet": "0.0.1",
"level-sublevel": "^6.4.6",
"ltest": "2.0.0",
"slump": "^2.0.0",
"tape": ">=2.14.0 <2.15.0-0"

@@ -40,0 +42,0 @@ },

@@ -12,3 +12,3 @@ # Level TTL

Requires [LevelUP](https://github.com/rvagg/node-levelup) (or [Level](https://github.com/level/level)) to be installed separately.
Requires [LevelUP](https://github.com/rvagg/node-levelup), [Level](https://github.com/level/level) or [level-hyper](https://github.com/Level/level-hyper) to be installed separately.

@@ -73,2 +73,35 @@ ***Note 1: Version 1.0.0 data stores are not backward compatible with previous versions. If you have unexpired entries in a data store managed by pre-1.0.0, don't expect them to expire if you upgrade to 1.0.0+.*** *This is due to a level-sublevel change. It is also recommended that you only use level-sublevel 6.0.0+ with level-ttl.*

### `opts.sub`
You can provide a custom storage for the meta data by using the `opts.sub` property. If it's set, that storage will contain all the ttl meta data. A use case for this would be to avoid mixing data and meta data in the same keyspace, since if it's not set, all data will be sharing the same keyspace.
A db for the data and a separate to store the meta data:
```js
var level = require('level')
, ttl = require('level-ttl')
, meta = level('./meta')
, db = ttl(level('./db'), { sub: meta })
, batch = [
{ type: 'put', key: 'foo', value: 'foovalue' }
, { type: 'put', key: 'bar', value: 'barvalue' }
]
db.batch(batch, { ttl: 100 }, function (err) {
db.createReadStream()
.on('data', function (data) {
console.log('data', data)
})
.on('end', function () {
meta.createReadStream()
.on('data', function (data) {
console.log('meta', data)
})
})
})
```
For more examples on this please check the tests involving `level-sublevel`.
### Shutting down

@@ -75,0 +108,0 @@

@@ -6,2 +6,4 @@ const tape = require('tape')

, xtend = require('xtend')
, sublevel = require('level-sublevel')
, random = require('slump')

@@ -50,5 +52,16 @@ function fixtape (t) {

// test that the standard API is working as it should
// kind of a lame test but we know they should throw
false && test('test single ttl entry', function (db, t) {
function randomPutBatch (length) {
var batch = []
, randomize = function () {
return random.string({ enc: 'base58', length: 10 })
}
for (var i = 0; i < length; ++i) {
batch.push({ type: 'put', key: randomize(), value: randomize() })
}
return batch
}
test('single ttl entry', function (t, db) {
t.throws(db.put.bind(db), { name: 'WriteError', message: 'put() requires key and value arguments' })

@@ -59,3 +72,3 @@ t.throws(db.del.bind(db), { name: 'WriteError', message: 'del() requires a key argument' })

test('test single ttl entry with put', function (t, db, createReadStream) {
test('single ttl entry with put', function (t, db, createReadStream) {
db.put('foo', 'foovalue', function (err) {

@@ -92,3 +105,3 @@ t.notOk(err, 'no error')

test('test multiple ttl entries with put', function (t, db, createReadStream) {
test('multiple ttl entries with put', function (t, db, createReadStream) {
var expect = function (delay, keys) {

@@ -132,3 +145,3 @@ setTimeout(function () {

test('test multiple ttl entries with batch-put', function (t, db, createReadStream) {
test('multiple ttl entries with batch-put', function (t, db, createReadStream) {
var expect = function (delay, keys) {

@@ -179,3 +192,3 @@ setTimeout(function () {

test('test prolong entry life with additional put', function (t, db, createReadStream) {
test('prolong entry life with additional put', function (t, db, createReadStream) {
var putBar = function () {

@@ -218,3 +231,3 @@ db.put('bar', 'barvalue', { ttl: 250 })

test('test prolong entry life with ttl(key, ttl)', function (t, db, createReadStream) {
test('prolong entry life with ttl(key, ttl)', function (t, db, createReadStream) {
var ttlBar = function () {

@@ -258,3 +271,3 @@ db.ttl('bar', 250)

test('test del', function (t, db, createReadStream) {
test('del removes both key and its ttl meta data', function (t, db, createReadStream) {
var verify = function (base, delay) {

@@ -278,4 +291,4 @@ setTimeout(function () {

}
contains(t, arr, 'foo', 'foovalue')
contains(t, arr, 'bar', 'barvalue')
contains(t, arr, 'foo', 'foovalue')
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')

@@ -301,3 +314,3 @@ contains(t, arr, '!ttl!bar', /\d{13}/)

test('test del with db value encoding', function (t, db, createReadStream) {
test('del removes both key and its ttl meta data (value encoding)', function (t, db, createReadStream) {
var verify = function (base, delay) {

@@ -321,4 +334,4 @@ setTimeout(function () {

}
contains(t, arr, 'foo', '{"v":"foovalue"}')
contains(t, arr, 'bar', '{"v":"barvalue"}')
contains(t, arr, 'foo', '{"v":"foovalue"}')
contains(t, arr, /!ttl!x!\d{13}!bar/, 'bar')

@@ -360,6 +373,5 @@ contains(t, arr, '!ttl!bar', /\d{13}/)

test('test stop() method stops interval and doesn\'t hold process up', function (t, db, createReadStream, close) {
t.equals(intervals, 1, '1 interval timer')
db.put( 'foo', 'bar1', { ttl: 25 })
db.put( 'foo', 'bar1', { ttl: 25 })
setTimeout(function () {

@@ -371,2 +383,3 @@ db.get('foo', function (err, value) {

}, 40)
setTimeout(function () {

@@ -379,2 +392,3 @@ db.get('foo', function (err, value) {

}, 80)
setTimeout(function () {

@@ -390,5 +404,3 @@ db.stop(function () {

}, 120)
})
}

@@ -408,2 +420,3 @@

}, 50)
setTimeout(function () {

@@ -424,3 +437,2 @@ db.get('foo', function (err, value) {

t.ok(!err, 'no error')
setTimeout(function () {

@@ -432,2 +444,3 @@ db.get('foo', function (err, value) {

}, 50)
setTimeout(function () {

@@ -451,3 +464,2 @@ db.get('foo', function (err, value) {

t.ok(!err, 'no error')
setTimeout(function () {

@@ -463,2 +475,3 @@ db.get('foo', function (err, value) {

}, 50)
setTimeout(function () {

@@ -496,2 +509,3 @@ db.get('foo', function (err, value) {

}, 50)
setTimeout(function () {

@@ -522,1 +536,37 @@ db.get('foo', function (err, value) {

})
ltest('data and level-sublevel ttl meta data separation', function (t, db, createReadStream) {
var subDb = sublevel(db)
, meta = subDb.sublevel('meta')
, ttldb = ttl(db, { sub: meta })
, batch = randomPutBatch(5)
ttldb.batch(batch, { ttl: 10000 }, function (err) {
t.ok(!err, 'no error')
db2arr(createReadStream, t, function (err, arr) {
t.notOk(err, 'no error')
batch.forEach(function (item) {
contains(t, arr, '!meta!' + item.key, /\d{13}/)
contains(t, arr, new RegExp("!meta!x!\\d{13}!" + item.key), item.key)
})
t.end()
})
})
})
ltest('that level-sublevel data expires properly', function (t, db, createReadStream) {
var subDb = sublevel(db)
, meta = subDb.sublevel('meta')
, ttldb = ttl(db, { checkFrequency: 50, sub: meta })
ttldb.batch(randomPutBatch(50), { ttl: 100 }, function (err) {
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)
})
})
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