thread-stream 0.13.2 → 0.15.1

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.
@@ -1,16 +1,13 @@
1
1
  version: 2
2
2
  updates:
3
- - package-ecosystem: github-actions
4
- directory: '/'
5
- ignore:
6
- - dependency-name: 'actions/*'
7
- update-types:
8
- ['version-update:semver-minor', 'version-update:semver-patch']
9
- schedule:
10
- interval: daily
11
- open-pull-requests-limit: 10
12
- - package-ecosystem: npm
13
- directory: '/'
14
- schedule:
15
- interval: daily
16
- open-pull-requests-limit: 10
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
@@ -19,10 +19,10 @@ jobs:
19
19
 
20
20
  steps:
21
21
 
22
- - uses: actions/checkout@v2
22
+ - uses: actions/checkout@v3
23
23
 
24
24
  - name: Use Node.js
25
- uses: actions/setup-node@v2
25
+ uses: actions/setup-node@v3
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
 
@@ -31,6 +31,7 @@ jobs:
31
31
  npm install --ignore-scripts
32
32
 
33
33
  - name: Run tests
34
+ shell: bash
34
35
  run: |
35
36
  npm run test:ci
36
37
 
@@ -55,6 +56,6 @@ jobs:
55
56
  needs: test
56
57
  runs-on: ubuntu-latest
57
58
  steps:
58
- - uses: fastify/github-action-merge-dependabot@v2.7.1
59
+ - uses: fastify/github-action-merge-dependabot@v3.0.2
59
60
  with:
60
61
  github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -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
20
+ - uses: actions/checkout@v3
21
21
  - name: Use Node.js ${{ matrix.node-version }}
22
- uses: actions/setup-node@v2
22
+ uses: actions/setup-node@v3
23
23
  with:
24
24
  node-version: ${{ matrix.node-version }}
25
25
  - name: Use pnpm
@@ -29,6 +29,7 @@ jobs:
29
29
  - name: Install dependancies
30
30
  run: pnpm install
31
31
  - name: Tests
32
+ shell: bash
32
33
  run: pnpm run test:ci
33
34
 
34
35
  yarn-pnp:
@@ -39,9 +40,9 @@ jobs:
39
40
  os: [macOS-latest]
40
41
  node-version: [12, 14, 16]
41
42
  steps:
42
- - uses: actions/checkout@v2
43
+ - uses: actions/checkout@v3
43
44
  - name: Use Node.js ${{ matrix.node-version }}
44
- uses: actions/setup-node@v2
45
+ uses: actions/setup-node@v3
45
46
  with:
46
47
  node-version: ${{ matrix.node-version }}
47
48
  - name: Use yarn
@@ -56,4 +57,5 @@ jobs:
56
57
  # needed due the yarn.lock file in repository's .gitignore
57
58
  YARN_ENABLE_IMMUTABLE_INSTALLS: false
58
59
  - name: Tests
60
+ shell: bash
59
61
  run: yarn run test:yarn
package/.taprc CHANGED
@@ -1,2 +1,3 @@
1
1
  jobs: 1
2
2
  check-coverage: false
3
+ timeout: 60000
package/README.md CHANGED
@@ -2,7 +2,6 @@
2
2
  [![npm version](https://img.shields.io/npm/v/thread-stream)](https://www.npmjs.com/package/thread-stream)
3
3
  [![Build Status](https://img.shields.io/github/workflow/status/pinojs/thread-stream/CI)](https://github.com/pinojs/thread-stream/actions)
4
4
  [![Known Vulnerabilities](https://snyk.io/test/github/pinojs/thread-stream/badge.svg)](https://snyk.io/test/github/pinojs/thread-stream)
5
- [![Coverage Status](https://coveralls.io/repos/github/pinojs/thread-stream/badge.svg?branch=master)](https://coveralls.io/github/pinojs/thread-stream?branch=master)
6
5
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
7
6
 
8
7
  A streaming way to send data to a Node.js Worker Thread.
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { EventEmitter } from 'events'
2
+
3
+ declare class ThreadStream extends EventEmitter {
4
+ constructor(opts: {})
5
+ write (data: string): boolean
6
+ end (): void
7
+ }
8
+
9
+ export = ThreadStream;
package/index.js CHANGED
@@ -104,6 +104,11 @@ 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
 
@@ -246,7 +251,7 @@ class ThreadStream extends EventEmitter {
246
251
 
247
252
  end () {
248
253
  if (this[kImpl].destroyed) {
249
- throw new Error('the worker has exited')
254
+ return
250
255
  }
251
256
 
252
257
  this[kImpl].ending = true
@@ -255,7 +260,10 @@ class ThreadStream extends EventEmitter {
255
260
 
256
261
  flush (cb) {
257
262
  if (this[kImpl].destroyed) {
258
- throw new Error('the worker has exited')
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
@@ -278,7 +286,7 @@ class ThreadStream extends EventEmitter {
278
286
 
279
287
  flushSync () {
280
288
  if (this[kImpl].destroyed) {
281
- throw new Error('the worker has exited')
289
+ return
282
290
  }
283
291
 
284
292
  writeSync(this)
package/lib/worker.js CHANGED
@@ -7,6 +7,7 @@ const { waitDiff } = require('./wait')
7
7
 
8
8
  const {
9
9
  dataBuf,
10
+ filename,
10
11
  stateBuf
11
12
  } = workerData
12
13
 
@@ -18,7 +19,25 @@ const data = Buffer.from(dataBuf)
18
19
  async function start () {
19
20
  let fn
20
21
  try {
21
- fn = (await realImport(workerData.filename)).default
22
+ // TODO: fix loading .ts files in Windows. See: ../test/ts.test.ts
23
+ if (filename.endsWith('.ts') || filename.endsWith('.cts')) {
24
+ // TODO: add support for the TSM modules loader ( https://github.com/lukeed/tsm ).
25
+ if (process[Symbol.for('ts-node.register.instance')]) {
26
+ realRequire('ts-node/register')
27
+ } else if (process.env.TS_NODE_DEV) {
28
+ realRequire('ts-node-dev')
29
+ }
30
+ // TODO: Support ES imports once tsc, tap & ts-node provide better compatibility guarantees.
31
+ fn = realRequire(decodeURIComponent(filename.replace('file://', '')))
32
+ } else {
33
+ fn = (await realImport(filename))
34
+ }
35
+
36
+ // Depending on how the default export is performed, and on how the code is
37
+ // transpiled, we may find cases of two nested "default" objects.
38
+ // See https://github.com/pinojs/pino/issues/1243#issuecomment-982774762
39
+ if (typeof fn === 'object') fn = fn.default
40
+ if (typeof fn === 'object') fn = fn.default
22
41
  } catch (error) {
23
42
  // A yarn user that tries to start a ThreadStream for an external module
24
43
  // provides a filename pointing to a zip file.
@@ -29,8 +48,8 @@ async function start () {
29
48
  // More details at https://github.com/pinojs/pino/pull/1113
30
49
  // The error codes may change based on the node.js version (ENOTDIR > 12, ERR_MODULE_NOT_FOUND <= 12 )
31
50
  if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND') &&
32
- workerData.filename.startsWith('file://')) {
33
- fn = realRequire(decodeURIComponent(workerData.filename.replace('file://', '')))
51
+ filename.startsWith('file://')) {
52
+ fn = realRequire(decodeURIComponent(filename.replace('file://', '')))
34
53
  } else {
35
54
  throw error
36
55
  }
package/package.json CHANGED
@@ -1,26 +1,34 @@
1
1
  {
2
2
  "name": "thread-stream",
3
- "version": "0.13.2",
3
+ "version": "0.15.1",
4
4
  "description": "A streaming way to send data to a Node.js Worker Thread",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "real-require": "^0.1.0"
8
8
  },
9
9
  "devDependencies": {
10
+ "@types/node": "^12.0.0",
11
+ "@types/tap": "^15.0.0",
10
12
  "desm": "^1.1.0",
11
13
  "fastbench": "^1.0.1",
12
14
  "husky": "^7.0.0",
13
15
  "sonic-boom": "^2.0.1",
14
16
  "standard": "^16.0.3",
15
- "tap": "^15.0.0",
17
+ "tap": "^16.0.0",
18
+ "ts-node": "^10.7.0",
19
+ "typescript": "^4.6.0",
16
20
  "why-is-node-running": "^2.2.0"
17
21
  },
18
22
  "scripts": {
19
- "test": "standard && tap test/*.test.*js",
20
- "test:ci": "standard && tap \"test/**/*.test.*js\" --no-check-coverage --coverage-report=lcovonly",
21
- "test:yarn": "tap \"test/**/*.test.js\" --no-check-coverage",
23
+ "test": "standard && npm run transpile && tap test/*.test.*js && tap --ts test/*.test.*ts",
24
+ "test:ci": "standard && npm run transpile && npm run test:ci:js && npm run test:ci:ts",
25
+ "test:ci:js": "tap --no-check-coverage --coverage-report=lcovonly \"test/**/*.test.*js\"",
26
+ "test:ci:ts": "tap --ts --no-check-coverage --coverage-report=lcovonly \"test/**/*.test.*ts\"",
27
+ "test:yarn": "npm run transpile && tap \"test/**/*.test.js\" --no-check-coverage",
28
+ "transpile": "sh ./test/ts/transpile.sh",
22
29
  "prepare": "husky install"
23
30
  },
31
+ "standard": { "ignore": ["test/ts/**/*"] },
24
32
  "repository": {
25
33
  "type": "git",
26
34
  "url": "git+https://github.com/mcollina/thread-stream.git"
package/test/base.test.js CHANGED
@@ -246,3 +246,28 @@ 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(5)
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
+ t.doesNotThrow(() => stream.flushSync())
271
+ t.doesNotThrow(() => stream.end())
272
+ })
273
+ })
package/test/esm.test.mjs CHANGED
@@ -36,3 +36,12 @@ function basic (text, filename) {
36
36
 
37
37
  basic('esm with path', join(import.meta.url, 'to-file.mjs'))
38
38
  basic('esm with file URL', pathToFileURL(join(import.meta.url, 'to-file.mjs')).href)
39
+
40
+ basic('(ts -> es6) esm with path', join(import.meta.url, 'ts', 'to-file.es6.mjs'))
41
+ basic('(ts -> es6) esm with file URL', pathToFileURL(join(import.meta.url, 'ts', 'to-file.es6.mjs')).href)
42
+
43
+ basic('(ts -> es2017) esm with path', join(import.meta.url, 'ts', 'to-file.es2017.mjs'))
44
+ basic('(ts -> es2017) esm with file URL', pathToFileURL(join(import.meta.url, 'ts', 'to-file.es2017.mjs')).href)
45
+
46
+ basic('(ts -> esnext) esm with path', join(import.meta.url, 'ts', 'to-file.esnext.mjs'))
47
+ basic('(ts -> esnext) esm with file URL', pathToFileURL(join(import.meta.url, 'ts', 'to-file.esnext.mjs')).href)
@@ -0,0 +1 @@
1
+ export declare function file(): string
@@ -0,0 +1,30 @@
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
+ function basic (esVersion) {
9
+ test(`transpiled-ts-to-${esVersion}`, function (t) {
10
+ t.plan(2)
11
+
12
+ const dest = file()
13
+ const stream = new ThreadStream({
14
+ filename: join(__dirname, 'ts', `to-file.${esVersion}.cjs`),
15
+ workerData: { dest },
16
+ sync: true
17
+ })
18
+
19
+ // There are arbitrary checks, the important aspect of this test is to ensure
20
+ // that we can properly load the transpiled file into our worker thread.
21
+ t.same(stream.writableEnded, false)
22
+ stream.end()
23
+ t.same(stream.writableEnded, true)
24
+ })
25
+ }
26
+
27
+ basic('es5')
28
+ basic('es6')
29
+ basic('es2017')
30
+ basic('esnext')
@@ -0,0 +1,10 @@
1
+ import { type PathLike, type WriteStream, createWriteStream } from 'fs'
2
+ import { once } from 'events'
3
+
4
+ export default async function run (
5
+ opts: { dest: PathLike },
6
+ ): Promise<WriteStream> {
7
+ const stream = createWriteStream(opts.dest)
8
+ await once(stream, 'open')
9
+ return stream
10
+ }
@@ -0,0 +1,19 @@
1
+ #!/bin/sh
2
+
3
+ set -e
4
+
5
+ cd ./test/ts;
6
+
7
+ if (echo "${npm_config_user_agent}" | grep "yarn"); then
8
+ export RUNNER="yarn";
9
+ else
10
+ export RUNNER="npx";
11
+ fi
12
+
13
+ test ./to-file.ts -ot ./to-file.es5.cjs || ("${RUNNER}" tsc --target es5 ./to-file.ts && mv ./to-file.js ./to-file.es5.cjs);
14
+ test ./to-file.ts -ot ./to-file.es6.mjs || ("${RUNNER}" tsc --target es6 ./to-file.ts && mv ./to-file.js ./to-file.es6.mjs);
15
+ test ./to-file.ts -ot ./to-file.es6.cjs || ("${RUNNER}" tsc --target es6 --module commonjs ./to-file.ts && mv ./to-file.js ./to-file.es6.cjs);
16
+ test ./to-file.ts -ot ./to-file.es2017.mjs || ("${RUNNER}" tsc --target es2017 ./to-file.ts && mv ./to-file.js ./to-file.es2017.mjs);
17
+ test ./to-file.ts -ot ./to-file.es2017.cjs || ("${RUNNER}" tsc --target es2017 --module commonjs ./to-file.ts && mv ./to-file.js ./to-file.es2017.cjs);
18
+ test ./to-file.ts -ot ./to-file.esnext.mjs || ("${RUNNER}" tsc --target esnext --module esnext ./to-file.ts && mv ./to-file.js ./to-file.esnext.mjs);
19
+ test ./to-file.ts -ot ./to-file.esnext.cjs || ("${RUNNER}" tsc --target esnext --module commonjs ./to-file.ts && mv ./to-file.js ./to-file.esnext.cjs);
@@ -0,0 +1,39 @@
1
+ import { test } from 'tap'
2
+ import { readFile } from 'fs'
3
+ import ThreadStream from '../index.js'
4
+ import { join } from 'path'
5
+ import { file } from './helper.js'
6
+
7
+
8
+ test('typescript module', function (t) {
9
+ if (process.platform === 'win32') {
10
+ // TODO: Implement .ts files loading support for Windows
11
+ t.plan(0)
12
+ return
13
+ }
14
+
15
+ t.plan(5)
16
+
17
+ const dest = file()
18
+ const stream = new ThreadStream({
19
+ filename: join(__dirname, 'ts', 'to-file.ts'),
20
+ workerData: { dest },
21
+ sync: true
22
+ })
23
+
24
+ stream.on('finish', () => {
25
+ readFile(dest, 'utf8', (err, data) => {
26
+ t.error(err)
27
+ t.equal(data, 'hello world\nsomething else\n')
28
+ })
29
+ })
30
+
31
+ stream.on('close', () => {
32
+ t.pass('close emitted')
33
+ })
34
+
35
+ t.ok(stream.write('hello world\n'))
36
+ t.ok(stream.write('something else\n'))
37
+
38
+ stream.end()
39
+ })