-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from zippiehq/dev
Dev
- Loading branch information
Showing
10 changed files
with
1,379 additions
and
611 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2018 Zippie Ltd. | ||
* | ||
* 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. | ||
*/ | ||
export class MessageChannel { | ||
constructor (sender, receiver) { | ||
this.sender = sender | ||
this.receiver = receiver | ||
this.counterId = 0 | ||
|
||
this.responders = {} | ||
|
||
receiver.addEventListener('message', this.onMessage.bind(this)) | ||
} | ||
|
||
request (request) { | ||
return new Promise(function (resolve, reject) { | ||
var reqId = 'request-' + this.counterId++ | ||
this.responders[reqId] = [resolve, reject] | ||
|
||
this.sender.postMessage(Object.assign({requestId: reqId}, request)) | ||
|
||
//TODO: Add request timeout signaling. | ||
}.bind(this)) | ||
} | ||
|
||
onMessage (event) { | ||
console.log('CL-API: Service worker message:', event) | ||
|
||
if (!event.data.requestId) { | ||
console.error('Unsolicited message received', event) | ||
return | ||
} | ||
|
||
var responder = this.responders[event.data.requestId] | ||
delete this.responders[event.data.requestId] | ||
|
||
if (!responder) { | ||
console.error('Unrecognised requestId:', event.data.requestId) | ||
return | ||
} | ||
|
||
responder[0](event.data) | ||
} | ||
} | ||
|
||
export class WindowMessageChannel { | ||
constructor (target, receiver) { | ||
this.target = target | ||
this.receiver = receiver | ||
this.counterId = 0 | ||
|
||
this.responders = {} | ||
|
||
receiver.addEventListener('message', this.onMessage.bind(this)) | ||
} | ||
|
||
postMessage (data) { | ||
this.target.postMessage(data, '*') | ||
} | ||
|
||
request (request) { | ||
return new Promise(function (resolve, reject) { | ||
var reqId = 'request-' + this.counterId++ | ||
this.responders[reqId] = [resolve, reject] | ||
|
||
this.postMessage(Object.assign({requestId: reqId}, request)) | ||
|
||
//TODO: Add request timeout signaling. | ||
}.bind(this)) | ||
} | ||
|
||
onMessage (event) { | ||
console.log('CL-API: Client message:', event) | ||
|
||
if (!event.data.requestId) { | ||
console.error('Unsolicited message received', event) | ||
return | ||
} | ||
|
||
var responder = this.responders[event.data.requestId] | ||
delete this.responders[event.data.requestId] | ||
|
||
if (!responder) { | ||
console.error('Unrecognised requestId:', event.data.requestId) | ||
return | ||
} | ||
|
||
responder[0](event.data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Copyright (c) 2018 Zippie Ltd. | ||
* | ||
* 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. | ||
*/ | ||
const api = require('./api.js') | ||
const vault = require('./vault.js') | ||
const version = require('../version.js') | ||
|
||
const store = require('store') | ||
|
||
var worker | ||
|
||
const VAULT_MESSAGE_HANDLERS = [ | ||
new vault.RootMessageHandler(), | ||
new vault.VaultMessageHandler() | ||
] | ||
|
||
// | ||
// Worker initialization | ||
// | ||
window.addEventListener('load', function () { | ||
if (!('serviceWorker' in navigator)) { | ||
console.error('SERVICE WORKERS NOT SUPPORTED IN THIS BROWSER!') | ||
return | ||
} | ||
|
||
// Register service worker | ||
navigator.serviceWorker.register('worker-bundle.js') | ||
.then(function (registration) { | ||
console.log('CL: Service worker registered:', registration) | ||
|
||
if (registration.installing) { | ||
console.log('CL: installing') | ||
worker = registration.installing | ||
} else if (registration.waiting) { | ||
console.log('CL: waiting') | ||
worker = registration.waiting | ||
} else if (registration.active) { | ||
console.log('CL: active') | ||
worker = registration.active | ||
} | ||
|
||
if (!worker) { | ||
console.log('CL: Failed to get a handle on worker!') | ||
return | ||
} | ||
|
||
worker.addEventListener('statechange', function (event) { | ||
console.log('CL: Service worker state changed:', worker.state) | ||
|
||
if (worker.state === 'redundant') { | ||
console.log('CL: Reloading...') | ||
window.location.reload() | ||
} | ||
}) | ||
|
||
worker.addEventListener('error', function (event) { | ||
console.log('CL: Service worker error:', event) | ||
}) | ||
|
||
worker.addEventListener('messageerror', function (event) { | ||
console.log('CL: Service worker message error:', event) | ||
}) | ||
|
||
navigator.serviceWorker.addEventListener('message', function (event) { | ||
console.log('CL: Service worker message:', event) | ||
}) | ||
|
||
// Create channel between iframe -> worker | ||
window.VaultChannel = new api.MessageChannel(worker, navigator.serviceWorker) | ||
window.VaultChannel.request({'version': {}}) | ||
.then(r => { | ||
console.log("Service worker version:", r.result) | ||
}) | ||
|
||
// Migrate from LocalStorage to ServiceWorker IndexedDB | ||
let doStoreMigration = store.get('vaultSetup') | ||
if (doStoreMigration) { | ||
let keys = [ | ||
'authkey', 'localkey', 'localslice_e', 'vaultSetup' | ||
] | ||
|
||
console.log("CL: Migrating identity from LocalStorage to IndexedDB") | ||
for (var i = 0; i < keys.length; i++) { | ||
let key = keys[i] | ||
|
||
VaultChannel.request({'store.set': { | ||
key: key, | ||
value: store.get(key)} | ||
}) | ||
} | ||
store.clearAll() | ||
} | ||
|
||
console.log('Zippie Vault Version:', version) | ||
|
||
if (window.top == window.self || window.location.hash.startsWith('#iframe=')) { | ||
vault.setup().then(() => { | ||
console.log('Setup done') | ||
}) | ||
} else { | ||
// we're in an iframe, time to listen for commands | ||
console.log('Zippie Vault listening') | ||
parent.postMessage({'ready' : true}, '*') | ||
} | ||
}) | ||
.catch(function (error) { | ||
console.error('Failed to register service worker:', error) | ||
return | ||
}) | ||
}) | ||
|
||
// | ||
// Worker incoming message dispatcher | ||
// | ||
self.addEventListener('message', function (event) { | ||
console.log('CL: message received:', event.data) | ||
|
||
for (var i = 0; i < VAULT_MESSAGE_HANDLERS.length; i++) { | ||
var handler = VAULT_MESSAGE_HANDLERS[i] | ||
if (!handler.respondsTo(event)) continue | ||
if (handler.process(event)) return | ||
} | ||
|
||
console.log('CL: message type unrecognized:', event) | ||
}) | ||
|
Oops, something went wrong.