level-iterator-stream
Advanced tools
Comparing version 1.3.1 to 2.0.0
@@ -1,9 +0,10 @@ | ||
var iteratorStream = require('./'); | ||
var leveldown = require('leveldown'); | ||
var iteratorStream = require('./') | ||
var leveldown = require('leveldown') | ||
var path = require('path') | ||
var db = leveldown(__dirname + '/db'); | ||
db.open(function(err){ | ||
if (err) throw err; | ||
var db = leveldown(path.join(__dirname, 'db')) | ||
db.open(function (err) { | ||
if (err) throw err | ||
var ops = []; | ||
var ops = [] | ||
for (var i = 0; i < 1000; i++) { | ||
@@ -14,13 +15,13 @@ ops.push({ | ||
value: String(Math.random()) | ||
}); | ||
}) | ||
} | ||
db.batch(ops, function(err){ | ||
if (err) throw err; | ||
db.batch(ops, function (err) { | ||
if (err) throw err | ||
var stream = iteratorStream(db.iterator()); | ||
stream.on('data', function(kv){ | ||
console.log('%s -> %s', kv.key, kv.value); | ||
}); | ||
}); | ||
}); | ||
var stream = iteratorStream(db.iterator()) | ||
stream.on('data', function (kv) { | ||
console.log('%s -> %s', kv.key, kv.value) | ||
}) | ||
}) | ||
}) |
80
index.js
@@ -1,56 +0,50 @@ | ||
var inherits = require('inherits'); | ||
var Readable = require('readable-stream').Readable; | ||
var extend = require('xtend'); | ||
var EncodingError = require('level-errors').EncodingError; | ||
var inherits = require('inherits') | ||
var Readable = require('readable-stream').Readable | ||
var extend = require('xtend') | ||
module.exports = ReadStream; | ||
inherits(ReadStream, Readable); | ||
module.exports = ReadStream | ||
inherits(ReadStream, Readable) | ||
function ReadStream(iterator, options){ | ||
if (!(this instanceof ReadStream)) return new ReadStream(iterator, options); | ||
function ReadStream (iterator, options) { | ||
if (!(this instanceof ReadStream)) return new ReadStream(iterator, options) | ||
options = options || {} | ||
Readable.call(this, extend(options, { | ||
objectMode: true | ||
})); | ||
this._iterator = iterator; | ||
this._destroyed = false; | ||
this._decoder = null; | ||
if (options && options.decoder) this._decoder = options.decoder; | ||
this.on('end', this._cleanup.bind(this)); | ||
})) | ||
this._iterator = iterator | ||
this._destroyed = false | ||
this._options = options | ||
this.on('end', this._cleanup.bind(this)) | ||
} | ||
ReadStream.prototype._read = function(){ | ||
var self = this; | ||
if (this._destroyed) return; | ||
ReadStream.prototype._read = function () { | ||
var self = this | ||
var options = this._options | ||
if (this._destroyed) return | ||
this._iterator.next(function(err, key, value){ | ||
if (self._destroyed) return; | ||
if (err) return self.emit('error', err); | ||
this._iterator.next(function (err, key, value) { | ||
if (self._destroyed) return | ||
if (err) return self.emit('error', err) | ||
if (key === undefined && value === undefined) { | ||
self.push(null); | ||
self.push(null) | ||
} else if (options.keys !== false && options.values === false) { | ||
self.push(key) | ||
} else if (options.keys === false && options.values !== false) { | ||
self.push(value) | ||
} else { | ||
if (!self._decoder) return self.push({ key: key, value: value }); | ||
try { | ||
var value = self._decoder(key, value); | ||
} catch (err) { | ||
self.emit('error', new EncodingError(err)); | ||
self.push(null); | ||
return; | ||
} | ||
self.push(value); | ||
self.push({ key: key, value: value }) | ||
} | ||
}); | ||
}; | ||
}) | ||
} | ||
ReadStream.prototype.destroy = | ||
ReadStream.prototype._cleanup = function(){ | ||
var self = this; | ||
if (this._destroyed) return; | ||
this._destroyed = true; | ||
ReadStream.prototype._cleanup = function () { | ||
var self = this | ||
if (this._destroyed) return | ||
this._destroyed = true | ||
this._iterator.end(function(err){ | ||
if (err) return self.emit('error', err); | ||
self.emit('close'); | ||
}); | ||
}; | ||
this._iterator.end(function (err) { | ||
if (err) return self.emit('error', err) | ||
self.emit('close') | ||
}) | ||
} |
The MIT License (MIT) | ||
===================== | ||
Copyright (c) 2012-2015 LevelUP contributors | ||
Copyright (c) 2012-2017 LevelUP contributors | ||
--------------------------------------- | ||
@@ -6,0 +6,0 @@ |
{ | ||
"name": "level-iterator-stream", | ||
"version": "1.3.1", | ||
"version": "2.0.0", | ||
"description": "Turn a leveldown iterator into a readable stream", | ||
"scripts": { | ||
"test": "make test" | ||
"test": "standard && node test.js" | ||
}, | ||
@@ -11,14 +11,12 @@ "repository": "Level/iterator-stream", | ||
"inherits": "^2.0.1", | ||
"level-errors": "^1.0.3", | ||
"readable-stream": "^1.0.33", | ||
"readable-stream": "^2.0.5", | ||
"xtend": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"abstract-leveldown": "^2.1.0", | ||
"level-codec": "^6.0.0", | ||
"leveldown": "^0.10.4", | ||
"tape": "^3.5.0", | ||
"through2": "^0.6.3" | ||
"leveldown": "^1.4.1", | ||
"standard": "^10.0.3", | ||
"tape": "^4.4.0", | ||
"through2": "^2.0.0" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -6,5 +6,5 @@ | ||
**Turn a leveldown iterator into a readable stream** | ||
> Turn a leveldown iterator into a readable stream | ||
[![Build Status](https://travis-ci.org/Level/iterator-stream.png)](https://travis-ci.org/Level/iterator-stream) | ||
[![Build Status](https://travis-ci.org/Level/iterator-stream.png)](https://travis-ci.org/Level/iterator-stream) [![Greenkeeper badge](https://badges.greenkeeper.io/Level/iterator-stream.svg)](https://greenkeeper.io/) | ||
@@ -14,14 +14,14 @@ ## Example | ||
```js | ||
var iteratorStream = require('level-iterator-stream'); | ||
var leveldown = require('leveldown'); | ||
const iteratorStream = require('level-iterator-stream') | ||
const leveldown = require('leveldown') | ||
var db = leveldown(__dirname + '/db'); | ||
db.open(function(err){ | ||
if (err) throw err; | ||
const db = leveldown(__dirname + '/db') | ||
db.open(function (err) { | ||
if (err) throw err | ||
var stream = iteratorStream(db.iterator()); | ||
stream.on('data', function(kv){ | ||
console.log('%s -> %s', kv.key, kv.value); | ||
}); | ||
}); | ||
const stream = iteratorStream(db.iterator()) | ||
stream.on('data', function (kv) { | ||
console.log('%s -> %s', kv.key, kv.value) | ||
}) | ||
}) | ||
``` | ||
@@ -43,4 +43,3 @@ | ||
If `options.decoder` is passed, each key/value pair will be transformed by it. | ||
Otherwise, an object with `{ key, value }` will be emitted. | ||
Set `options.keys` or `options.values` to `false` to only get values / keys. Otherwise receive `{ key, value }` objects. | ||
@@ -55,7 +54,8 @@ When the stream ends, the `iterator` will be closed and afterwards a | ||
* [@juliangruber](https://github.com/juliangruber) | ||
* [@ralphtheninja](https://github.com/ralphtheninja) | ||
## License & copyright | ||
Copyright (c) 2012-2015 LevelUP contributors. | ||
Copyright (c) 2012-2017 LevelUP contributors. | ||
LevelUP is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details. |
195
test.js
@@ -1,10 +0,8 @@ | ||
var test = require('tape'); | ||
var leveldown = require('leveldown'); | ||
var iteratorStream = require('./'); | ||
var through2 = require('through2'); | ||
var abstract = require('abstract-leveldown'); | ||
var Codec = require('level-codec'); | ||
var EncodingError = require('level-errors').EncodingError; | ||
var test = require('tape') | ||
var path = require('path') | ||
var leveldown = require('leveldown') | ||
var iteratorStream = require('./') | ||
var through2 = require('through2') | ||
var db; | ||
var db | ||
var data = [ | ||
@@ -14,64 +12,129 @@ { type: 'put', key: 'foobatch1', value: 'bar1' }, | ||
{ type: 'put', key: 'foobatch3', value: 'bar3' } | ||
]; | ||
] | ||
test('setup', function(t){ | ||
db = leveldown(__dirname + '/db-test'); | ||
db.open(function(err){ | ||
t.error(err); | ||
db.batch(data, function(err){ | ||
t.error(err); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
test('setup', function (t) { | ||
db = leveldown(path.join(__dirname, 'db-test')) | ||
db.open(function (err) { | ||
t.error(err, 'no error') | ||
db.batch(data, function (err) { | ||
t.error(err, 'no error') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
test('simple', function(t){ | ||
var idx = 0; | ||
var stream = iteratorStream(db.iterator()); | ||
stream.pipe(through2.obj(function(kv, _, done){ | ||
t.ok(Buffer.isBuffer(kv.key)); | ||
t.ok(Buffer.isBuffer(kv.value)); | ||
t.equal(kv.key.toString(), data[idx].key); | ||
t.equal(kv.value.toString(), data[idx].value); | ||
idx++; | ||
done(); | ||
}, function(){ | ||
t.equal(idx, data.length); | ||
stream.on('close', function(){ | ||
t.end(); | ||
}); | ||
})); | ||
}); | ||
test('keys and values', function (t) { | ||
var idx = 0 | ||
var stream = iteratorStream(db.iterator()) | ||
stream.pipe(through2.obj(function (kv, _, done) { | ||
t.ok(Buffer.isBuffer(kv.key)) | ||
t.ok(Buffer.isBuffer(kv.value)) | ||
t.equal(kv.key.toString(), data[idx].key) | ||
t.equal(kv.value.toString(), data[idx].value) | ||
idx++ | ||
done() | ||
}, function () { | ||
t.equal(idx, data.length) | ||
stream.on('close', function () { | ||
t.end() | ||
}) | ||
})) | ||
}) | ||
test('destroy', function(t){ | ||
var stream = iteratorStream(db.iterator()); | ||
stream.on('close', t.end.bind(t)); | ||
stream.destroy(); | ||
}); | ||
test('.destroy closes the stream', function (t) { | ||
var stream = iteratorStream(db.iterator()) | ||
stream.on('close', t.end.bind(t)) | ||
stream.destroy() | ||
}) | ||
test('decoder', function(t){ | ||
var codec = new Codec({ valueEncoding: 'binary' }); | ||
var stream = iteratorStream(db.iterator(), { | ||
decoder: codec.createStreamDecoder({ values: true }) | ||
}); | ||
stream.once('data', function(value){ | ||
t.ok(Buffer.isBuffer(value)); | ||
t.equal(value.toString(), 'bar1'); | ||
t.end(); | ||
}); | ||
}); | ||
test('.destroy during iterator.next 1', function (t) { | ||
var stream | ||
var iterator = db.iterator() | ||
var next = iterator.next.bind(iterator) | ||
iterator.next = function (cb) { | ||
t.pass('should be called once') | ||
next(cb) | ||
stream.destroy() | ||
} | ||
stream = iteratorStream(iterator) | ||
stream.on('data', function (data) { | ||
t.fail('should not be called') | ||
}) | ||
stream.on('close', t.end.bind(t)) | ||
}) | ||
test('decoder error', function(t){ | ||
t.plan(2); | ||
var codec = new Codec({ valueEncoding: 'json' }); | ||
var stream = iteratorStream(db.iterator(), { | ||
decoder: codec.createStreamDecoder({ values: true }) | ||
}); | ||
stream.once('error', function(err){ | ||
t.ok(err instanceof EncodingError); | ||
}); | ||
stream.once('close', function(){ | ||
t.ok(true); | ||
}); | ||
stream.on('data', function(){}); | ||
}); | ||
test('.destroy during iterator.next 2', function (t) { | ||
var stream | ||
var iterator = db.iterator() | ||
var next = iterator.next.bind(iterator) | ||
var count = 0 | ||
iterator.next = function (cb) { | ||
t.pass('should be called') | ||
next(cb) | ||
if (++count === 2) { | ||
stream.destroy() | ||
} | ||
} | ||
stream = iteratorStream(iterator) | ||
stream.on('data', function (data) { | ||
t.pass('should be called') | ||
}) | ||
stream.on('close', t.end.bind(t)) | ||
}) | ||
test('.destroy after iterator.next 1', function (t) { | ||
var stream | ||
var iterator = db.iterator() | ||
var next = iterator.next.bind(iterator) | ||
iterator.next = function (cb) { | ||
next(function (err, key, value) { | ||
stream.destroy() | ||
cb(err, key, value) | ||
t.pass('should be called') | ||
}) | ||
} | ||
stream = iteratorStream(iterator) | ||
stream.on('data', function (data) { | ||
t.fail('should not be called') | ||
}) | ||
stream.on('close', t.end.bind(t)) | ||
}) | ||
test('.destroy after iterator.next 2', function (t) { | ||
var stream | ||
var iterator = db.iterator() | ||
var next = iterator.next.bind(iterator) | ||
var count = 0 | ||
iterator.next = function (cb) { | ||
next(function (err, key, value) { | ||
if (++count === 2) { | ||
stream.destroy() | ||
} | ||
cb(err, key, value) | ||
t.pass('should be called') | ||
}) | ||
} | ||
stream = iteratorStream(iterator) | ||
stream.on('data', function (data) { | ||
t.pass('should be called') | ||
}) | ||
stream.on('close', t.end.bind(t)) | ||
}) | ||
test('keys=false', function (t) { | ||
var stream = iteratorStream(db.iterator(), { keys: false }) | ||
stream.once('data', function (value) { | ||
stream.destroy() | ||
t.equal(value.toString(), 'bar1') | ||
t.end() | ||
}) | ||
}) | ||
test('values=false', function (t) { | ||
var stream = iteratorStream(db.iterator(), { values: false }) | ||
stream.once('data', function (key) { | ||
stream.destroy() | ||
t.equal(key.toString(), 'foobatch1') | ||
t.end() | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9246
3
4
196
2
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedlevel-errors@^1.0.3
- Removederrno@0.1.8(transitive)
- Removedisarray@0.0.1(transitive)
- Removedlevel-errors@1.1.2(transitive)
- Removedprr@1.0.1(transitive)
- Removedreadable-stream@1.1.14(transitive)
- Removedstring_decoder@0.10.31(transitive)
Updatedreadable-stream@^2.0.5