blob: 1889e3c04ec7c8fd1b86198c6993ed19d7ce9af4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
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))
port.onDisconnect.addListener(this._onDisconnect.bind(this))
}
// private
PortDuplexStream.prototype._onMessage = function (msg) {
if (Buffer.isBuffer(msg)) {
delete msg._isBuffer
var data = new Buffer(msg)
// console.log('PortDuplexStream - saw message as buffer', data)
this.push(data)
} else {
// console.log('PortDuplexStream - saw message', msg)
this.push(msg)
}
}
PortDuplexStream.prototype._onDisconnect = function () {
try {
// this.end()
this.emit('close')
} catch (err) {
this.emit('error', err)
}
}
// stream plumbing
PortDuplexStream.prototype._read = noop
PortDuplexStream.prototype._write = function (msg, encoding, cb) {
try {
if (Buffer.isBuffer(msg)) {
var data = msg.toJSON()
data._isBuffer = true
// console.log('PortDuplexStream - sent message as buffer', data)
this._port.postMessage(data)
} else {
// console.log('PortDuplexStream - sent message', msg)
this._port.postMessage(msg)
}
cb()
} catch (err) {
console.error(err)
// this.emit('error', err)
cb(new Error('PortDuplexStream - disconnected'))
}
}
// util
function noop () {}
|