Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ This module works with `yarn` in PnP (plug'n play) mode too!
### Emit events

You can emit events on the ThreadStream from your worker using [`worker.parentPort.postMessage()`](https://nodejs.org/api/worker_threads.html#workerparentport).
The message (JSON object) must have the following data structure:
Messages that do not carry a thread-stream protocol `code` are ignored.
For custom events, the message (JSON object) must have the following data structure:

```js
parentPort.postMessage({
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ function onWorkerMessage (msg) {
return
}

// Node.js watch mode may send internal worker messages that do not
// participate in thread-stream's worker protocol.
if (msg?.code == null) {
return
}

switch (msg.code) {
case 'READY':
// Replace the FakeWeakRef with a
Expand Down
19 changes: 19 additions & 0 deletions test/message-without-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const { Writable } = require('stream')
const { parentPort } = require('worker_threads')

async function run () {
parentPort.postMessage({
internal: 'watch-mode'
})

return new Writable({
autoDestroy: true,
write (chunk, enc, cb) {
cb()
}
})
}

module.exports = run
28 changes: 28 additions & 0 deletions test/watch-mode.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const { test } = require('tap')
const { join } = require('path')
const ThreadStream = require('..')

test('ignores worker messages without a protocol code', function (t) {
t.plan(2)

const stream = new ThreadStream({
filename: join(__dirname, 'message-without-code.js'),
sync: false
})

const errors = []
stream.on('error', err => {
errors.push(err)
})

stream.on('ready', () => {
t.ok(stream.write('hello world\n'))
stream.end()
})

stream.on('finish', () => {
t.same(errors, [])
})
})
Loading