A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/lib/port-stream.js

52 lines
1.1 KiB

const Duplex = require('readable-stream').Duplex
const inherits = require('util').inherits
const noop = function(){}
module.exports = PortDuplexStream
inherits(PortDuplexStream, Duplex)
9 years ago
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))
}
// private
9 years ago
PortDuplexStream.prototype._onMessage = function (msg) {
if (Buffer.isBuffer(msg)) {
delete msg._isBuffer
var data = new Buffer(msg)
this.push(data)
} else {
this.push(msg)
}
}
9 years ago
PortDuplexStream.prototype._onDisconnect = function () {
this.destroy()
}
// stream plumbing
PortDuplexStream.prototype._read = noop
9 years ago
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)
}
9 years ago
} catch (err) {
return cb(new Error('PortDuplexStream - disconnected'))
}
cb()
}