thread-stream 0.14.0 → 0.15.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.
@@ -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 }}
@@ -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:
@@ -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/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
@@ -251,7 +251,7 @@ class ThreadStream extends EventEmitter {
251
251
 
252
252
  end () {
253
253
  if (this[kImpl].destroyed) {
254
- throw new Error('the worker has exited')
254
+ return
255
255
  }
256
256
 
257
257
  this[kImpl].ending = true
@@ -286,7 +286,7 @@ class ThreadStream extends EventEmitter {
286
286
 
287
287
  flushSync () {
288
288
  if (this[kImpl].destroyed) {
289
- throw new Error('the worker has exited')
289
+ return
290
290
  }
291
291
 
292
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
+ if (filename.endsWith('.ts') || filename.endsWith('.cts')) {
23
+ // TODO: add support for the TSM modules loader ( https://github.com/lukeed/tsm ).
24
+ if (!process[Symbol.for('ts-node.register.instance')]) {
25
+ realRequire('ts-node/register')
26
+ } else if (process.env.TS_NODE_DEV) {
27
+ realRequire('ts-node-dev')
28
+ }
29
+ // TODO: Support ES imports once tsc, tap & ts-node provide better compatibility guarantees.
30
+ // Remove extra forwardslash on Windows
31
+ fn = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : '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.14.0",
3
+ "version": "0.15.2",
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
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
@@ -248,7 +248,7 @@ test('pass down MessagePorts', async function (t) {
248
248
  })
249
249
 
250
250
  test('destroy does not error', function (t) {
251
- t.plan(3)
251
+ t.plan(5)
252
252
 
253
253
  const dest = file()
254
254
  const stream = new ThreadStream({
@@ -267,5 +267,7 @@ test('destroy does not error', function (t) {
267
267
  stream.flush((err) => {
268
268
  t.equal(err.message, 'the worker has exited')
269
269
  })
270
+ t.doesNotThrow(() => stream.flushSync())
271
+ t.doesNotThrow(() => stream.end())
270
272
  })
271
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,33 @@
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
+ t.plan(5)
10
+
11
+ const dest = file()
12
+ const stream = new ThreadStream({
13
+ filename: join(__dirname, 'ts', 'to-file.ts'),
14
+ workerData: { dest },
15
+ sync: true
16
+ })
17
+
18
+ stream.on('finish', () => {
19
+ readFile(dest, 'utf8', (err, data) => {
20
+ t.error(err)
21
+ t.equal(data, 'hello world\nsomething else\n')
22
+ })
23
+ })
24
+
25
+ stream.on('close', () => {
26
+ t.pass('close emitted')
27
+ })
28
+
29
+ t.ok(stream.write('hello world\n'))
30
+ t.ok(stream.write('something else\n'))
31
+
32
+ stream.end()
33
+ })