abstract-leveldown
Advanced tools
Comparing version 0.7.1 to 0.7.2
@@ -6,5 +6,13 @@ /* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
this._operations = [] | ||
this._written = false | ||
} | ||
AbstractChainedBatch.prototype._checkWritten = function () { | ||
if (this._written) | ||
throw new Error('write() already called on this batch') | ||
} | ||
AbstractChainedBatch.prototype.put = function (key, value) { | ||
this._checkWritten() | ||
var err = this._db._checkKeyValue(key, 'key', this._db._isBuffer) | ||
@@ -24,2 +32,4 @@ if (err) throw err | ||
AbstractChainedBatch.prototype.del = function (key) { | ||
this._checkWritten() | ||
var err = this._db._checkKeyValue(key, 'key', this._db._isBuffer) | ||
@@ -36,2 +46,4 @@ if (err) throw err | ||
AbstractChainedBatch.prototype.clear = function () { | ||
this._checkWritten() | ||
this._operations = [] | ||
@@ -42,2 +54,4 @@ return this | ||
AbstractChainedBatch.prototype.write = function (options, callback) { | ||
this._checkWritten() | ||
if (typeof options == 'function') | ||
@@ -50,2 +64,4 @@ callback = options | ||
this._written = true | ||
if (typeof this._db._batch == 'function') | ||
@@ -52,0 +68,0 @@ return this._db._batch(this._operations, options, callback) |
@@ -10,22 +10,24 @@ /* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
AbstractIterator.prototype.next = function (callback) { | ||
var self = this | ||
if (typeof callback != 'function') | ||
throw new Error('next() requires a callback argument') | ||
if (this._ended) | ||
if (self._ended) | ||
return callback(new Error('cannot call next() after end()')) | ||
if (this._nexting) | ||
if (self._nexting) | ||
return callback(new Error('cannot call next() before previous next() has completed')) | ||
this._nexting = true | ||
if (typeof this._next == 'function') { | ||
return this._next(function () { | ||
this._nexting = false | ||
self._nexting = true | ||
if (typeof self._next == 'function') { | ||
return self._next(function () { | ||
self._nexting = false | ||
callback.apply(null, arguments) | ||
}.bind(this)) | ||
}) | ||
} | ||
process.nextTick(function () { | ||
this._nexting = false | ||
self._nexting = false | ||
callback() | ||
}.bind(this)) | ||
}) | ||
} | ||
@@ -32,0 +34,0 @@ |
@@ -41,2 +41,3 @@ /* Copyright (c) 2013 Rod Vagg, MIT License */ | ||
AbstractLevelDOWN.prototype.get = function (key, options, callback) { | ||
var self = this | ||
if (typeof options == 'function') | ||
@@ -46,12 +47,12 @@ callback = options | ||
throw new Error('get() requires a callback argument') | ||
var err = this._checkKeyValue(key, 'key', this._isBuffer) | ||
var err = self._checkKeyValue(key, 'key', self._isBuffer) | ||
if (err) return callback(err) | ||
if (!this._isBuffer(key)) key = String(key) | ||
if (!self._isBuffer(key)) key = String(key) | ||
if (typeof options != 'object') | ||
options = {} | ||
if (typeof this._get == 'function') | ||
return this._get(key, options, callback) | ||
if (typeof self._get == 'function') | ||
return self._get(key, options, callback) | ||
process.nextTick(callback.bind(null, new Error('NotFound'))) | ||
process.nextTick(function () { callback(new Error('NotFound')) }) | ||
} | ||
@@ -69,6 +70,7 @@ | ||
if (!this._isBuffer(key)) key = String(key) | ||
if (!this._isBuffer(value)) value = String(value) | ||
// coerce value to string in node, dont touch it in browser | ||
// (indexeddb can store any JS type) | ||
if (!this._isBuffer(value) && !process.browser) value = String(value) | ||
if (typeof options != 'object') | ||
options = {} | ||
if (typeof this._put == 'function') | ||
@@ -149,3 +151,3 @@ return this._put(key, value, options, callback) | ||
process.nextTick(callback.bind(null, null, 0)) | ||
process.nextTick(function () { callback(null, 0) }) | ||
} | ||
@@ -184,2 +186,2 @@ | ||
module.exports.AbstractLevelDOWN = AbstractLevelDOWN | ||
module.exports.AbstractIterator = AbstractIterator | ||
module.exports.AbstractIterator = AbstractIterator |
@@ -111,3 +111,3 @@ var db | ||
t.notOk(value, 'value not returned') | ||
t.ok(/NotFound/.test(err.message), 'NotFound error') | ||
t.ok((/NotFound/i).test(err.message), 'NotFound error') | ||
done() | ||
@@ -143,2 +143,2 @@ }) | ||
module.exports.tearDown(test, testCommon) | ||
} | ||
} |
@@ -20,2 +20,3 @@ var db | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -31,2 +32,3 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -42,2 +44,3 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -53,2 +56,3 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -64,2 +68,3 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -75,2 +80,3 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -86,2 +92,3 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -97,2 +104,3 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
@@ -113,7 +121,60 @@ | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
test('test batch#put() after write()', function (t) { | ||
var batch = db.batch().put('foo', 'bar') | ||
batch.write(function () {}) | ||
try { | ||
batch.put('boom', 'bang') | ||
} catch (err) { | ||
t.equal(err.message, 'write() already called on this batch', 'correct error message') | ||
return t.end() | ||
} | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
test('test batch#del() after write()', function (t) { | ||
var batch = db.batch().put('foo', 'bar') | ||
batch.write(function () {}) | ||
try { | ||
batch.del('foo') | ||
} catch (err) { | ||
t.equal(err.message, 'write() already called on this batch', 'correct error message') | ||
return t.end() | ||
} | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
test('test batch#clear() after write()', function (t) { | ||
var batch = db.batch().put('foo', 'bar') | ||
batch.write(function () {}) | ||
try { | ||
batch.clear() | ||
} catch (err) { | ||
t.equal(err.message, 'write() already called on this batch', 'correct error message') | ||
return t.end() | ||
} | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
test('test batch#write() after write()', function (t) { | ||
var batch = db.batch().put('foo', 'bar') | ||
batch.write(function () {}) | ||
try { | ||
batch.write(function (err) {}) | ||
} catch (err) { | ||
t.equal(err.message, 'write() already called on this batch', 'correct error message') | ||
return t.end() | ||
} | ||
t.fail('should have thrown') | ||
t.end() | ||
}) | ||
} | ||
module.exports.batch = function (test, testCommon) { | ||
test('basic batch', function (t) { | ||
test('test basic batch', function (t) { | ||
db.batch( | ||
@@ -120,0 +181,0 @@ [ |
@@ -50,3 +50,3 @@ var db | ||
if (err) message = err.message | ||
t.ok(message.match(/NotFound/), 'returned NotFound') | ||
t.ok(message.match(/NotFound/i), 'returned NotFound') | ||
t.end() | ||
@@ -58,2 +58,9 @@ }) | ||
}) | ||
test('test del on non-existent key', function (t) { | ||
db.del('blargh', function (err) { | ||
t.notOk(err, 'should not error on delete') | ||
t.end() | ||
}) | ||
}) | ||
} | ||
@@ -72,2 +79,2 @@ | ||
module.exports.tearDown(test, testCommon) | ||
} | ||
} |
@@ -96,2 +96,3 @@ var db | ||
}) | ||
iterator.next(function(err) { | ||
@@ -365,2 +366,10 @@ t.ok(err, 'returned error') | ||
test('test iterator with start after database end', function (t) { | ||
collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a' }), function (err, data) { | ||
t.notOk(err, 'no error') | ||
t.equal(data.length, 0, 'correct number of entries') | ||
t.end() | ||
}) | ||
}) | ||
test('test iterator with start after database end and reverse=true', function (t) { | ||
@@ -367,0 +376,0 @@ collectEntries(db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '9a', reverse: true }), function (err, data) { |
@@ -44,11 +44,17 @@ /**** SETUP & UTILITY STUFF ****/ | ||
var result | ||
var result, valueString | ||
if (_value instanceof ArrayBuffer) { | ||
result = String.fromCharCode.apply(null, new Uint16Array(_value)) | ||
} else { | ||
t.ok(typeof Buffer != 'undefined' && _value instanceof Buffer) | ||
t.ok(typeof Buffer != 'undefined' && _value instanceof Buffer, 'is a Buffer') | ||
result = _value.toString() | ||
} | ||
t.equals(result, value.toString()) | ||
if (value instanceof ArrayBuffer) { | ||
value = String.fromCharCode.apply(null, new Uint16Array(value)) | ||
} else { | ||
value = value.toString() | ||
} | ||
t.equals(result, value) | ||
db.del(key, function (err) { | ||
@@ -58,3 +64,3 @@ t.notOk(err, 'no error, deleted key/value for `' + key + '`') | ||
t.ok(err, 'entry propertly deleted') | ||
t.ok(err.message.match(/NotFound/)) | ||
t.ok(err.message.match(/NotFound/i), 'is NotFound') | ||
t.end() | ||
@@ -110,6 +116,8 @@ }) | ||
// Buffer key | ||
makePutGetDelSuccessfulTest('Buffer key', testBuffer, 'foo') | ||
if (!process.browser) { | ||
// Buffer key | ||
makePutGetDelSuccessfulTest('Buffer key', testBuffer, 'foo') | ||
} | ||
// non-empty Array as a key | ||
// non-empty Array as a value | ||
makePutGetDelSuccessfulTest('Array value', 'foo', [1,2,3,4]) | ||
@@ -143,6 +151,6 @@ } | ||
// standard Buffer value | ||
makePutGetDelSuccessfulTest('Buffer key', 'foo', testBuffer) | ||
makePutGetDelSuccessfulTest('Buffer value', 'foo', testBuffer) | ||
// non-empty Array as a value | ||
makePutGetDelSuccessfulTest('Array value', [1,2,3,4], 'foo') | ||
// non-empty Array as a key | ||
makePutGetDelSuccessfulTest('Array key', [1,2,3,4], 'foo') | ||
} | ||
@@ -149,0 +157,0 @@ |
@@ -63,2 +63,16 @@ var db | ||
}) | ||
if (process.browser) { | ||
test('test object value put()', function (t) { | ||
db.put('dood', {pete: 'sampras'}, function (err) { | ||
t.notOk(err, 'no error') | ||
db.get('dood', { asBuffer: false }, function (err, value) { | ||
t.notOk(err, 'no error') | ||
t.equal(JSON.stringify(value), JSON.stringify({pete: 'sampras'})) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
@@ -65,0 +79,0 @@ |
{ | ||
"name" : "abstract-leveldown" | ||
, "description" : "An abstract prototype matching the LevelDOWN API" | ||
, "version" : "0.7.1" | ||
, "version" : "0.7.2" | ||
, "homepage" : "https://github.com/rvagg/node-abstract-leveldown" | ||
@@ -6,0 +6,0 @@ , "contributors" : [ |
const path = require('path') | ||
, fs = require('fs') | ||
, rimraf = require('rimraf') | ||
, test = require('tap').test | ||
@@ -6,0 +5,0 @@ var dbidx = 0 |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86655
1932