aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/port-stream.js
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2018-08-21 06:39:03 +0800
committerDan Finlay <dan@danfinlay.com>2018-08-21 06:45:50 +0800
commit6ce119d1fbf458fa93c63198da7e5bff3045d955 (patch)
treefb18f9bbad5317ec68a6e4388a50c24f5bd027f8 /app/scripts/lib/port-stream.js
parent887cad973f25f43d2d4502ff31657f156a44b188 (diff)
downloaddexon-wallet-6ce119d1fbf458fa93c63198da7e5bff3045d955.tar.gz
dexon-wallet-6ce119d1fbf458fa93c63198da7e5bff3045d955.tar.zst
dexon-wallet-6ce119d1fbf458fa93c63198da7e5bff3045d955.zip
Move inpage-provider and port-stream outside
With the creation of the [metamask-extension-provider](https://github.com/MetaMask/metamask-extension-provider) we have our first non-core module that is dependent on the inpage-provider and port-stream. To reduce the size of its dependencies, I have moved the [metamask-inpage-provider](https://github.com/MetaMask/metamask-inpage-provider) into its own module, as well as [extension-port-stream](https://github.com/MetaMask/extension-port-stream). This allows them to be more easily depended & iterated on by external projects.
Diffstat (limited to 'app/scripts/lib/port-stream.js')
-rw-r--r--app/scripts/lib/port-stream.js80
1 files changed, 0 insertions, 80 deletions
diff --git a/app/scripts/lib/port-stream.js b/app/scripts/lib/port-stream.js
deleted file mode 100644
index fd65d94f..00000000
--- a/app/scripts/lib/port-stream.js
+++ /dev/null
@@ -1,80 +0,0 @@
-const Duplex = require('readable-stream').Duplex
-const inherits = require('util').inherits
-const noop = function () {}
-
-module.exports = PortDuplexStream
-
-inherits(PortDuplexStream, Duplex)
-
-/**
- * Creates a stream that's both readable and writable.
- * The stream supports arbitrary objects.
- *
- * @class
- * @param {Object} port Remote Port object
- */
-function PortDuplexStream (port) {
- Duplex.call(this, {
- objectMode: true,
- })
- this._port = port
- port.onMessage.addListener(this._onMessage.bind(this))
- port.onDisconnect.addListener(this._onDisconnect.bind(this))
-}
-
-/**
- * Callback triggered when a message is received from
- * the remote Port associated with this Stream.
- *
- * @private
- * @param {Object} msg - Payload from the onMessage listener of Port
- */
-PortDuplexStream.prototype._onMessage = function (msg) {
- if (Buffer.isBuffer(msg)) {
- delete msg._isBuffer
- var data = new Buffer(msg)
- this.push(data)
- } else {
- this.push(msg)
- }
-}
-
-/**
- * Callback triggered when the remote Port
- * associated with this Stream disconnects.
- *
- * @private
- */
-PortDuplexStream.prototype._onDisconnect = function () {
- this.destroy()
-}
-
-/**
- * Explicitly sets read operations to a no-op
- */
-PortDuplexStream.prototype._read = noop
-
-
-/**
- * Called internally when data should be written to
- * this writable stream.
- *
- * @private
- * @param {*} msg Arbitrary object to write
- * @param {string} encoding Encoding to use when writing payload
- * @param {Function} cb Called when writing is complete or an error occurs
- */
-PortDuplexStream.prototype._write = function (msg, encoding, cb) {
- try {
- if (Buffer.isBuffer(msg)) {
- var data = msg.toJSON()
- data._isBuffer = true
- this._port.postMessage(data)
- } else {
- this._port.postMessage(msg)
- }
- } catch (err) {
- return cb(new Error('PortDuplexStream - disconnected'))
- }
- cb()
-}