thread-stream 2.1.0 → 2.2.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/.husky/pre-commit +2 -2
- package/.taprc +2 -1
- package/index.js +8 -1
- package/lib/worker.js +8 -8
- package/package.json +1 -1
- package/test/context.test.js +21 -0
- package/test/event.test.js +3 -2
- package/test/get-context.js +22 -0
- package/test/{indexes.js → indexes.test.js} +1 -1
package/.husky/pre-commit
CHANGED
package/.taprc
CHANGED
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
|
|
|
@@ -55,7 +57,12 @@ function createWorker (stream, opts) {
|
|
|
55
57
|
: pathToFileURL(filename).href,
|
|
56
58
|
dataBuf: stream[kImpl].dataBuf,
|
|
57
59
|
stateBuf: stream[kImpl].stateBuf,
|
|
58
|
-
workerData
|
|
60
|
+
workerData: {
|
|
61
|
+
$context: {
|
|
62
|
+
threadStreamVersion: version
|
|
63
|
+
},
|
|
64
|
+
...workerData
|
|
65
|
+
}
|
|
59
66
|
}
|
|
60
67
|
})
|
|
61
68
|
|
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
|
|
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
|
-
|
|
31
|
+
worker = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
|
|
32
32
|
} else {
|
|
33
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
59
|
-
if (typeof
|
|
58
|
+
if (typeof worker === 'object') worker = worker.default
|
|
59
|
+
if (typeof worker === 'object') worker = worker.default
|
|
60
60
|
|
|
61
|
-
destination = await
|
|
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
|
@@ -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
|
+
})
|
package/test/event.test.js
CHANGED
|
@@ -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',
|
|
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
|