thread-stream 0.11.1 → 0.11.2

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.
@@ -19,10 +19,10 @@ jobs:
19
19
 
20
20
  steps:
21
21
 
22
- - uses: actions/checkout@v2.3.4
22
+ - uses: actions/checkout@v2.3.5
23
23
 
24
24
  - name: Use Node.js
25
- uses: actions/setup-node@v2.4.0
25
+ uses: actions/setup-node@v2.4.1
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
 
@@ -17,9 +17,9 @@ jobs:
17
17
  os: [macOS-latest, windows-latest]
18
18
  node-version: [12, 14, 16]
19
19
  steps:
20
- - uses: actions/checkout@v2.3.4
20
+ - uses: actions/checkout@v2.3.5
21
21
  - name: Use Node.js ${{ matrix.node-version }}
22
- uses: actions/setup-node@v2.4.0
22
+ uses: actions/setup-node@v2.4.1
23
23
  with:
24
24
  node-version: ${{ matrix.node-version }}
25
25
  - name: Use pnpm
@@ -39,9 +39,9 @@ jobs:
39
39
  os: [macOS-latest]
40
40
  node-version: [12, 14, 16]
41
41
  steps:
42
- - uses: actions/checkout@v2.3.4
42
+ - uses: actions/checkout@v2.3.5
43
43
  - name: Use Node.js ${{ matrix.node-version }}
44
- uses: actions/setup-node@v2.4.0
44
+ uses: actions/setup-node@v2.4.1
45
45
  with:
46
46
  node-version: ${{ matrix.node-version }}
47
47
  - name: Use yarn
package/.taprc ADDED
@@ -0,0 +1,2 @@
1
+ jobs: 1
2
+ check-coverage: false
package/index.js CHANGED
@@ -9,6 +9,10 @@ const {
9
9
  WRITE_INDEX,
10
10
  READ_INDEX
11
11
  } = require('./lib/indexes')
12
+ const buffer = require('buffer')
13
+
14
+ // V8 limit for string size
15
+ const MAX_STRING = buffer.constants.MAX_STRING_LENGTH
12
16
 
13
17
  class FakeWeakRef {
14
18
  constructor (value) {
@@ -28,6 +32,9 @@ const FinalizationRegistry = global.FinalizationRegistry || class FakeFinalizati
28
32
  const WeakRef = global.WeakRef || FakeWeakRef
29
33
 
30
34
  const registry = new FinalizationRegistry((worker) => {
35
+ if (worker.exited) {
36
+ return
37
+ }
31
38
  worker.terminate()
32
39
  })
33
40
 
@@ -122,6 +129,7 @@ function nextFlush (stream) {
122
129
  function onWorkerMessage (msg) {
123
130
  const stream = this.stream.deref()
124
131
  if (stream === undefined) {
132
+ this.exited = true
125
133
  // Terminate the worker.
126
134
  this.terminate()
127
135
  return
@@ -148,6 +156,7 @@ function onWorkerMessage (msg) {
148
156
  break
149
157
  case 'ERROR':
150
158
  stream.closed = true
159
+ stream.worker.exited = true
151
160
  // TODO only remove our own
152
161
  stream.worker.removeAllListeners('exit')
153
162
  stream.worker.terminate().then(null, () => {})
@@ -168,6 +177,7 @@ function onWorkerExit (code) {
168
177
  }
169
178
  registry.unregister(stream)
170
179
  stream.closed = true
180
+ stream.worker.exited = true
171
181
  setImmediate(function () {
172
182
  if (code !== 0) {
173
183
  stream.emit('error', new Error('The worker thread exited'))
@@ -219,6 +229,12 @@ class ThreadStream extends EventEmitter {
219
229
  throw new Error('the worker has exited')
220
230
  }
221
231
 
232
+ if (this.flushing && this.buf.length + data.length >= MAX_STRING) {
233
+ // process._rawDebug('write: flushing')
234
+ this._writeSync()
235
+ this.flushing = true // we are still flushing
236
+ }
237
+
222
238
  if (!this.ready || this.flushing) {
223
239
  this.buf += data
224
240
  return this._hasSpace()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thread-stream",
3
- "version": "0.11.1",
3
+ "version": "0.11.2",
4
4
  "description": "A streaming way to send data to a Node.js Worker Thread",
5
5
  "main": "index.js",
6
6
  "devDependencies": {
@@ -9,10 +9,11 @@
9
9
  "husky": "^7.0.0",
10
10
  "sonic-boom": "^2.0.1",
11
11
  "standard": "^16.0.3",
12
- "tap": "^15.0.0"
12
+ "tap": "^15.0.0",
13
+ "why-is-node-running": "^2.2.0"
13
14
  },
14
15
  "scripts": {
15
- "test": "standard && tap --no-check-coverage test/*.test.*js",
16
+ "test": "standard && tap test/*.test.*js",
16
17
  "test:ci": "standard && tap \"test/**/*.test.*js\" --no-check-coverage --coverage-report=lcovonly",
17
18
  "test:yarn": "tap \"test/**/*.test.js\" --no-check-coverage",
18
19
  "prepare": "husky install"
package/test/esm.test.mjs CHANGED
@@ -1,29 +1,9 @@
1
1
  import { test } from 'tap'
2
- import { tmpdir } from 'os'
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) {
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}-${process.hrtime().toString()}-${count++}`)
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()
@@ -0,0 +1,40 @@
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('ready', () => {
21
+ t.pass('ready emitted')
22
+
23
+ const buf = Buffer.alloc(1024).fill('x').toString() // 1 KB
24
+
25
+ // This writes 1 GB of data
26
+ for (let i = 0; i < 1024 * 1024; i++) {
27
+ length += buf.length
28
+ stream.write(buf)
29
+ }
30
+
31
+ stream.end()
32
+ })
33
+
34
+ stream.on('close', () => {
35
+ stat(dest, (err, f) => {
36
+ t.error(err)
37
+ t.equal(f.size, length)
38
+ t.end()
39
+ })
40
+ })