Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

level-iterator-stream

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

level-iterator-stream - npm Package Compare versions

Comparing version 1.3.1 to 2.0.0

dude.js

31

example.js

@@ -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)
})
})
})

@@ -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 &amp; 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.

@@ -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

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