cross-domain-storage
Advanced tools
Comparing version 1.0.8 to 2.0.0
@@ -1,5 +0,5 @@ | ||
var prefix = 'sessionAccessId-'; | ||
const prefix = 'sessionAccessId-'; | ||
function getId(data) { | ||
var id; | ||
let id; | ||
@@ -6,0 +6,0 @@ if (data && data.id && ~data.id.indexOf(prefix)) { |
@@ -1,5 +0,6 @@ | ||
var crel = require('crel'); | ||
var prefix = 'sessionAccessId-'; | ||
var getId = require('../getId'); | ||
const crel = require('crel'); | ||
const getId = require('../getId'); | ||
const prefix = 'sessionAccessId-'; | ||
function createId() { | ||
@@ -12,12 +13,11 @@ return prefix + Date.now(); | ||
var iframe; | ||
var contentWindow; | ||
var callbacks = {}; | ||
var sessionRequests = []; | ||
var connected = false; | ||
var closed = true; | ||
var connectedTimeout; | ||
var isLoaded = false; | ||
let contentWindow; | ||
let callbacks = {}; | ||
const sessionRequests = []; | ||
let connected = false; | ||
let closed = true; | ||
let connectedTimeout; | ||
let isLoaded = false; | ||
iframe = crel('iframe', { | ||
const iframe = crel('iframe', { | ||
src: source, | ||
@@ -27,3 +27,3 @@ width: 0, | ||
style: 'display: none;', | ||
onload: function() { | ||
onload() { | ||
isLoaded = true; | ||
@@ -46,4 +46,4 @@ }, | ||
function handleMessage(event) { | ||
var response = event.data; | ||
var sessionAccessId = getId(response); | ||
const response = event.data; | ||
const sessionAccessId = getId(response); | ||
@@ -56,14 +56,8 @@ if (sessionAccessId === 'sessionAccessId-connected') { | ||
if (response.connectError) { | ||
for (var key in callbacks) { | ||
if (callbacks[key]) { | ||
callbacks[key](response.error); | ||
} | ||
} | ||
Object.keys(callbacks).forEach(key => callbacks[key](response.error)); | ||
callbacks = {}; | ||
return; | ||
} | ||
var callback = callbacks[sessionAccessId]; | ||
const callback = callbacks[sessionAccessId]; | ||
@@ -89,6 +83,6 @@ if (sessionAccessId && callback) { | ||
if (!connected && method !== 'connect') { | ||
sessionRequests.push(arguments); | ||
sessionRequests.push([method, key, value, callback]); | ||
} | ||
var id = createId(); | ||
const id = createId(); | ||
@@ -100,8 +94,8 @@ callbacks[id] = callback; | ||
{ | ||
method: method, | ||
key: key, | ||
value: value, | ||
id: id, | ||
method, | ||
key, | ||
value, | ||
id, | ||
}, | ||
source | ||
source, | ||
); | ||
@@ -131,3 +125,3 @@ } | ||
while (sessionRequests.length) { | ||
message.apply(null, sessionRequests.pop()); | ||
message(...sessionRequests.pop()); | ||
} | ||
@@ -144,7 +138,7 @@ | ||
return { | ||
get: get, | ||
set: set, | ||
remove: remove, | ||
close: close, | ||
get, | ||
set, | ||
remove, | ||
close, | ||
}; | ||
}; |
@@ -1,11 +0,9 @@ | ||
var getId = require('../getId'); | ||
var methods = require('./methods'); | ||
const getId = require('../getId'); | ||
const methods = require('./methods'); | ||
module.exports = function storageHost(allowedDomains) { | ||
function handleMessage(event) { | ||
var data = event.data; | ||
var domain = allowedDomains.find(function (allowedDomain) { | ||
return event.origin === allowedDomain.origin; | ||
}); | ||
var id = getId(data); | ||
const { data } = event; | ||
const domain = allowedDomains.find(allowedDomain => event.origin === allowedDomain.origin); | ||
const id = getId(data); | ||
@@ -17,7 +15,10 @@ if (!id) { | ||
if (!domain) { | ||
event.source.postMessage({ | ||
id: id, | ||
connectError: true, | ||
error: event.origin + ' is not an allowed domain', | ||
}, event.origin); | ||
event.source.postMessage( | ||
{ | ||
id, | ||
connectError: true, | ||
error: `${event.origin} is not an allowed domain`, | ||
}, | ||
event.origin, | ||
); | ||
@@ -27,9 +28,12 @@ return; | ||
var method = data.method; | ||
const { method } = data; | ||
if (!~domain.allowedMethods.indexOf(method) && method !== 'connect') { | ||
event.source.postMessage({ | ||
id: id, | ||
error: method + ' is not an allowed method from ' + event.origin, | ||
}, event.origin); | ||
event.source.postMessage( | ||
{ | ||
id, | ||
error: `${method} is not an allowed method from ${event.origin}`, | ||
}, | ||
event.origin, | ||
); | ||
@@ -49,4 +53,4 @@ return; | ||
return { | ||
close: close, | ||
close, | ||
}; | ||
}; |
@@ -1,44 +0,41 @@ | ||
var connectId = 'sessionAccessId-connected', | ||
parseJSON = require('try-parse-json'); | ||
const connectId = 'sessionAccessId-connected'; | ||
function parseJSONValue(value) { | ||
if (value == null) { | ||
return value; | ||
} | ||
var result = parseJSON(value); | ||
if (result instanceof Error) { | ||
result = null; | ||
} | ||
return result; | ||
} | ||
module.exports = { | ||
get: function (event, data) { | ||
event.source.postMessage({ | ||
id: data.id, | ||
data: parseJSONValue(window.localStorage.getItem(data.key)), | ||
}, event.origin); | ||
get(event, data) { | ||
event.source.postMessage( | ||
{ | ||
id: data.id, | ||
data: window.localStorage.getItem(data.key), | ||
}, | ||
event.origin, | ||
); | ||
}, | ||
set: function (event, data) { | ||
window.localStorage.setItem(data.key, JSON.stringify(data.value)); | ||
set(event, data) { | ||
window.localStorage.setItem(data.key, data.value); | ||
event.source.postMessage({ | ||
id: data.id, | ||
}, event.origin); | ||
event.source.postMessage( | ||
{ | ||
id: data.id, | ||
}, | ||
event.origin, | ||
); | ||
}, | ||
remove: function (event, data) { | ||
window.localStorage.removeItem(data.key, data.value); | ||
remove(event, data) { | ||
window.localStorage.removeItem(data.key); | ||
event.source.postMessage({ | ||
id: data.id, | ||
}, event.origin); | ||
event.source.postMessage( | ||
{ | ||
id: data.id, | ||
}, | ||
event.origin, | ||
); | ||
}, | ||
connect: function (event) { | ||
event.source.postMessage({ | ||
id: connectId, | ||
}, event.origin); | ||
connect(event) { | ||
event.source.postMessage( | ||
{ | ||
id: connectId, | ||
}, | ||
event.origin, | ||
); | ||
}, | ||
}; |
{ | ||
"name": "cross-domain-storage", | ||
"version": "1.0.8", | ||
"description": "Cross domain local storage", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node tests" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/MatthewLarner/cross-domain-storage.git" | ||
}, | ||
"author": "Matt Larner <matt.larner.dev@gmail.com>", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/MatthewLarner/cross-domain-storage/issues" | ||
}, | ||
"homepage": "https://github.com/MatthewLarner/cross-domain-storage#readme", | ||
"dependencies": { | ||
"crel": "^3.0.0", | ||
"try-parse-json": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"automagic-ui": "^1.3.1", | ||
"browserify": "^16.2.3", | ||
"crel": "^3.0.0", | ||
"doc-js": "^1.3.1", | ||
"file-server": "^1.0.5", | ||
"open": "0.0.5", | ||
"static-server": "^2.0.2", | ||
"tape": "^4.9.2", | ||
"watchify": "^3.11.1" | ||
} | ||
"name": "cross-domain-storage", | ||
"version": "2.0.0", | ||
"description": "Cross domain local storage", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node tests", | ||
"lint": "eslint . --fix --ext .jsx --ext .js" | ||
}, | ||
"lint-staged": { | ||
"*.js": [ | ||
"prettier --write", | ||
"eslint --fix" | ||
], | ||
"*.json": [ | ||
"prettier --write" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged", | ||
"pre-push": "npm run lint" | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/MatthewLarner/cross-domain-storage.git" | ||
}, | ||
"author": "Matt Larner <matt.larner.dev@gmail.com>", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/MatthewLarner/cross-domain-storage/issues" | ||
}, | ||
"homepage": "https://github.com/MatthewLarner/cross-domain-storage#readme", | ||
"dependencies": { | ||
"crel": "^4.2.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^16.5.0", | ||
"doc-js": "^1.3.1", | ||
"eslint": "^6.1.0", | ||
"eslint-config-airbnb": "^18.0.1", | ||
"eslint-config-prettier": "^6.10.0", | ||
"eslint-plugin-import": "^2.20.1", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-react": "^7.18.2", | ||
"eslint-plugin-react-hooks": "^1.7.0", | ||
"husky": "^4.2.1", | ||
"lint-staged": "^10.0.7", | ||
"open": "^7.0.2", | ||
"prettier": "^1.19.1", | ||
"static-server": "^2.0.2", | ||
"tape": "^4.13.0", | ||
"watchify": "^3.11.1" | ||
} | ||
} |
@@ -8,2 +8,3 @@ Allows sharing of local storage across domains. | ||
## Install | ||
npm i cross-domain-storage | ||
@@ -14,2 +15,3 @@ | ||
### Host | ||
```javascript | ||
@@ -20,17 +22,20 @@ var createHost = require('cross-domain-storage/host'); | ||
#### host(allowedDomains) | ||
Call with an array of allowed domains. | ||
```javascript | ||
var storageHost = createHost([ | ||
{ | ||
origin: 'http://www.foo.com', | ||
allowedMethods: ['get', 'set', 'remove'] | ||
}, | ||
{ | ||
origin: 'http://www.bar.com', | ||
allowedMethods: ['get'] | ||
} | ||
]); | ||
{ | ||
origin: 'http://www.foo.com', | ||
allowedMethods: ['get', 'set', 'remove'], | ||
}, | ||
{ | ||
origin: 'http://www.bar.com', | ||
allowedMethods: ['get'], | ||
}, | ||
]); | ||
``` | ||
#### host.close() | ||
```javascript | ||
@@ -42,7 +47,9 @@ storageHost.close(); | ||
### Guest | ||
```javascript | ||
var createGuest = require('cross-domain-storage/guest') | ||
var createGuest = require('cross-domain-storage/guest'); | ||
``` | ||
#### guest(hostURL) | ||
Create a guest and connect to the host. | ||
@@ -54,3 +61,3 @@ | ||
// Hosted on http://www.foo.com | ||
var bazStorage = createGuest('http://www.baz.com/accessStorage'); | ||
var bazStorage = createGuest('http://www.baz.com/accessStorage'); | ||
``` | ||
@@ -68,2 +75,4 @@ | ||
_NOTE: The keys and the values in localStorage are always strings thus objects, numbers etc used as keys or values will be automatically converted to strings._ | ||
```javascript | ||
@@ -70,0 +79,0 @@ bazStorage.set('foo', 'bar', function(error, data) { |
@@ -1,26 +0,66 @@ | ||
var crel = require('crel'), | ||
doc = require('doc-js'), | ||
createStorageGuest = require('../../guest'); | ||
const crel = require('crel'); | ||
const doc = require('doc-js'); | ||
const createStorageGuest = require('../../guest'); | ||
var instructions = crel( | ||
'div', | ||
{ | ||
class: 'instructions', | ||
}, | ||
crel('h3', 'cross-domain-storage guest'), | ||
); | ||
const instructions = crel('div', crel('h3', 'cross-domain-storage guest'), 'If not all green then view the console.'); | ||
doc.ready(function() { | ||
/* eslint-disable */ | ||
// TODO: Make actualy tests not just this nested nightmare | ||
doc.ready(() => { | ||
crel(document.body, instructions); | ||
const storageGuest = createStorageGuest('http://localhost:9123'); | ||
var storageGuest = createStorageGuest('http://localhost:9123'); | ||
storageGuest.get('foo', (error, data) => { | ||
console.log('1. foo should be bar: ', { error, data }); | ||
crel(instructions, crel('div', { style: `color:${data === 'bar' ? 'green' : 'red'}` }, 'foo should be "bar"')); | ||
// TODO: actually write some tests here | ||
storageGuest.get('foo', function(error, data) { | ||
console.log('Guest callback: ', error, data); | ||
}); | ||
storageGuest.set('foo', 'cabbage', (error, data) => { | ||
console.log('2. set cabbage:', { error, data }); | ||
storageGuest.set('foo', 'cabbage', function(error, data) { | ||
console.log('Set callback:', error, data); | ||
storageGuest.get('foo', (error, data) => { | ||
console.log('3. foo should be cabbage: ', { error, data }); | ||
crel( | ||
instructions, | ||
crel( | ||
'div', | ||
{ | ||
style: `color:${data === 'cabbage' ? 'green' : 'red'}`, | ||
}, | ||
'foo should be "cabbage"', | ||
), | ||
); | ||
storageGuest.remove('foo', (error, data) => { | ||
console.log('4. foo removed: ', { error, data }); | ||
storageGuest.get('foo', (error, data) => { | ||
console.log('5. foo should be empty: ', { error, data }); | ||
crel( | ||
instructions, | ||
crel('div', { style: `color:${!data ? 'green' : 'red'}` }, 'foo should be empty'), | ||
); | ||
storageGuest.set('foo', { beep: 'boop' }, (error, data) => { | ||
console.log('6. set an object: ', { error, data }); | ||
storageGuest.get('foo', (error, data) => { | ||
console.log('7. foo should be [object Object]: ', { error, data }); | ||
crel( | ||
instructions, | ||
crel( | ||
'div', | ||
{ style: `color:${data === '[object Object]' ? 'green' : 'red'}` }, | ||
'foo should be [object Object]', | ||
), | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
/* eslint-enable */ |
@@ -1,19 +0,19 @@ | ||
var crel = require('crel'), | ||
doc = require('doc-js'), | ||
createStorageHost = require('../../host'); | ||
const crel = require('crel'); | ||
const doc = require('doc-js'); | ||
const createStorageHost = require('../../host'); | ||
var instructions = crel('div', { | ||
class: 'instructions', | ||
}, | ||
crel('h3', 'cross-domain-storage host') | ||
const instructions = crel( | ||
'div', | ||
{ | ||
class: 'instructions', | ||
}, | ||
crel('h3', 'cross-domain-storage host'), | ||
); | ||
doc.ready(function () { | ||
crel(document.body, | ||
instructions | ||
); | ||
doc.ready(() => { | ||
crel(document.body, instructions); | ||
window.localStorage.setItem('foo', JSON.stringify('bar')); | ||
window.localStorage.setItem('foo', 'bar'); | ||
var storageHost = createStorageHost([ | ||
createStorageHost([ | ||
{ | ||
@@ -20,0 +20,0 @@ origin: 'http://localhost:9124', |
@@ -1,29 +0,31 @@ | ||
var StaticServer = require('static-server'), | ||
hostServer = new StaticServer({ | ||
rootPath: './tests/host', | ||
port: 9123, | ||
}), | ||
guestServer = new StaticServer({ | ||
rootPath: './tests/guest', | ||
port: 9124, | ||
}), | ||
fs = require('fs'), | ||
open = require('open'), | ||
browserify = require('browserify'), | ||
watchify = require('watchify'), | ||
hostBundler = browserify({ | ||
entries: ['tests/host/index.js'], | ||
cache: {}, | ||
packageCache: {}, | ||
plugin: [watchify], | ||
debug: true, | ||
}), | ||
guestBundler = browserify({ | ||
entries: ['tests/guest/index.js'], | ||
cache: {}, | ||
packageCache: {}, | ||
plugin: [watchify], | ||
debug: true, | ||
}); | ||
const StaticServer = require('static-server'); | ||
const hostServer = new StaticServer({ | ||
rootPath: './tests/host', | ||
port: 9123, | ||
}); | ||
const guestServer = new StaticServer({ | ||
rootPath: './tests/guest', | ||
port: 9124, | ||
}); | ||
const fs = require('fs'); | ||
const open = require('open'); | ||
const browserify = require('browserify'); | ||
const watchify = require('watchify'); | ||
const hostBundler = browserify({ | ||
entries: ['tests/host/index.js'], | ||
cache: {}, | ||
packageCache: {}, | ||
plugin: [watchify], | ||
debug: true, | ||
}); | ||
const guestBundler = browserify({ | ||
entries: ['tests/guest/index.js'], | ||
cache: {}, | ||
packageCache: {}, | ||
plugin: [watchify], | ||
debug: true, | ||
}); | ||
function bundleHost() { | ||
@@ -30,0 +32,0 @@ hostBundler.bundle().pipe(fs.createWriteStream('./tests/host/index.browser.js')); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
185187
1
16
2444
92
16
+ Addedcrel@4.2.1(transitive)
- Removedtry-parse-json@^1.0.0
- Removedcrel@3.1.0(transitive)
- Removedtry-parse-json@1.0.2(transitive)
Updatedcrel@^4.2.0