Skip to content

Commit

Permalink
Merge pull request #16 from Level/2.x
Browse files Browse the repository at this point in the history
re-implement v2 branch as 2.x
  • Loading branch information
ralphtheninja authored Aug 28, 2017
2 parents 51e0ad2 + 73d8bb8 commit b19c7c7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
node_js:
- 4
- 8
- 7
- 6
branches:
only:
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@

<img alt="LevelDB Logo" height="100" src="http://leveldb.org/img/logo.svg">

**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
Expand All @@ -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.
Expand All @@ -49,6 +48,7 @@ $ npm install level-iterator-stream
## Publishers

* [@juliangruber](https://github.com/juliangruber)
* [@ralphtheninja](https://github.com/ralphtheninja)

## License &amp; copyright

Expand Down
21 changes: 8 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
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) {
if (self._destroyed) return
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 })
}
})
}
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 10 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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) {
Expand All @@ -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 () {})
})

0 comments on commit b19c7c7

Please sign in to comment.