thread-stream 0.13.0 → 0.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.
- package/.github/dependabot.yml +11 -10
- package/.github/workflows/ci.yml +3 -3
- package/.github/workflows/package-manager-ci.yml +5 -5
- package/.taprc +1 -0
- package/index.js +10 -2
- package/lib/worker.js +1 -1
- package/package.json +2 -2
- package/test/base.test.js +23 -0
- package/test/commonjs-fallback.test.js +27 -0
- package/test/dir with spaces/test-package.zip +0 -0
- package/test/multibyte-chars.test.mjs +23 -0
- package/test/string-limit-2.test.js +35 -0
package/.github/dependabot.yml
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
version: 2
|
|
2
2
|
updates:
|
|
3
|
-
- package-ecosystem: github-actions
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
- package-ecosystem: "github-actions"
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: "monthly"
|
|
7
|
+
open-pull-requests-limit: 10
|
|
8
|
+
|
|
9
|
+
- package-ecosystem: "npm"
|
|
10
|
+
directory: "/"
|
|
11
|
+
schedule:
|
|
12
|
+
interval: "weekly"
|
|
13
|
+
open-pull-requests-limit: 10
|
package/.github/workflows/ci.yml
CHANGED
|
@@ -19,10 +19,10 @@ jobs:
|
|
|
19
19
|
|
|
20
20
|
steps:
|
|
21
21
|
|
|
22
|
-
- uses: actions/checkout@
|
|
22
|
+
- uses: actions/checkout@v3
|
|
23
23
|
|
|
24
24
|
- name: Use Node.js
|
|
25
|
-
uses: actions/setup-node@
|
|
25
|
+
uses: actions/setup-node@v3
|
|
26
26
|
with:
|
|
27
27
|
node-version: ${{ matrix.node-version }}
|
|
28
28
|
|
|
@@ -55,6 +55,6 @@ jobs:
|
|
|
55
55
|
needs: test
|
|
56
56
|
runs-on: ubuntu-latest
|
|
57
57
|
steps:
|
|
58
|
-
- uses: fastify/github-action-merge-dependabot@v2.
|
|
58
|
+
- uses: fastify/github-action-merge-dependabot@v2.7.1
|
|
59
59
|
with:
|
|
60
60
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -17,13 +17,13 @@ jobs:
|
|
|
17
17
|
os: [macOS-latest, windows-latest]
|
|
18
18
|
node-version: [12, 14, 16]
|
|
19
19
|
steps:
|
|
20
|
-
- uses: actions/checkout@
|
|
20
|
+
- uses: actions/checkout@v3
|
|
21
21
|
- name: Use Node.js ${{ matrix.node-version }}
|
|
22
|
-
uses: actions/setup-node@
|
|
22
|
+
uses: actions/setup-node@v3
|
|
23
23
|
with:
|
|
24
24
|
node-version: ${{ matrix.node-version }}
|
|
25
25
|
- name: Use pnpm
|
|
26
|
-
uses: pnpm/action-setup@v2.
|
|
26
|
+
uses: pnpm/action-setup@v2.2.1
|
|
27
27
|
with:
|
|
28
28
|
version: ^6.0.0
|
|
29
29
|
- name: Install dependancies
|
|
@@ -39,9 +39,9 @@ jobs:
|
|
|
39
39
|
os: [macOS-latest]
|
|
40
40
|
node-version: [12, 14, 16]
|
|
41
41
|
steps:
|
|
42
|
-
- uses: actions/checkout@
|
|
42
|
+
- uses: actions/checkout@v3
|
|
43
43
|
- name: Use Node.js ${{ matrix.node-version }}
|
|
44
|
-
uses: actions/setup-node@
|
|
44
|
+
uses: actions/setup-node@v3
|
|
45
45
|
with:
|
|
46
46
|
node-version: ${{ matrix.node-version }}
|
|
47
47
|
- name: Use yarn
|
package/.taprc
CHANGED
package/index.js
CHANGED
|
@@ -104,13 +104,18 @@ function nextFlush (stream) {
|
|
|
104
104
|
} else {
|
|
105
105
|
// multi-byte utf-8
|
|
106
106
|
stream.flush(() => {
|
|
107
|
+
// err is already handled in flush()
|
|
108
|
+
if (stream.destroyed) {
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
|
|
107
112
|
Atomics.store(stream[kImpl].state, READ_INDEX, 0)
|
|
108
113
|
Atomics.store(stream[kImpl].state, WRITE_INDEX, 0)
|
|
109
114
|
|
|
110
115
|
// Find a toWrite length that fits the buffer
|
|
111
116
|
// it must exists as the buffer is at least 4 bytes length
|
|
112
117
|
// and the max utf-8 length for a char is 4 bytes.
|
|
113
|
-
while (toWriteBytes > stream[kImpl].
|
|
118
|
+
while (toWriteBytes > stream[kImpl].data.length) {
|
|
114
119
|
leftover = leftover / 2
|
|
115
120
|
toWrite = stream[kImpl].buf.slice(0, leftover)
|
|
116
121
|
toWriteBytes = Buffer.byteLength(toWrite)
|
|
@@ -255,7 +260,10 @@ class ThreadStream extends EventEmitter {
|
|
|
255
260
|
|
|
256
261
|
flush (cb) {
|
|
257
262
|
if (this[kImpl].destroyed) {
|
|
258
|
-
|
|
263
|
+
if (typeof cb === 'function') {
|
|
264
|
+
process.nextTick(cb, new Error('the worker has exited'))
|
|
265
|
+
}
|
|
266
|
+
return
|
|
259
267
|
}
|
|
260
268
|
|
|
261
269
|
// TODO write all .buf
|
package/lib/worker.js
CHANGED
|
@@ -30,7 +30,7 @@ async function start () {
|
|
|
30
30
|
// The error codes may change based on the node.js version (ENOTDIR > 12, ERR_MODULE_NOT_FOUND <= 12 )
|
|
31
31
|
if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND') &&
|
|
32
32
|
workerData.filename.startsWith('file://')) {
|
|
33
|
-
fn = realRequire(workerData.filename.replace('file://', ''))
|
|
33
|
+
fn = realRequire(decodeURIComponent(workerData.filename.replace('file://', '')))
|
|
34
34
|
} else {
|
|
35
35
|
throw error
|
|
36
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "thread-stream",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "A streaming way to send data to a Node.js Worker Thread",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"husky": "^7.0.0",
|
|
13
13
|
"sonic-boom": "^2.0.1",
|
|
14
14
|
"standard": "^16.0.3",
|
|
15
|
-
"tap": "^
|
|
15
|
+
"tap": "^16.0.0",
|
|
16
16
|
"why-is-node-running": "^2.2.0"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
package/test/base.test.js
CHANGED
|
@@ -246,3 +246,26 @@ test('pass down MessagePorts', async function (t) {
|
|
|
246
246
|
|
|
247
247
|
t.equal(strings, 'hello world\nsomething else\n')
|
|
248
248
|
})
|
|
249
|
+
|
|
250
|
+
test('destroy does not error', function (t) {
|
|
251
|
+
t.plan(3)
|
|
252
|
+
|
|
253
|
+
const dest = file()
|
|
254
|
+
const stream = new ThreadStream({
|
|
255
|
+
filename: join(__dirname, 'to-file.js'),
|
|
256
|
+
workerData: { dest },
|
|
257
|
+
sync: false
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
stream.on('ready', () => {
|
|
261
|
+
t.pass('ready emitted')
|
|
262
|
+
stream.worker.terminate()
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
stream.on('error', (err) => {
|
|
266
|
+
t.equal(err.message, 'The worker thread exited')
|
|
267
|
+
stream.flush((err) => {
|
|
268
|
+
t.equal(err.message, 'the worker has exited')
|
|
269
|
+
})
|
|
270
|
+
})
|
|
271
|
+
})
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('tap')
|
|
4
|
+
const { join } = require('path')
|
|
5
|
+
const { MessageChannel } = require('worker_threads')
|
|
6
|
+
const { once } = require('events')
|
|
4
7
|
const ThreadStream = require('..')
|
|
5
8
|
|
|
6
9
|
const isYarnPnp = process.versions.pnp !== undefined
|
|
@@ -27,3 +30,27 @@ test('yarn module resolution', { skip: !isYarnPnp }, t => {
|
|
|
27
30
|
t.ok(stream.writable)
|
|
28
31
|
stream.end()
|
|
29
32
|
})
|
|
33
|
+
|
|
34
|
+
test('yarn module resolution for directories with special characters', { skip: !isYarnPnp }, async t => {
|
|
35
|
+
t.plan(3)
|
|
36
|
+
|
|
37
|
+
const { port1, port2 } = new MessageChannel()
|
|
38
|
+
const stream = new ThreadStream({
|
|
39
|
+
filename: join(__dirname, 'dir with spaces', 'test-package.zip', 'worker.js'),
|
|
40
|
+
workerData: { port: port1 },
|
|
41
|
+
workerOpts: {
|
|
42
|
+
transferList: [port1]
|
|
43
|
+
},
|
|
44
|
+
sync: false
|
|
45
|
+
})
|
|
46
|
+
t.teardown(() => {
|
|
47
|
+
stream.end()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
t.ok(stream.write('hello world\n'))
|
|
51
|
+
t.ok(stream.write('something else\n'))
|
|
52
|
+
|
|
53
|
+
const [strings] = await once(port2, 'message')
|
|
54
|
+
|
|
55
|
+
t.equal(strings, 'hello world\nsomething else\n')
|
|
56
|
+
})
|
|
Binary file
|
|
@@ -49,3 +49,26 @@ test('break up utf8 multibyte (async)', (t) => {
|
|
|
49
49
|
stream.write(longString)
|
|
50
50
|
stream.end()
|
|
51
51
|
})
|
|
52
|
+
|
|
53
|
+
test('break up utf8 multibyte several times bigger than write buffer', (t) => {
|
|
54
|
+
t.plan(2)
|
|
55
|
+
const longString = '\u03A3'.repeat(32)
|
|
56
|
+
|
|
57
|
+
const dest = file()
|
|
58
|
+
const stream = new ThreadStream({
|
|
59
|
+
bufferSize: 15, // this must be odd
|
|
60
|
+
filename: join(import.meta.url, 'to-file.js'),
|
|
61
|
+
workerData: { dest },
|
|
62
|
+
sync: false
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
stream.on('finish', () => {
|
|
66
|
+
readFile(dest, 'utf8', (err, data) => {
|
|
67
|
+
t.error(err)
|
|
68
|
+
t.equal(data, longString)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
stream.write(longString)
|
|
73
|
+
stream.end()
|
|
74
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const { join } = require('path')
|
|
5
|
+
const { file } = require('./helper')
|
|
6
|
+
const { createReadStream } = require('fs')
|
|
7
|
+
const ThreadStream = require('..')
|
|
8
|
+
const buffer = require('buffer')
|
|
9
|
+
|
|
10
|
+
const MAX_STRING = buffer.constants.MAX_STRING_LENGTH
|
|
11
|
+
|
|
12
|
+
t.plan(1)
|
|
13
|
+
|
|
14
|
+
const dest = file()
|
|
15
|
+
const stream = new ThreadStream({
|
|
16
|
+
filename: join(__dirname, 'to-file.js'),
|
|
17
|
+
workerData: { dest },
|
|
18
|
+
sync: false
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
stream.on('close', async () => {
|
|
22
|
+
t.comment('close emitted')
|
|
23
|
+
let buf
|
|
24
|
+
for await (const chunk of createReadStream(dest)) {
|
|
25
|
+
buf = chunk
|
|
26
|
+
}
|
|
27
|
+
t.equal('asd', buf.toString().slice(-3))
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
stream.on('ready', () => {
|
|
31
|
+
t.comment('open emitted')
|
|
32
|
+
stream.write('a'.repeat(MAX_STRING - 2))
|
|
33
|
+
stream.write('asd')
|
|
34
|
+
stream.end()
|
|
35
|
+
})
|