thread-stream 0.14.0 → 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.
@@ -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
 
@@ -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/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/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.14.0",
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
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/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
+ })