Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.
/ level-js Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
.DS_Store
.DS_Store
npm-debug.log
package-lock.json
32 changes: 10 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict'

module.exports = Level

var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var util = require('util')
var Iterator = require('./iterator')
var toBuffer = require('typedarray-to-buffer')
var mixedToBuffer = require('./util/mixed-to-buffer')
var setImmediate = require('./util/immediate')
var support = require('./util/support')

var DEFAULT_PREFIX = 'level-js-'

Expand All @@ -18,24 +22,9 @@ function Level (location, opts) {

util.inherits(Level, AbstractLevelDOWN)

// Detect binary key support (IndexedDB Second Edition)
Level.binaryKeys = (function () {
try {
indexedDB.cmp(new Uint8Array(0), 0)
return true
} catch (err) {
return false
}
})()

Level.arrayKeys = (function () {
try {
indexedDB.cmp([1], 0)
return true
} catch (err) {
return false
}
})()
// Detect binary and array key support (IndexedDB Second Edition)
Level.binaryKeys = support.binaryKeys(indexedDB)
Level.arrayKeys = support.arrayKeys(indexedDB)

Level.prototype._open = function (options, callback) {
var req = indexedDB.open(this.prefix + this.location, this.version)
Expand Down Expand Up @@ -88,8 +77,7 @@ Level.prototype._get = function (key, options, callback) {
}

if (options.asBuffer) {
if (value instanceof Uint8Array) value = toBuffer(value)
else value = Buffer.from(String(value))
value = mixedToBuffer(value)
}

callback(null, value)
Expand Down Expand Up @@ -134,7 +122,7 @@ Level.prototype._iterator = function (options) {
}

Level.prototype._batch = function (operations, options, callback) {
if (operations.length === 0) return setTimeout(callback, 0)
if (operations.length === 0) return setImmediate(callback)

var store = this.store('readwrite')
var transaction = store.transaction
Expand Down
27 changes: 11 additions & 16 deletions iterator.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict'

var util = require('util')
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var ltgt = require('ltgt')
var toBuffer = require('typedarray-to-buffer')
var mixedToBuffer = require('./util/mixed-to-buffer')
var setImmediate = require('./util/immediate')
var noop = function () {}

// TODO: move this to a util, to be used by get() and iterators.
function mixedToBuffer (value) {
if (value instanceof Uint8Array) return toBuffer(value)
else return Buffer.from(String(value))
}

module.exports = Iterator

function Iterator (db, location, options) {
Expand Down Expand Up @@ -114,41 +111,39 @@ Iterator.prototype.maybeNext = function () {
}
}

// TODO: use setImmediate (see memdown)
Iterator.prototype._next = function (callback) {
if (this._aborted) {
// The error should be picked up by either next() or end().
var err = this._error
this._error = null

setTimeout(function() {
setImmediate(function() {
callback(err)
}, 0)
})
} else if (this._cache.length > 0) {
var key = this._cache.shift()
var value = this._cache.shift()

if (this._keyAsBuffer) key = mixedToBuffer(key)
if (this._valueAsBuffer) value = mixedToBuffer(value)

setTimeout(function() {
setImmediate(function() {
callback(null, key, value)
}, 0)
})
} else if (this._completed) {
setTimeout(callback, 0)
setImmediate(callback)
} else {
this._callback = callback
}
}

// TODO: use setImmediate (see memdown)
Iterator.prototype._end = function (callback) {
if (this._aborted || this._completed) {
var err = this._error

setTimeout(function () {
setImmediate(function () {
callback(err)
}, 0)
})

return
}
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"main": "index.js",
"scripts": {
"test": "airtap --local --no-coverage test/index.js",
"test-end": "airtap --local --no-coverage test/manual/end-test.js",
"test-browsers": "airtap --sauce-connect --loopback airtap.local --no-coverage test/index.js"
},
"repository": {
Expand All @@ -19,7 +18,7 @@
"author": "max ogden",
"license": "MIT",
"devDependencies": {
"airtap": "0.0.5",
"airtap": "0.0.7",
"browserify": "~16.2.2",
"encoding-down": "~5.0.2",
"levelup": "~3.0.0",
Expand All @@ -28,7 +27,11 @@
},
"dependencies": {
"abstract-leveldown": "~5.0.0",
"immediate": "~3.2.3",
"ltgt": "^2.1.2",
"typedarray-to-buffer": "~3.1.5"
},
"browser": {
"./util/immediate.js": "./util/immediate-browser.js"
}
}
22 changes: 12 additions & 10 deletions test/custom-tests.js → test/custom-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

var levelup = require('levelup')

module.exports.all = function(leveljs, tape, testCommon) {
tape('setUp', testCommon.setUp)
module.exports = function (leveljs, test, testCommon) {
test('setUp', testCommon.setUp)

tape('buffer value', function(t) {
test('buffer value', function(t) {
var level = leveljs(testCommon.location())
level.open(function(err) {
t.notOk(err, 'no error')
Expand All @@ -22,7 +24,7 @@ module.exports.all = function(leveljs, tape, testCommon) {
// 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.
leveljs.binaryKeys && tape('buffer keys', function (t) {
leveljs.binaryKeys && test('buffer keys', function (t) {
var db = leveljs(testCommon.location())

db.open(function (err) {
Expand Down Expand Up @@ -60,7 +62,7 @@ module.exports.all = function(leveljs, tape, testCommon) {
})

// Adapted from a memdown test.
leveljs.binaryKeys && tape('iterator stringifies buffer input', function (t) {
leveljs.binaryKeys && test('iterator stringifies buffer input', function (t) {
t.plan(6)

var db = leveljs(testCommon.location())
Expand All @@ -85,7 +87,7 @@ module.exports.all = function(leveljs, tape, testCommon) {
})

// TODO: merge this and the test below. Test all types.
tape('store native JS types', function(t) {
test('store native JS types', function(t) {
var level = leveljs(testCommon.location())
level.open(function(err) {
t.notOk(err, 'no error')
Expand All @@ -101,7 +103,7 @@ module.exports.all = function(leveljs, tape, testCommon) {
})
})

tape('store NaN value', function(t) {
test('store NaN value', function(t) {
var level = leveljs(testCommon.location())
level.open(function(err) {
t.notOk(err, 'no error')
Expand All @@ -119,7 +121,7 @@ module.exports.all = function(leveljs, tape, testCommon) {
// NOTE: in chrome (at least) indexeddb gets buggy if you try and destroy a db,
// then create it again, then try and destroy it again. these avoid doing that

tape('test levelup .destroy w/ string', function(t) {
test('test levelup .destroy w/ string', function(t) {
var location = testCommon.location()
var db = levelup(leveljs(location))
db.put('key', 'value', function (err) {
Expand All @@ -142,7 +144,7 @@ module.exports.all = function(leveljs, tape, testCommon) {
})
})

tape('test levelup .destroy w/ db instance', function(t) {
test('test levelup .destroy w/ db instance', function(t) {
var location = testCommon.location()
var db = levelup(leveljs(location))
db.put('key', 'value', function (err) {
Expand All @@ -165,5 +167,5 @@ module.exports.all = function(leveljs, tape, testCommon) {
})
})

tape('teardown', testCommon.tearDown)
test('teardown', testCommon.tearDown)
}
30 changes: 28 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
'use strict'

// Promise polyfill for IE and others.
if (process.browser && typeof Promise !== 'function') {
global.Promise = require('pinkie')
}

require('./test')
require('./test-levelup')
// Load IndexedDBShim
// require('./util/idb-shim.js')()

var test = require('tape')
var leveljs = require('..')
var testCommon = require('./util/test-common')

// Test feature detection
require('./support-test')(leveljs, test)

// Test abstract-leveldown compliance
require('abstract-leveldown/abstract/leveldown-test').args(leveljs, test, testCommon)
require('abstract-leveldown/abstract/open-test').open(leveljs, test, testCommon)
require('abstract-leveldown/abstract/put-test').all(leveljs, test, testCommon)
require('abstract-leveldown/abstract/del-test').all(leveljs, test, testCommon)
require('abstract-leveldown/abstract/get-test').all(leveljs, test, testCommon)
require('abstract-leveldown/abstract/put-get-del-test').all(leveljs, test, testCommon)
require('abstract-leveldown/abstract/batch-test').all(leveljs, test, testCommon)
require('abstract-leveldown/abstract/chained-batch-test').all(leveljs, test, testCommon)
require('abstract-leveldown/abstract/close-test').close(leveljs, test, testCommon)
require('abstract-leveldown/abstract/iterator-test').all(leveljs, test, testCommon)
require('abstract-leveldown/abstract/iterator-range-test').all(leveljs, test, testCommon)

// Additional tests for this implementation
require('./custom-test')(leveljs, test, testCommon)
require('./levelup-test')(leveljs, test, testCommon)
62 changes: 62 additions & 0 deletions test/levelup-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

// NOTE: Temporary integration tests, can be removed later. Remember
// to also remove the encoding-down devDependency.

var levelup = require('levelup')
var encoding = require('encoding-down')

module.exports = function (leveljs, test, testCommon) {
test('setup', testCommon.setUp)

test('levelup put', function (t) {
t.plan(4)

var down = leveljs(testCommon.location())
var db = levelup(down)

db.put('name', 'LevelUP string', function (err) {
t.ifError(err, 'no put error')

db.get('name', { asBuffer: false }, function (err, value) {
t.ifError(err, 'no get error')
t.is(value, 'LevelUP string')

db.close(function (err) {
t.ifError(err, 'no close error')
})
})
})
})

test('binary', function (t) {
t.plan(9)

var down = leveljs(testCommon.location())
var db = levelup(encoding(down, { valueEncoding: 'binary' }))
var buf = Buffer.from('00ff', 'hex')

db.put('binary', buf, function (err) {
t.ifError(err, 'no put error')

db.get('binary', function (err, value) {
t.ifError(err, 'no get error')
t.ok(Buffer.isBuffer(value), 'is a buffer')
t.same(value, buf)

db.get('binary', { valueEncoding: 'id' }, function (err, value) {
t.ifError(err, 'no get error')
t.notOk(Buffer.isBuffer(value), 'is not a buffer')
t.ok(value instanceof Uint8Array, 'is a Uint8Array')
t.same(Buffer.from(value), buf)

db.close(function (err) {
t.ifError(err, 'no close error')
})
})
})
})
})

test('teardown', testCommon.tearDown)
}
51 changes: 0 additions & 51 deletions test/manual/end-test.js

This file was deleted.

Loading