Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit f994042

Browse files
committed
2.1.1 - make sure to store Uint8Arrays
1 parent 5b2984f commit f994042

5 files changed

Lines changed: 58 additions & 23 deletions

File tree

index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ Level.prototype._get = function (key, options, callback) {
4242
// 'NotFound' error, consistent with LevelDOWN API
4343
return callback(new Error('NotFound'))
4444
}
45-
if (options.asBuffer !== false && !Buffer.isBuffer(value))
46-
value = new Buffer(String(value))
45+
// by default return buffers, unless explicitly told not to
46+
var asBuffer = true
47+
if (options.asBuffer === false) asBuffer = false
48+
if (options.raw) asBuffer = false
49+
if (asBuffer) {
50+
if (value instanceof Uint8Array) value = new Buffer(value)
51+
else value = new Buffer(String(value))
52+
}
4753
return callback(null, value, key)
4854
}, callback)
4955
}
@@ -54,21 +60,27 @@ Level.prototype._del = function(id, options, callback) {
5460

5561
Level.prototype._put = function (key, value, options, callback) {
5662
if (value instanceof ArrayBuffer) {
57-
value = String.fromCharCode.apply(null, new Uint16Array(value))
63+
value = new Buffer(new Uint8Array(value))
5864
}
5965
var obj = this.convertEncoding(key, value, options)
66+
if (Buffer.isBuffer(obj.value)) {
67+
obj.value = new Uint8Array(value.toArrayBuffer())
68+
}
6069
this.idb.put(obj.key, obj.value, function() { callback() }, callback)
6170
}
6271

6372
Level.prototype.convertEncoding = function(key, value, options) {
73+
if (options.raw) return {key: key, value: value}
6474
if (value) {
6575
var stringed = value.toString()
6676
if (stringed === 'NaN') value = 'NaN'
6777
}
6878
var valEnc = options.valueEncoding
6979
var obj = {key: key, value: value}
7080
if (value && (!valEnc || valEnc !== 'binary')) {
71-
if (typeof obj.value !== 'object') obj.value = stringed
81+
if (typeof obj.value !== 'object') {
82+
obj.value = stringed
83+
}
7284
}
7385
return obj
7486
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "level-js",
3-
"version": "2.0.0",
3+
"version": "2.1.1",
44
"description": "leveldown/leveldb library for browsers using IndexedDB",
55
"main": "index.js",
66
"scripts": {
@@ -26,7 +26,7 @@
2626
"xtend": "~2.1.2",
2727
"tape": "~2.10.2",
2828
"browserify": "~3.32.0",
29-
"abstract-leveldown": "~0.11.4"
29+
"abstract-leveldown": "~0.12.0"
3030
},
3131
"testling": {
3232
"files": "test/test.js",

test/custom-tests.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
var levelup = require('levelup')
22

3+
module.exports.setUp = function (leveldown, test, testCommon) {
4+
test('setUp common', testCommon.setUp)
5+
test('setUp db', function (t) {
6+
db = leveldown(testCommon.location())
7+
db.open(t.end.bind(t))
8+
})
9+
}
10+
311
module.exports.all = function(leveljs, tape, testCommon) {
412

13+
module.exports.setUp(leveljs, tape, testCommon)
14+
15+
tape('store native JS types with raw = true', function(t) {
16+
var level = leveljs(testCommon.location())
17+
level.open(function(err) {
18+
t.notOk(err, 'no error')
19+
level.put('key', true, { raw: true }, function (err) {
20+
t.notOk(err, 'no error')
21+
level.get('key', { raw: true }, function(err, value) {
22+
t.notOk(err, 'no error')
23+
t.ok(typeof value === 'boolean', 'is boolean type')
24+
t.ok(value, 'is truthy')
25+
t.end()
26+
})
27+
})
28+
})
29+
})
30+
531
// NOTE: in chrome (at least) indexeddb gets buggy if you try and destroy a db,
632
// then create it again, then try and destroy it again. these avoid doing that
733

8-
tape('test .destroy w/ string', function(t) {
34+
tape('test levelup .destroy w/ string', function(t) {
935
var level = levelup('destroy-test', {db: leveljs})
1036
level.put('key', 'value', function (err) {
1137
t.notOk(err, 'no error')
@@ -27,7 +53,7 @@ module.exports.all = function(leveljs, tape, testCommon) {
2753
})
2854
})
2955

30-
tape('test .destroy w/ db instance', function(t) {
56+
tape('test levelup .destroy w/ db instance', function(t) {
3157
var level = levelup('destroy-test-2', {db: leveljs})
3258
level.put('key', 'value', function (err) {
3359
t.notOk(err, 'no error')

test/test.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@ var testCommon = require('./testCommon')
55
// load IndexedDBShim in the tests
66
require('./idb-shim.js')()
77

8-
var str = 'foo'
9-
var testBuffer = new ArrayBuffer(str.length * 2)
10-
var bufView = new Uint16Array(testBuffer);
11-
for (var i = 0, strLen = str.length; i < strLen; i++) {
12-
bufView[i] = str.charCodeAt(i)
13-
}
14-
15-
// non abstract-leveldown tests:
16-
require('./custom-tests.js').all(leveljs, tape, testCommon)
8+
var testBuffer = new Buffer('foo')
179

1810
/*** compatibility with basic LevelDOWN API ***/
1911
require('abstract-leveldown/abstract/leveldown-test').args(leveljs, tape, testCommon)
@@ -27,3 +19,6 @@ require('abstract-leveldown/abstract/chained-batch-test').all(leveljs, tape, tes
2719
require('abstract-leveldown/abstract/close-test').close(leveljs, tape, testCommon)
2820
require('abstract-leveldown/abstract/iterator-test').all(leveljs, tape, testCommon)
2921
require('abstract-leveldown/abstract/ranges-test').all(leveljs, tape, testCommon)
22+
23+
// non abstract-leveldown tests:
24+
require('./custom-tests.js').all(leveljs, tape, testCommon)

test/testCommon.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ var dbidx = 0
1010

1111
, cleanup = function (callback) {
1212
var list = []
13+
if (dbidx === 0) return callback()
1314
for (var i = 0; i < dbidx; i++) {
1415
list.push('_leveldown_test_db_' + i)
1516
}
16-
var ret = 0
17-
function done (e) {
18-
if (++ret == list.length) callback()
17+
18+
function destroy() {
19+
var f = list.pop()
20+
if (list.length === 0) return callback()
21+
leveljs.destroy(f, destroy)
1922
}
20-
list.forEach(function (f) {
21-
leveljs.destroy(f, done)
22-
})
23+
24+
destroy()
2325
}
2426

2527
, setUp = function (t) {

0 commit comments

Comments
 (0)