thread-stream 2.1.0 → 2.3.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.
@@ -29,7 +29,7 @@ jobs:
29
29
  persist-credentials: false
30
30
 
31
31
  - name: Dependency review
32
- uses: actions/dependency-review-action@v2
32
+ uses: actions/dependency-review-action@v3
33
33
 
34
34
  test:
35
35
  name: Test
@@ -23,7 +23,7 @@ jobs:
23
23
  with:
24
24
  node-version: ${{ matrix.node-version }}
25
25
  - name: Use pnpm
26
- uses: pnpm/action-setup@v2.2.2
26
+ uses: pnpm/action-setup@v2.2.4
27
27
  with:
28
28
  version: ^6.0.0
29
29
  - name: Install dependancies
package/.husky/pre-commit CHANGED
@@ -1,4 +1,4 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
3
 
4
4
  npm test
package/.taprc CHANGED
@@ -1,3 +1,4 @@
1
1
  jobs: 1
2
2
  check-coverage: false
3
- timeout: 60000
3
+ # in seconds
4
+ timeout: 60
package/bench.js CHANGED
@@ -30,7 +30,7 @@ const MAX = 10000
30
30
 
31
31
  let str = ''
32
32
 
33
- for (let i = 0; i < 10; i++) {
33
+ for (let i = 0; i < 100; i++) {
34
34
  str += 'hello'
35
35
  }
36
36
 
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
 
3
+ const { version } = require('./package.json')
3
4
  const { EventEmitter } = require('events')
4
5
  const { Worker } = require('worker_threads')
5
6
  const { join } = require('path')
@@ -29,6 +30,7 @@ class FakeWeakRef {
29
30
 
30
31
  const FinalizationRegistry = global.FinalizationRegistry || class FakeFinalizationRegistry {
31
32
  register () {}
33
+
32
34
  unregister () {}
33
35
  }
34
36
 
@@ -49,13 +51,19 @@ function createWorker (stream, opts) {
49
51
 
50
52
  const worker = new Worker(toExecute, {
51
53
  ...opts.workerOpts,
54
+ trackUnmanagedFds: false,
52
55
  workerData: {
53
56
  filename: filename.indexOf('file://') === 0
54
57
  ? filename
55
58
  : pathToFileURL(filename).href,
56
59
  dataBuf: stream[kImpl].dataBuf,
57
60
  stateBuf: stream[kImpl].stateBuf,
58
- workerData
61
+ workerData: {
62
+ $context: {
63
+ threadStreamVersion: version
64
+ },
65
+ ...workerData
66
+ }
59
67
  }
60
68
  })
61
69
 
package/lib/worker.js CHANGED
@@ -17,7 +17,7 @@ const state = new Int32Array(stateBuf)
17
17
  const data = Buffer.from(dataBuf)
18
18
 
19
19
  async function start () {
20
- let fn
20
+ let worker
21
21
  try {
22
22
  if (filename.endsWith('.ts') || filename.endsWith('.cts')) {
23
23
  // TODO: add support for the TSM modules loader ( https://github.com/lukeed/tsm ).
@@ -28,9 +28,9 @@ async function start () {
28
28
  }
29
29
  // TODO: Support ES imports once tsc, tap & ts-node provide better compatibility guarantees.
30
30
  // Remove extra forwardslash on Windows
31
- fn = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
31
+ worker = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
32
32
  } else {
33
- fn = (await realImport(filename))
33
+ worker = (await realImport(filename))
34
34
  }
35
35
  } catch (error) {
36
36
  // A yarn user that tries to start a ThreadStream for an external module
@@ -43,10 +43,10 @@ async function start () {
43
43
  // The error codes may change based on the node.js version (ENOTDIR > 12, ERR_MODULE_NOT_FOUND <= 12 )
44
44
  if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND') &&
45
45
  filename.startsWith('file://')) {
46
- fn = realRequire(decodeURIComponent(filename.replace('file://', '')))
46
+ worker = realRequire(decodeURIComponent(filename.replace('file://', '')))
47
47
  } else if (error.code === undefined) {
48
48
  // When bundled with pkg, an undefined error is thrown when called with realImport
49
- fn = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
49
+ worker = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
50
50
  } else {
51
51
  throw error
52
52
  }
@@ -55,10 +55,10 @@ async function start () {
55
55
  // Depending on how the default export is performed, and on how the code is
56
56
  // transpiled, we may find cases of two nested "default" objects.
57
57
  // See https://github.com/pinojs/pino/issues/1243#issuecomment-982774762
58
- if (typeof fn === 'object') fn = fn.default
59
- if (typeof fn === 'object') fn = fn.default
58
+ if (typeof worker === 'object') worker = worker.default
59
+ if (typeof worker === 'object') worker = worker.default
60
60
 
61
- destination = await fn(workerData.workerData)
61
+ destination = await worker(workerData.workerData)
62
62
 
63
63
  destination.on('error', function (err) {
64
64
  Atomics.store(state, WRITE_INDEX, -2)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thread-stream",
3
- "version": "2.1.0",
3
+ "version": "2.3.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",
@@ -0,0 +1,21 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const { join } = require('path')
5
+ const ThreadStream = require('..')
6
+ const { version } = require('../package.json')
7
+ require('why-is-node-running')
8
+
9
+ test('get context', (t) => {
10
+ const stream = new ThreadStream({
11
+ filename: join(__dirname, 'get-context.js'),
12
+ workerData: {},
13
+ sync: true
14
+ })
15
+ t.on('end', () => stream.end())
16
+ stream.on('context', (ctx) => {
17
+ t.same(ctx.threadStreamVersion, version)
18
+ t.end()
19
+ })
20
+ stream.write('hello')
21
+ })
@@ -3,13 +3,15 @@
3
3
  const { test } = require('tap')
4
4
  const { join } = require('path')
5
5
  const ThreadStream = require('..')
6
+ require('why-is-node-running')
6
7
 
7
- test('event propagate', function (t) {
8
+ test('event propagate', (t) => {
8
9
  const stream = new ThreadStream({
9
10
  filename: join(__dirname, 'emit-event.js'),
10
11
  workerData: {},
11
12
  sync: true
12
13
  })
14
+ t.on('end', () => stream.end())
13
15
  stream.on('socketError', function (a, b, c, n, error) {
14
16
  t.same(a, 'list')
15
17
  t.same(b, 'of')
@@ -19,5 +21,4 @@ test('event propagate', function (t) {
19
21
  t.end()
20
22
  })
21
23
  stream.write('hello')
22
- stream.end()
23
24
  })
@@ -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 (opts) {
7
+ return new Writable({
8
+ autoDestroy: true,
9
+ write (chunk, enc, cb) {
10
+ if (parentPort) {
11
+ parentPort.postMessage({
12
+ code: 'EVENT',
13
+ name: 'context',
14
+ args: opts.$context
15
+ })
16
+ }
17
+ cb()
18
+ }
19
+ })
20
+ }
21
+
22
+ module.exports = run
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const test = require('tape')
3
+ const { test } = require('tap')
4
4
  const indexes = require('../lib/indexes')
5
5
 
6
6
  for (const index of Object.keys(indexes)) {