Repository: marcuswestin/store.js
Branch: master
Commit: fe77ae9e7ca1
Files: 66
Total size: 117.3 KB
Directory structure:
gitextract_elajj8ig/
├── .circleci/
│ └── config.yml
├── .gitignore
├── COPYRIGHT
├── Changelog
├── LICENSE
├── Makefile
├── README-More.md
├── README.md
├── dist/
│ ├── store.everything.js
│ ├── store.legacy.js
│ ├── store.modern.js
│ ├── store.tests.js
│ └── store.v1-backcompat.js
├── package.json
├── plugins/
│ ├── all.js
│ ├── all_tests.js
│ ├── compression.js
│ ├── compression_test.js
│ ├── defaults.js
│ ├── defaults_test.js
│ ├── dump.js
│ ├── dump_test.js
│ ├── events.js
│ ├── events_test.js
│ ├── expire.js
│ ├── expire_test.js
│ ├── json2.js
│ ├── json2_test.js
│ ├── lib/
│ │ ├── json2.js
│ │ └── lz-string.js
│ ├── observe.js
│ ├── observe_test.js
│ ├── operations.js
│ ├── operations_test.js
│ ├── update.js
│ ├── update_test.js
│ ├── v1-backcompat.js
│ └── v1-backcompat_test.js
├── scripts/
│ ├── compile-builds.js
│ ├── create-tunnel.js
│ ├── release.sh
│ ├── run-browser-tests-live-reload.js
│ ├── run-node-tests.js
│ ├── run-saucelabs-tests.js
│ └── saucelabs/
│ ├── list-supported-browsers.js
│ ├── saucelabs-api.js
│ ├── saucelabs-platformSets.js
│ ├── saucelabs.js
│ └── tunnel.js
├── src/
│ ├── store-engine.js
│ └── util.js
├── storages/
│ ├── all.js
│ ├── cookieStorage.js
│ ├── localStorage.js
│ ├── memoryStorage.js
│ ├── oldFF-globalStorage.js
│ ├── oldIE-userDataStorage.js
│ └── sessionStorage.js
├── sublime-storejs.sublime-project
└── tests/
├── bugs/
│ ├── all.js
│ ├── gh-215.js
│ ├── gh-235.js
│ ├── gh-236.js
│ └── gh-239.js
├── tests.js
└── util.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .circleci/config.yml
================================================
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:7.10
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mongo:3.4.4
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
# run tests!
- run: yarn test
================================================
FILE: .gitignore
================================================
node_modules
npm-debug.log
*.sublime-workspace
================================================
FILE: COPYRIGHT
================================================
Copyright (c) 2023 - Marcus Westin
================================================
FILE: Changelog
================================================
v2.0.0
+ New pluggable architecture!
+ New storages: localStorage, cookieStorage, memoryStorage, sessionStorage, oldFF-globalStorage, oldIE-userDataStorage.
+ New plugins: defaults, dump, events, expire, json2, observe, operations, update, v1-backcompat.
+ New builds: everything, legacy, modern, v1-backcompat, minimal (17k, 13k, 12k, 6k respectively)
v1.3.20
+ Fully automated test framework
+ Fix infinite loop in IE
v1.3.19
+ Use umdjs to export store.js (#116, #111, #96, #91, #78, ...)
+ Make compatible with "use strict" mode (#111)
+ Fix store.clear in legacy-IE code path (#103)
+ Roadmap for v1.4.x and v2.x.x
v1.3.17
+ Add store.has
+ Add store.get default value, e.g `store.get('foo', 1)`
+ Fix IE strict mode compatability issue (#105)
+ Added store.version
v1.3.16
+ Improve environment/module.exports detection (#88, github.com/benrudolph)
v1.3.15
+ Enable inlining the minified build
+ Fix AMD issue (https://github.com/marcuswestin/store.js/issues/85)
+ Fix for keys starting with a digit in IE7 (https://github.com/marcuswestin/store.js/issues/83)
v1.3.14
+ Makefile
+ Fix old-IE getAll/forEach, actually this time. I think
v1.3.12
+ Fix old-IE forEach again. Hrm...
v1.3.11
+ Fix old-IE forEach
v1.3.10
+ Add store.forEach
+ Add bower.json (sign, I know, yet another package file to maintain)
+ Add MIT license header
v1.3.9
+ Make store.js work in Node.js (using any working localStorage shim for Node.js)
v1.3.8
+ Fix closing tag for IE7 (GH issue #68)
v1.3.7
+ Fix store.getAll for IE6
v1.3.6
+ Remove globalStorage and drop FF 2.0/3.0 support (See https://github.com/marcuswestin/store.js/issues/44)
v1.3.5
+ Now store.set returns the set value: `store.set(key, value) == value`
+ Values previously set with localStorage directly are now parsed handler by store.js: `localStorage['foo'] = 1; assert(store.get('foo') == 1)`
v1.3.4
+ Add store.enabled
+ Deprecate store.disabled
+ Add link to Jack Franklin's screencast
v1.3.3
+ Fix IE keys beginning with numeric characters (nice find @pauldwaite)
v1.3.2
+ Implement store.getAll() (patch by @blq)
v1.3.0
+ Use uglify.js for minifying store.min.js and store+json.min.js
+ Add build script
v1.2.0
+ Remove same-path restrictions in IE6/7! (Thanks @mjpizz!)
+ Support CommonJS and AMD module systems (Thanks @pereckerdal!)
+ Fix: store.set('foo', undefined); store.get('foo') no longer throws (Thanks @buger!)
v1.1.1
+ Publish in npm as "store" rather than "store.js"
+ Add commonjs export for require support
+ Add supported browsers Chrome 6-11, Firefox 4.0
v1.1.0
+ First versioned version.
+ API: store.set, store.get, store.remove, store.clear, store.transact
+ Minified versions are included: store.min.js for store.js only, and store+json2.min.js for store.js and json2.js
TODO
- Get around IE6/7 per-directory restrition. @lrbabe/@louis_remi had the idea of putting the store.js API in an anonymous iframe a la https://github.com/meebo/embed-code and see what directory restriction that would fall under
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2010-2017 Marcus Westin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
================================================
FILE: Makefile
================================================
test: test-node lint
install:
yarn install
test-node:
node scripts/run-node-tests.js
test-browser:
node scripts/run-browser-tests-live-reload.js
test-full: build test
node scripts/run-saucelabs-tests.js
test-ie: build test
node scripts/run-saucelabs-tests.js ie
test-safari: build test
node scripts/run-saucelabs-tests.js safari
test-firefox: build test
node scripts/run-saucelabs-tests.js firefox
test-chrome: build test
node scripts/run-saucelabs-tests.js chrome
test-android: build test
node scripts/run-saucelabs-tests.js android
test-ios: build test
node scripts/run-saucelabs-tests.js ios
test-opera: build test
node scripts/run-saucelabs-tests.js opera
tunnel:
node scripts/create-tunnel.js
build:
node scripts/compile-builds.js
lint:
./node_modules/.bin/eslint tests/* src/* plugins/* storages/* \
--ignore-pattern src/addon/lib/json2.js \
================================================
FILE: README-More.md
================================================
How does it work?
------------------
store.js uses localStorage when available, and falls back on the userData behavior in IE6 and IE7. No flash to slow down your page load. No cookies to fatten your network requests.
store.js depends on JSON for serialization to disk.
Installation
------------
Just grab [store.min.js] or [store+json2.min.js] and include them with a script tag.
`store.enabled` flag
--------------------
If your product depends on store.js, you must check the `store.enabled` flag first:
```html
```
LocalStorage may sometimes appear to be available but throw an error when used. An example is Safari's private browsing mode. Other browsers allow the user to temporarily disable localStorage. Store.js detects these conditions and sets the `store.enabled` flag appropriately.
Screencast
-----------
[Introductory Screencast to Store.js](http://javascriptplayground.com/blog/2012/06/javascript-local-storage-store-js-tutorial) by Jack Franklin.
Contributors & Forks
--------------------
Contributors: https://github.com/marcuswestin/store.js/graphs/contributors
Forks: https://github.com/marcuswestin/store.js/network/members
In node.js
----------
store.js works as expected in node.js, assuming that global.localStorage has been set:
```
global.localStorage = require('localStorage')
var store = require('./store')
store.set('foo', 1)
console.log(store.get('foo'))
```
Supported browsers
------------------
- Tested in iOS 4+
- Tested in Firefox 3.5
- Tested in Firefox 3.6
- Tested in Firefox 4.0+
- Support dropped for Firefox < 3.5 (see notes below)
- Tested in Chrome 5
- Tested in Chrome 6
- Tested in Chrome 7
- Tested in Chrome 8
- Tested in Chrome 10
- Tested in Chrome 11+
- Tested in Safari 4
- Tested in Safari 5
- Tested in IE6
- Tested in IE7
- Tested in IE8
- Tested in IE9
- Tested in IE10
- Tested in Opera 10
- Tested in Opera 11
- Tested in Opera 12
- Tested in Node.js v0.10.4 (with https://github.com/coolaj86/node-localStorage 1.0.2)
*Private mode* Store.js may not work while browsing in private mode. This is as it should be. Check the `store.enabled` flag before relying on store.js.
*Saucelabs.com rocks* Extensive browser testing of store.js is possible thanks to Saucelabs.com. Check them out, they're awesome.
*Firefox 3.0 & 2.0:* Support for FF 2 & 3 was dropped in v1.3.6. If you require support for ancient versions of FF, use v1.3.5 of store.js.
*Important note:* In IE6 and IE7, many special characters are not allowed in the keys used to store any key/value pair. With [@mferretti](https://github.com/mferretti)'s help, there's a suitable workaround which replaces most forbidden characters with "___".
Storage limits
--------------
- IE6 & IE7: 1MB total, but 128kb per "path" or "document" (see http://msdn.microsoft.com/en-us/library/ms531424(v=vs.85).aspx)
- See http://dev-test.nemikor.com/web-storage/support-test/ for a list of limits per browser
Unsupported browsers
-------------------
- Firefox 1.0: no means (beside cookies and flash)
- Safari 2: no means (beside cookies and flash)
- Safari 3: no synchronous api (has asynch sqlite api, but store.js is synch)
- Opera 9: don't know if there is synchronous api for storing data locally
- Firefox 1.5: don't know if there is synchronous api for storing data locally
- Microsoft IIS & IE7: With meta tag & "charset=iso-8859-1", things stop working. See issue #47.
Some notes on serialization
---------------------------
localStorage, when used without store.js, calls toString on all stored values. This means that you can't conveniently store and retrieve numbers, objects or arrays:
```js
localStorage.myage = 24
localStorage.myage !== 24
localStorage.myage === '24'
localStorage.user = { name: 'marcus', likes: 'javascript' }
localStorage.user === "[object Object]"
localStorage.tags = ['javascript', 'localStorage', 'store.js']
localStorage.tags.length === 32
localStorage.tags === "javascript,localStorage,store.js"
```
What we want (and get with store.js) is
```js
store.set('myage', 24)
store.get('myage') === 24
store.set('user', { name: 'marcus', likes: 'javascript' })
alert("Hi my name is " + store.get('user').name + "!")
store.set('tags', ['javascript', 'localStorage', 'store.js'])
alert("We've got " + store.get('tags').length + " tags here")
```
The native serialization engine of javascript is JSON. Rather than leaving it up to you to serialize and deserialize your values, store.js uses JSON.stringify() and JSON.parse() on each call to store.set() and store.get(), respectively.
Some browsers do not have native support for JSON. For those browsers you should include [JSON2.js] \(non-minified copy is included in this repo).
No sessionStorage/auto-expiration?
----------------------------------
No. I believe there is no way to provide sessionStorage semantics cross browser. However, it is trivial to expire values on read on top of store.js:
```js
var storeWithExpiration = {
set: function(key, val, exp) {
store.set(key, { val:val, exp:exp, time:new Date().getTime() })
},
get: function(key) {
var info = store.get(key)
if (!info) { return null }
if (new Date().getTime() - info.time > info.exp) { return null }
return info.val
}
}
storeWithExpiration.set('foo', 'bar', 1000)
setTimeout(function() { console.log(storeWithExpiration.get('foo')) }, 500) // -> "bar"
setTimeout(function() { console.log(storeWithExpiration.get('foo')) }, 1500) // -> null
```
[JSON2.js]: https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js
[store.min.js]: https://raw.github.com/marcuswestin/store.js/master/store.min.js
[store+json2.min.js]: https://raw.github.com/marcuswestin/store.js/master/store+json2.min.js
================================================
FILE: README.md
================================================
Store.js
========
Cross-browser storage for all use cases, used across the web.
[](https://circleci.com/gh/marcuswestin/store.js)
[](https://badge.fury.io/js/store)
[](https://npm-stat.com/charts.html?package=store)
Store.js has been around since 2010 ([first commit](https://github.com/marcuswestin/store.js/commit/cb0198c2c02ff5f17c084276eeb4f28c79849d5e), [v1 release](https://news.ycombinator.com/item?id=1468802)). It is used in production on tens of thousands of websites, such as cnn.com, dailymotion.com, & many more.
Store.js provides basic key/value storage functionality (`get/set/remove/each`) as well as a rich set of plug-in [storages](#user-content-storages) and extra [functionality](#user-content-plugins).
1. [Basic Usage](#user-content-basic-usage)
- All you need to get started
- [API](#user-content-api)
- [Installation](#user-content-installation)
2. [Supported Browsers](#user-content-supported-browsers)
- All of them, pretty much :)
- [List of supported browsers](#user-content-list-of-supported-browsers)
3. [Plugins](#user-content-plugins)
- Additional common functionality
- [List of all Plugins](#user-content-list-of-all-plugins)
- [Using Plugins](#user-content-using-plugins)
- [Write your own Plugin](#user-content-write-your-own-plugin)
4. [Builds](#user-content-builds)
- Choose which build is right for you
- [List of default Builds](#user-content-list-of-default-builds)
- [Make your own Build](#user-content-make-your-own-build)
5. [Storages](#user-content-storages)
- Storages provide underlying persistence
- [List of all Storages](#user-content-list-of-all-storages)
- [Storages limits](#user-content-storages-limits)
- [Write your own Storage](#user-content-write-your-own-storage)
Basic Usage
-----------
All you need to know to get started:
### API
store.js exposes a simple API for cross-browser local storage:
```js
// Store current user
store.set('user', { name:'Marcus' })
// Get current user
store.get('user')
// Remove current user
store.remove('user')
// Clear all keys
store.clearAll()
// Loop over all stored values
store.each(function(value, key) {
console.log(key, '==', value)
})
```
### Installation
Using npm:
```sh
npm i store
```
```js
// Example store.js usage with npm
var store = require('store')
store.set('user', { name:'Marcus' })
store.get('user').name == 'Marcus'
```
Using script tag (first download one of the [builds](dist/)):
```html
```
Supported Browsers
------------------
All of them, pretty much :)
To support all browsers (including IE 6, IE 7, Firefox 4, etc.), use `require('store')` (alias for `require('store/dist/store.legacy')`) or [store.legacy.min.js](dist/store.legacy.min.js).
To save some kilobytes but still support all modern browsers, use `require('store/dist/store.modern')` or [store.modern.min.js](dist/store.modern.min.js) instead.
### List of supported browsers
- Tested on IE6+
- Tested on iOS 8+
- Tested on Android 4+
- Tested on Firefox 4+
- Tested on Chrome 27+
- Tested on Safari 5+
- Tested on Opera 11+
- Tested on Node (with https://github.com/coolaj86/node-localStorage)
Plugins
-------
Plugins provide additional common functionality that some users might need:
### List of all Plugins
- [all.js](plugins/all.js): All the plugins in one handy place.
- [defaults.js](plugins/defaults.js): Declare default values. [Example usage](plugins/defaults_test.js)
- [dump.js](plugins/dump.js): Dump all stored values. [Example usage](plugins/dump_test.js)
- [events.js](plugins/events.js): Get notified when stored values change. [Example usage](plugins/events_test.js)
- [expire.js](plugins/expire.js): Expire stored values at a given time. [Example usage](plugins/expire_test.js)
- [observe.js](plugins/observe.js): Observe stored values and their changes. [Example usage](plugins/observe_test.js)
- [operations.js](plugins/operations.js): Useful operations like push, shift & assign. [Example usage](plugins/operations_test.js)
- [update.js](plugins/update.js): Update a stored object, or create it if null. [Example usage](plugins/update_test.js)
- [v1-backcompat.js](plugins/v1-backcompat.js): Full backwards compatibility with store.js v1. [Example usage](plugins/v1-backcompat_test.js)
### Using Plugins
With npm:
```js
// Example plugin usage:
var expirePlugin = require('store/plugins/expire')
store.addPlugin(expirePlugin)
```
If you're using script tags, you can either use [store.everything.min.js](dist/store.everything.min.js) (which
has all plugins built-in), or clone this repo to add or modify a build and run `make build`.
### Write your own plugin
A store.js plugin is a function that returns an object that gets added to the store.
If any of the plugin functions overrides existing functions, the plugin function can still call
the original function using the first argument (super_fn).
```js
// Example plugin that stores a version history of every value
var versionHistoryPlugin = function() {
var historyStore = this.namespace('history')
return {
set: function(super_fn, key, value) {
var history = historyStore.get(key) || []
history.push(value)
historyStore.set(key, history)
return super_fn()
},
getHistory: function(key) {
return historyStore.get(key)
}
}
}
store.addPlugin(versionHistoryPlugin)
store.set('foo', 'bar 1')
store.set('foo', 'bar 2')
store.getHistory('foo') == ['bar 1', 'bar 2']
```
Let me know if you need more info on writing plugins. For the moment I recommend
taking a look at the [current plugins](plugins/). Good example plugins are
[plugins/defaults](plugins/defaults.js), [plugins/expire](plugins/expire.js) and
[plugins/events](plugins/events.js).
Builds
------
Choose which build is right for you!
### List of default builds
- [store.everything.min.js](dist/store.everything.min.js): All the plugins, all the storages. [Source](dist/store.everything.js)
- [store.legacy.min.js](dist/store.legacy.min.js): Full support for all tested browsers. Add plugins separately. [Source](dist/store.legacy.js)
- [store.modern.min.js](dist/store.modern.min.js): Full support for all modern browsers. Add plugins separately. [Source](dist/store.modern.js)
- [store.v1-backcompat.min.js](dist/store.v1-backcompat.min.js): Full backwards compatibility with [store.js v1](https://github.com/marcuswestin/store.js/releases/tag/v1.3.20). [Source](dist/store.v1-backcompat.js)
### Make your own Build
If you're using npm you can create your own build:
```js
// Example custom build usage:
var engine = require('store/src/store-engine')
var storages = [
require('store/storages/localStorage'),
require('store/storages/cookieStorage')
]
var plugins = [
require('store/plugins/defaults'),
require('store/plugins/expire')
]
var store = engine.createStore(storages, plugins)
store.set('foo', 'bar', new Date().getTime() + 3000) // Using expire plugin to expire in 3 seconds
```
Storages
--------
Store.js will pick the best available storage, and automatically falls back to the first available storage that works:
### List of all Storages
- [all.js](storages/all.js) All the storages in one handy place.
- [localStorage.js](storages/localStorage.js) Store values in localStorage. Great for all modern browsers.
- [sessionStorage.js](storages/sessionStorage.js) Store values in sessionStorage.
- [cookieStorage.js](storages/cookieStorage.js) Store values in cookies. Useful for Safari Private mode.
- [memoryStorage.js](storages/memoryStorage.js) Store values in memory. Great fallback to ensure store functionality at all times.
- [oldFF-globalStorage.js](storages/oldFF-globalStorage.js) Store values in globalStorage. Only useful for legacy Firefox 3+.
- [oldIE-userDataStorage.js](storages/oldIE-userDataStorage.js) Store values in userData. Only useful for legacy IE 6+.
### Storages limits
Each storage has different limits, restrictions and overflow behavior on different browser. For example, Android has has a 4.57M localStorage limit in 4.0, a 2.49M limit in 4.1, and a 4.98M limit in 4.2... Yeah.
To simplify things we provide these recommendations to ensure cross browser behavior:
| Storage | Targets | Recommendations | More info |
|:----------------|:-----------------------|:--------------------------------|:-------------------------------------------------|
| all | All browsers | Store < 1 million characters | (Except Safari Private mode) |
| all | All & Private mode | Store < 32 thousand characters | (Including Safari Private mode) |
| localStorage | Modern browsers | Max 2mb (~1M chars) | [limits][local-limits], [android][local-android] |
| sessionStorage | Modern browsers | Max 5mb (~2M chars) | [limits][session-limits] |
| cookieStorage | Safari Private mode | Max 4kb (~2K chars) | [limits][cookie-limits] |
| userDataStorage | IE5, IE6 & IE7 | Max 64kb (~32K chars) | [limits][userdata-limits] |
| globalStorage | Firefox 2-5 | Max 5mb (~2M chars) | [limits][global-limits] |
| memoryStorage | All browsers, fallback | Does not persist across pages! | |
[local-limits]: https://arty.name/localstorage.html
[local-android]: http://dev-test.nemikor.com/web-storage/support-test/
[session-limits]: http://stackoverflow.com/questions/15840976/how-large-is-html5-session-storage
[cookie-limits]: http://browsercookielimits.squawky.net/
[userdata-limits]: https://msdn.microsoft.com/en-us/library/ms533015(v=vs.85).aspx
[global-limits]: https://github.com/jeremydurham/persist-js/blob/master/README.md#4-size-limits
[more]: https://www.html5rocks.com/en/tutorials/offline/quota-research/
### Write your own Storage
Chances are you won't ever need another storage. But if you do...
See [storages/](storages/) for examples. Two good examples are [memoryStorage](storages/memoryStorage.js) and [localStorage](storages/localStorage.js).
Basically, you just need an object that looks like this:
```js
// Example custom storage
var storage = {
name: 'myStorage',
read: function(key) { ... },
write: function(key, value) { ... },
each: function(fn) { ... },
remove: function(key) { ... },
clearAll: function() { ... }
}
var store = require('store').createStore(storage)
```
================================================
FILE: dist/store.everything.js
================================================
var engine = require('../src/store-engine')
var storages = require('../storages/all')
var plugins = require('../plugins/all')
module.exports = engine.createStore(storages, plugins)
================================================
FILE: dist/store.legacy.js
================================================
var engine = require('../src/store-engine')
var storages = require('../storages/all')
var plugins = [require('../plugins/json2')]
module.exports = engine.createStore(storages, plugins)
================================================
FILE: dist/store.modern.js
================================================
var engine = require('../src/store-engine')
var storages = [
require('../storages/localStorage'),
require('../storages/sessionStorage'),
require('../storages/cookieStorage'),
require('../storages/memoryStorage'),
]
var plugins = []
module.exports = engine.createStore(storages, plugins)
================================================
FILE: dist/store.tests.js
================================================
// store-test-suite will run all the store.js tests
// and report the results.
var tests = require('../tests/tests')
tests.runTests()
================================================
FILE: dist/store.v1-backcompat.js
================================================
var engine = require('../src/store-engine')
var storages = require('../storages/all')
var plugins = [require('../plugins/v1-backcompat')]
module.exports = engine.createStore(storages, plugins)
================================================
FILE: package.json
================================================
{
"name": "store",
"version": "2.0.12",
"description": "A localStorage wrapper for all browsers without using cookies or flash. Uses localStorage, globalStorage, and userData behavior under the hood",
"main": "dist/store.legacy.js",
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "git://github.com/marcuswestin/store.js.git"
},
"author": "Marcus Westin ",
"license": "MIT",
"bugs": {
"url": "http://github.com/marcuswestin/store.js/issues"
},
"homepage": "https://github.com/marcuswestin/store.js#readme",
"devDependencies": {
"babel-preset-es2015": "^6.22.0",
"babelify": "^7.3.0",
"browserify": "^14.1.0",
"budo": "^7.1.0",
"eslint": "^3.12.2",
"localStorage": "^1.0.3",
"lodash": "^3.10.1",
"ngrok": "^2.2.6",
"request": "^2.67.0",
"tinytest": "^1.1.3",
"uglify-js": "^2.7.5"
},
"eslintConfig": {
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"globals": {
"test": false,
"assert": false,
"print": false
},
"rules": {
"comma-dangle": ["error", "only-multiline"],
"no-unused-vars": [
"error",
{
"vars": "all",
"args": "none",
"varsIgnorePattern": "^_$"
}
],
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"semi": [
"error",
"never"
]
}
},
"engines": {
"node": "*"
},
"directories": {
"lib": "."
}
}
================================================
FILE: plugins/all.js
================================================
module.exports = [
require('./compression'),
require('./defaults'),
require('./dump'),
require('./events'),
require('./observe'),
require('./expire'),
require('./json2'),
require('./operations'),
require('./update'),
require('./v1-backcompat'),
]
================================================
FILE: plugins/all_tests.js
================================================
module.exports = {
"compression": require('./compression_test'),
"defaults": require('./defaults_test'),
"dump": require('./dump_test'),
"events": require('./events_test'),
"observe": require('./observe_test'),
"expire": require('./expire_test'),
"json2": require('./json2_test'),
"operations": require('./operations_test'),
"update": require('./update_test'),
"v1-backcompat": require('./v1-backcompat_test'),
}
================================================
FILE: plugins/compression.js
================================================
const LZString = require('./lib/lz-string')
module.exports = compressionPlugin
function compressionPlugin() {
return {
get: get,
set: set,
}
function get(super_fn, key) {
var val = super_fn(key)
if (!val) { return val }
var decompressed = LZString.decompress(val)
// fallback to existing values that are not compressed
return (decompressed == null) ? val : this._deserialize(decompressed)
}
function set(super_fn, key, val) {
var compressed = LZString.compress(this._serialize(val))
super_fn(key, compressed)
}
}
================================================
FILE: plugins/compression_test.js
================================================
var { deepEqual } = require('../tests/util')
module.exports = {
plugin: require('./compression'),
setup: setup,
}
function setup(store) {
test('string compression size', function() {
var str = 'foo'
var serialized = store._serialize(str)
store.set('foo', str)
assert(store.raw.get('foo').length < serialized.length, 'compressed string should be smaller than uncompressed')
assert(deepEqual(store.get('foo'), str), 'value should be equal')
})
test('object compression', function () {
var obj = { one: { two: 3 }}
var serialized = store._serialize(obj)
store.set('foo', obj)
assert(store.raw.get('foo').length < serialized.length, 'compressed object should be smaller than uncompressed')
assert(deepEqual(store.get('foo'), obj), 'should deep equal original object')
store.remove('foo')
})
test('decompress uncopmressed data', function () {
store.raw.set('foo', 'baz')
assert(store.get('foo') == 'baz', 'value should be baz')
store.remove('foo')
})
test('decompress non-existing data', function () {
assert(store.get('bar') == undefined, 'value should be undefined')
store.remove('bar')
})
}
================================================
FILE: plugins/defaults.js
================================================
module.exports = defaultsPlugin
function defaultsPlugin() {
var defaultValues = {}
return {
defaults: defaults,
get: get
}
function defaults(_, values) {
defaultValues = values
}
function get(super_fn, key) {
var val = super_fn()
return (val !== undefined ? val : defaultValues[key])
}
}
================================================
FILE: plugins/defaults_test.js
================================================
module.exports = {
plugin: require('./defaults'),
setup: setup,
}
function setup(store) {
test('defaults', function() {
store.defaults({ foo: 'bar' })
assert(store.get('foo') == 'bar')
store.set('foo', 'bar2')
assert(store.get('foo') == 'bar2')
store.remove('foo')
assert(store.get('foo') == 'bar')
})
}
================================================
FILE: plugins/dump.js
================================================
module.exports = dumpPlugin
function dumpPlugin() {
return {
dump: dump
}
function dump(_) {
var res = {}
this.each(function(val, key) {
res[key] = val
})
return res
}
}
================================================
FILE: plugins/dump_test.js
================================================
var { each } = require('../src/util')
var { deepEqual } = require('../tests/util')
module.exports = {
plugin: require('./dump'),
setup: setup,
}
function setup(store) {
test('dump', function() {
var allValues = {
'foo': 'bar',
'cat': { mat:true },
'hat': 'bat'
}
each(allValues, function(val, key) {
store.set(key, val)
})
assert(deepEqual(store.dump(), allValues))
store.clearAll()
assert(deepEqual(store.dump(), {}))
})
}
================================================
FILE: plugins/events.js
================================================
var util = require('../src/util')
var bind = util.bind
var each = util.each
var create = util.create
var slice = util.slice
module.exports = eventsPlugin
function eventsPlugin() {
var pubsub = _newPubSub()
return {
watch: watch,
unwatch: unwatch,
once: once,
set: set,
remove: remove,
clearAll: clearAll
}
// new pubsub functions
function watch(_, key, listener) {
return pubsub.on(key, bind(this, listener))
}
function unwatch(_, subId) {
pubsub.off(subId)
}
function once(_, key, listener) {
pubsub.once(key, bind(this, listener))
}
// overwrite function to fire when appropriate
function set(super_fn, key, val) {
var oldVal = this.get(key)
super_fn()
pubsub.fire(key, val, oldVal)
}
function remove(super_fn, key) {
var oldVal = this.get(key)
super_fn()
pubsub.fire(key, undefined, oldVal)
}
function clearAll(super_fn) {
var oldVals = {}
this.each(function(val, key) {
oldVals[key] = val
})
super_fn()
each(oldVals, function(oldVal, key) {
pubsub.fire(key, undefined, oldVal)
})
}
}
function _newPubSub() {
return create(_pubSubBase, {
_id: 0,
_subSignals: {},
_subCallbacks: {}
})
}
var _pubSubBase = {
_id: null,
_subCallbacks: null,
_subSignals: null,
on: function(signal, callback) {
if (!this._subCallbacks[signal]) {
this._subCallbacks[signal] = {}
}
this._id += 1
this._subCallbacks[signal][this._id] = callback
this._subSignals[this._id] = signal
return this._id
},
off: function(subId) {
var signal = this._subSignals[subId]
delete this._subCallbacks[signal][subId]
delete this._subSignals[subId]
},
once: function(signal, callback) {
var subId = this.on(signal, bind(this, function() {
callback.apply(this, arguments)
this.off(subId)
}))
},
fire: function(signal) {
var args = slice(arguments, 1)
each(this._subCallbacks[signal], function(callback) {
callback.apply(this, args)
})
}
}
================================================
FILE: plugins/events_test.js
================================================
module.exports = {
plugin: require('./events'),
setup: setup,
}
function setup(store) {
test('events', function() {
store.set('foo', 'bar')
var expectationNone = _createExpectation('expectNone', undefined)
store.watch('foo', function(){})
var expectation1 = _createExpectation('foo', 'bar')
var expectationOnce = _createExpectation('foo', 'bar', true)
store.watch('foo', function(){})
expectation1.add('bar2')
expectationOnce.add('bar2')
store.set('foo', 'bar2')
expectation1.add(undefined)
store.remove('foo')
expectation1.add('bar3')
store.set('foo', 'bar3')
var expectation2 = _createExpectation('foo', 'bar3')
expectation1.add(undefined)
expectation2.add(undefined)
store.clearAll() // Should fire for foo
store.clearAll() // Should not fire anything
expectation1.unwatch()
expectation2.add('bar4')
store.set('foo', 'bar4') // Should only fire for expectation2
expectation1.check()
expectationOnce.check()
expectation2.check()
expectationNone.check()
expectation2.unwatch()
})
function _createExpectation(key, firstOldVal, useOnce) {
var expectation = {
values: [firstOldVal],
count: 0,
add: function(value) {
this.values.push(value)
},
check: function() {
assert(expectation.count + 1 == expectation.values.length)
},
unwatch: function() {
store.unwatch(watchId)
}
}
var watchId = (useOnce
? store.once(key, callback)
: store.watch(key, callback)
)
function callback(val, oldVal) {
expectation.count += 1
assert(expectation.values[expectation.count] == val)
assert(expectation.values[expectation.count - 1] == oldVal)
}
return expectation
}
}
================================================
FILE: plugins/expire.js
================================================
var namespace = 'expire_mixin'
module.exports = expirePlugin
function expirePlugin() {
var expirations = this.createStore(this.storage, null, this._namespacePrefix+namespace)
return {
set: expire_set,
get: expire_get,
remove: expire_remove,
getExpiration: getExpiration,
removeExpiredKeys: removeExpiredKeys
}
function expire_set(super_fn, key, val, expiration) {
if (!this.hasNamespace(namespace)) {
expirations.set(key, expiration)
}
return super_fn()
}
function expire_get(super_fn, key) {
if (!this.hasNamespace(namespace)) {
_checkExpiration.call(this, key)
}
return super_fn()
}
function expire_remove(super_fn, key) {
if (!this.hasNamespace(namespace)) {
expirations.remove(key)
}
return super_fn()
}
function getExpiration(_, key) {
return expirations.get(key)
}
function removeExpiredKeys(_) {
var keys = []
this.each(function(val, key) {
keys.push(key)
})
for (var i=0; i 0) {
setTimeout(function() {
attempt(remaining - 1, duration * 2)
}, 0)
return false
}
return assert(false)
})
}
function runTests(duration, check) {
var expiration = new Date().getTime() + duration
store.set('foo', 'bar', expiration)
if (!check(store.get('foo') == 'bar')) { return }
setTimeout(function() {
if (!check(new Date().getTime() > expiration)) { return }
if (!check(store.get('foo') == undefined)) { return }
store.set('foo', 'bar')
setTimeout(function() {
if (!check(store.get('foo') == 'bar')) { return }
done()
}, 5)
}, duration)
}
})
test('remove expired keys', function() {
var key = 'expired'
store.set(key, 'bar', new Date().getTime() - 1000)
assert(store.getExpiration(key) > 0)
store.removeExpiredKeys()
assert(!store.getExpiration(key))
})
}
================================================
FILE: plugins/json2.js
================================================
module.exports = json2Plugin
function json2Plugin() {
require('./lib/json2')
return {}
}
================================================
FILE: plugins/json2_test.js
================================================
module.exports = {
plugin: require('./json2'),
setup: setup,
}
function setup(store) {
test('serialization with json2', function() {
store.set('foo', { bar:'cat' })
assert(store.get('foo').bar === 'cat')
})
}
================================================
FILE: plugins/lib/json2.js
================================================
/* eslint-disable */
// json2.js
// 2016-10-28
// Public Domain.
// NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
// See http://www.JSON.org/js.html
// This code should be minified before deployment.
// See http://javascript.crockford.com/jsmin.html
// USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
// NOT CONTROL.
// This file creates a global JSON object containing two methods: stringify
// and parse. This file provides the ES5 JSON capability to ES3 systems.
// If a project might run on IE8 or earlier, then this file should be included.
// This file does nothing on ES5 systems.
// JSON.stringify(value, replacer, space)
// value any JavaScript value, usually an object or array.
// replacer an optional parameter that determines how object
// values are stringified for objects. It can be a
// function or an array of strings.
// space an optional parameter that specifies the indentation
// of nested structures. If it is omitted, the text will
// be packed without extra whitespace. If it is a number,
// it will specify the number of spaces to indent at each
// level. If it is a string (such as "\t" or " "),
// it contains the characters used to indent at each level.
// This method produces a JSON text from a JavaScript value.
// When an object value is found, if the object contains a toJSON
// method, its toJSON method will be called and the result will be
// stringified. A toJSON method does not serialize: it returns the
// value represented by the name/value pair that should be serialized,
// or undefined if nothing should be serialized. The toJSON method
// will be passed the key associated with the value, and this will be
// bound to the value.
// For example, this would serialize Dates as ISO strings.
// Date.prototype.toJSON = function (key) {
// function f(n) {
// // Format integers to have at least two digits.
// return (n < 10)
// ? "0" + n
// : n;
// }
// return this.getUTCFullYear() + "-" +
// f(this.getUTCMonth() + 1) + "-" +
// f(this.getUTCDate()) + "T" +
// f(this.getUTCHours()) + ":" +
// f(this.getUTCMinutes()) + ":" +
// f(this.getUTCSeconds()) + "Z";
// };
// You can provide an optional replacer method. It will be passed the
// key and value of each member, with this bound to the containing
// object. The value that is returned from your method will be
// serialized. If your method returns undefined, then the member will
// be excluded from the serialization.
// If the replacer parameter is an array of strings, then it will be
// used to select the members to be serialized. It filters the results
// such that only members with keys listed in the replacer array are
// stringified.
// Values that do not have JSON representations, such as undefined or
// functions, will not be serialized. Such values in objects will be
// dropped; in arrays they will be replaced with null. You can use
// a replacer function to replace those with JSON values.
// JSON.stringify(undefined) returns undefined.
// The optional space parameter produces a stringification of the
// value that is filled with line breaks and indentation to make it
// easier to read.
// If the space parameter is a non-empty string, then that string will
// be used for indentation. If the space parameter is a number, then
// the indentation will be that many spaces.
// Example:
// text = JSON.stringify(["e", {pluribus: "unum"}]);
// // text is '["e",{"pluribus":"unum"}]'
// text = JSON.stringify(["e", {pluribus: "unum"}], null, "\t");
// // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
// text = JSON.stringify([new Date()], function (key, value) {
// return this[key] instanceof Date
// ? "Date(" + this[key] + ")"
// : value;
// });
// // text is '["Date(---current time---)"]'
// JSON.parse(text, reviver)
// This method parses a JSON text to produce an object or array.
// It can throw a SyntaxError exception.
// The optional reviver parameter is a function that can filter and
// transform the results. It receives each of the keys and values,
// and its return value is used instead of the original value.
// If it returns what it received, then the structure is not modified.
// If it returns undefined then the member is deleted.
// Example:
// // Parse the text. Values that look like ISO date strings will
// // be converted to Date objects.
// myData = JSON.parse(text, function (key, value) {
// var a;
// if (typeof value === "string") {
// a =
// /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
// if (a) {
// return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
// +a[5], +a[6]));
// }
// }
// return value;
// });
// myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
// var d;
// if (typeof value === "string" &&
// value.slice(0, 5) === "Date(" &&
// value.slice(-1) === ")") {
// d = new Date(value.slice(5, -1));
// if (d) {
// return d;
// }
// }
// return value;
// });
// This is a reference implementation. You are free to copy, modify, or
// redistribute.
/*jslint
eval, for, this
*/
/*property
JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
lastIndex, length, parse, prototype, push, replace, slice, stringify,
test, toJSON, toString, valueOf
*/
// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.
if (typeof JSON !== "object") {
JSON = {};
}
(function () {
"use strict";
var rx_one = /^[\],:{}\s]*$/;
var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
var rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
var rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
function f(n) {
// Format integers to have at least two digits.
return n < 10
? "0" + n
: n;
}
function this_value() {
return this.valueOf();
}
if (typeof Date.prototype.toJSON !== "function") {
Date.prototype.toJSON = function () {
return isFinite(this.valueOf())
? this.getUTCFullYear() + "-" +
f(this.getUTCMonth() + 1) + "-" +
f(this.getUTCDate()) + "T" +
f(this.getUTCHours()) + ":" +
f(this.getUTCMinutes()) + ":" +
f(this.getUTCSeconds()) + "Z"
: null;
};
Boolean.prototype.toJSON = this_value;
Number.prototype.toJSON = this_value;
String.prototype.toJSON = this_value;
}
var gap;
var indent;
var meta;
var rep;
function quote(string) {
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
rx_escapable.lastIndex = 0;
return rx_escapable.test(string)
? "\"" + string.replace(rx_escapable, function (a) {
var c = meta[a];
return typeof c === "string"
? c
: "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
}) + "\""
: "\"" + string + "\"";
}
function str(key, holder) {
// Produce a string from holder[key].
var i; // The loop counter.
var k; // The member key.
var v; // The member value.
var length;
var mind = gap;
var partial;
var value = holder[key];
// If the value has a toJSON method, call it to obtain a replacement value.
if (value && typeof value === "object" &&
typeof value.toJSON === "function") {
value = value.toJSON(key);
}
// If we were called with a replacer function, then call the replacer to
// obtain a replacement value.
if (typeof rep === "function") {
value = rep.call(holder, key, value);
}
// What happens next depends on the value's type.
switch (typeof value) {
case "string":
return quote(value);
case "number":
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value)
? String(value)
: "null";
case "boolean":
case "null":
// If the value is a boolean or null, convert it to a string. Note:
// typeof null does not produce "null". The case is included here in
// the remote chance that this gets fixed someday.
return String(value);
// If the type is "object", we might be dealing with an object or an array or
// null.
case "object":
// Due to a specification blunder in ECMAScript, typeof null is "object",
// so watch out for that case.
if (!value) {
return "null";
}
// Make an array to hold the partial results of stringifying this object value.
gap += indent;
partial = [];
// Is the value an array?
if (Object.prototype.toString.apply(value) === "[object Array]") {
// The value is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.
length = value.length;
for (i = 0; i < length; i += 1) {
partial[i] = str(i, value) || "null";
}
// Join all of the elements together, separated with commas, and wrap them in
// brackets.
v = partial.length === 0
? "[]"
: gap
? "[\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "]"
: "[" + partial.join(",") + "]";
gap = mind;
return v;
}
// If the replacer is an array, use it to select the members to be stringified.
if (rep && typeof rep === "object") {
length = rep.length;
for (i = 0; i < length; i += 1) {
if (typeof rep[i] === "string") {
k = rep[i];
v = str(k, value);
if (v) {
partial.push(quote(k) + (
gap
? ": "
: ":"
) + v);
}
}
}
} else {
// Otherwise, iterate through all of the keys in the object.
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = str(k, value);
if (v) {
partial.push(quote(k) + (
gap
? ": "
: ":"
) + v);
}
}
}
}
// Join all of the member texts together, separated with commas,
// and wrap them in braces.
v = partial.length === 0
? "{}"
: gap
? "{\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "}"
: "{" + partial.join(",") + "}";
gap = mind;
return v;
}
}
// If the JSON object does not yet have a stringify method, give it one.
if (typeof JSON.stringify !== "function") {
meta = { // table of character substitutions
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
"\"": "\\\"",
"\\": "\\\\"
};
JSON.stringify = function (value, replacer, space) {
// The stringify method takes a value and an optional replacer, and an optional
// space parameter, and returns a JSON text. The replacer can be a function
// that can replace values, or an array of strings that will select the keys.
// A default replacer method can be provided. Use of the space parameter can
// produce text that is more easily readable.
var i;
gap = "";
indent = "";
// If the space parameter is a number, make an indent string containing that
// many spaces.
if (typeof space === "number") {
for (i = 0; i < space; i += 1) {
indent += " ";
}
// If the space parameter is a string, it will be used as the indent string.
} else if (typeof space === "string") {
indent = space;
}
// If there is a replacer, it must be a function or an array.
// Otherwise, throw an error.
rep = replacer;
if (replacer && typeof replacer !== "function" &&
(typeof replacer !== "object" ||
typeof replacer.length !== "number")) {
throw new Error("JSON.stringify");
}
// Make a fake root object containing our value under the key of "".
// Return the result of stringifying the value.
return str("", {"": value});
};
}
// If the JSON object does not yet have a parse method, give it one.
if (typeof JSON.parse !== "function") {
JSON.parse = function (text, reviver) {
// The parse method takes a text and an optional reviver function, and returns
// a JavaScript value if the text is a valid JSON text.
var j;
function walk(holder, key) {
// The walk method is used to recursively walk the resulting structure so
// that modifications can be made.
var k;
var v;
var value = holder[key];
if (value && typeof value === "object") {
for (k in value) {
if (Object.prototype.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
}
}
}
}
return reviver.call(holder, key, value);
}
// Parsing happens in four stages. In the first stage, we replace certain
// Unicode characters with escape sequences. JavaScript handles many characters
// incorrectly, either silently deleting them, or treating them as line endings.
text = String(text);
rx_dangerous.lastIndex = 0;
if (rx_dangerous.test(text)) {
text = text.replace(rx_dangerous, function (a) {
return "\\u" +
("0000" + a.charCodeAt(0).toString(16)).slice(-4);
});
}
// In the second stage, we run the text against regular expressions that look
// for non-JSON patterns. We are especially concerned with "()" and "new"
// because they can cause invocation, and "=" because it can cause mutation.
// But just to be safe, we want to reject all unexpected forms.
// We split the second stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace the JSON backslash pairs with "@" (a non-JSON character). Second, we
// replace all simple value tokens with "]" characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or "]" or
// "," or ":" or "{" or "}". If that is so, then the text is safe for eval.
if (
rx_one.test(
text
.replace(rx_two, "@")
.replace(rx_three, "]")
.replace(rx_four, "")
)
) {
// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The "{" operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
j = eval("(" + text + ")");
// In the optional fourth stage, we recursively walk the new structure, passing
// each name/value pair to a reviver function for possible transformation.
return (typeof reviver === "function")
? walk({"": j}, "")
: j;
}
// If the text is not JSON parseable, then a SyntaxError is thrown.
throw new SyntaxError("JSON.parse");
};
}
}());
================================================
FILE: plugins/lib/lz-string.js
================================================
/* eslint-disable */
// Copyright (c) 2013 Pieroxy
// This work is free. You can redistribute it and/or modify it
// under the terms of the WTFPL, Version 2
// For more information see LICENSE.txt or http://www.wtfpl.net/
//
// For more information, the home page:
// http://pieroxy.net/blog/pages/lz-string/testing.html
//
// LZ-based compression algorithm, version 1.4.4
var LZString = (function() {
// private property
var f = String.fromCharCode;
var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
var baseReverseDic = {};
function getBaseValue(alphabet, character) {
if (!baseReverseDic[alphabet]) {
baseReverseDic[alphabet] = {};
for (var i=0 ; i>> 8;
buf[i*2+1] = current_value % 256;
}
return buf;
},
//decompress from uint8array (UCS-2 big endian format)
decompressFromUint8Array:function (compressed) {
if (compressed===null || compressed===undefined){
return LZString.decompress(compressed);
} else {
var buf=new Array(compressed.length/2); // 2 bytes per character
for (var i=0, TotalLen=buf.length; i> 1;
}
} else {
value = 1;
for (i=0 ; i> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
} else {
value = context_dictionary[context_w];
for (i=0 ; i> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
// Add wc to the dictionary.
context_dictionary[context_wc] = context_dictSize++;
context_w = String(context_c);
}
}
// Output the code for w.
if (context_w !== "") {
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
if (context_w.charCodeAt(0)<256) {
for (i=0 ; i> 1;
}
} else {
value = 1;
for (i=0 ; i> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
} else {
value = context_dictionary[context_w];
for (i=0 ; i> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
}
// Mark the end of the stream
value = 2;
for (i=0 ; i> 1;
}
// Flush the last char
while (true) {
context_data_val = (context_data_val << 1);
if (context_data_position == bitsPerChar-1) {
context_data.push(getCharFromInt(context_data_val));
break;
}
else context_data_position++;
}
return context_data.join('');
},
decompress: function (compressed) {
if (compressed == null) return "";
if (compressed == "") return null;
return LZString._decompress(compressed.length, 32768, function(index) { return compressed.charCodeAt(index); });
},
_decompress: function (length, resetValue, getNextValue) {
var dictionary = [],
next,
enlargeIn = 4,
dictSize = 4,
numBits = 3,
entry = "",
result = [],
i,
w,
bits, resb, maxpower, power,
c,
data = {val:getNextValue(0), position:resetValue, index:1};
for (i = 0; i < 3; i += 1) {
dictionary[i] = i;
}
bits = 0;
maxpower = Math.pow(2,2);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
switch (next = bits) {
case 0:
bits = 0;
maxpower = Math.pow(2,8);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
c = f(bits);
break;
case 1:
bits = 0;
maxpower = Math.pow(2,16);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
c = f(bits);
break;
case 2:
return "";
}
dictionary[3] = c;
w = c;
result.push(c);
while (true) {
if (data.index > length) {
return "";
}
bits = 0;
maxpower = Math.pow(2,numBits);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
switch (c = bits) {
case 0:
bits = 0;
maxpower = Math.pow(2,8);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = f(bits);
c = dictSize-1;
enlargeIn--;
break;
case 1:
bits = 0;
maxpower = Math.pow(2,16);
power=1;
while (power!=maxpower) {
resb = data.val & data.position;
data.position >>= 1;
if (data.position == 0) {
data.position = resetValue;
data.val = getNextValue(data.index++);
}
bits |= (resb>0 ? 1 : 0) * power;
power <<= 1;
}
dictionary[dictSize++] = f(bits);
c = dictSize-1;
enlargeIn--;
break;
case 2:
return result.join('');
}
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
if (dictionary[c]) {
entry = dictionary[c];
} else {
if (c === dictSize) {
entry = w + w.charAt(0);
} else {
return null;
}
}
result.push(entry);
// Add w+entry[0] to the dictionary.
dictionary[dictSize++] = w + entry.charAt(0);
enlargeIn--;
w = entry;
if (enlargeIn == 0) {
enlargeIn = Math.pow(2, numBits);
numBits++;
}
}
}
};
return LZString;
})();
if (typeof define === 'function' && define.amd) {
define(function () { return LZString; });
} else if( typeof module !== 'undefined' && module != null ) {
module.exports = LZString
}
================================================
FILE: plugins/observe.js
================================================
var eventsPlugin = require('./events')
module.exports = [eventsPlugin, observePlugin]
function observePlugin() {
return {
observe: observe,
unobserve: unobserve
}
function observe(_, key, callback) {
var subId = this.watch(key, callback)
callback(this.get(key))
return subId
}
function unobserve(_, subId) {
this.unwatch(subId)
}
}
================================================
FILE: plugins/observe_test.js
================================================
module.exports = {
plugin: require('./observe'),
setup: setup,
}
function setup(store) {
test('observe', function() {
store.clearAll()
var count = -1
var expect = [undefined]
var obsId = store.observe('foo', function(val, oldVal) {
count += 1
assert(expect[count] == val)
assert(expect[count - 1] == oldVal)
}) // count == 1
store.unobserve(obsId)
expect.push('bar')
store.set('foo', 'bar')
store.observe('foo', function(val, oldVal) {
count += 1
assert(expect[count] == val)
assert(expect[count - 1] == oldVal)
}) // count == 2
expect.push('bar2')
store.set('foo', 'bar2') // count == 3
assert(count + 1 == expect.length)
})
}
================================================
FILE: plugins/operations.js
================================================
var util = require('../src/util')
var slice = util.slice
var assign = util.assign
var updatePlugin = require('./update')
module.exports = [updatePlugin, operationsPlugin]
function operationsPlugin() {
return {
// array
push: push,
pop: pop,
shift: shift,
unshift: unshift,
// obj
assign: assign_,
}
// array
function push(_, key, val1, val2, val3, etc) {
return _arrayOp.call(this, 'push', arguments)
}
function pop(_, key) {
return _arrayOp.call(this, 'pop', arguments)
}
function shift(_, key) {
return _arrayOp.call(this, 'shift', arguments)
}
function unshift(_, key, val1, val2, val3, etc) {
return _arrayOp.call(this, 'unshift', arguments)
}
// obj
function assign_(_, key, props1, props2, props3, etc) {
var varArgs = slice(arguments, 2)
return this.update(key, {}, function(val) {
if (typeof val != 'object') {
throw new Error('store.assign called for non-object value with key "'+key+'"')
}
varArgs.unshift(val)
return assign.apply(Object, varArgs)
})
}
// internal
///////////
function _arrayOp(arrayFn, opArgs) {
var res
var key = opArgs[1]
var rest = slice(opArgs, 2)
this.update(key, [], function(arrVal) {
res = Array.prototype[arrayFn].apply(arrVal, rest)
})
return res
}
}
================================================
FILE: plugins/operations_test.js
================================================
var { each, map } = require('../src/util')
var { deepEqual } = require('../tests/util')
module.exports = {
plugin: require('./operations'),
setup: setup,
}
function setup(store) {
test('push', function() {
_testArrayOp('push', [], [
[],
['a'],
['b','c'],
[null],
[[], {}]
])
})
test('unshift', function() {
_testArrayOp('unshift', undefined, [
[],
['a'],
['b','c'],
[null],
[[], {}]
])
})
test('pop', function() {
var arr = ['a', 'b', 'c', null, [[], {}]]
// Call pop arr.length + 1 times. No args each time
var argsList = map(arr, function() { return [] }).concat([])
_testArrayOp('pop', arr, argsList)
})
test('shift', function() {
var arr = ['a', 'b', 'c', null, [[], {}]]
// Call shift arr.length + 1 times. No args each time
var argsList = map(arr, function() { return [] }).concat([])
_testArrayOp('shift', arr, argsList)
})
test('assign', function() {
store.clearAll()
var expect = { bar:'cat', mat:{ hat:'bat', arr:[1,2,3] }}
store.assign('foo', expect)
assert(deepEqual(store.get('foo'), expect))
var add = { bar:'cat2', mat:{ hat:'bat2' }, newProp:'newProp'}
store.assign('foo', add)
each(add, function(val, key) {
expect[key] = val
})
assert(deepEqual(store.get('foo'), expect))
})
function _testArrayOp(fnName, arr, argLists) {
var key = 'test-'+fnName
store.set(key, arr)
arr = (arr || [])
var arrFn = arr[fnName]
var storeFn = store[fnName]
each(argLists, function(args) {
var expectedFnResult = arrFn.apply(arr, args)
var actualFnResult = storeFn.apply(store, [key].concat(args))
assert(deepEqual(expectedFnResult, actualFnResult))
var actual = store.get(key)
assert(deepEqual(arr, actual))
})
}
}
================================================
FILE: plugins/update.js
================================================
module.exports = updatePlugin
function updatePlugin() {
return {
update: update
}
function update(_, key, optDefaultVal, updateFn) {
if (arguments.length == 3) {
updateFn = optDefaultVal
optDefaultVal = undefined
}
var val = this.get(key, optDefaultVal)
var retVal = updateFn(val)
this.set(key, retVal != undefined ? retVal : val)
}
}
================================================
FILE: plugins/update_test.js
================================================
module.exports = {
plugin: require('./update'),
setup: setup,
}
function setup(store) {
test('update', function() {
store.set('foo', { cat:'mat' })
assert(store.get('foo').cat == 'mat')
store.update('foo', function(foo) {
foo.cat = 'mat2'
})
assert(store.get('foo').cat == 'mat2')
})
test('update return value', function() {
store.clearAll()
store.update('foo', function(foo) {
assert(foo == undefined)
return { cat:'mat4' }
})
assert(store.get('foo').cat == 'mat4')
})
test('update default value', function() {
store.clearAll()
store.update('foo2', {}, function(foo2) {
foo2.bar = 'cat'
})
assert(store.get('foo2').bar == 'cat')
})
test('update default value + return', function() {
store.clearAll()
store.update('foo2', [], function(foor2) {
return { bar2:'cat2' }
})
assert(typeof store.get('foo2') == 'object')
assert(store.get('foo2').bar == undefined)
assert(store.get('foo2').bar2 == 'cat2')
})
}
================================================
FILE: plugins/v1-backcompat.js
================================================
var dumpPlugin = require('./dump')
var json2Plugin = require('./json2')
module.exports = [dumpPlugin, json2Plugin, v1BackcompatPlugin]
function v1BackcompatPlugin() {
this.disabled = !this.enabled
return {
has: backcompat_has,
transact: backcompat_transact,
clear: backcompat_clear,
forEach: backcompat_forEach,
getAll: backcompat_getAll,
serialize: backcompat_serialize,
deserialize: backcompat_deserialize,
}
}
function backcompat_has(_, key) {
return this.get(key) !== undefined
}
function backcompat_transact(_, key, defaultVal, transactionFn) {
if (transactionFn == null) {
transactionFn = defaultVal
defaultVal = null
}
if (defaultVal == null) {
defaultVal = {}
}
var val = this.get(key, defaultVal)
var ret = transactionFn(val)
this.set(key, ret === undefined ? val : ret)
}
function backcompat_clear(_) {
return this.clearAll.call(this)
}
function backcompat_forEach(_, fn) {
return this.each.call(this, function(val, key) {
fn(key, val)
})
}
function backcompat_getAll(_) {
return this.dump.call(this)
}
function backcompat_serialize(_, value) {
return JSON.stringify(value)
}
function backcompat_deserialize(_, value) {
if (typeof value != 'string') { return undefined }
try { return JSON.parse(value) }
catch(e) { return value || undefined }
}
================================================
FILE: plugins/v1-backcompat_test.js
================================================
module.exports = {
plugin: require('./v1-backcompat'),
setup: setup,
}
function setup(store) {
test('backwards compatability with v1', function() {
store.clear()
assert(typeof store.disabled == 'boolean')
assert(typeof store.enabled == 'boolean')
assert(typeof store.version == 'string')
assert(typeof store.set == 'function')
assert(typeof store.get == 'function')
assert(typeof store.has == 'function')
assert(typeof store.remove == 'function')
assert(typeof store.clear == 'function')
assert(typeof store.transact == 'function')
assert(typeof store.getAll == 'function')
assert(typeof store.forEach == 'function')
assert(typeof store.serialize == 'function')
assert(typeof store.deserialize == 'function')
store.transact('foosact', function(val) {
assert(typeof val == 'object', "new key is not an object at beginning of transaction")
val.foo = 'foo'
})
store.transact('foosact', function(val) {
assert(val.foo == 'foo', "first transaction did not register")
val.bar = 'bar'
})
assert(store.getAll().foosact.foo == 'foo')
var wasCalled = false
store.forEach(function(key, val) {
wasCalled = true
assert(key == 'foosact')
assert(val.foo == 'foo')
})
assert(wasCalled)
assert(store.serialize({}) == '{}')
assert(store.get('foosact').bar == 'bar', "second transaction did not register")
})
}
================================================
FILE: scripts/compile-builds.js
================================================
#!/usr/local/bin/node
var fs = require('fs')
var path = require('path')
var browserify = require('browserify')
var UglifyJS = require('uglify-js')
var base = __dirname + '/..'
module.exports = {
run: run,
}
if (require.main === module) {
main()
}
function main() {
run(function(err) {
if (err) { throw err }
})
}
function run(callback) {
var dir = base+'/dist'
fs.readdir(dir, function(err, items) {
next()
function next() {
var item = items.shift()
if (!item) {
return callback()
}
if (item[0] == '.') {
return next()
}
if (item.match(/\.min\.js$/)) {
return next()
}
var input = path.resolve(dir+'/'+item)
var output = input.replace(/\.js$/, '.min.js')
console.log('compile', input, '->', output)
compileFile(input, output, function(err) {
if (err) {
return callback(err)
}
next()
})
}
})
}
function compileFile(input, output, callback) {
var copyright = '/* store.js - Copyright (c) 2010-2017 Marcus Westin */'
// TODO: sourcemaps - depends on https://github.com/mishoo/UglifyJS2/issues/520
browserify([input], { standalone:'store', expose:'store' }) // TODO: sourcemaps - use `debug:true`
.transform('babelify', { presets:['es2015'] }) // TODO: sourcemaps - use `sourceMaps:true`
.bundle(processResult)
function processResult(err, buf) {
if (err) { return callback(err) }
var code = buf.toString()
code = minify(code)
var result = copyright+'\n'+code
fs.writeFile(output, result, function(err) {
if (err) { return callback(err) }
var b = Buffer.byteLength(result, 'utf8')
var k = Math.round(b/1000)
console.log(k+'k \t('+b+')')
callback()
})
}
}
function minify(code) {
var minified = UglifyJS.minify(code, {
fromString: true,
compress: { screw_ie8:false },
mangle: { screw_ie8:false },
output: { screw_ie8:false },
// warnings: true,
// mangleProperties: { reserved:[] },
})
return minified.code // TODO: sourcemaps - use `result.map`.
}
================================================
FILE: scripts/create-tunnel.js
================================================
var port = 9575
var tunnel = require('./saucelabs/tunnel')
tunnel.setup(port, function(err, url) {
console.log("Tunnel up and running at", url)
})
================================================
FILE: scripts/release.sh
================================================
#!/bin/bash
set -e
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # scripts/
cd ../ # store.js project root
VERSION=$1
if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$VERSION is not a valid semver (e.g 2.1.3)"
exit -1
fi
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ "$GIT_BRANCH" != "master" ]; then
echo "release.sh must be called from branch master (current: $GIT_BRANCH)"
exit -1
fi
if ! git diff-index --quiet HEAD --; then
echo "git repo is dirty. Commit all changes before using release.sh"
exit -1
fi
echo
echo "> Bump package.json version:"
echo
sed -E s/'"version"\: "[0-9]+\.[0-9]+\.[0-9]+"'/'"version"\: "'$VERSION'"'/ package.json \
> /tmp/package.json && \
mv /tmp/package.json package.json
cat package.json | grep $VERSION -C 1
echo
echo "> Bump store-engine.js version:"
echo
sed -E s/"version\: '[0-9]+\.[0-9]+\.[0-9]+'"/"version\: '$VERSION'"/ src/store-engine.js \
> /tmp/store-engine.js && \
mv /tmp/store-engine.js src/store-engine.js
cat src/store-engine.js | grep $VERSION -C 1
if [[ ! `git diff --stat` =~ "2 files changed, 2 insertions(+), 2 deletions(-)" ]]; then
echo "WARNING! Expected exactly 2 changes in 2 files after replacing version number. Bailing! (check git status and git diff)"
exit -1
fi
echo
while true; do
read -p "> Ready to build, commit, tag and release v$VERSION? (y/n): " yn
case $yn in
[Yy]* ) break;;
[NnQq]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
echo
echo "> Build dists"
node scripts/compile-builds.js
echo
echo "> git commit/push/tag/push --tags"
set -x
git add dist/* package.json src/store-engine.js
git commit -m "v$VERSION"
git push $ORIGIN $BRANCH
git tag -a "v$VERSION" -m "Tag v$VERSION"
git push --tags
set +x
echo
echo "> npm publish"
npm publish
================================================
FILE: scripts/run-browser-tests-live-reload.js
================================================
#!/usr/bin/env node
var budo = require('budo')
budo(__dirname+'/../dist/store.tests.js', {
live: true,
stream: process.stdout,
port: 9966,
debug: true,
open: true,
title: 'store.js browser tests',
})
================================================
FILE: scripts/run-node-tests.js
================================================
#!/usr/local/bin/node
// simulate localStorage - must be done before importing tests
var { Global } = require('../src/util')
Global.localStorage = require('localStorage')
// Import and run tests
var tests = require('../tests/tests')
tests.runTests()
================================================
FILE: scripts/run-saucelabs-tests.js
================================================
#!/usr/local/bin/node
var port = 9574
var username = 'storejs'
var password = new Buffer('ZjhjMzUyNjgtNzc2ZC00ZjlkLWEwNWUtN2FkM2Q0ZDgyNzk5', 'base64').toString('utf8')
// TODO: Contribute to npm-saucelabs? Create new module?
var saucelabs = require('./saucelabs/saucelabs')
var tunnel = require('./saucelabs/tunnel')
main(function(err) {
if (err) { throw err }
log('All done!')
})
function main() {
var platformSetNames = process.argv.slice(2)
var platformSets = platformSetNames.map(function(platformSetName) {
var platformSet = saucelabs.platformSets[platformSetName]
if (!platformSet) {
throw new Error("Unknown platform set: "+platformSetName)
}
return platformSet
})
if (platformSets.length == 0) {
var s = saucelabs.platformSets
var platformSets = [
// All supported platforms:
///////////////////////////
s.ie,
s.safari,
s.firefox,
s.chrome,
s.android,
s.ios,
s.opera,
// Specific test targets for development:
/////////////////////////////////////////
// s.fast,
// s.ie6, s.ie7, s.ie8,
// s.ie9, s.ie10, s.ie11,
// s.firefox4, s.firefox5,
// s.ie10,
]
}
tunnel.setup(port, function(err, url) {
if (err) { throw err }
saucelabs.setAuth(username, password)
saucelabs.runTest(url, platformSets, onDone)
function onDone(err) {
if (err) {
console.log('Error', err)
process.exit(1)
} else {
log('All tests passed!')
process.exit(0)
}
}
})
}
function log() {
console.log.apply(console, arguments)
}
================================================
FILE: scripts/saucelabs/list-supported-browsers.js
================================================
#!/usr/local/bin/node
var username = 'storejs'
var password = new Buffer('ZjhjMzUyNjgtNzc2ZC00ZjlkLWEwNWUtN2FkM2Q0ZDgyNzk5', 'base64').toString('utf8')
var saucelabs = require('./saucelabs')
saucelabs.setAuth(username, password)
saucelabs.listAllSupportedPlatforms(function(err, res) {
if (err) { throw err }
for (var i=0; i
store.js test runner
store.js test runner