thread-stream 0.13.1 → 0.15.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.
@@ -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
 
@@ -17,18 +17,19 @@ 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
26
- uses: pnpm/action-setup@v2.0.1
26
+ uses: pnpm/action-setup@v2.2.1
27
27
  with:
28
28
  version: ^6.0.0
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/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,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].buf.length) {
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
- 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
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.1",
3
+ "version": "0.15.0",
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,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
+ })
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
@@ -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
+ })
@@ -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
+ })