diff --git a/.travis.yml b/.travis.yml index 2efd7e8..64cf6d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js node_js: - - 4 + - 8 + - 7 - 6 branches: only: diff --git a/README.md b/README.md index 868d1ab..0917fb3 100644 --- a/README.md +++ b/README.md @@ -3,25 +3,25 @@ LevelDB Logo -**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) [![Greenkeeper badge](https://badges.greenkeeper.io/Level/iterator-stream.svg)](https://greenkeeper.io/) ## Example ```js -var iteratorStream = require('level-iterator-stream'); -var leveldown = require('leveldown'); - -var 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 iteratorStream = require('level-iterator-stream') +const leveldown = require('leveldown') + +const db = leveldown(__dirname + '/db') +db.open(function (err) { + if (err) throw err + + const stream = iteratorStream(db.iterator()) + stream.on('data', function (kv) { + console.log('%s -> %s', kv.key, kv.value) + }) +}) ``` ## Installation @@ -38,8 +38,7 @@ $ npm install level-iterator-stream `require('readable-stream').Readable` constructor, with `objectMode` forced to `true`. - 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. When the stream ends, the `iterator` will be closed and afterwards a `"close"` event emitted. @@ -49,6 +48,7 @@ $ npm install level-iterator-stream ## Publishers * [@juliangruber](https://github.com/juliangruber) +* [@ralphtheninja](https://github.com/ralphtheninja) ## License & copyright diff --git a/index.js b/index.js index d441867..d03392e 100644 --- a/index.js +++ b/index.js @@ -1,25 +1,25 @@ var inherits = require('inherits') var Readable = require('readable-stream').Readable var extend = require('xtend') -var EncodingError = require('level-errors').EncodingError module.exports = ReadStream inherits(ReadStream, Readable) 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._options = options this.on('end', this._cleanup.bind(this)) } ReadStream.prototype._read = function () { var self = this + var options = this._options if (this._destroyed) return this._iterator.next(function (err, key, value) { @@ -27,17 +27,12 @@ ReadStream.prototype._read = function () { if (err) return self.emit('error', err) if (key === undefined && value === undefined) { self.push(null) - } else { - if (!self._decoder) return self.push({ key: key, value: value }) - - try { - value = self._decoder(key, value) - } catch (err) { - self.emit('error', new EncodingError(err)) - self.push(null) - return - } + } else if (options.keys !== false && options.values === false) { + self.push(key) + } else if (options.keys === false && options.values !== false) { self.push(value) + } else { + self.push({ key: key, value: value }) } }) } diff --git a/package.json b/package.json index 2617452..e0410da 100644 --- a/package.json +++ b/package.json @@ -8,12 +8,10 @@ "repository": "Level/iterator-stream", "dependencies": { "inherits": "^2.0.1", - "level-errors": "^1.0.3", "readable-stream": "^2.0.5", "xtend": "^4.0.0" }, "devDependencies": { - "level-codec": "^7.0.0", "leveldown": "^1.4.1", "standard": "^10.0.3", "tape": "^4.4.0", diff --git a/test.js b/test.js index 1c9fe3a..33860ad 100644 --- a/test.js +++ b/test.js @@ -3,8 +3,6 @@ var path = require('path') var leveldown = require('leveldown') var iteratorStream = require('./') var through2 = require('through2') -var Codec = require('level-codec') -var EncodingError = require('level-errors').EncodingError var db var data = [ @@ -24,7 +22,7 @@ test('setup', function (t) { }) }) -test('simple', function (t) { +test('keys and values', function (t) { var idx = 0 var stream = iteratorStream(db.iterator()) stream.pipe(through2.obj(function (kv, _, done) { @@ -48,29 +46,20 @@ test('destroy', function (t) { stream.destroy() }) -test('decoder', function (t) { - var codec = new Codec({ valueEncoding: 'binary' }) - var stream = iteratorStream(db.iterator(), { - decoder: codec.createStreamDecoder({ values: true }) - }) +test('keys=false', function (t) { + var stream = iteratorStream(db.iterator(), { keys: false }) stream.once('data', function (value) { - t.ok(Buffer.isBuffer(value)) + stream.destroy() t.equal(value.toString(), 'bar1') t.end() }) }) -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) +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() }) - stream.on('data', function () {}) })