From 7ce4031b5fba09ff2b452e11194e07261ba604a4 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 16 Jun 2018 18:00:38 +0200 Subject: [PATCH 1/4] test that Buffer key returns as ArrayBuffer with keyAsBuffer=false --- test/key-type-test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/key-type-test.js b/test/key-type-test.js index ae9e634..e9e2529 100644 --- a/test/key-type-test.js +++ b/test/key-type-test.js @@ -16,6 +16,7 @@ var types = [ { type: 'ArrayBuffer', ctor: true, allowFailure: true, key: ta(Buffer).buffer }, { type: 'Int8Array', ctor: true, allowFailure: true, createKey: ta, view: true }, { type: 'Uint8Array', ctor: true, allowFailure: true, createKey: ta, view: true }, + { name: 'Buffer', type: 'Uint8Array', ctor: true, allowFailure: true, key: ta(Buffer), view: true }, { type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createKey: ta, view: true }, { type: 'Int16Array', ctor: true, allowFailure: true, createKey: ta, view: true }, { type: 'Uint16Array', ctor: true, allowFailure: true, createKey: ta, view: true }, From bc22ca5e3806c744e376afc566ea45f04672b0ba Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 16 Jun 2018 18:01:14 +0200 Subject: [PATCH 2/4] test that Buffer value returns as Uint8Array with asBuffer=false --- test/structured-clone-test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/structured-clone-test.js b/test/structured-clone-test.js index 8a3356f..3e3dbbf 100644 --- a/test/structured-clone-test.js +++ b/test/structured-clone-test.js @@ -42,6 +42,7 @@ var types = [ // Don't allow failure as this is the primary type for binary (Buffer) data { type: 'Uint8Array', ctor: true, createValue: ta }, + { name: 'Buffer', type: 'Uint8Array', ctor: true, value: ta(Buffer) }, { type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createValue: ta }, { type: 'Int16Array', ctor: true, allowFailure: true, createValue: ta }, From 5e9f5e86f503e00e7a02b05c99c7c72552c7cd6f Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 16 Jun 2018 18:02:20 +0200 Subject: [PATCH 3/4] test combinations of Buffer/ArrayBuffer/Uint8Array values and asBuffer true/false --- test/custom-test.js | 84 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/test/custom-test.js b/test/custom-test.js index baf1dd8..76fe600 100644 --- a/test/custom-test.js +++ b/test/custom-test.js @@ -47,7 +47,7 @@ module.exports = function (leveljs, test, testCommon) { }) }) - test('buffer value', function (t) { + test('put Buffer value, get Buffer value', function (t) { var level = leveljs(testCommon.location()) level.open(function (err) { t.notOk(err, 'no error') @@ -63,6 +63,88 @@ module.exports = function (leveljs, test, testCommon) { }) }) + test('put Buffer value, get Uint8Array value', function (t) { + var level = leveljs(testCommon.location()) + level.open(function (err) { + t.notOk(err, 'no error') + level.put('key', Buffer.from('00ff', 'hex'), function (err) { + t.notOk(err, 'no error') + level.get('key', { asBuffer: false }, function (err, value) { + t.notOk(err, 'no error') + t.notOk(Buffer.isBuffer(value), 'is not a buffer') + t.ok(value instanceof Uint8Array, 'is a Uint8Array') + t.same(Buffer.from(value), Buffer.from('00ff', 'hex')) + level.close(t.end.bind(t)) + }) + }) + }) + }) + + test('put Uint8Array value, get Buffer value', function (t) { + var level = leveljs(testCommon.location()) + level.open(function (err) { + t.notOk(err, 'no error') + level.put('key', new Uint8Array(Buffer.from('00ff', 'hex').buffer), function (err) { + t.notOk(err, 'no error') + level.get('key', function (err, value) { + t.notOk(err, 'no error') + t.ok(Buffer.isBuffer(value), 'is buffer') + t.same(value, Buffer.from('00ff', 'hex')) + level.close(t.end.bind(t)) + }) + }) + }) + }) + + test('put Uint8Array value, get Uint8Array value', function (t) { + var level = leveljs(testCommon.location()) + level.open(function (err) { + t.notOk(err, 'no error') + level.put('key', new Uint8Array(Buffer.from('00ff', 'hex').buffer), function (err) { + t.notOk(err, 'no error') + level.get('key', { asBuffer: false }, function (err, value) { + t.notOk(err, 'no error') + t.notOk(Buffer.isBuffer(value), 'is not a buffer') + t.ok(value instanceof Uint8Array, 'is a Uint8Array') + t.same(Buffer.from(value), Buffer.from('00ff', 'hex')) + level.close(t.end.bind(t)) + }) + }) + }) + }) + + test('put ArrayBuffer value, get Buffer value', function (t) { + var level = leveljs(testCommon.location()) + level.open(function (err) { + t.notOk(err, 'no error') + level.put('key', Buffer.from('00ff', 'hex').buffer, function (err) { + t.notOk(err, 'no error') + level.get('key', function (err, value) { + t.notOk(err, 'no error') + t.ok(Buffer.isBuffer(value), 'is buffer') + t.same(value, Buffer.from('00ff', 'hex')) + level.close(t.end.bind(t)) + }) + }) + }) + }) + + test('put ArrayBuffer value, get ArrayBuffer value', function (t) { + var level = leveljs(testCommon.location()) + level.open(function (err) { + t.notOk(err, 'no error') + level.put('key', Buffer.from('00ff', 'hex').buffer, function (err) { + t.notOk(err, 'no error') + level.get('key', { asBuffer: false }, function (err, value) { + t.notOk(err, 'no error') + t.ok(value instanceof ArrayBuffer, 'is a ArrayBuffer') + t.same(Buffer.from(value), Buffer.from('00ff', 'hex')) + level.close(t.end.bind(t)) + }) + }) + }) + }) + // This should be covered by abstract-leveldown tests, but that's // prevented by process.browser checks (Level/abstract-leveldown#121). // This test is adapted from memdown. From d1d681bc916837456d3e3e03bb0f02d675f3fd80 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 16 Jun 2018 18:02:52 +0200 Subject: [PATCH 4/4] remove obsolete comment --- util/mixed-to-buffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/mixed-to-buffer.js b/util/mixed-to-buffer.js index f27ec9f..023f913 100644 --- a/util/mixed-to-buffer.js +++ b/util/mixed-to-buffer.js @@ -4,6 +4,6 @@ var toBuffer = require('typedarray-to-buffer') module.exports = function (value) { if (value instanceof Uint8Array) return toBuffer(value) - else if (value instanceof ArrayBuffer) return Buffer.from(value) // For keys. + else if (value instanceof ArrayBuffer) return Buffer.from(value) else return Buffer.from(String(value)) }