localstorage-down
Advanced tools
Comparing version 0.6.3 to 0.6.4
57
index.js
@@ -79,34 +79,36 @@ 'use strict'; | ||
if (!self.initStarted) { | ||
self.initStarted = true; | ||
self._init(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
self.db.container.keys(function (err, keys) { | ||
process.nextTick(function () { | ||
self.initStarted = true; | ||
self._init(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
self._keys = keys; | ||
if (self._startkey) { | ||
var index = utils.sortedIndexOf(self._keys, self._startkey); | ||
var startkey = (index >= self._keys.length || index < 0) ? | ||
undefined : self._keys[index]; | ||
self._pos = index; | ||
if (self._reverse) { | ||
if (self._exclusiveStart || startkey !== self._startkey) { | ||
self._pos--; | ||
self.db.container.keys(function (err, keys) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
self._keys = keys; | ||
if (self._startkey) { | ||
var index = utils.sortedIndexOf(self._keys, self._startkey); | ||
var startkey = (index >= self._keys.length || index < 0) ? | ||
undefined : self._keys[index]; | ||
self._pos = index; | ||
if (self._reverse) { | ||
if (self._exclusiveStart || startkey !== self._startkey) { | ||
self._pos--; | ||
} | ||
} else if (self._exclusiveStart && startkey === self._startkey) { | ||
self._pos++; | ||
} | ||
} else if (self._exclusiveStart && startkey === self._startkey) { | ||
self._pos++; | ||
} else { | ||
self._pos = self._reverse ? self._keys.length - 1 : 0; | ||
} | ||
} else { | ||
self._pos = self._reverse ? self._keys.length - 1 : 0; | ||
} | ||
onInitComplete(); | ||
onInitComplete(); | ||
self.initCompleted = true; | ||
var i = -1; | ||
while (++i < self.onInitCompleteListeners) { | ||
nextTick(self.onInitCompleteListeners[i]); | ||
} | ||
self.initCompleted = true; | ||
var i = -1; | ||
while (++i < self.onInitCompleteListeners.length) { | ||
nextTick(self.onInitCompleteListeners[i]); | ||
} | ||
}); | ||
}); | ||
@@ -117,3 +119,3 @@ }); | ||
} else { | ||
onInitComplete(); | ||
process.nextTick(onInitComplete); | ||
} | ||
@@ -300,3 +302,2 @@ }; | ||
module.exports = LD; |
@@ -14,2 +14,7 @@ 'use strict'; | ||
// We use humble-localstorage as a wrapper for localStorage because | ||
// it falls back to an in-memory implementation in environments without | ||
// localStorage, like Node or Safari private browsing. | ||
var storage = require('humble-localstorage'); | ||
function callbackify(callback, fun) { | ||
@@ -42,5 +47,5 @@ var val; | ||
var i = -1; | ||
var len = window.localStorage.length; | ||
var len = storage.length; | ||
while (++i < len) { | ||
var fullKey = window.localStorage.key(i); | ||
var fullKey = storage.key(i); | ||
if (fullKey.substring(0, prefixLen) === self._prefix) { | ||
@@ -58,3 +63,3 @@ keys.push(fullKey.substring(prefixLen)); | ||
callbackify(callback, function () { | ||
window.localStorage.setItem(self._prefix + key, value); | ||
storage.setItem(self._prefix + key, value); | ||
}); | ||
@@ -66,3 +71,3 @@ }; | ||
callbackify(callback, function () { | ||
return window.localStorage.getItem(self._prefix + key); | ||
return storage.getItem(self._prefix + key); | ||
}); | ||
@@ -74,3 +79,3 @@ }; | ||
callbackify(callback, function () { | ||
window.localStorage.removeItem(self._prefix + key); | ||
storage.removeItem(self._prefix + key); | ||
}); | ||
@@ -82,7 +87,10 @@ }; | ||
callbackify(callback, function () { | ||
Object.keys(localStorage).forEach(function (key) { | ||
if (key.substring(0, prefix.length) === prefix) { | ||
localStorage.removeItem(key); | ||
var len = storage.length; | ||
var i = -1; | ||
while (++i < len) { | ||
var key = storage.key(i); | ||
if (key && key.substring(0, prefix.length) === prefix) { | ||
storage.removeItem(key); | ||
} | ||
}); | ||
} | ||
}); | ||
@@ -89,0 +97,0 @@ }; |
@@ -15,3 +15,3 @@ { | ||
], | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"main": "index.js", | ||
@@ -22,2 +22,3 @@ "dependencies": { | ||
"d64": "^1.0.0", | ||
"humble-localstorage": "^1.4.2", | ||
"inherits": "^2.0.1", | ||
@@ -27,7 +28,10 @@ "tiny-queue": "0.2.0" | ||
"devDependencies": { | ||
"beefy": "~1.1.0", | ||
"browserify": "^4.1.2", | ||
"es5-shim": "^4.3.1", | ||
"istanbul": "^0.4.1", | ||
"istanbul-coveralls": "^1.0.3", | ||
"jshint": "^2.5.0", | ||
"levelup": "^0.18.2", | ||
"tape": "^2.12.3", | ||
"jshint": "^2.5.0" | ||
"zuul": "^3.7.3" | ||
}, | ||
@@ -42,4 +46,8 @@ "repository": { | ||
"scripts": { | ||
"test": "npm run jshint && beefy tests/test.js", | ||
"jshint": "jshint -c .jshintrc *.js tests/*.js" | ||
"test": "npm run jshint && tape tests/*.js", | ||
"test-saucelabs": "npm run jshint && zuul --ui tape --browser-name $BROWSER_NAME --browser-version $BROWSER_VERSION tests/*.js", | ||
"test-zuul-local": "npm run jshint && zuul --ui tape --local tests/*.js", | ||
"jshint": "jshint -c .jshintrc *.js tests/*.js", | ||
"coverage": "istanbul cover ./node_modules/.bin/tape tests/*.js && istanbul check-coverage --lines 50 --function 50 --statements 50 --branches 50", | ||
"report-coverage": "npm run coverage && istanbul-coveralls --no-rm" | ||
}, | ||
@@ -46,0 +54,0 @@ "testling": { |
@@ -1,19 +0,12 @@ | ||
# localstorage-down | ||
# localstorage-down [![Build Status](https://travis-ci.org/No9/localstorage-down.svg)](https://travis-ci.org/No9/localstorage-down) [![Coverage Status](https://coveralls.io/repos/No9/localstorage-down/badge.svg?branch=master&service=github)](https://coveralls.io/github/No9/localstorage-down?branch=master) | ||
Localstorage implementation of leveldown for mobile and desktop browsers. | ||
LocalStorage implementation of [leveldown](https://github.com/Level/leveldown) for mobile and desktop browsers. The idea is to be able to use the [level](http://github.com/level) stack on any browser that supports LocalStorage. | ||
The idea is to be able to use the level stack on phone and desktops. | ||
The scenarios envisaged are: | ||
The scenarios envisaged are : | ||
1. Occasionally connected clients | ||
2. Ad-hoc networks where clients need to sync directly with each other | ||
2. Adhoc Networks where clients need to sync directly with each other. | ||
This project is intended for use with the [level eco-system](https://github.com/level/). | ||
## Status | ||
[![browser support](https://ci.testling.com/no9/localstorage-down.png)](https://ci.testling.com/no9/localstorage-down) | ||
## Install | ||
@@ -27,3 +20,3 @@ | ||
Basically we support [any browser that has localStorage](http://caniuse.com/namevalue-storage), but since we also rely on an ES5 environment due to dependencies from abstract-leveldown, in practice you will need the following shims in order to work correctly on all browsers (e.g. IE 8/9): | ||
Basically we support [any browser that has LocalStorage](http://caniuse.com/namevalue-storage), but since we also rely on an ES5 environment due to dependencies from [abstract-leveldown](https://github.com/Level/abstract-leveldown), in practice you will need the following shims in order to work correctly on all browsers (e.g. IE 8-9, Android 2.3): | ||
@@ -33,2 +26,15 @@ * [typedarray](https://github.com/substack/typedarray) for binary storage | ||
We run [automated tests](http://travis-ci.org/No9/localstorage-down) in the following browsers: | ||
* **Firefox**: 40-latest | ||
* **Chrome**: 44-beta | ||
* **IE**: 9-11 | ||
* **Edge**: latest | ||
* **Safari**: 7.0-latest | ||
* **iPhone**: 8.0-latest | ||
* **Android**: 4.1-latest | ||
In environments without LocalStorage, such as Node or Safari private browsing, this module | ||
will fall back to a temporary in-memory implementation, thanks to [humble-localstorage](https://www.npmjs.com/package/humble-localstorage). | ||
## Example | ||
@@ -38,20 +44,18 @@ | ||
``` | ||
npm install localstorage-down | ||
npm install levelup | ||
npm install browserify -g | ||
npm install beefy -g | ||
``` | ||
npm install localstorage-down | ||
npm install levelup | ||
npm install browserify -g | ||
npm install beefy -g | ||
Create a file called index.js and enter the following: | ||
Create a file called `index.js` and enter the following: | ||
``` | ||
```js | ||
var localstorage = require('localstorage-down'); | ||
var levelup = require('levelup'); | ||
var db = levelup('/does/not/matter', { db: localstorage }); | ||
var db = levelup('dbname', { db: localstorage }); | ||
db.put('name', 'Yuri Irsenovich Kim'); | ||
db.put('dob', '16 February 1941'); | ||
db.put('spouse', 'Kim Young-sook'); | ||
db.put('occupation', 'Clown'); | ||
db.put('name', 'James Dean'); | ||
db.put('dob', 'February 8, 1931'); | ||
db.put('occupation', 'Rebel'); | ||
db.put('cause', 'none'); | ||
@@ -77,5 +81,3 @@ db.readStream() | ||
``` | ||
beefy index.js | ||
``` | ||
beefy index.js | ||
@@ -86,3 +88,3 @@ See the output : | ||
Listen to John Cage : | ||
Listen to John Cage: | ||
@@ -93,15 +95,27 @@ http://www.youtube.com/watch?v=ExUosomc8Uc | ||
``` | ||
npm run test | ||
``` | ||
npm run test | ||
Browse to [http://localhost:9966](http://localhost:9966). | ||
View console logs in the browser to see test output. | ||
This will run tests in Node against `localstorage-memory`. | ||
## Contributors | ||
To test in Saucelabs, you can run e.g.: | ||
Anton Whalley https://github.com/no9 | ||
BROWSER_NAME=firefox BROWSER_VERSION="38..latest" npm run test-saucelabs | ||
Adam Shih https://github.com/adamshih | ||
Or to test in Zuul locally: | ||
Nolan Lawson https://github.com/nolanlawson | ||
npm run test-zuul-local | ||
To test and check code coverage, run: | ||
npm run coverage | ||
To test and report code coverage to Coveralls, run: | ||
npm run report-coverage | ||
## Contributors | ||
* [Anton Whalley](https://github.com/no9) | ||
* [Adam Shih](https://github.com/adamshih) | ||
* [Nolan Lawson](https://github.com/nolanlawson) | ||
* [Many more!](https://github.com/No9/localstorage-down/graphs/contributors) |
@@ -21,3 +21,3 @@ 'use strict'; | ||
db2.put('key2', 'value2', function (err) { | ||
t.notOk(err, 'no error'); | ||
t.notOk(err, 'no error' ); | ||
db.put('key', 'value', function (err) { | ||
@@ -192,2 +192,35 @@ t.notOk(err, 'no error'); | ||
}); | ||
tape('next() callback is dezalgofied', function (t) { | ||
var db = leveldown('aaaaaa'); | ||
var noerr = function (err) { | ||
t.error(err, 'opens crrectly'); | ||
}; | ||
var noop = function () {}; | ||
var iterator; | ||
db.open(noerr); | ||
db.put('1', '1', noop); | ||
db.put('2', '2', noop); | ||
iterator = db.iterator({ keyAsBuffer: false, valueAsBuffer: false, start: '1' }); | ||
var zalgoReleased = false; | ||
iterator.next(function (err, key, value) { | ||
zalgoReleased = true; | ||
t.notOk(err, 'no error'); | ||
var zalgoReleased2 = false; | ||
iterator.next(function (err, key, value) { | ||
zalgoReleased2 = true; | ||
t.notOk(err, 'no error'); | ||
var zalgoReleased3 = false; | ||
iterator.next(function (err, key, value) { | ||
zalgoReleased3 = true; | ||
t.notOk(err, 'no error'); | ||
t.end(); | ||
}); | ||
t.ok(!zalgoReleased3, 'zalgo not released (3)'); | ||
}); | ||
t.ok(!zalgoReleased2, 'zalgo not released (2)'); | ||
}); | ||
t.ok(!zalgoReleased, 'zalgo not released (1)'); | ||
}); | ||
}; |
'use strict'; | ||
require('es5-shim'); | ||
var tape = require('tape'); | ||
@@ -4,0 +6,0 @@ var localstorage = require('../'); |
'use strict'; | ||
var storage = require('humble-localstorage'); | ||
var dbidx = 0; | ||
@@ -13,7 +15,4 @@ var theLocation = function () { | ||
var cleanup = function (callback) { | ||
storage.clear(); | ||
if (window.localStorage) { | ||
window.localStorage.clear(); | ||
} | ||
return callback(); | ||
@@ -20,0 +19,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
74551
27
799
116
6
8
+ Addedhumble-localstorage@^1.4.2
+ Addedhas-localstorage@1.0.1(transitive)
+ Addedhumble-localstorage@1.4.2(transitive)
+ Addedlocalstorage-memory@1.0.3(transitive)