This repository was archived by the owner on Dec 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Fix and test key types #130
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3f73696
don't stringify keys (except fallbacks, booleans and NaN)
vweevers 184ad0a
fix conversion of binary cursor key (which is an ArrayBuffer) to Buffer
vweevers b3a2240
test all valid key types
vweevers 4eaa630
test that iterator yields Buffer keys
vweevers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /* global indexedDB */ | ||
|
|
||
| 'use strict' | ||
|
|
||
| var ta = require('./util/create-typed-array') | ||
| var support = require('../util/support') | ||
|
|
||
| var types = [ | ||
| { type: 'number', value: -20 }, | ||
| { type: '+Infinity', value: Infinity }, | ||
| { type: '-Infinity', value: -Infinity }, | ||
| { type: 'string', value: 'test' }, | ||
| { type: 'Date', ctor: true, value: new Date() }, | ||
| { type: 'Array', ctor: true, allowFailure: true, value: [0, '1'] }, | ||
| { type: 'ArrayBuffer', ctor: true, allowFailure: true, value: ta(Buffer).buffer }, | ||
| { type: 'Int8Array', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Uint8Array', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Uint8ClampedArray', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Int16Array', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Uint16Array', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Int32Array', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Uint32Array', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Float32Array', ctor: true, allowFailure: true, createValue: ta, view: true }, | ||
| { type: 'Float64Array', ctor: true, allowFailure: true, createValue: ta, view: true } | ||
| ] | ||
|
|
||
| // TODO: test types that are not supported by IndexedDB Second Edition | ||
| // - Date NaN (should be rejected by IndexedDB) | ||
| // - empty array (should be rejected by abstract-leveldown) | ||
| // - array containing null (should be rejected by IndexedDB) | ||
| // - cyclical array (not sure) | ||
| // - Array(10) (not sure) | ||
| // var illegalTypes = [] | ||
|
|
||
| // TODO: test types that are not supported by IndexedDB Second Edition, but get | ||
| // stringified for abstract-leveldown compatibility. | ||
| // - NaN | ||
| // - boolean | ||
| // var stringifiedTypes = [] | ||
|
|
||
| module.exports = function (leveljs, test, testCommon) { | ||
| var db | ||
|
|
||
| test('setUp', testCommon.setUp) | ||
| test('open', function (t) { | ||
| db = leveljs(testCommon.location()) | ||
| db.open(t.end.bind(t)) | ||
| }) | ||
|
|
||
| types.forEach(function (item) { | ||
| var testName = item.name || item.type | ||
|
|
||
| test('key type: ' + testName, function (t) { | ||
| var Constructor = item.ctor ? global[item.type] : null | ||
| var skip = item.allowFailure ? 'pass' : 'fail' | ||
| var input = item.value | ||
|
|
||
| if (item.ctor && !Constructor) { | ||
| t[skip]('constructor is undefined in this environment') | ||
| return t.end() | ||
| } | ||
|
|
||
| if (item.createValue) { | ||
| try { | ||
| input = item.createValue(Constructor) | ||
| } catch (err) { | ||
| t[skip]('constructor is not spec-compliant in this environment') | ||
| return t.end() | ||
| } | ||
| } | ||
|
|
||
| if (!support.test(input)(indexedDB)) { | ||
| t[skip]('type is not supported in this environment') | ||
| return t.end() | ||
| } | ||
|
|
||
| db.put(input, testName, function (err) { | ||
| t.ifError(err, 'no put error') | ||
|
|
||
| db.get(input, { asBuffer: false }, function (err, value) { | ||
| t.ifError(err, 'no get error') | ||
| t.same(value, testName, 'correct value') | ||
|
|
||
| var it = db.iterator({ keyAsBuffer: false, valueAsBuffer: false }) | ||
|
|
||
| testCommon.collectEntries(it, function (err, entries) { | ||
| t.ifError(err, 'no iterator error') | ||
| t.is(entries.length, 1, '1 entry') | ||
|
|
||
| var key = entries[0].key | ||
| var value = entries[0].value | ||
|
|
||
| if (Constructor) { | ||
| var type = item.view ? 'ArrayBuffer' : item.type | ||
| var expected = '[object ' + type + ']' | ||
| var actual = Object.prototype.toString.call(key) | ||
|
|
||
| if (actual === expected) { | ||
| t.is(actual, expected, 'prototype') | ||
| } else { | ||
| t[skip]('(de)serializing is not supported by this environment') | ||
| return t.end() | ||
| } | ||
|
|
||
| if (item.view) { | ||
| t.ok(key instanceof ArrayBuffer, 'key is instanceof ArrayBuffer') | ||
| t.same(Buffer.from(new Constructor(key)), ta(Buffer), 'correct octets') | ||
| } else { | ||
| t.ok(key instanceof Constructor, 'key is instanceof ' + type) | ||
| t.same(key, input, 'correct key') | ||
| } | ||
| } else { | ||
| t.is(key, input, 'correct key') | ||
| } | ||
|
|
||
| t.same(value, testName, 'correct value') | ||
|
|
||
| db.del(input, function (err) { | ||
| t.ifError(err, 'no del error') | ||
| t.end() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('close', function (t) { db.close(t.end.bind(t)) }) | ||
| test('tearDown', testCommon.tearDown) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 'use strict' | ||
|
|
||
| var bytes = [0, 127] | ||
|
|
||
| // Replacement for TypedArray.from(bytes) | ||
| module.exports = function (TypedArray) { | ||
| var arr = new TypedArray(bytes.length) | ||
| for (var i = 0; i < bytes.length; i++) arr[i] = bytes[i] | ||
| return arr | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,15 @@ | ||
| 'use strict' | ||
|
|
||
| exports.binaryKeys = function (impl) { | ||
| try { | ||
| impl.cmp(new Uint8Array(0), 0) | ||
| return true | ||
| } catch (err) { | ||
| return false | ||
| exports.test = function (key) { | ||
| return function test (impl) { | ||
| try { | ||
| impl.cmp(key, 0) | ||
| return true | ||
| } catch (err) { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| exports.arrayKeys = function (impl) { | ||
| try { | ||
| impl.cmp([1], 0) | ||
| return true | ||
| } catch (err) { | ||
| return false | ||
| } | ||
| } | ||
| exports.binaryKeys = exports.test(new Uint8Array(0)) | ||
| exports.arrayKeys = exports.test([1]) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!