thread-stream 0.11.1 → 0.13.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/workflows/ci.yml +3 -3
- package/.github/workflows/package-manager-ci.yml +4 -4
- package/.taprc +2 -0
- package/index.js +295 -211
- package/lib/worker.js +9 -2
- package/package.json +7 -3
- package/test/base.test.js +38 -102
- package/test/bundlers.test.js +33 -0
- package/test/close-on-gc.js +8 -10
- package/test/commonjs-fallback.test.js +3 -3
- package/test/create-and-exit.js +5 -7
- package/test/custom-worker.js +9 -0
- package/test/end.test.js +22 -38
- package/test/esm.test.mjs +8 -36
- package/test/helper.js +8 -1
- package/test/multibyte-chars.test.mjs +6 -10
- package/test/string-limit.test.js +36 -0
- package/test/thread-management.test.js +5 -5
package/package.json
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "thread-stream",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "A streaming way to send data to a Node.js Worker Thread",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"real-require": "^0.1.0"
|
|
8
|
+
},
|
|
6
9
|
"devDependencies": {
|
|
7
10
|
"desm": "^1.1.0",
|
|
8
11
|
"fastbench": "^1.0.1",
|
|
9
12
|
"husky": "^7.0.0",
|
|
10
13
|
"sonic-boom": "^2.0.1",
|
|
11
14
|
"standard": "^16.0.3",
|
|
12
|
-
"tap": "^15.0.0"
|
|
15
|
+
"tap": "^15.0.0",
|
|
16
|
+
"why-is-node-running": "^2.2.0"
|
|
13
17
|
},
|
|
14
18
|
"scripts": {
|
|
15
|
-
"test": "standard && tap
|
|
19
|
+
"test": "standard && tap test/*.test.*js",
|
|
16
20
|
"test:ci": "standard && tap \"test/**/*.test.*js\" --no-check-coverage --coverage-report=lcovonly",
|
|
17
21
|
"test:yarn": "tap \"test/**/*.test.js\" --no-check-coverage",
|
|
18
22
|
"prepare": "husky install"
|
package/test/base.test.js
CHANGED
|
@@ -9,7 +9,7 @@ const { MessageChannel } = require('worker_threads')
|
|
|
9
9
|
const { once } = require('events')
|
|
10
10
|
|
|
11
11
|
test('base sync=true', function (t) {
|
|
12
|
-
t.plan(
|
|
12
|
+
t.plan(15)
|
|
13
13
|
|
|
14
14
|
const dest = file()
|
|
15
15
|
const stream = new ThreadStream({
|
|
@@ -18,35 +18,36 @@ test('base sync=true', function (t) {
|
|
|
18
18
|
sync: true
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
stream.
|
|
22
|
-
t.pass('drain')
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
stream.on('ready', () => {
|
|
26
|
-
t.pass('ready emitted')
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
t.ok(stream.write('hello world\n'))
|
|
30
|
-
t.ok(stream.write('something else\n'))
|
|
31
|
-
t.ok(stream.writable)
|
|
32
|
-
|
|
33
|
-
stream.end()
|
|
21
|
+
t.same(stream.writableObjectMode, false)
|
|
34
22
|
|
|
23
|
+
t.same(stream.writableFinished, false)
|
|
35
24
|
stream.on('finish', () => {
|
|
25
|
+
t.same(stream.writableFinished, true)
|
|
36
26
|
readFile(dest, 'utf8', (err, data) => {
|
|
37
27
|
t.error(err)
|
|
38
28
|
t.equal(data, 'hello world\nsomething else\n')
|
|
39
29
|
})
|
|
40
30
|
})
|
|
41
31
|
|
|
32
|
+
t.same(stream.closed, false)
|
|
42
33
|
stream.on('close', () => {
|
|
34
|
+
t.same(stream.closed, true)
|
|
43
35
|
t.notOk(stream.writable)
|
|
44
36
|
t.pass('close emitted')
|
|
45
37
|
})
|
|
38
|
+
|
|
39
|
+
t.same(stream.writableNeedDrain, false)
|
|
40
|
+
t.ok(stream.write('hello world\n'))
|
|
41
|
+
t.ok(stream.write('something else\n'))
|
|
42
|
+
t.ok(stream.writable)
|
|
43
|
+
|
|
44
|
+
t.same(stream.writableEnded, false)
|
|
45
|
+
stream.end()
|
|
46
|
+
t.same(stream.writableEnded, true)
|
|
46
47
|
})
|
|
47
48
|
|
|
48
49
|
test('overflow sync=true', function (t) {
|
|
49
|
-
t.plan(
|
|
50
|
+
t.plan(3)
|
|
50
51
|
|
|
51
52
|
const dest = file()
|
|
52
53
|
const stream = new ThreadStream({
|
|
@@ -56,11 +57,6 @@ test('overflow sync=true', function (t) {
|
|
|
56
57
|
sync: true
|
|
57
58
|
})
|
|
58
59
|
|
|
59
|
-
stream.on('ready', () => {
|
|
60
|
-
t.pass('ready emitted')
|
|
61
|
-
write()
|
|
62
|
-
})
|
|
63
|
-
|
|
64
60
|
let count = 0
|
|
65
61
|
|
|
66
62
|
// Write 10 chars, 20 times
|
|
@@ -75,6 +71,8 @@ test('overflow sync=true', function (t) {
|
|
|
75
71
|
setImmediate(write)
|
|
76
72
|
}
|
|
77
73
|
|
|
74
|
+
write()
|
|
75
|
+
|
|
78
76
|
stream.on('finish', () => {
|
|
79
77
|
t.pass('finish emitted')
|
|
80
78
|
})
|
|
@@ -96,13 +94,10 @@ test('overflow sync=false', function (t) {
|
|
|
96
94
|
sync: false
|
|
97
95
|
})
|
|
98
96
|
|
|
99
|
-
stream.on('ready', () => {
|
|
100
|
-
t.pass('ready emitted')
|
|
101
|
-
write()
|
|
102
|
-
})
|
|
103
|
-
|
|
104
97
|
let count = 0
|
|
105
98
|
|
|
99
|
+
t.same(stream.writableNeedDrain, false)
|
|
100
|
+
|
|
106
101
|
// Write 10 chars, 20 times
|
|
107
102
|
function write () {
|
|
108
103
|
if (count++ === 20) {
|
|
@@ -111,12 +106,17 @@ test('overflow sync=false', function (t) {
|
|
|
111
106
|
return
|
|
112
107
|
}
|
|
113
108
|
|
|
114
|
-
stream.write('aaaaaaaaaa')
|
|
109
|
+
if (!stream.write('aaaaaaaaaa')) {
|
|
110
|
+
t.same(stream.writableNeedDrain, true)
|
|
111
|
+
}
|
|
115
112
|
// do not wait for drain event
|
|
116
113
|
setImmediate(write)
|
|
117
114
|
}
|
|
118
115
|
|
|
116
|
+
write()
|
|
117
|
+
|
|
119
118
|
stream.on('drain', () => {
|
|
119
|
+
t.same(stream.writableNeedDrain, false)
|
|
120
120
|
t.pass('drain')
|
|
121
121
|
})
|
|
122
122
|
|
|
@@ -134,7 +134,7 @@ test('overflow sync=false', function (t) {
|
|
|
134
134
|
})
|
|
135
135
|
|
|
136
136
|
test('over the bufferSize at startup', function (t) {
|
|
137
|
-
t.plan(
|
|
137
|
+
t.plan(6)
|
|
138
138
|
|
|
139
139
|
const dest = file()
|
|
140
140
|
const stream = new ThreadStream({
|
|
@@ -144,20 +144,6 @@ test('over the bufferSize at startup', function (t) {
|
|
|
144
144
|
sync: true
|
|
145
145
|
})
|
|
146
146
|
|
|
147
|
-
stream.on('drain', () => {
|
|
148
|
-
t.pass('drain')
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
stream.on('ready', () => {
|
|
152
|
-
t.pass('ready emitted')
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
t.ok(stream.write('hello'))
|
|
156
|
-
t.notOk(stream.write(' world\n'))
|
|
157
|
-
t.notOk(stream.write('something else\n'))
|
|
158
|
-
|
|
159
|
-
stream.end()
|
|
160
|
-
|
|
161
147
|
stream.on('finish', () => {
|
|
162
148
|
readFile(dest, 'utf8', (err, data) => {
|
|
163
149
|
t.error(err)
|
|
@@ -168,10 +154,16 @@ test('over the bufferSize at startup', function (t) {
|
|
|
168
154
|
stream.on('close', () => {
|
|
169
155
|
t.pass('close emitted')
|
|
170
156
|
})
|
|
157
|
+
|
|
158
|
+
t.ok(stream.write('hello'))
|
|
159
|
+
t.ok(stream.write(' world\n'))
|
|
160
|
+
t.ok(stream.write('something else\n'))
|
|
161
|
+
|
|
162
|
+
stream.end()
|
|
171
163
|
})
|
|
172
164
|
|
|
173
165
|
test('over the bufferSize at startup (async)', function (t) {
|
|
174
|
-
t.plan(
|
|
166
|
+
t.plan(6)
|
|
175
167
|
|
|
176
168
|
const dest = file()
|
|
177
169
|
const stream = new ThreadStream({
|
|
@@ -181,14 +173,6 @@ test('over the bufferSize at startup (async)', function (t) {
|
|
|
181
173
|
sync: false
|
|
182
174
|
})
|
|
183
175
|
|
|
184
|
-
stream.on('drain', () => {
|
|
185
|
-
t.pass('drain')
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
stream.on('ready', () => {
|
|
189
|
-
t.pass('ready emitted')
|
|
190
|
-
})
|
|
191
|
-
|
|
192
176
|
t.ok(stream.write('hello'))
|
|
193
177
|
t.notOk(stream.write(' world\n'))
|
|
194
178
|
t.notOk(stream.write('something else\n'))
|
|
@@ -216,15 +200,6 @@ test('flushSync sync=false', function (t) {
|
|
|
216
200
|
sync: false
|
|
217
201
|
})
|
|
218
202
|
|
|
219
|
-
stream.on('ready', () => {
|
|
220
|
-
t.pass('ready emitted')
|
|
221
|
-
|
|
222
|
-
for (let count = 0; count < 20; count++) {
|
|
223
|
-
stream.write('aaaaaaaaaa')
|
|
224
|
-
}
|
|
225
|
-
stream.flushSync()
|
|
226
|
-
})
|
|
227
|
-
|
|
228
203
|
stream.on('drain', () => {
|
|
229
204
|
t.pass('drain')
|
|
230
205
|
stream.end()
|
|
@@ -241,50 +216,11 @@ test('flushSync sync=false', function (t) {
|
|
|
241
216
|
t.end()
|
|
242
217
|
})
|
|
243
218
|
})
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
test('flushSync on ready if sync=true', function (t) {
|
|
247
|
-
t.plan(4)
|
|
248
|
-
|
|
249
|
-
const dest = file()
|
|
250
|
-
const stream = new ThreadStream({
|
|
251
|
-
filename: join(__dirname, 'to-file.js'),
|
|
252
|
-
workerData: { dest },
|
|
253
|
-
sync: true
|
|
254
|
-
})
|
|
255
|
-
|
|
256
|
-
stream.on('ready', () => {
|
|
257
|
-
readFile(dest, 'utf8', (err, data) => {
|
|
258
|
-
t.error(err)
|
|
259
|
-
t.equal(data, 'hello world\nsomething else\n')
|
|
260
|
-
stream.end()
|
|
261
|
-
})
|
|
262
|
-
})
|
|
263
219
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
test('flush on ready if sync=false', function (t) {
|
|
269
|
-
t.plan(4)
|
|
270
|
-
|
|
271
|
-
const dest = file()
|
|
272
|
-
const stream = new ThreadStream({
|
|
273
|
-
filename: join(__dirname, 'to-file.js'),
|
|
274
|
-
workerData: { dest },
|
|
275
|
-
sync: false
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
stream.on('ready', () => {
|
|
279
|
-
readFile(dest, 'utf8', (err, data) => {
|
|
280
|
-
t.error(err)
|
|
281
|
-
t.equal(data, 'hello world\nsomething else\n')
|
|
282
|
-
stream.end()
|
|
283
|
-
})
|
|
284
|
-
})
|
|
285
|
-
|
|
286
|
-
t.ok(stream.write('hello world\n'))
|
|
287
|
-
t.ok(stream.write('something else\n'))
|
|
220
|
+
for (let count = 0; count < 20; count++) {
|
|
221
|
+
stream.write('aaaaaaaaaa')
|
|
222
|
+
}
|
|
223
|
+
stream.flushSync()
|
|
288
224
|
})
|
|
289
225
|
|
|
290
226
|
test('pass down MessagePorts', async function (t) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const { join } = require('path')
|
|
5
|
+
const { file } = require('./helper')
|
|
6
|
+
const ThreadStream = require('..')
|
|
7
|
+
|
|
8
|
+
test('bundlers support', function (t) {
|
|
9
|
+
t.plan(1)
|
|
10
|
+
|
|
11
|
+
globalThis.__bundlerPathsOverrides = {
|
|
12
|
+
'thread-stream-worker': join(__dirname, 'custom-worker.js')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const dest = file()
|
|
16
|
+
|
|
17
|
+
process.on('uncaughtException', error => {
|
|
18
|
+
console.log(error)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const stream = new ThreadStream({
|
|
22
|
+
filename: join(__dirname, 'to-file.js'),
|
|
23
|
+
workerData: { dest },
|
|
24
|
+
sync: true
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
stream.worker.removeAllListeners('message')
|
|
28
|
+
stream.worker.once('message', message => {
|
|
29
|
+
t.equal(message.code, 'CUSTOM-WORKER-CALLED')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
stream.end()
|
|
33
|
+
})
|
package/test/close-on-gc.js
CHANGED
|
@@ -15,16 +15,14 @@ function setup () {
|
|
|
15
15
|
|
|
16
16
|
worker = stream.worker
|
|
17
17
|
|
|
18
|
-
stream.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
setImmediate(gc) // eslint-disable-line
|
|
27
|
-
})
|
|
18
|
+
stream.write('hello')
|
|
19
|
+
stream.write(' ')
|
|
20
|
+
stream.write('world\n')
|
|
21
|
+
stream.flushSync()
|
|
22
|
+
stream.unref()
|
|
23
|
+
|
|
24
|
+
// the stream object goes out of scope here
|
|
25
|
+
setImmediate(gc) // eslint-disable-line
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
setup()
|
|
@@ -6,7 +6,7 @@ const ThreadStream = require('..')
|
|
|
6
6
|
const isYarnPnp = process.versions.pnp !== undefined
|
|
7
7
|
|
|
8
8
|
test('yarn module resolution', { skip: !isYarnPnp }, t => {
|
|
9
|
-
t.plan(
|
|
9
|
+
t.plan(6)
|
|
10
10
|
|
|
11
11
|
const modulePath = require.resolve('pino-elasticsearch')
|
|
12
12
|
t.match(modulePath, /.*\.zip.*/)
|
|
@@ -17,13 +17,13 @@ test('yarn module resolution', { skip: !isYarnPnp }, t => {
|
|
|
17
17
|
sync: true
|
|
18
18
|
})
|
|
19
19
|
|
|
20
|
+
t.same(stream.writableErrored, null)
|
|
20
21
|
stream.on('error', (err) => {
|
|
22
|
+
t.same(stream.writableErrored, err)
|
|
21
23
|
t.pass('error emitted')
|
|
22
|
-
t.equal(err.message, 'Missing node(s) option', 'module custom error')
|
|
23
24
|
})
|
|
24
25
|
|
|
25
26
|
t.ok(stream.write('hello world\n'))
|
|
26
27
|
t.ok(stream.writable)
|
|
27
|
-
|
|
28
28
|
stream.end()
|
|
29
29
|
})
|
package/test/create-and-exit.js
CHANGED
|
@@ -9,10 +9,8 @@ const stream = new ThreadStream({
|
|
|
9
9
|
sync: true
|
|
10
10
|
})
|
|
11
11
|
|
|
12
|
-
stream.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
stream.unref()
|
|
18
|
-
})
|
|
12
|
+
stream.write('hello')
|
|
13
|
+
stream.write(' ')
|
|
14
|
+
stream.write('world\n')
|
|
15
|
+
stream.flushSync()
|
|
16
|
+
stream.unref()
|
package/test/end.test.js
CHANGED
|
@@ -7,7 +7,7 @@ const { file } = require('./helper')
|
|
|
7
7
|
const ThreadStream = require('..')
|
|
8
8
|
|
|
9
9
|
test('destroy support', function (t) {
|
|
10
|
-
t.plan(
|
|
10
|
+
t.plan(7)
|
|
11
11
|
|
|
12
12
|
const dest = file()
|
|
13
13
|
const stream = new ThreadStream({
|
|
@@ -16,33 +16,25 @@ test('destroy support', function (t) {
|
|
|
16
16
|
sync: true
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
stream.on('
|
|
20
|
-
t.
|
|
19
|
+
stream.on('close', () => {
|
|
20
|
+
t.notOk(stream.writable)
|
|
21
|
+
t.pass('close emitted')
|
|
21
22
|
})
|
|
22
23
|
|
|
23
|
-
stream.
|
|
24
|
-
|
|
24
|
+
t.ok(stream.write('hello world\n'))
|
|
25
|
+
t.ok(stream.write('something else\n'))
|
|
26
|
+
t.ok(stream.writable)
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
t.ok(stream.write('something else\n'))
|
|
28
|
-
t.ok(stream.writable)
|
|
28
|
+
stream.end()
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
t.error(err)
|
|
34
|
-
t.equal(data, 'hello world\nsomething else\n')
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
stream.on('close', () => {
|
|
39
|
-
t.notOk(stream.writable)
|
|
40
|
-
t.pass('close emitted')
|
|
30
|
+
readFile(dest, 'utf8', (err, data) => {
|
|
31
|
+
t.error(err)
|
|
32
|
+
t.equal(data, 'hello world\nsomething else\n')
|
|
41
33
|
})
|
|
42
34
|
})
|
|
43
35
|
|
|
44
36
|
test('synchronous _final support', function (t) {
|
|
45
|
-
t.plan(
|
|
37
|
+
t.plan(7)
|
|
46
38
|
|
|
47
39
|
const dest = file()
|
|
48
40
|
const stream = new ThreadStream({
|
|
@@ -51,27 +43,19 @@ test('synchronous _final support', function (t) {
|
|
|
51
43
|
sync: true
|
|
52
44
|
})
|
|
53
45
|
|
|
54
|
-
stream.on('
|
|
55
|
-
t.
|
|
46
|
+
stream.on('close', () => {
|
|
47
|
+
t.notOk(stream.writable)
|
|
48
|
+
t.pass('close emitted')
|
|
56
49
|
})
|
|
57
50
|
|
|
58
|
-
stream.
|
|
59
|
-
|
|
51
|
+
t.ok(stream.write('hello world\n'))
|
|
52
|
+
t.ok(stream.write('something else\n'))
|
|
53
|
+
t.ok(stream.writable)
|
|
60
54
|
|
|
61
|
-
|
|
62
|
-
t.ok(stream.write('something else\n'))
|
|
63
|
-
t.ok(stream.writable)
|
|
55
|
+
stream.end()
|
|
64
56
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
t.error(err)
|
|
69
|
-
t.equal(data, 'hello world\nsomething else\n')
|
|
70
|
-
})
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
stream.on('close', () => {
|
|
74
|
-
t.notOk(stream.writable)
|
|
75
|
-
t.pass('close emitted')
|
|
57
|
+
readFile(dest, 'utf8', (err, data) => {
|
|
58
|
+
t.error(err)
|
|
59
|
+
t.equal(data, 'hello world\nsomething else\n')
|
|
76
60
|
})
|
|
77
61
|
})
|
package/test/esm.test.mjs
CHANGED
|
@@ -1,33 +1,13 @@
|
|
|
1
1
|
import { test } from 'tap'
|
|
2
|
-
import {
|
|
3
|
-
import { unlinkSync, readFile } from 'fs'
|
|
2
|
+
import { readFile } from 'fs'
|
|
4
3
|
import ThreadStream from '../index.js'
|
|
5
4
|
import { join } from 'desm'
|
|
6
|
-
import path from 'path'
|
|
7
5
|
import { pathToFileURL } from 'url'
|
|
8
|
-
|
|
9
|
-
const files = []
|
|
10
|
-
let count = 0
|
|
11
|
-
|
|
12
|
-
function file () {
|
|
13
|
-
const file = path.join(tmpdir(), `thread-stream-${process.pid}-${process.hrtime().toString()}-${count++}`)
|
|
14
|
-
files.push(file)
|
|
15
|
-
return file
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
process.on('beforeExit', () => {
|
|
19
|
-
for (const file of files) {
|
|
20
|
-
try {
|
|
21
|
-
unlinkSync(file)
|
|
22
|
-
} catch (e) {
|
|
23
|
-
console.log(e)
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
})
|
|
6
|
+
import { file } from './helper.js'
|
|
27
7
|
|
|
28
8
|
function basic (text, filename) {
|
|
29
9
|
test(text, function (t) {
|
|
30
|
-
t.plan(
|
|
10
|
+
t.plan(5)
|
|
31
11
|
|
|
32
12
|
const dest = file()
|
|
33
13
|
const stream = new ThreadStream({
|
|
@@ -36,19 +16,6 @@ function basic (text, filename) {
|
|
|
36
16
|
sync: true
|
|
37
17
|
})
|
|
38
18
|
|
|
39
|
-
stream.on('drain', () => {
|
|
40
|
-
t.pass('drain')
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
stream.on('ready', () => {
|
|
44
|
-
t.pass('ready emitted')
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
t.ok(stream.write('hello world\n'))
|
|
48
|
-
t.ok(stream.write('something else\n'))
|
|
49
|
-
|
|
50
|
-
stream.end()
|
|
51
|
-
|
|
52
19
|
stream.on('finish', () => {
|
|
53
20
|
readFile(dest, 'utf8', (err, data) => {
|
|
54
21
|
t.error(err)
|
|
@@ -59,6 +26,11 @@ function basic (text, filename) {
|
|
|
59
26
|
stream.on('close', () => {
|
|
60
27
|
t.pass('close emitted')
|
|
61
28
|
})
|
|
29
|
+
|
|
30
|
+
t.ok(stream.write('hello world\n'))
|
|
31
|
+
t.ok(stream.write('something else\n'))
|
|
32
|
+
|
|
33
|
+
stream.end()
|
|
62
34
|
})
|
|
63
35
|
}
|
|
64
36
|
|
package/test/helper.js
CHANGED
|
@@ -3,24 +3,31 @@
|
|
|
3
3
|
const { join } = require('path')
|
|
4
4
|
const { tmpdir } = require('os')
|
|
5
5
|
const { unlinkSync } = require('fs')
|
|
6
|
+
const why = require('why-is-node-running')
|
|
7
|
+
const t = require('tap')
|
|
6
8
|
|
|
7
9
|
const files = []
|
|
8
10
|
let count = 0
|
|
9
11
|
|
|
10
12
|
function file () {
|
|
11
|
-
const file = join(tmpdir(), `thread-stream-${process.pid}-${
|
|
13
|
+
const file = join(tmpdir(), `thread-stream-${process.pid}-${count++}`)
|
|
12
14
|
files.push(file)
|
|
13
15
|
return file
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
process.on('beforeExit', () => {
|
|
19
|
+
t.comment('unlink files')
|
|
17
20
|
for (const file of files) {
|
|
18
21
|
try {
|
|
22
|
+
t.comment(`unliking ${file}`)
|
|
19
23
|
unlinkSync(file)
|
|
20
24
|
} catch (e) {
|
|
21
25
|
console.log(e)
|
|
22
26
|
}
|
|
23
27
|
}
|
|
28
|
+
t.comment('unlink completed')
|
|
24
29
|
})
|
|
25
30
|
|
|
26
31
|
module.exports.file = file
|
|
32
|
+
|
|
33
|
+
setInterval(why, 10000).unref()
|
|
@@ -16,17 +16,15 @@ test('break up utf8 multibyte (sync)', (t) => {
|
|
|
16
16
|
sync: true
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
stream.on('ready', function () {
|
|
20
|
-
stream.write(longString)
|
|
21
|
-
stream.end()
|
|
22
|
-
})
|
|
23
|
-
|
|
24
19
|
stream.on('finish', () => {
|
|
25
20
|
readFile(dest, 'utf8', (err, data) => {
|
|
26
21
|
t.error(err)
|
|
27
22
|
t.equal(data, longString)
|
|
28
23
|
})
|
|
29
24
|
})
|
|
25
|
+
|
|
26
|
+
stream.write(longString)
|
|
27
|
+
stream.end()
|
|
30
28
|
})
|
|
31
29
|
|
|
32
30
|
test('break up utf8 multibyte (async)', (t) => {
|
|
@@ -41,15 +39,13 @@ test('break up utf8 multibyte (async)', (t) => {
|
|
|
41
39
|
sync: false
|
|
42
40
|
})
|
|
43
41
|
|
|
44
|
-
stream.on('ready', function () {
|
|
45
|
-
stream.write(longString)
|
|
46
|
-
stream.end()
|
|
47
|
-
})
|
|
48
|
-
|
|
49
42
|
stream.on('finish', () => {
|
|
50
43
|
readFile(dest, 'utf8', (err, data) => {
|
|
51
44
|
t.error(err)
|
|
52
45
|
t.equal(data, longString)
|
|
53
46
|
})
|
|
54
47
|
})
|
|
48
|
+
|
|
49
|
+
stream.write(longString)
|
|
50
|
+
stream.end()
|
|
55
51
|
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const { join } = require('path')
|
|
5
|
+
const { file } = require('./helper')
|
|
6
|
+
const { stat } = require('fs')
|
|
7
|
+
const ThreadStream = require('..')
|
|
8
|
+
|
|
9
|
+
t.setTimeout(30000)
|
|
10
|
+
|
|
11
|
+
const dest = file()
|
|
12
|
+
const stream = new ThreadStream({
|
|
13
|
+
filename: join(__dirname, 'to-file.js'),
|
|
14
|
+
workerData: { dest },
|
|
15
|
+
sync: false
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
let length = 0
|
|
19
|
+
|
|
20
|
+
stream.on('close', () => {
|
|
21
|
+
stat(dest, (err, f) => {
|
|
22
|
+
t.error(err)
|
|
23
|
+
t.equal(f.size, length)
|
|
24
|
+
t.end()
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
const buf = Buffer.alloc(1024).fill('x').toString() // 1 KB
|
|
29
|
+
|
|
30
|
+
// This writes 1 GB of data
|
|
31
|
+
for (let i = 0; i < 1024 * 1024; i++) {
|
|
32
|
+
length += buf.length
|
|
33
|
+
stream.write(buf)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
stream.end()
|
|
@@ -25,7 +25,7 @@ test('emit error if thread exits', async function (t) {
|
|
|
25
25
|
sync: true
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
-
stream.on('ready',
|
|
28
|
+
stream.on('ready', () => {
|
|
29
29
|
stream.write('hello world\n')
|
|
30
30
|
})
|
|
31
31
|
|
|
@@ -53,7 +53,7 @@ test('emit error if thread exits', async function (t) {
|
|
|
53
53
|
sync: true
|
|
54
54
|
})
|
|
55
55
|
|
|
56
|
-
stream.on('ready',
|
|
56
|
+
stream.on('ready', () => {
|
|
57
57
|
stream.write('hello world\n')
|
|
58
58
|
})
|
|
59
59
|
|
|
@@ -81,7 +81,7 @@ test('emit error if thread have unhandledRejection', async function (t) {
|
|
|
81
81
|
sync: true
|
|
82
82
|
})
|
|
83
83
|
|
|
84
|
-
stream.on('ready',
|
|
84
|
+
stream.on('ready', () => {
|
|
85
85
|
stream.write('hello world\n')
|
|
86
86
|
})
|
|
87
87
|
|
|
@@ -109,7 +109,7 @@ test('emit error if worker stream emit error', async function (t) {
|
|
|
109
109
|
sync: true
|
|
110
110
|
})
|
|
111
111
|
|
|
112
|
-
stream.on('ready',
|
|
112
|
+
stream.on('ready', () => {
|
|
113
113
|
stream.write('hello world\n')
|
|
114
114
|
})
|
|
115
115
|
|
|
@@ -137,7 +137,7 @@ test('emit error if thread have uncaughtException', async function (t) {
|
|
|
137
137
|
sync: true
|
|
138
138
|
})
|
|
139
139
|
|
|
140
|
-
stream.on('ready',
|
|
140
|
+
stream.on('ready', () => {
|
|
141
141
|
stream.write('hello world\n')
|
|
142
142
|
})
|
|
143
143
|
|