localstorage-down
Advanced tools
Comparing version 0.4.4 to 0.5.0
367
index.js
@@ -1,121 +0,117 @@ | ||
var util = require('util') | ||
, AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN | ||
, AbstractIterator = require('abstract-leveldown').AbstractIterator | ||
, noop = function () {} | ||
, setImmediate = global.setImmediate || process.nextTick | ||
'use strict'; | ||
function ldIterator (db, options) { | ||
var util = require('util'); | ||
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN; | ||
var AbstractIterator = require('abstract-leveldown').AbstractIterator; | ||
var noop = function () { | ||
}; | ||
var nextTick = global.setImmediate || process.nextTick; | ||
function LDIterator(db, options) { | ||
AbstractIterator.call(this, db) | ||
var emptybuffer = new Buffer(0) | ||
AbstractIterator.call(this, db); | ||
this._dbsize = this.db.container.length(); | ||
this._reverse = !!options.reverse | ||
this._reverse = !!options.reverse; | ||
// Test for empty buffer in end | ||
if(options.end instanceof Buffer){ | ||
if(options.end.length = 0) | ||
this._end = this.db.container.key(this._dbsize - 1) | ||
}else{ | ||
this._end = options.end | ||
if (options.end instanceof Buffer) { | ||
if (options.end.length === 0) { | ||
this._end = this.db.container.key(this._dbsize - 1); | ||
} | ||
} else { | ||
this._end = options.end; | ||
} | ||
this._limit = options.limit | ||
this._count = 0 | ||
this._limit = options.limit; | ||
this._count = 0; | ||
if (options.start) { | ||
// if pos is more than the size of the database then set pos to the end | ||
var found = false; | ||
for (var i = 0; i < this._dbsize; i++) { | ||
if (this.db.container.key(i) >= options.start) { | ||
this._pos = i | ||
//Make sure we step back for mid values e.g 49.5 test | ||
if(this._reverse){ | ||
if(this.db.container.key(i) > options.start){ | ||
this._pos = i -1 | ||
}else{ | ||
this._pos = i | ||
} | ||
} | ||
found = true; | ||
break | ||
} | ||
this._pos = this.db.container.indexOfKey(options.start); | ||
if (this._reverse && this.db.container.key(this._pos) !== options.start) { | ||
this._pos--; | ||
} | ||
if(!found){ | ||
this._pos = this._reverse ? this._dbsize - 1 : -1 | ||
} | ||
} else { | ||
this._pos = this._reverse ? this._dbsize - 1 : 0 | ||
this._pos = this._reverse ? this._dbsize - 1 : 0; | ||
} | ||
} | ||
util.inherits(ldIterator, AbstractIterator) | ||
util.inherits(LDIterator, AbstractIterator); | ||
ldIterator.prototype._next = function (callback) { | ||
if (this._pos >= this.db.container.length() || this._pos < 0) | ||
return setImmediate(callback) | ||
var key = this.db.container.key(this._pos) | ||
, value | ||
LDIterator.prototype._next = function (callback) { | ||
if (!!this._end && (this._reverse ? key < this._end : key > this._end)) | ||
return setImmediate(callback) | ||
if (this._pos >= this.db.container.length() || this._pos < 0) { | ||
return nextTick(callback); | ||
} | ||
var key = this.db.container.key(this._pos); | ||
var value; | ||
if (!!this._end && (this._reverse ? key < this._end : key > this._end)) { | ||
return nextTick(callback); | ||
} | ||
if (!!this._limit && this._limit > 0 && this._count++ >= this._limit) | ||
return setImmediate(callback) | ||
value = this.db.container.getItem(key) | ||
this._pos += this._reverse ? -1 : 1 | ||
if (!!this._limit && this._limit > 0 && this._count++ >= this._limit) { | ||
return nextTick(callback); | ||
} | ||
setImmediate(callback.bind(null, undefined, key, value)) | ||
} | ||
value = this.db.container.getItem(key); | ||
this._pos += this._reverse ? -1 : 1; | ||
function ld (location) { | ||
if (!(this instanceof ld)) return new ld(location) | ||
AbstractLevelDOWN.call(this, location) | ||
var wstore = require('./localstorage').localStorage; | ||
this.container = new wstore(location); | ||
} | ||
nextTick(callback.bind(null, undefined, key, value)); | ||
}; | ||
util.inherits(ld, AbstractLevelDOWN) | ||
function LD(location) { | ||
if (!(this instanceof LD)) { | ||
return new LD(location); | ||
} | ||
AbstractLevelDOWN.call(this, location); | ||
var Wstore = require('./localstorage').LocalStorage; | ||
this.container = new Wstore(location); | ||
} | ||
ld.prototype._open = function (options, callback) { | ||
setImmediate(function () { callback(null, this) }.bind(this)) | ||
util.inherits(LD, AbstractLevelDOWN); | ||
} | ||
LD.prototype._open = function (options, callback) { | ||
nextTick(function () { | ||
callback(null, this); | ||
}.bind(this)); | ||
}; | ||
ld.prototype._put = function (key, value, options, callback) { | ||
LD.prototype._put = function (key, value, options, callback) { | ||
var err = checkKeyValue(key, 'key') | ||
var err = checkKeyValue(key, 'key'); | ||
if (err) return callback(err) | ||
if (err) { | ||
return callback(err); | ||
} | ||
err = checkKeyValue(value, 'value') | ||
err = checkKeyValue(value, 'value'); | ||
if (err) return callback(err) | ||
if (err) { | ||
return callback(err); | ||
} | ||
if(typeof value == 'object' && !Buffer.isBuffer(value) && value.buffer == undefined){ | ||
var obj = {}; | ||
obj.storetype = "json"; | ||
obj.data = value; | ||
value = JSON.stringify(obj) | ||
} | ||
if (typeof value === 'object' && !Buffer.isBuffer(value) && value.buffer === undefined) { | ||
var obj = {}; | ||
obj.storetype = "json"; | ||
obj.data = value; | ||
value = JSON.stringify(obj); | ||
} | ||
this.container.setItem(key, value); | ||
setImmediate(callback) | ||
} | ||
nextTick(callback); | ||
}; | ||
ld.prototype._get = function (key, options, callback) { | ||
LD.prototype._get = function (key, options, callback) { | ||
var err = checkKeyValue(key, 'key') | ||
var err = checkKeyValue(key, 'key'); | ||
if (err) return callback(err) | ||
if (!isBuffer(key)){ | ||
key = String(key) | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!isBuffer(key)) { | ||
key = String(key); | ||
} | ||
var value = this.container.getItem(key); | ||
@@ -125,13 +121,15 @@ | ||
// 'NotFound' error, consistent with LevelDOWN API | ||
return setImmediate(function () { callback(new Error('NotFound: ')) }) | ||
return nextTick(function () { | ||
callback(new Error('NotFound: ')); | ||
}); | ||
} | ||
if (options.asBuffer !== false && !Buffer.isBuffer(value)){ | ||
value = new Buffer(String(value)) | ||
if (options.asBuffer !== false && !Buffer.isBuffer(value)) { | ||
value = new Buffer(String(value)); | ||
} | ||
if(options.asBuffer === false){ | ||
if(value.indexOf("{\"storetype\":\"json\",\"data\"") > -1){ | ||
if (options.asBuffer === false) { | ||
if (value.indexOf("{\"storetype\":\"json\",\"data\"") > -1) { | ||
var res = JSON.parse(value); | ||
@@ -142,36 +140,44 @@ value = res.data; | ||
setImmediate(function () { | ||
callback(null, value) | ||
}) | ||
} | ||
nextTick(function () { | ||
callback(null, value); | ||
}); | ||
}; | ||
ld.prototype._del = function (key, options, callback) { | ||
var err = checkKeyValue(key, 'key') | ||
LD.prototype._del = function (key, options, callback) { | ||
if (err) return callback(err) | ||
if (!isBuffer(key)) key = String(key) | ||
var err = checkKeyValue(key, 'key'); | ||
this.container.removeItem(key); | ||
setImmediate(callback) | ||
} | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!isBuffer(key)) { | ||
key = String(key); | ||
} | ||
ld.prototype._batch = function (array, options, callback) { | ||
var err | ||
, i = 0 | ||
, key | ||
, value | ||
this.container.removeItem(key); | ||
nextTick(callback); | ||
}; | ||
LD.prototype._batch = function (array, options, callback) { | ||
var err; | ||
var i = 0; | ||
var key; | ||
var value; | ||
if (Array.isArray(array)) { | ||
for (; i < array.length; i++) { | ||
if (array[i]) { | ||
key = Buffer.isBuffer(array[i].key) ? array[i].key : String(array[i].key) | ||
err = checkKeyValue(key, 'key') | ||
if (err) return setImmediate(callback.bind(null, err)) | ||
key = Buffer.isBuffer(array[i].key) ? array[i].key : String(array[i].key); | ||
err = checkKeyValue(key, 'key'); | ||
if (err) { | ||
return nextTick(callback.bind(null, err)); | ||
} | ||
if (array[i].type === 'del') { | ||
this._del(array[i].key, options, noop) | ||
this._del(array[i].key, options, noop); | ||
} else if (array[i].type === 'put') { | ||
value = Buffer.isBuffer(array[i].value) ? array[i].value : String(array[i].value) | ||
err = checkKeyValue(value, 'value') | ||
if (err) return setImmediate(callback.bind(null, err)) | ||
this._put(key, value, options, noop) | ||
value = Buffer.isBuffer(array[i].value) ? array[i].value : String(array[i].value); | ||
err = checkKeyValue(value, 'value'); | ||
if (err) { | ||
return nextTick(callback.bind(null, err)); | ||
} | ||
this._put(key, value, options, noop); | ||
} | ||
@@ -181,115 +187,60 @@ } | ||
} | ||
setImmediate(callback) | ||
} | ||
nextTick(callback); | ||
}; | ||
ld.prototype._iterator = function (options) { | ||
return new ldIterator(this, options) | ||
} | ||
LD.prototype._iterator = function (options) { | ||
return new LDIterator(this, options); | ||
}; | ||
ld.destroy = function (name, callback) { | ||
LD.destroy = function (name, callback) { | ||
try { | ||
Object.keys(localStorage) | ||
.forEach(function (key) { | ||
if (key.substring(0, name.length + 1) == (name + "!")) { | ||
localStorage.removeItem(key) | ||
if (key.substring(0, name.length + 1) === (name + "!")) { | ||
localStorage.removeItem(key); | ||
} | ||
}) | ||
callback() | ||
}); | ||
callback(); | ||
} catch (e) { | ||
// fail gracefully if no localStorage | ||
// fail gracefully if no LocalStorage | ||
} | ||
} | ||
}; | ||
function subarray(start, end) { | ||
return this.slice(start, end) | ||
function isBuffer(buf) { | ||
return buf instanceof ArrayBuffer; | ||
} | ||
function set_(array, offset) { | ||
if (arguments.length < 2) offset = 0 | ||
for (var i = 0, n = array.length; i < n; ++i, ++offset) | ||
this[offset] = array[i] & 0xFF | ||
} | ||
// we need typed arrays | ||
function TypedArray(arg1) { | ||
var result; | ||
if (typeof arg1 === "number") { | ||
result = new Array(arg1); | ||
for (var i = 0; i < arg1; ++i) | ||
result[i] = 0; | ||
} else | ||
result = arg1.slice(0) | ||
result.subarray = subarray | ||
result.buffer = result | ||
result.byteLength = result.length | ||
result.set = set_ | ||
if (typeof arg1 === "object" && arg1.buffer) | ||
result.buffer = arg1.buffer | ||
return result | ||
function checkKeyValue(obj, type) { | ||
if (obj === null || obj === undefined) { | ||
return new Error(type + ' cannot be `null` or `undefined`'); | ||
} | ||
if (obj === null || obj === undefined) { | ||
return new Error(type + ' cannot be `null` or `undefined`'); | ||
} | ||
} | ||
if (type === 'key') { | ||
/* | ||
if(!window.Uint8Array){ | ||
window.Uint8Array = TypedArray; | ||
window.Uint16Array = TypedArray; | ||
window.Uint32Array = TypedArray; | ||
window.Int32Array = TypedArray; | ||
} | ||
*/ | ||
function isBuffer(buf) { | ||
return buf instanceof ArrayBuffer | ||
} | ||
function checkKeyValue (obj, type) { | ||
if (obj === null || obj === undefined) | ||
return new Error(type + ' cannot be `null` or `undefined`') | ||
if (obj === null || obj === undefined) | ||
return new Error(type + ' cannot be `null` or `undefined`') | ||
if(type === 'key'){ | ||
if(obj instanceof Boolean){ | ||
return new Error(type + ' cannot be `null` or `undefined`') | ||
if (obj instanceof Boolean) { | ||
return new Error(type + ' cannot be `null` or `undefined`'); | ||
} | ||
if(obj === ''){ | ||
return new Error(type + ' cannot be empty') | ||
if (obj === '') { | ||
return new Error(type + ' cannot be empty'); | ||
} | ||
/* | ||
if(isBuffer(obj)) { | ||
return new Error(type + 'cannot be an empty Buffer') | ||
}*/ | ||
} | ||
if(obj.toString().indexOf("[object ArrayBuffer]") == 0){ | ||
if(obj.byteLength == 0 || obj.byteLength == undefined){ | ||
return new Error(type + ' cannot be an empty Buffer') | ||
} | ||
} | ||
/*if(obj.toString().indexOf("[object Uint8Array]") == 0){ | ||
if(obj.byteLength == 0 || obj.byteLength == undefined){ | ||
return new Error(type + ' cannot be an empty ArrayBuffer') | ||
} | ||
} */ | ||
if (obj.toString().indexOf("[object ArrayBuffer]") === 0) { | ||
if (obj.byteLength === 0 || obj.byteLength === undefined) { | ||
return new Error(type + ' cannot be an empty Buffer'); | ||
} | ||
} | ||
if (isBuffer(obj)) { | ||
if (obj.length === 0) | ||
return new Error(type + ' cannot be an empty Buffer') | ||
} else if (String(obj) === '') | ||
return new Error(type + ' cannot be an empty String') | ||
if (obj.length === 0) { | ||
return new Error(type + ' cannot be an empty Buffer'); | ||
} | ||
} else if (String(obj) === '') { | ||
return new Error(type + ' cannot be an empty String'); | ||
} | ||
} | ||
module.exports = ld | ||
module.exports = LD; |
@@ -1,102 +0,96 @@ | ||
function localStorage(dbname){ | ||
this._partition = dbname; | ||
this._keys = []; | ||
'use strict'; | ||
for (var i = 0; i < window.localStorage.length; i++){ | ||
if(window.localStorage.key(i).indexOf(dbname + '!') == 0) | ||
this._keys.push(window.localStorage.key(i)) | ||
var arrayBuffPrefix = 'ArrayBuffer:'; | ||
var arrayBuffRegex = new RegExp('^' + arrayBuffPrefix); | ||
var uintPrefix = 'Uint8Array:'; | ||
var uintRegex = new RegExp('^' + uintPrefix); | ||
var utils = require('./utils'); | ||
function LocalStorage(dbname) { | ||
this._keys = []; | ||
this._prefix = dbname + '!'; | ||
var prefixLen = this._prefix.length; | ||
var i = -1; | ||
var len = window.localStorage.length; | ||
while (++i < len) { | ||
var fullKey = window.localStorage.key(i); | ||
if (fullKey.substring(0, prefixLen) === this._prefix) { | ||
this._keys.push(fullKey.substring(prefixLen)); | ||
} | ||
this._keys.sort(); | ||
} | ||
this._keys.sort(); | ||
} | ||
//key: Returns the name of the key at the position specified. | ||
localStorage.prototype.key = function (keyindex){ | ||
var retVal = this._keys[keyindex]; | ||
if(typeof retVal !== 'undefined'){ | ||
// this needs to be a last and first; | ||
return this._keys[keyindex].replace(this._partition + '!', "").replace("!bin"); | ||
}else{ | ||
return retVal; | ||
} | ||
} | ||
LocalStorage.prototype.key = function (keyindex) { | ||
var retVal = this._keys[keyindex]; | ||
if (typeof retVal !== 'undefined') { | ||
// this needs to be a last and first; | ||
retVal = retVal.replace('!bin'); | ||
} | ||
return retVal; | ||
}; | ||
// returns the key index if found, else the index where | ||
// the key should be inserted | ||
LocalStorage.prototype.indexOfKey = function(key) { | ||
return utils.sortedIndexOf(this._keys, key); | ||
}; | ||
//setItem: Saves and item at the key provided. | ||
localStorage.prototype.setItem = function (key, value){ | ||
key = this._partition + "!" + key; | ||
if(value instanceof ArrayBuffer) { | ||
var bencode = "ArrayBuffer:"; | ||
value = bencode + btoa(String.fromCharCode.apply(null, value)) | ||
} | ||
if(value instanceof Uint8Array){ | ||
var bencode = "Uint8Array:"; | ||
value = bencode + btoa(String.fromCharCode.apply(null, value)) | ||
} | ||
for (var i = 0; i < this._keys.length; i++) { | ||
if (this._keys[i] === key) { | ||
window.localStorage.setItem(key, value); | ||
return; | ||
} | ||
} | ||
LocalStorage.prototype.setItem = function (key, value) { | ||
if (value instanceof ArrayBuffer) { | ||
value = arrayBuffPrefix + btoa(String.fromCharCode.apply(null, value)); | ||
} else if (value instanceof Uint8Array) { | ||
value = uintPrefix + btoa(String.fromCharCode.apply(null, value)); | ||
} | ||
this._keys.push(key) | ||
this._keys.sort() | ||
window.localStorage.setItem(key, value); | ||
} | ||
var idx = utils.sortedIndexOf(this._keys, key); | ||
if (this._keys[idx] !== key) { | ||
this._keys.splice(idx, 0, key); | ||
} | ||
window.localStorage.setItem(this._prefix + key, value); | ||
}; | ||
//getItem: Returns the item identified by it's key. | ||
localStorage.prototype.getItem = function (key){ | ||
key = this._partition + "!" + key | ||
var retval = window.localStorage.getItem(key) | ||
if(retval == null){ | ||
return undefined; | ||
} | ||
if(retval.indexOf('ArrayBuffer:') == 0) { | ||
var value = retval.replace("ArrayBuffer:", ""); | ||
retval = new ArrayBuffer(atob(value).split('').map(function(c) { | ||
return c.charCodeAt(0); | ||
})); | ||
return retval; | ||
} | ||
if(retval.indexOf('Uint8Array:') == 0) { | ||
var value = retval.replace("Uint8Array:", ""); | ||
//This should be in but there seems to be a bug in TAPE? | ||
/* | ||
retval = new Uint8Array(atob(value).split('').map(function(c) { | ||
return c.charCodeAt(0); | ||
})); | ||
*/ | ||
return atob(value); | ||
} | ||
LocalStorage.prototype.getItem = function (key) { | ||
var value; | ||
var retval = window.localStorage.getItem(this._prefix + key); | ||
if (retval == null) { | ||
return undefined; | ||
} | ||
if (arrayBuffRegex.test(retval)) { | ||
value = retval.substring(arrayBuffPrefix.length); | ||
retval = new ArrayBuffer(atob(value).split('').map(function (c) { | ||
return c.charCodeAt(0); | ||
})); | ||
return retval; | ||
} | ||
} else if (uintRegex.test(retval)) { | ||
value = retval.substring(uintPrefix.length); | ||
retval = new Uint8Array(atob(value).split('').map(function(c) { | ||
return c.charCodeAt(0); | ||
})); | ||
return retval; | ||
} | ||
return retval; | ||
}; | ||
//removeItem: Removes the item identified by it's key. | ||
localStorage.prototype.removeItem = function (key){ | ||
key = this._partition + "!" + key | ||
for(var i = this._keys.length; i >= 0; i--) { | ||
if(this._keys[i] === key) { | ||
this._keys.splice(i, 1); | ||
window.localStorage.removeItem(key); | ||
} | ||
} | ||
} | ||
LocalStorage.prototype.removeItem = function (key) { | ||
//clear: Removes all of the key value pairs. | ||
localStorage.prototype.clear = function (){ | ||
window.localStorage.clear() | ||
} | ||
var idx = utils.sortedIndexOf(this._keys, key); | ||
if (this._keys[idx] === key) { | ||
this._keys.splice(idx, 1); | ||
window.localStorage.removeItem(this._prefix + key); | ||
} | ||
}; | ||
localStorage.prototype.length = function(){ | ||
LocalStorage.prototype.length = function () { | ||
return this._keys.length; | ||
}; | ||
return this._keys.length | ||
} | ||
exports.localStorage = localStorage; | ||
exports.LocalStorage = LocalStorage; |
@@ -6,3 +6,4 @@ { | ||
"Anton Whalley <antonwhalley@vodafone.ie> (https://github.com/no9)", | ||
"Adam Shih (https://github.com/adamshih)" | ||
"Adam Shih (https://github.com/adamshih)", | ||
"Nolan Lawson (https://github.com/nolanlawson)" | ||
], | ||
@@ -15,10 +16,13 @@ "keywords": [ | ||
], | ||
"version": "0.4.4", | ||
"version": "0.5.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"abstract-leveldown": "0.11.3" | ||
"abstract-leveldown": "~0.12.0" | ||
}, | ||
"devDependencies": { | ||
"beefy": "~1.1.0", | ||
"browserify": "^4.1.2", | ||
"levelup": "^0.18.2", | ||
"tape": "2.3.2" | ||
"tape": "^2.12.3", | ||
"jshint": "^2.5.0" | ||
}, | ||
@@ -32,2 +36,6 @@ "repository": { | ||
}, | ||
"scripts": { | ||
"test": "npm run jshint && beefy tests/test.js", | ||
"jshint": "jshint -c .jshintrc *.js tests/*.js" | ||
}, | ||
"testling": { | ||
@@ -34,0 +42,0 @@ "files": [ |
@@ -22,3 +22,3 @@ # localstorage-down | ||
``` | ||
npm install localstorage-down | ||
npm install localstorage-down | ||
``` | ||
@@ -33,3 +33,2 @@ | ||
npm install levelup | ||
npm install beefy -g | ||
npm install browserify -g | ||
@@ -41,6 +40,5 @@ ``` | ||
``` | ||
var leveldown = require('localstorage-down') | ||
var localstorage = require('localstorage-down') | ||
, levelup = require('levelup') | ||
, factory = function (location) { return new leveldown(location) } | ||
, db = levelup('/does/not/matter', { db: factory }) | ||
, db = levelup('/does/not/matter', { db: localstorage }) | ||
@@ -85,3 +83,3 @@ db.put('name', 'Yuri Irsenovich Kim') | ||
``` | ||
beefy tests/test | ||
npm run test | ||
``` | ||
@@ -97,1 +95,3 @@ | ||
Adam Shih https://github.com/adamshih | ||
Nolan Lawson https://github.com/nolanlawson |
@@ -1,45 +0,47 @@ | ||
var levelup = require('levelup') | ||
'use strict'; | ||
var levelup = require('levelup'); | ||
module.exports.setUp = function (leveldown, test, testCommon) { | ||
test('setUp common', testCommon.setUp) | ||
test('setUp db', function (t) { | ||
db = leveldown(testCommon.location()) | ||
db.open(t.end.bind(t)) | ||
}) | ||
} | ||
test('setUp common', testCommon.setUp); | ||
test('setUp db', function (t) { | ||
var db = leveldown(testCommon.location()); | ||
db.open(t.end.bind(t)); | ||
}); | ||
}; | ||
module.exports.all = function (leveldown, tape, testCommon) { | ||
module.exports.setUp(leveldown, tape, testCommon) | ||
module.exports.setUp(leveldown, tape, testCommon); | ||
tape('test .destroy', function(t) { | ||
var db = levelup('destroy-test', {db: leveldown}) | ||
var db2 = levelup('other-db', {db: leveldown}) | ||
db2.put('key2', 'value2', function (err) { | ||
t.notOk(err, 'no error') | ||
db.put('key', 'value', function (err) { | ||
t.notOk(err, 'no error') | ||
db.get('key', function (err, value) { | ||
t.notOk(err, 'no error') | ||
t.equal(value, 'value', 'should have value') | ||
db.close(function (err) { | ||
t.notOk(err, 'no error') | ||
leveldown.destroy('destroy-test', function (err) { | ||
t.notOk(err, 'no error') | ||
var db3 = levelup('destroy-test', {db: leveldown}) | ||
db3.get('key', function (err, value) { | ||
t.ok(err, 'key is not there') | ||
db2.get('key2', function (err, value) { | ||
t.notOk(err, 'no error') | ||
t.equal(value, 'value2', 'should have value2') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
tape('test .destroy', function(t) { | ||
var db = levelup('destroy-test', {db: leveldown}); | ||
var db2 = levelup('other-db', {db: leveldown}); | ||
db2.put('key2', 'value2', function (err) { | ||
t.notOk(err, 'no error'); | ||
db.put('key', 'value', function (err) { | ||
t.notOk(err, 'no error'); | ||
db.get('key', function (err, value) { | ||
t.notOk(err, 'no error'); | ||
t.equal(value, 'value', 'should have value'); | ||
db.close(function (err) { | ||
t.notOk(err, 'no error'); | ||
leveldown.destroy('destroy-test', function (err) { | ||
t.notOk(err, 'no error'); | ||
var db3 = levelup('destroy-test', {db: leveldown}); | ||
db3.get('key', function (err, value) { | ||
t.ok(err, 'key is not there'); | ||
db2.get('key2', function (err, value) { | ||
t.notOk(err, 'no error'); | ||
t.equal(value, 'value2', 'should have value2'); | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}; |
@@ -1,70 +0,73 @@ | ||
var tape = require('tape') | ||
, leveldown = require('../') | ||
, factory = function (location) { | ||
'use strict'; | ||
return new leveldown(location) | ||
} | ||
, testCommon = require('./testCommon') | ||
, testBuffer = new Uint8Array('hello'.split('').map(function(c) { | ||
return c.charCodeAt(0); | ||
})); | ||
var tape = require('tape'); | ||
var localstorage = require('../'); | ||
var testCommon = require('./testCommon'); | ||
var testBuffer = new Uint8Array('hello'.split('').map(function (c) { | ||
return c.charCodeAt(0); | ||
})); | ||
//testBuffer[0] = '☃' | ||
require('abstract-leveldown/abstract/leveldown-test').args(localstorage, tape); | ||
require('abstract-leveldown/abstract/open-test').args(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/del-test').all(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/put-test').all(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/get-test').all(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/put-get-del-test').all( | ||
localstorage, tape, testCommon, testBuffer); | ||
require('abstract-leveldown/abstract/close-test').close(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/iterator-test').all(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/leveldown-test').args(factory, tape) | ||
require('abstract-leveldown/abstract/open-test').args(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/del-test').all(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/put-test').all(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/get-test').all(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/put-get-del-test').all(factory, tape, testCommon, testBuffer) | ||
require('abstract-leveldown/abstract/close-test').close(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/iterator-test').all(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/chained-batch-test').all(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/approximate-size-test').setUp(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/approximate-size-test').args(localstorage, tape, testCommon); | ||
require('abstract-leveldown/abstract/chained-batch-test').all(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/approximate-size-test').setUp(factory, tape, testCommon) | ||
require('abstract-leveldown/abstract/approximate-size-test').args(factory, tape, testCommon) | ||
// | ||
// TODO: uncomment these when they're passing | ||
// | ||
//require('abstract-leveldown/abstract/ranges-test').all(localstorage, tape, testCommon) | ||
//require('abstract-leveldown/abstract/batch-test').all(localstorage, tape, testCommon) | ||
require('./custom-tests.js').all(leveldown, tape, testCommon) | ||
require('./custom-tests.js').all(localstorage, tape, testCommon); | ||
// we need typed arrays | ||
function TypedArray(arg1) { | ||
var result; | ||
if (typeof arg1 === "number") { | ||
result = new Array(arg1); | ||
for (var i = 0; i < arg1; ++i) { | ||
result[i] = 0; | ||
} | ||
} else { | ||
result = arg1.slice(0); | ||
result.buffer = result; | ||
result.byteLength = result.length; | ||
function subarray(start, end) { | ||
return this.slice(start, end) | ||
} | ||
function set_(array, offset) { | ||
result.subarray = function (start, end) { | ||
return result.slice(start, end); | ||
}; | ||
if (arguments.length < 2) offset = 0 | ||
result.set = function (array, offset) { | ||
for (var i = 0, n = array.length; i < n; ++i, ++offset) | ||
this[offset] = array[i] & 0xFF | ||
} | ||
// we need typed arrays | ||
function TypedArray(arg1) { | ||
var result; | ||
if (typeof arg1 === "number") { | ||
result = new Array(arg1); | ||
for (var i = 0; i < arg1; ++i) | ||
result[i] = 0; | ||
} else | ||
result = arg1.slice(0) | ||
if (arguments.length < 2) { | ||
offset = 0; | ||
} | ||
result.subarray = subarray | ||
result.buffer = result | ||
result.byteLength = result.length | ||
result.set = set_ | ||
for (var i = 0, n = array.length; i < n; ++i, ++offset) { | ||
result[offset] = array[i] & 0xFF; | ||
} | ||
}; | ||
} | ||
if (typeof arg1 === "object" && arg1.buffer) | ||
result.buffer = arg1.buffer | ||
return result | ||
if (typeof arg1 === "object" && arg1.buffer) { | ||
result.buffer = arg1.buffer; | ||
} | ||
return result; | ||
} | ||
if (!window.Uint8Array) { | ||
window.Uint8Array = TypedArray; | ||
window.Uint32Array = TypedArray; | ||
window.Int32Array = TypedArray; | ||
} | ||
if(!window.Uint8Array){ | ||
window.Uint8Array = TypedArray; | ||
window.Uint32Array = TypedArray; | ||
window.Int32Array = TypedArray; | ||
} |
@@ -1,111 +0,89 @@ | ||
var dbidx = 0 | ||
'use strict'; | ||
, location = function () { | ||
return '_leveldown_test_db_' + dbidx++ | ||
} | ||
var dbidx = 0; | ||
var theLocation = function () { | ||
return '_leveldown_test_db_' + dbidx++; | ||
}; | ||
, lastLocation = function () { | ||
return '_leveldown_test_db_' + dbidx | ||
} | ||
var lastLocation = function () { | ||
return '_leveldown_test_db_' + dbidx; | ||
}; | ||
, cleanup = function (callback) { | ||
var cleanup = function (callback) { | ||
if(window.localStorage){ | ||
window.localStorage.clear() | ||
} | ||
return callback(); | ||
//TODO indexDB needs replacing with something else | ||
indexedDB.webkitGetDatabaseNames().onsuccess = function(e, list){ | ||
if (!list) return callback() | ||
list = list.filter(function (f) { | ||
return (/^_leveldown_test_db_/).test(f) | ||
}) | ||
if (window.localStorage) { | ||
window.localStorage.clear(); | ||
} | ||
if (!list.length) return callback() | ||
return callback(); | ||
}; | ||
var ret = 0 | ||
function done () { | ||
if (++ret == list.length) | ||
callback() | ||
} | ||
list.forEach(function (f) { | ||
indexedDB.deleteDatabase(f) | ||
.onsuccess = done | ||
.onerror = done | ||
}) | ||
} | ||
} | ||
var setUp = function (t) { | ||
cleanup(function (err) { | ||
t.notOk(err, 'cleanup returned an error'); | ||
t.end(); | ||
}); | ||
}; | ||
, setUp = function (t) { | ||
cleanup(function (err) { | ||
t.notOk(err, 'cleanup returned an error') | ||
t.end() | ||
}) | ||
} | ||
var tearDown = function (t) { | ||
setUp(t); // same cleanup! | ||
}; | ||
, tearDown = function (t) { | ||
setUp(t) // same cleanup! | ||
} | ||
var collectEntries = function (iterator, callback) { | ||
var data = []; | ||
var next = function () { | ||
iterator.next(function (err, key, value) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if ((!arguments.length) || (key === undefined) || (key === null)) { | ||
return iterator.end(function (err) { | ||
callback(err, data); | ||
}); | ||
} | ||
, collectEntries = function (iterator, callback) { | ||
var data = [] | ||
, next = function () { | ||
iterator.next(function (err, key, value) { | ||
if (err) return callback(err) | ||
if ((!arguments.length) || (key === undefined) || (key === null)) { | ||
return iterator.end(function (err) { | ||
callback(err, data) | ||
}) | ||
} | ||
data.push({ key: key, value: value }) | ||
process.nextTick(next) | ||
}) | ||
} | ||
next() | ||
} | ||
data.push({ key: key, value: value }); | ||
process.nextTick(next); | ||
}); | ||
}; | ||
next(); | ||
}; | ||
, makeExistingDbTest = function (name, test, leveldown, testFn) { | ||
test(name, function (t) { | ||
cleanup(function () { | ||
var loc = location() | ||
, db = leveldown(loc) | ||
, done = function (close) { | ||
if (close === false) | ||
return cleanup(t.end.bind(t)) | ||
db.close(function (err) { | ||
t.notOk(err, 'no error from close()') | ||
cleanup(t.end.bind(t)) | ||
}) | ||
} | ||
db.open(function (err) { | ||
t.notOk(err, 'no error from open()') | ||
db.batch( | ||
[ | ||
{ type: 'put', key: 'one', value: '1' } | ||
, { type: 'put', key: 'two', value: '2' } | ||
, { type: 'put', key: 'three', value: '3' } | ||
] | ||
, function (err) { | ||
t.notOk(err, 'no error from batch()') | ||
testFn(db, t, done, loc) | ||
} | ||
) | ||
}) | ||
}) | ||
}) | ||
} | ||
var makeExistingDbTest = function (name, test, leveldown, testFn) { | ||
test(name, function (t) { | ||
cleanup(function () { | ||
var loc = location(); | ||
var db = leveldown(loc); | ||
var done = function (close) { | ||
if (close === false) { | ||
return cleanup(t.end.bind(t)); | ||
} | ||
db.close(function (err) { | ||
t.notOk(err, 'no error from close()'); | ||
cleanup(t.end.bind(t)); | ||
}); | ||
}; | ||
db.open(function (err) { | ||
t.notOk(err, 'no error from open()'); | ||
db.batch([ | ||
{ type: 'put', key: 'one', value: '1' }, | ||
{ type: 'put', key: 'two', value: '2' }, | ||
{ type: 'put', key: 'three', value: '3' } | ||
], function (err) { | ||
t.notOk(err, 'no error from batch()'); | ||
testFn(db, t, done, loc); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
module.exports = { | ||
location : location | ||
, cleanup : cleanup | ||
, lastLocation : lastLocation | ||
, setUp : setUp | ||
, tearDown : tearDown | ||
, collectEntries : collectEntries | ||
, makeExistingDbTest : makeExistingDbTest | ||
} | ||
location: theLocation, | ||
cleanup: cleanup, | ||
lastLocation: lastLocation, | ||
setUp: setUp, | ||
tearDown: tearDown, | ||
collectEntries: collectEntries, | ||
makeExistingDbTest: makeExistingDbTest | ||
}; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
42781
22
5
468
2
+ Addedabstract-leveldown@0.12.4(transitive)
+ Addedxtend@3.0.0(transitive)
- Removedabstract-leveldown@0.11.3(transitive)
- Removedobject-keys@0.4.0(transitive)
- Removedxtend@2.1.2(transitive)
Updatedabstract-leveldown@~0.12.0