Level/encoding-down

module tries to decode an empty string and fails when using createKeyStream and JSON

Closed this issue · 2 comments

const levelup = require('levelup');
const leveldown = require('leveldown');
const encode = require('encoding-down');

var enc = encode(leveldown('./mydb'), {
	valueEncoding: 'json',
	keyEncoding: 'json',
});
var lv = levelup(enc);

(async function() {
	// adding some random data
	await lv.put('asdf.1', 'aaaaaa');
	await lv.put('asdf.2', 'bbbbbb');
	await lv.put('asdf.3', 'cccccc');

	lv.createKeyStream()
		.on('data', function(data) {
			console.log('data');
			console.log(data);
		})
		.on('error', function(err) {
			console.log('error');
			console.log(err);
		})
		.on('end', function() {
			console.log('end');
		});
})();

output:

error
EncodingError: Unexpected end of JSON input
    at (...)/node_modules/encoding-down/index.js:105:17
    at (...)/node_modules/leveldown/node_modules/abstract-leveldown/abstract-iterator.js:24:16
    at (...)/node_modules/leveldown/iterator.js:43:7
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Confirmed. This is a bug, thanks for the report!

@ralphtheninja @juliangruber I suspect the leveldown iterator yields empty strings rather than undefined if options.values === false. But encoding-down only skips undefined, and doesn't consider options.values at all:

encoding-down/index.js

Lines 101 to 103 in 1e1bd3b

if (typeof value !== 'undefined') {
value = self.codec.decodeValue(value, self.opts)
}

Fix released in 3.0.1