thread-stream 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/index.js +7 -0
- package/package.json +1 -1
- package/test/emit-event.js +22 -0
- package/test/event.test.js +23 -0
package/README.md
CHANGED
|
@@ -84,6 +84,31 @@ stream.end()
|
|
|
84
84
|
|
|
85
85
|
This module works with `yarn` in PnP (plug'n play) mode too!
|
|
86
86
|
|
|
87
|
+
### Emit events
|
|
88
|
+
|
|
89
|
+
You can emit events on the ThreadStream from your worker using [`worker.parentPort.postMessage()`](https://nodejs.org/api/worker_threads.html#workerparentport).
|
|
90
|
+
The message (JSON object) must have the following data structure:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
parentPort.postMessage({
|
|
94
|
+
code: 'EVENT',
|
|
95
|
+
name: 'eventName',
|
|
96
|
+
args: ['list', 'of', 'args', 123, new Error('Boom')]
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
On your ThreadStream, you can add a listener function for this event name:
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
const stream = new ThreadStream({
|
|
104
|
+
filename: join(__dirname, 'worker.js'),
|
|
105
|
+
workerData: {},
|
|
106
|
+
})
|
|
107
|
+
stream.on('eventName', function (a, b, c, n, err) {
|
|
108
|
+
console.log('received:', a, b, c, n, err) // received: list of args 123 Error: Boom
|
|
109
|
+
})
|
|
110
|
+
```
|
|
111
|
+
|
|
87
112
|
## License
|
|
88
113
|
|
|
89
114
|
MIT
|
package/index.js
CHANGED
|
@@ -163,6 +163,13 @@ function onWorkerMessage (msg) {
|
|
|
163
163
|
case 'ERROR':
|
|
164
164
|
destroy(stream, msg.err)
|
|
165
165
|
break
|
|
166
|
+
case 'EVENT':
|
|
167
|
+
if (Array.isArray(msg.args)) {
|
|
168
|
+
stream.emit(msg.name, ...msg.args)
|
|
169
|
+
} else {
|
|
170
|
+
stream.emit(msg.name, msg.args)
|
|
171
|
+
}
|
|
172
|
+
break
|
|
166
173
|
default:
|
|
167
174
|
destroy(stream, new Error('this should not happen: ' + msg.code))
|
|
168
175
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Writable } = require('stream')
|
|
4
|
+
const parentPort = require('worker_threads').parentPort
|
|
5
|
+
|
|
6
|
+
async function run () {
|
|
7
|
+
return new Writable({
|
|
8
|
+
autoDestroy: true,
|
|
9
|
+
write (chunk, enc, cb) {
|
|
10
|
+
if (parentPort) {
|
|
11
|
+
parentPort.postMessage({
|
|
12
|
+
code: 'EVENT',
|
|
13
|
+
name: 'socketError',
|
|
14
|
+
args: ['list', 'of', 'args', 123, new Error('unable to write data to the TCP socket')]
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
cb()
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = run
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const { join } = require('path')
|
|
5
|
+
const ThreadStream = require('..')
|
|
6
|
+
|
|
7
|
+
test('event propagate', function (t) {
|
|
8
|
+
const stream = new ThreadStream({
|
|
9
|
+
filename: join(__dirname, 'emit-event.js'),
|
|
10
|
+
workerData: {},
|
|
11
|
+
sync: true
|
|
12
|
+
})
|
|
13
|
+
stream.on('socketError', function (a, b, c, n, error) {
|
|
14
|
+
t.same(a, 'list')
|
|
15
|
+
t.same(b, 'of')
|
|
16
|
+
t.same(c, 'args')
|
|
17
|
+
t.same(n, 123)
|
|
18
|
+
t.same(error, new Error('unable to write data to the TCP socket'))
|
|
19
|
+
t.end()
|
|
20
|
+
})
|
|
21
|
+
stream.write('hello')
|
|
22
|
+
stream.end()
|
|
23
|
+
})
|