thread-stream 2.4.1 → 2.6.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/.github/workflows/ci.yml +2 -2
- package/.github/workflows/package-manager-ci.yml +3 -3
- package/README.md +22 -1
- package/index.d.ts +20 -0
- package/index.js +3 -0
- package/package.json +4 -4
- package/test/on-message.js +18 -0
- package/test/post-message.test.js +24 -0
package/.github/workflows/ci.yml
CHANGED
|
@@ -29,7 +29,7 @@ jobs:
|
|
|
29
29
|
persist-credentials: false
|
|
30
30
|
|
|
31
31
|
- name: Dependency review
|
|
32
|
-
uses: actions/dependency-review-action@
|
|
32
|
+
uses: actions/dependency-review-action@v4
|
|
33
33
|
|
|
34
34
|
test:
|
|
35
35
|
name: Test
|
|
@@ -51,7 +51,7 @@ jobs:
|
|
|
51
51
|
persist-credentials: false
|
|
52
52
|
|
|
53
53
|
- name: Setup Node ${{ matrix.node-version }}
|
|
54
|
-
uses: actions/setup-node@
|
|
54
|
+
uses: actions/setup-node@v4
|
|
55
55
|
with:
|
|
56
56
|
node-version: ${{ matrix.node-version }}
|
|
57
57
|
|
|
@@ -19,11 +19,11 @@ jobs:
|
|
|
19
19
|
steps:
|
|
20
20
|
- uses: actions/checkout@v3
|
|
21
21
|
- name: Use Node.js ${{ matrix.node-version }}
|
|
22
|
-
uses: actions/setup-node@
|
|
22
|
+
uses: actions/setup-node@v4
|
|
23
23
|
with:
|
|
24
24
|
node-version: ${{ matrix.node-version }}
|
|
25
25
|
- name: Use pnpm
|
|
26
|
-
uses: pnpm/action-setup@
|
|
26
|
+
uses: pnpm/action-setup@v3.0.0
|
|
27
27
|
with:
|
|
28
28
|
version: ^6.0.0
|
|
29
29
|
- name: Install dependancies
|
|
@@ -42,7 +42,7 @@ jobs:
|
|
|
42
42
|
steps:
|
|
43
43
|
- uses: actions/checkout@v3
|
|
44
44
|
- name: Use Node.js ${{ matrix.node-version }}
|
|
45
|
-
uses: actions/setup-node@
|
|
45
|
+
uses: actions/setup-node@v4
|
|
46
46
|
with:
|
|
47
47
|
node-version: ${{ matrix.node-version }}
|
|
48
48
|
- name: Use yarn
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# thread-stream
|
|
2
2
|
[](https://www.npmjs.com/package/thread-stream)
|
|
3
|
-
[](https://github.com/pinojs/thread-stream/actions)
|
|
4
4
|
[](https://standardjs.com/)
|
|
5
5
|
|
|
6
6
|
A streaming way to send data to a Node.js Worker Thread.
|
|
@@ -109,6 +109,27 @@ stream.on('eventName', function (a, b, c, n, err) {
|
|
|
109
109
|
})
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
+
### Post Messages
|
|
113
|
+
|
|
114
|
+
You can post messages to the worker by emitting a `message` event on the ThreadStream.
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
const stream = new ThreadStream({
|
|
118
|
+
filename: join(__dirname, 'worker.js'),
|
|
119
|
+
workerData: {},
|
|
120
|
+
})
|
|
121
|
+
stream.emit('message', message)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
On your worker, you can listen for this message using [`worker.parentPort.on('message', cb)`](https://nodejs.org/api/worker_threads.html#event-message).
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
const { parentPort } = require('worker_threads')
|
|
128
|
+
parentPort.on('message', function (message) {
|
|
129
|
+
console.log('received:', message)
|
|
130
|
+
})
|
|
131
|
+
```
|
|
132
|
+
|
|
112
133
|
## License
|
|
113
134
|
|
|
114
135
|
MIT
|
package/index.d.ts
CHANGED
|
@@ -68,6 +68,26 @@ declare class ThreadStream extends EventEmitter {
|
|
|
68
68
|
* @throws {Error} if the stream is already flushing, if it fails to flush or if it takes more than 10 seconds to flush.
|
|
69
69
|
*/
|
|
70
70
|
flushSync(): void
|
|
71
|
+
/**
|
|
72
|
+
* Synchronously calls each of the listeners registered for the event named`eventName`, in the order they were registered, passing the supplied arguments
|
|
73
|
+
* to each.
|
|
74
|
+
*
|
|
75
|
+
* @param eventName the name of the event.
|
|
76
|
+
* @param args the arguments to be passed to the event handlers.
|
|
77
|
+
* @returns {boolean} `true` if the event had listeners, `false` otherwise.
|
|
78
|
+
*/
|
|
79
|
+
emit(eventName: string | symbol, ...args: any[]): boolean {
|
|
80
|
+
return super.emit(eventName, ...args);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Post a message to the Worker with specified data and an optional list of transferable objects.
|
|
84
|
+
*
|
|
85
|
+
* @param eventName the name of the event, specifically 'message'.
|
|
86
|
+
* @param message message data to be sent to the Worker.
|
|
87
|
+
* @param transferList an optional list of transferable objects to be transferred to the Worker context.
|
|
88
|
+
* @returns {boolean} true if the event had listeners, false otherwise.
|
|
89
|
+
*/
|
|
90
|
+
emit(eventName: 'message', message: any, transferList?: Transferable[]): boolean
|
|
71
91
|
}
|
|
72
92
|
|
|
73
93
|
export = ThreadStream;
|
package/index.js
CHANGED
|
@@ -228,6 +228,9 @@ class ThreadStream extends EventEmitter {
|
|
|
228
228
|
|
|
229
229
|
// TODO (fix): Make private?
|
|
230
230
|
this.worker = createWorker(this, opts) // TODO (fix): make private
|
|
231
|
+
this.on('message', (message, transferList) => {
|
|
232
|
+
this.worker.postMessage(message, transferList)
|
|
233
|
+
})
|
|
231
234
|
}
|
|
232
235
|
|
|
233
236
|
write (data) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "thread-stream",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "A streaming way to send data to a Node.js Worker Thread",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -12,13 +12,13 @@
|
|
|
12
12
|
"@types/tap": "^15.0.0",
|
|
13
13
|
"desm": "^1.3.0",
|
|
14
14
|
"fastbench": "^1.0.1",
|
|
15
|
-
"husky": "^
|
|
16
|
-
"pino-elasticsearch": "^
|
|
15
|
+
"husky": "^9.0.6",
|
|
16
|
+
"pino-elasticsearch": "^8.0.0",
|
|
17
17
|
"sonic-boom": "^3.0.0",
|
|
18
18
|
"standard": "^17.0.0",
|
|
19
19
|
"tap": "^16.2.0",
|
|
20
20
|
"ts-node": "^10.8.0",
|
|
21
|
-
"typescript": "^
|
|
21
|
+
"typescript": "^5.3.2",
|
|
22
22
|
"why-is-node-running": "^2.2.2"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parentPort } = require('worker_threads')
|
|
4
|
+
const { Writable } = require('stream')
|
|
5
|
+
|
|
6
|
+
function run () {
|
|
7
|
+
parentPort.once('message', function ({ text, takeThisPortPlease }) {
|
|
8
|
+
takeThisPortPlease.postMessage(`received: ${text}`)
|
|
9
|
+
})
|
|
10
|
+
return new Writable({
|
|
11
|
+
autoDestroy: true,
|
|
12
|
+
write (chunk, enc, cb) {
|
|
13
|
+
cb()
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = run
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const { join } = require('path')
|
|
5
|
+
const { once } = require('events')
|
|
6
|
+
const { MessageChannel } = require('worker_threads')
|
|
7
|
+
const ThreadStream = require('..')
|
|
8
|
+
|
|
9
|
+
test('message events emitted on the stream are posted to the worker', async function (t) {
|
|
10
|
+
t.plan(1)
|
|
11
|
+
|
|
12
|
+
const { port1, port2 } = new MessageChannel()
|
|
13
|
+
const stream = new ThreadStream({
|
|
14
|
+
filename: join(__dirname, 'on-message.js'),
|
|
15
|
+
sync: false
|
|
16
|
+
})
|
|
17
|
+
t.teardown(() => {
|
|
18
|
+
stream.end()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
stream.emit('message', { text: 'hello', takeThisPortPlease: port1 }, [port1])
|
|
22
|
+
const [confirmation] = await once(port2, 'message')
|
|
23
|
+
t.equal(confirmation, 'received: hello')
|
|
24
|
+
})
|