aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2015-12-19 14:05:16 +0800
committerkumavis <aaron@kumavis.me>2015-12-19 14:05:16 +0800
commit72a747165dda417aa7968e44b404eb90707202a2 (patch)
tree0fd4c52727815665b8851ca80b2b8d9dc462b19a /app/scripts/lib
parentcc935a1ebaa73d855056d143ef548ca982a5f8e8 (diff)
downloadtangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.gz
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.bz2
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.lz
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.xz
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.tar.zst
tangerine-wallet-browser-72a747165dda417aa7968e44b404eb90707202a2.zip
migrate to ProviderEngine zero-client
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/local-message-stream.js53
-rw-r--r--app/scripts/lib/port-stream.js36
-rw-r--r--app/scripts/lib/stream-provider.js50
3 files changed, 139 insertions, 0 deletions
diff --git a/app/scripts/lib/local-message-stream.js b/app/scripts/lib/local-message-stream.js
new file mode 100644
index 000000000..42d193e04
--- /dev/null
+++ b/app/scripts/lib/local-message-stream.js
@@ -0,0 +1,53 @@
+const Duplex = require('readable-stream').Duplex
+const inherits = require('util').inherits
+
+module.exports = LocalMessageDuplexStream
+
+
+inherits(LocalMessageDuplexStream, Duplex)
+
+function LocalMessageDuplexStream(opts){
+ Duplex.call(this, {
+ objectMode: true,
+ })
+
+ // this._origin = opts.origin
+ this._name = opts.name
+ this._target = opts.target
+
+ // console.log('LocalMessageDuplexStream ('+this._name+') - initialized...')
+ window.addEventListener('message', this._onMessage.bind(this), false)
+}
+
+// private
+
+LocalMessageDuplexStream.prototype._onMessage = function(event){
+ var msg = event.data
+ // console.log('LocalMessageDuplexStream ('+this._name+') - heard message...')
+ // validate message
+ if (event.origin !== location.origin) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (event.origin !== location.origin) ')
+ if (typeof msg !== 'object') return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (typeof msg !== "object") ')
+ if (msg.target !== this._name) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (msg.target !== this._name) ', msg.target, this._name)
+ if (!msg.data) return //console.log('LocalMessageDuplexStream ('+this._name+') - rejected - (!msg.data) ')
+ // console.log('LocalMessageDuplexStream ('+this._name+') - accepted', msg.data)
+ // forward message
+ this.push(msg.data)
+}
+
+// stream plumbing
+
+LocalMessageDuplexStream.prototype._read = noop
+
+LocalMessageDuplexStream.prototype._write = function(data, encoding, cb){
+ // console.log('LocalMessageDuplexStream ('+this._name+') - sending message...')
+ var message = {
+ target: this._target,
+ data: data,
+ }
+ window.postMessage(message, location.origin)
+ cb()
+}
+
+// util
+
+function noop(){} \ No newline at end of file
diff --git a/app/scripts/lib/port-stream.js b/app/scripts/lib/port-stream.js
new file mode 100644
index 000000000..d256efc9a
--- /dev/null
+++ b/app/scripts/lib/port-stream.js
@@ -0,0 +1,36 @@
+const Duplex = require('readable-stream').Duplex
+const inherits = require('util').inherits
+
+module.exports = PortDuplexStream
+
+
+inherits(PortDuplexStream, Duplex)
+
+function PortDuplexStream(port){
+ Duplex.call(this, {
+ objectMode: true,
+ })
+ this._port = port
+ port.onMessage.addListener(this._onMessage.bind(this))
+}
+
+// private
+
+PortDuplexStream.prototype._onMessage = function(msg){
+ // console.log('PortDuplexStream - saw message', msg)
+ this.push(msg)
+}
+
+// stream plumbing
+
+PortDuplexStream.prototype._read = noop
+
+PortDuplexStream.prototype._write = function(msg, encoding, cb){
+ // console.log('PortDuplexStream - sent message', msg)
+ this._port.postMessage(msg)
+ cb()
+}
+
+// util
+
+function noop(){} \ No newline at end of file
diff --git a/app/scripts/lib/stream-provider.js b/app/scripts/lib/stream-provider.js
new file mode 100644
index 000000000..3e7f443c8
--- /dev/null
+++ b/app/scripts/lib/stream-provider.js
@@ -0,0 +1,50 @@
+const Duplex = require('readable-stream').Duplex
+const inherits = require('util').inherits
+
+module.exports = StreamProvider
+
+
+inherits(StreamProvider, Duplex)
+
+function StreamProvider(){
+ Duplex.call(this, {
+ objectMode: true,
+ })
+
+ this._handlers = {}
+}
+
+// public
+
+StreamProvider.prototype.send = function(payload){
+ throw new Error('StreamProvider - does not support synchronous RPC calls')
+}
+
+StreamProvider.prototype.sendAsync = function(payload, callback){
+// console.log('StreamProvider - sending payload', payload)
+ this._handlers[payload.id] = callback
+ this.push(payload)
+}
+
+// private
+
+StreamProvider.prototype._onResponse = function(payload){
+// console.log('StreamProvider - got response', payload)
+ var callback = this._handlers[payload.id]
+ if (!callback) throw new Error('StreamProvider - Unknown response id')
+ delete this._handlers[payload.id]
+ callback(null, payload)
+}
+
+// stream plumbing
+
+StreamProvider.prototype._read = noop
+
+StreamProvider.prototype._write = function(msg, encoding, cb){
+ this._onResponse(msg)
+ cb()
+}
+
+// util
+
+function noop(){} \ No newline at end of file