@zuplo/cli 7.13.10 → 7.14.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.
Files changed (69) hide show
  1. package/node_modules/@posthog/core/dist/cookie.d.ts +4 -0
  2. package/node_modules/@posthog/core/dist/cookie.d.ts.map +1 -1
  3. package/node_modules/@posthog/core/dist/cookie.js +8 -2
  4. package/node_modules/@posthog/core/dist/cookie.mjs +8 -2
  5. package/node_modules/@posthog/core/dist/posthog-core.d.ts +2 -0
  6. package/node_modules/@posthog/core/dist/posthog-core.d.ts.map +1 -1
  7. package/node_modules/@posthog/core/dist/posthog-core.js +19 -2
  8. package/node_modules/@posthog/core/dist/posthog-core.mjs +19 -2
  9. package/node_modules/@posthog/core/dist/utils/bot-detection.d.ts.map +1 -1
  10. package/node_modules/@posthog/core/dist/utils/bot-detection.js +1 -0
  11. package/node_modules/@posthog/core/dist/utils/bot-detection.mjs +1 -0
  12. package/node_modules/@posthog/core/package.json +2 -2
  13. package/node_modules/@posthog/core/src/cookie.ts +13 -2
  14. package/node_modules/@posthog/core/src/posthog-core.ts +25 -2
  15. package/node_modules/@posthog/core/src/utils/bot-detection.spec.ts +7 -0
  16. package/node_modules/@posthog/core/src/utils/bot-detection.ts +1 -0
  17. package/node_modules/@zuplo/core/customer.cli.minified.js +332 -332
  18. package/node_modules/@zuplo/core/index.minified.js +320 -320
  19. package/node_modules/@zuplo/core/package.json +1 -1
  20. package/node_modules/@zuplo/graphql/package.json +1 -1
  21. package/node_modules/@zuplo/openapi-tools/package.json +1 -1
  22. package/node_modules/@zuplo/otel/package.json +1 -1
  23. package/node_modules/@zuplo/runtime/out/esm/{chunk-SXGSDVGB.js → chunk-4SVXBKXH.js} +85 -85
  24. package/node_modules/@zuplo/runtime/out/esm/chunk-4SVXBKXH.js.map +1 -0
  25. package/node_modules/@zuplo/runtime/out/esm/chunk-VP5Q3DEN.js +26 -0
  26. package/node_modules/@zuplo/runtime/out/esm/chunk-VP5Q3DEN.js.map +1 -0
  27. package/node_modules/@zuplo/runtime/out/esm/{chunk-PXS4NOKU.js → chunk-WUZE2HKB.js} +17 -17
  28. package/node_modules/@zuplo/runtime/out/esm/chunk-WUZE2HKB.js.map +1 -0
  29. package/node_modules/@zuplo/runtime/out/esm/index.js +1 -1
  30. package/node_modules/@zuplo/runtime/out/esm/internal/index.js +1 -1
  31. package/node_modules/@zuplo/runtime/out/esm/mcp-gateway/index.js +1 -1
  32. package/node_modules/@zuplo/runtime/out/esm/mocks/index.js +1 -1
  33. package/node_modules/@zuplo/runtime/out/esm/sdks/aws/index.js +1 -1
  34. package/node_modules/@zuplo/runtime/out/types/index.d.ts +78 -1
  35. package/node_modules/@zuplo/runtime/out/types/internal/index.d.ts +137 -1
  36. package/node_modules/@zuplo/runtime/package.json +1 -1
  37. package/node_modules/express-rate-limit/dist/index.cjs +1 -1
  38. package/node_modules/express-rate-limit/dist/index.mjs +1 -1
  39. package/node_modules/express-rate-limit/package.json +1 -1
  40. package/node_modules/ip-address/README.md +39 -39
  41. package/node_modules/ip-address/dist/ipv6.js +1 -1
  42. package/node_modules/ip-address/dist/ipv6.js.map +1 -1
  43. package/node_modules/ip-address/package.json +1 -1
  44. package/node_modules/thread-stream/.github/workflows/ci.yml +1 -1
  45. package/node_modules/thread-stream/SECURITY.md +77 -0
  46. package/node_modules/thread-stream/index.d.ts +2 -1
  47. package/node_modules/thread-stream/index.js +65 -7
  48. package/node_modules/thread-stream/lib/wait.js +23 -5
  49. package/node_modules/thread-stream/package.json +10 -5
  50. package/node_modules/thread-stream/test/create-and-exit.js +1 -1
  51. package/node_modules/thread-stream/test/end.test.js +35 -0
  52. package/node_modules/thread-stream/test/error-flush.test.js +96 -0
  53. package/node_modules/thread-stream/test/error-immediate.js +17 -0
  54. package/node_modules/thread-stream/test/flush-ref-keeps-alive.js +23 -0
  55. package/node_modules/thread-stream/test/flush-unref-exit.js +23 -0
  56. package/node_modules/thread-stream/test/ready-race-worker.js +22 -0
  57. package/node_modules/thread-stream/test/ready.test.js +46 -0
  58. package/node_modules/thread-stream/test/thread-management.test.js +68 -0
  59. package/node_modules/thread-stream/test/to-file-sync.js +23 -0
  60. package/node_modules/thread-stream/test/types/index.test-d.ts +26 -0
  61. package/node_modules/thread-stream/test/wait.test.js +131 -0
  62. package/package.json +11 -11
  63. package/node_modules/@zuplo/runtime/out/esm/chunk-PXS4NOKU.js.map +0 -1
  64. package/node_modules/@zuplo/runtime/out/esm/chunk-SXGSDVGB.js.map +0 -1
  65. package/node_modules/@zuplo/runtime/out/esm/chunk-ZCJ5QBVD.js +0 -26
  66. package/node_modules/@zuplo/runtime/out/esm/chunk-ZCJ5QBVD.js.map +0 -1
  67. package/node_modules/thread-stream/.claude/settings.local.json +0 -15
  68. /package/node_modules/@zuplo/runtime/out/esm/{chunk-SXGSDVGB.js.LEGAL.txt → chunk-4SVXBKXH.js.LEGAL.txt} +0 -0
  69. /package/node_modules/@zuplo/runtime/out/esm/{chunk-PXS4NOKU.js.LEGAL.txt → chunk-WUZE2HKB.js.LEGAL.txt} +0 -0
@@ -33,6 +33,7 @@ function resetIndexes (stream) {
33
33
  Atomics.store(stream[kImpl].state, READ_INDEX, 0)
34
34
  Atomics.store(stream[kImpl].state, WRITE_INDEX, 0)
35
35
  })
36
+ Atomics.notify(stream[kImpl].state, READ_INDEX)
36
37
  }
37
38
 
38
39
  class FakeWeakRef {
@@ -110,6 +111,12 @@ function drain (stream) {
110
111
  function nextFlush (stream) {
111
112
  while (true) {
112
113
  const writeIndex = Atomics.load(stream[kImpl].state, WRITE_INDEX)
114
+ // The worker stores -2 in shared memory before its ERROR message is
115
+ // delivered, so the stream may not be destroyed yet.
116
+ if (stream.destroyed || writeIndex < 0) {
117
+ return
118
+ }
119
+
113
120
  const leftover = stream[kImpl].data.length - writeIndex
114
121
 
115
122
  if (leftover > 0) {
@@ -168,9 +175,11 @@ function onWorkerMessage (msg) {
168
175
 
169
176
  switch (msg.code) {
170
177
  case 'READY':
171
- // Replace the FakeWeakRef with a
172
- // proper one.
173
- this.stream = new WeakRef(stream)
178
+ // Allow inactive streams to be collected, but retain streams that are
179
+ // ending until the worker has emitted close.
180
+ if (!stream[kImpl].ending) {
181
+ this.stream = new WeakRef(stream)
182
+ }
174
183
 
175
184
  waitForRead(stream, () => {
176
185
  stream[kImpl].ready = true
@@ -248,6 +257,11 @@ class ThreadStream extends EventEmitter {
248
257
  this[kImpl].bufLen = 0
249
258
  this[kImpl].flushCallbacks = new Map()
250
259
  this[kImpl].nextFlushId = 0
260
+ this[kImpl].flushRetainCount = 0
261
+ // Worker threads start referenced. Track user-facing ref() / unref()
262
+ // separately so a pending flush(cb) can keep the loop alive without
263
+ // clobbering an explicit ref().
264
+ this[kImpl].userRef = true
251
265
 
252
266
  // TODO (fix): Make private?
253
267
  this.worker = createWorker(this, opts) // TODO (fix): make private
@@ -306,19 +320,39 @@ class ThreadStream extends EventEmitter {
306
320
  }
307
321
 
308
322
  this[kImpl].ending = true
323
+ // The worker normally holds only a weak reference after READY. Keep this
324
+ // stream alive while its destination runs any asynchronous finalizer.
325
+ this.worker.stream = new FakeWeakRef(this)
309
326
  end(this)
310
327
  }
311
328
 
312
329
  flush (cb) {
313
- cb = typeof cb === 'function' ? cb : noop
330
+ const userCb = typeof cb === 'function' ? cb : null
331
+ cb = userCb || noop
332
+
333
+ if (userCb) {
334
+ retainWorkerForPendingFlush(this)
335
+ }
336
+
337
+ let finished = false
338
+ const done = (err) => {
339
+ if (finished) {
340
+ return
341
+ }
342
+ finished = true
343
+ if (userCb) {
344
+ releaseWorkerForPendingFlush(this)
345
+ }
346
+ cb(err)
347
+ }
314
348
 
315
349
  flushBuffer(this, (err) => {
316
350
  if (err) {
317
- process.nextTick(cb, err)
351
+ process.nextTick(done, err)
318
352
  return
319
353
  }
320
354
 
321
- requestWorkerFlush(this, cb)
355
+ requestWorkerFlush(this, done)
322
356
  })
323
357
  }
324
358
 
@@ -332,10 +366,14 @@ class ThreadStream extends EventEmitter {
332
366
  }
333
367
 
334
368
  unref () {
335
- this.worker.unref()
369
+ this[kImpl].userRef = false
370
+ if (this[kImpl].flushRetainCount === 0) {
371
+ this.worker.unref()
372
+ }
336
373
  }
337
374
 
338
375
  ref () {
376
+ this[kImpl].userRef = true
339
377
  this.worker.ref()
340
378
  }
341
379
 
@@ -408,6 +446,23 @@ function waitForRead (stream, cb) {
408
446
  })
409
447
  }
410
448
 
449
+ function retainWorkerForPendingFlush (stream) {
450
+ if (stream[kImpl].flushRetainCount === 0) {
451
+ stream.worker.ref()
452
+ }
453
+ stream[kImpl].flushRetainCount++
454
+ }
455
+
456
+ function releaseWorkerForPendingFlush (stream) {
457
+ if (stream[kImpl].flushRetainCount === 0) {
458
+ return
459
+ }
460
+ stream[kImpl].flushRetainCount--
461
+ if (stream[kImpl].flushRetainCount === 0 && !stream[kImpl].userRef) {
462
+ stream.worker.unref()
463
+ }
464
+ }
465
+
411
466
  function requestWorkerFlush (stream, cb) {
412
467
  if (stream[kImpl].destroyed) {
413
468
  process.nextTick(cb, new Error('the worker has exited'))
@@ -594,6 +649,9 @@ function writeSync (stream) {
594
649
 
595
650
  while (stream[kImpl].bufLen !== 0) {
596
651
  const writeIndex = Atomics.load(stream[kImpl].state, WRITE_INDEX)
652
+ if (writeIndex < 0) {
653
+ throw new Error('_writeSync failed')
654
+ }
597
655
  const leftover = stream[kImpl].data.length - writeIndex
598
656
  if (leftover === 0) {
599
657
  flushSync(stream)
@@ -8,13 +8,23 @@ const WAIT_MS = 10000
8
8
  function wait (state, index, expected, timeout, done) {
9
9
  const max = timeout === Infinity ? Infinity : Date.now() + timeout
10
10
 
11
- const check = () => {
11
+ const check = (previous) => {
12
12
  const current = Atomics.load(state, index)
13
- if (current === expected) {
13
+ // Use >= rather than ===: the value this wait() observes (READ_INDEX) is
14
+ // advanced by the worker in jumps toward WRITE_INDEX, so it can skip over
15
+ // a snapshot of `expected` taken earlier (e.g. the READY handshake). Because
16
+ // the reader can never fall behind what has already been written, once it
17
+ // reaches or passes `expected` it is guaranteed to have caught up.
18
+ if (current >= expected) {
14
19
  done(null, 'ok')
15
20
  return
16
21
  }
17
22
 
23
+ if (previous !== undefined && current !== previous) {
24
+ done(null, 'not-equal')
25
+ return
26
+ }
27
+
18
28
  if (max !== Infinity && Date.now() > max) {
19
29
  done(null, 'timed-out')
20
30
  return
@@ -25,10 +35,18 @@ function wait (state, index, expected, timeout, done) {
25
35
  const result = Atomics.waitAsync(state, index, current, remaining)
26
36
 
27
37
  if (result.async) {
28
- result.value.then(check)
38
+ result.value.then((res) => {
39
+ if (res === 'ok') {
40
+ const latest = Atomics.load(state, index)
41
+ done(null, latest >= expected ? 'ok' : 'not-equal')
42
+ return
43
+ }
44
+
45
+ check(current)
46
+ })
29
47
  } else {
30
- // Value already changed (not-equal) - recheck on next tick
31
- setImmediate(check)
48
+ // Value changed between Atomics.load() and Atomics.waitAsync().
49
+ setImmediate(done, null, 'not-equal')
32
50
  }
33
51
  }
34
52
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thread-stream",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
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",
@@ -11,7 +11,7 @@
11
11
  "real-require": "^1.0.0"
12
12
  },
13
13
  "devDependencies": {
14
- "@types/node": "^25.0.2",
14
+ "@types/node": "^26.1.1",
15
15
  "@yao-pkg/pkg": "^6.0.0",
16
16
  "borp": "^1.0.0",
17
17
  "desm": "^1.3.0",
@@ -21,16 +21,21 @@
21
21
  "pino-elasticsearch": "^9.0.0",
22
22
  "sonic-boom": "^5.0.0",
23
23
  "ts-node": "^10.8.0",
24
+ "tsd": "^0.33.0",
24
25
  "typescript": "~5.7.3"
25
26
  },
26
27
  "scripts": {
27
28
  "build": "tsc --noEmit",
28
29
  "lint": "eslint",
29
- "test": "npm run lint && npm run build && npm run transpile && borp --pattern \"test/*.test.{js,mjs}\"",
30
- "test:ci": "npm run lint && npm run transpile && borp --pattern \"test/*.test.{js,mjs}\"",
31
- "test:yarn": "npm run transpile && borp --pattern \"test/*.test.js\"",
30
+ "test": "npm run lint && npm run build && npm run test:types && npm run transpile && borp --expose-gc --pattern \"test/*.test.{js,mjs}\"",
31
+ "test:ci": "npm run lint && npm run test:types && npm run transpile && borp --expose-gc --pattern \"test/*.test.{js,mjs}\"",
32
+ "test:types": "tsd",
33
+ "test:yarn": "npm run transpile && borp --expose-gc --pattern \"test/*.test.js\"",
32
34
  "transpile": "sh ./test/ts/transpile.sh"
33
35
  },
36
+ "tsd": {
37
+ "directory": "test/types"
38
+ },
34
39
  "repository": {
35
40
  "type": "git",
36
41
  "url": "git+https://github.com/mcollina/thread-stream.git"
@@ -4,7 +4,7 @@ const { join } = require('path')
4
4
  const ThreadStream = require('..')
5
5
 
6
6
  const stream = new ThreadStream({
7
- filename: join(__dirname, 'to-file.js'),
7
+ filename: join(__dirname, 'to-file-sync.js'),
8
8
  workerData: { dest: process.argv[2] },
9
9
  sync: true
10
10
  })
@@ -31,6 +31,41 @@ test('destroy support', function (t, done) {
31
31
  stream.end()
32
32
  })
33
33
 
34
+ test('end keeps the stream alive until close', { skip: typeof global.gc !== 'function' }, function (t, done) {
35
+ const dest = file()
36
+
37
+ const onClose = () => {
38
+ clearInterval(gcTimer)
39
+ clearTimeout(timeout)
40
+ readFile(dest, 'utf8', (err, data) => {
41
+ assert.ifError(err)
42
+ assert.strictEqual(data, 'hello world\nsomething else\n')
43
+ done()
44
+ })
45
+ }
46
+
47
+ function setup () {
48
+ const stream = new ThreadStream({
49
+ filename: join(__dirname, 'to-file-on-final.js'),
50
+ workerData: { dest },
51
+ sync: true
52
+ })
53
+
54
+ stream.on('close', onClose)
55
+ assert.ok(stream.write('hello world\n'))
56
+ assert.ok(stream.write('something else\n'))
57
+ stream.end()
58
+ }
59
+
60
+ setup()
61
+
62
+ const gcTimer = setInterval(global.gc, 1)
63
+ const timeout = setTimeout(() => {
64
+ clearInterval(gcTimer)
65
+ assert.fail('stream was collected before close')
66
+ }, 5000)
67
+ })
68
+
34
69
  test('synchronous _final support', function (t, done) {
35
70
  const dest = file()
36
71
  const stream = new ThreadStream({
@@ -0,0 +1,96 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('node:test')
4
+ const assert = require('node:assert')
5
+ const { once } = require('node:events')
6
+ const { join } = require('path')
7
+ const { WRITE_INDEX } = require('../lib/indexes')
8
+ const ThreadStream = require('..')
9
+
10
+ test('emit error if worker stream emit error (async mode)', async function (t) {
11
+ const stream = new ThreadStream({
12
+ filename: join(__dirname, 'error.js'),
13
+ sync: false
14
+ })
15
+
16
+ const closed = once(stream, 'close').catch(() => {})
17
+ stream.on('error', () => {})
18
+
19
+ await once(stream, 'ready')
20
+ stream.write('hello world\n')
21
+
22
+ let [err] = await once(stream, 'error')
23
+ assert.strictEqual(err.message, 'kaboom')
24
+
25
+ stream.write('noop');
26
+ [err] = await once(stream, 'error')
27
+ assert.strictEqual(err.message, 'the worker has exited')
28
+
29
+ stream.write('noop');
30
+ [err] = await once(stream, 'error')
31
+ assert.strictEqual(err.message, 'the worker has exited')
32
+
33
+ await closed
34
+ })
35
+
36
+ test('nextFlush does not crash when worker errors before flush', async function (t) {
37
+ const stream = new ThreadStream({
38
+ filename: join(__dirname, 'error-immediate.js'),
39
+ sync: false
40
+ })
41
+
42
+ const closed = once(stream, 'close').catch(() => {})
43
+ stream.on('error', () => {})
44
+
45
+ await once(stream, 'ready')
46
+
47
+ // Block the event loop
48
+ const int32 = new Int32Array(new SharedArrayBuffer(WRITE_INDEX))
49
+ Atomics.wait(int32, 0, 0, 50)
50
+
51
+ // stream.destroyed is still false (ERROR not yet processed).
52
+ // Writing triggers setImmediate(nextFlush, this).
53
+ stream.write('data')
54
+
55
+ // The event loop then processes pending messages before the
56
+ // check phase:
57
+ // 1. ERROR message → destroy → stream.destroyed = true
58
+ // 2. check phase: nextFlush runs → loads WRITE_INDEX = -2
59
+
60
+ await closed
61
+ })
62
+
63
+ test('nextFlush does not crash when the error sentinel is seen before the ERROR message', async function (t) {
64
+ const stream = new ThreadStream({
65
+ filename: join(__dirname, 'error-immediate.js'),
66
+ sync: false
67
+ })
68
+
69
+ const closed = once(stream, 'close').catch(() => {})
70
+ const errored = once(stream, 'error')
71
+
72
+ await once(stream, 'ready')
73
+
74
+ // Hold back worker messages so the shared WRITE_INDEX = -2 is
75
+ // observed while stream.destroyed is still false.
76
+ const [onMessage] = stream.worker.listeners('message')
77
+ const queued = []
78
+ stream.worker.removeListener('message', onMessage)
79
+ stream.worker.on('message', (msg) => queued.push(msg))
80
+
81
+ while (!queued.some((msg) => msg.code === 'ERROR')) {
82
+ await new Promise((resolve) => setTimeout(resolve, 10))
83
+ }
84
+
85
+ stream.write('data')
86
+ await new Promise((resolve) => setImmediate(resolve))
87
+ assert.strictEqual(stream.destroyed, false)
88
+
89
+ for (const msg of queued) {
90
+ onMessage.call(stream.worker, msg)
91
+ }
92
+
93
+ const [err] = await errored
94
+ assert.strictEqual(err.message, 'kaboom')
95
+ await closed
96
+ })
@@ -0,0 +1,17 @@
1
+ 'use strict'
2
+
3
+ const { EventEmitter } = require('events')
4
+
5
+ async function run (opts) {
6
+ const dest = new EventEmitter()
7
+ dest.write = function () { return true }
8
+ dest.end = function () { dest.emit('close') }
9
+
10
+ setTimeout(() => {
11
+ dest.emit('error', new Error('kaboom'))
12
+ }, 50)
13
+
14
+ return dest
15
+ }
16
+
17
+ module.exports = run
@@ -0,0 +1,23 @@
1
+ 'use strict'
2
+
3
+ const { join } = require('path')
4
+ const ThreadStream = require('..')
5
+
6
+ const stream = new ThreadStream({
7
+ filename: join(__dirname, 'to-file.js'),
8
+ workerData: { dest: process.argv[2] },
9
+ sync: false
10
+ })
11
+
12
+ stream.on('ready', () => {
13
+ stream.ref()
14
+ stream.write('hello world\n')
15
+
16
+ stream.flush((err) => {
17
+ if (err) {
18
+ console.error(err)
19
+ process.exit(2)
20
+ }
21
+ process.stdout.write('CALLBACK\n')
22
+ })
23
+ })
@@ -0,0 +1,23 @@
1
+ 'use strict'
2
+
3
+ const { join } = require('path')
4
+ const ThreadStream = require('..')
5
+
6
+ const stream = new ThreadStream({
7
+ filename: join(__dirname, 'to-file.js'),
8
+ workerData: { dest: process.argv[2] },
9
+ sync: false
10
+ })
11
+
12
+ stream.on('ready', () => {
13
+ stream.write('hello world\n')
14
+ stream.unref()
15
+
16
+ stream.flush((err) => {
17
+ if (err) {
18
+ console.error(err)
19
+ process.exit(2)
20
+ }
21
+ process.stdout.write('CALLBACK\n')
22
+ })
23
+ })
@@ -0,0 +1,22 @@
1
+ 'use strict'
2
+
3
+ const { parentPort, workerData } = require('node:worker_threads')
4
+ const { WRITE_INDEX, READ_INDEX } = require('../lib/indexes')
5
+
6
+ const state = new Int32Array(workerData.stateBuf)
7
+
8
+ parentPort.on('message', (msg) => {
9
+ if (msg?.code === 'ADVANCE') {
10
+ const end = Atomics.load(state, WRITE_INDEX)
11
+ Atomics.store(state, READ_INDEX, end)
12
+ Atomics.notify(state, READ_INDEX)
13
+ } else if (msg?.code === 'SHUTDOWN') {
14
+ process.exit(0)
15
+ }
16
+ })
17
+
18
+ parentPort.postMessage({ code: 'READY' })
19
+ parentPort.postMessage({
20
+ code: 'EVENT',
21
+ name: 'write-more'
22
+ })
@@ -0,0 +1,46 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('node:test')
4
+ const assert = require('node:assert')
5
+ const { once } = require('node:events')
6
+ const { join } = require('node:path')
7
+ const ThreadStream = require('..')
8
+
9
+ test('ready is emitted when the read index skips the startup snapshot', { timeout: 5000 }, async function (t) {
10
+ const previousOverrides = globalThis.__bundlerPathsOverrides
11
+ globalThis.__bundlerPathsOverrides = {
12
+ ...previousOverrides,
13
+ 'thread-stream-worker': join(__dirname, 'ready-race-worker.js')
14
+ }
15
+
16
+ t.after(() => {
17
+ globalThis.__bundlerPathsOverrides = previousOverrides
18
+ })
19
+
20
+ const stream = new ThreadStream({
21
+ filename: __filename,
22
+ sync: true
23
+ })
24
+ stream.on('error', () => {})
25
+
26
+ t.after(async () => {
27
+ if (!stream.destroyed) {
28
+ await stream.worker.terminate()
29
+ }
30
+ })
31
+
32
+ stream.on('write-more', () => {
33
+ stream.write('b')
34
+ stream.worker.postMessage({ code: 'ADVANCE' })
35
+ })
36
+
37
+ const ready = once(stream, 'ready')
38
+ const close = once(stream, 'close')
39
+
40
+ stream.write('a')
41
+ await ready
42
+ assert.strictEqual(stream.ready, true)
43
+
44
+ stream.worker.postMessage({ code: 'SHUTDOWN' })
45
+ await close
46
+ })
@@ -20,6 +20,74 @@ test('exits with 0', async function (t) {
20
20
  assert.strictEqual(data, 'hello world\n')
21
21
  })
22
22
 
23
+ test('flush callback fires after unref on an idle event loop', async function () {
24
+ const dest = file()
25
+ const child = fork(join(__dirname, 'flush-unref-exit.js'), [dest], {
26
+ silent: true
27
+ })
28
+
29
+ let stdout = ''
30
+ child.stdout.setEncoding('utf8')
31
+ child.stdout.on('data', (chunk) => {
32
+ stdout += chunk
33
+ })
34
+
35
+ const [code] = await once(child, 'exit')
36
+ assert.strictEqual(code, 0)
37
+ assert.match(stdout, /CALLBACK/)
38
+
39
+ const data = await readFile(dest, 'utf8')
40
+ assert.strictEqual(data, 'hello world\n')
41
+ })
42
+
43
+ test('flush callback does not unref a stream the user has ref\'d', async function () {
44
+ const dest = file()
45
+ const child = fork(join(__dirname, 'flush-ref-keeps-alive.js'), [dest], {
46
+ silent: true
47
+ })
48
+
49
+ let stdout = ''
50
+ child.stdout.setEncoding('utf8')
51
+ child.stdout.on('data', (chunk) => {
52
+ stdout += chunk
53
+ })
54
+
55
+ await new Promise((resolve, reject) => {
56
+ const onExit = (code) => {
57
+ reject(new Error(`child exited ${code} before the flush callback`))
58
+ }
59
+ const onData = () => {
60
+ if (!stdout.includes('CALLBACK')) {
61
+ return
62
+ }
63
+ child.stdout.off('data', onData)
64
+ child.off('exit', onExit)
65
+ resolve()
66
+ }
67
+ child.stdout.on('data', onData)
68
+ child.once('exit', onExit)
69
+ })
70
+
71
+ await new Promise((resolve, reject) => {
72
+ const timer = setTimeout(() => {
73
+ child.off('exit', onExit)
74
+ resolve()
75
+ }, 150)
76
+ function onExit (code) {
77
+ clearTimeout(timer)
78
+ reject(new Error(`child exited ${code} after a ref'd flush`))
79
+ }
80
+ child.once('exit', onExit)
81
+ })
82
+
83
+ child.kill()
84
+ await once(child, 'exit')
85
+ assert.match(stdout, /CALLBACK/)
86
+
87
+ const data = await readFile(dest, 'utf8')
88
+ assert.strictEqual(data, 'hello world\n')
89
+ })
90
+
23
91
  test('emit error if thread exits', async function (t) {
24
92
  const stream = new ThreadStream({
25
93
  filename: join(__dirname, 'exit.js'),
@@ -0,0 +1,23 @@
1
+ 'use strict'
2
+
3
+ const fs = require('fs')
4
+ const { Writable } = require('stream')
5
+
6
+ // Writes synchronously, so data is on disk as soon as the worker
7
+ // advances the read index. Useful when the process exits right after
8
+ // flushSync().
9
+ async function run (opts) {
10
+ const fd = fs.openSync(opts.dest, 'w')
11
+ return new Writable({
12
+ autoDestroy: true,
13
+ write (chunk, enc, cb) {
14
+ fs.writeSync(fd, chunk)
15
+ cb()
16
+ },
17
+ destroy (err, cb) {
18
+ fs.close(fd, () => cb(err))
19
+ }
20
+ })
21
+ }
22
+
23
+ module.exports = run
@@ -0,0 +1,26 @@
1
+ import { expectType } from "tsd";
2
+ import { type Transferable, MessageChannel } from "worker_threads";
3
+ import ThreadStream from "../../index";
4
+
5
+ const stream = new ThreadStream({ filename: "./worker.js" });
6
+
7
+ expectType<boolean>(stream.write("hello"));
8
+ expectType<void>(stream.end());
9
+ expectType<void>(stream.flush());
10
+ expectType<void>(stream.flushSync());
11
+
12
+ // emit overload for posting a message to the worker,
13
+ // optionally with a list of transferable objects
14
+ const { port1 } = new MessageChannel();
15
+ expectType<boolean>(stream.emit("message", { foo: "bar" }));
16
+ expectType<boolean>(stream.emit("message", { foo: "bar" }, [port1]));
17
+ expectType<boolean>(stream.emit("message", "data", [new ArrayBuffer(8)]));
18
+
19
+ // the transferList parameter of the 'message' overload must be Transferable[]
20
+ // (Parameters<> resolves to the last overload, which is the 'message' one)
21
+ expectType<Transferable[] | undefined>(
22
+ undefined as unknown as Parameters<ThreadStream["emit"]>[2],
23
+ );
24
+
25
+ // generic EventEmitter emit
26
+ expectType<boolean>(stream.emit("drain"));