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. 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 }, 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 }, 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)) }