Skip to content

Commit

Permalink
Merge f84f5fc into 9f99b81
Browse files Browse the repository at this point in the history
  • Loading branch information
castarco committed Mar 21, 2022
2 parents 9f99b81 + f84f5fc commit 01bd72c
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
npm install --ignore-scripts
- name: Run tests
shell: bash
run: |
npm run test:ci
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/package-manager-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- name: Install dependancies
run: pnpm install
- name: Tests
shell: bash
run: pnpm run test:ci

yarn-pnp:
Expand Down Expand Up @@ -56,4 +57,5 @@ jobs:
# needed due the yarn.lock file in repository's .gitignore
YARN_ENABLE_IMMUTABLE_INSTALLS: false
- name: Tests
shell: bash
run: yarn run test:yarn
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ dist

# TernJS port file
.tern-port

# Generated files
test/ts/*js
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { EventEmitter } from 'events'

declare class ThreadStream extends EventEmitter {
constructor(opts: {})
write (data: string): boolean
end (): void
}

export = ThreadStream;
25 changes: 22 additions & 3 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { waitDiff } = require('./wait')

const {
dataBuf,
filename,
stateBuf
} = workerData

Expand All @@ -18,7 +19,25 @@ const data = Buffer.from(dataBuf)
async function start () {
let fn
try {
fn = (await realImport(workerData.filename)).default
// TODO: fix loading .ts files in Windows. See: ../test/ts.test.ts
if (filename.endsWith('.ts') || filename.endsWith('.cts')) {
// TODO: add support for the TSM modules loader ( https://github.com/lukeed/tsm ).
if (process[Symbol.for('ts-node.register.instance')]) {
realRequire('ts-node/register')
} else if (process.env.TS_NODE_DEV) {
realRequire('ts-node-dev')
}
// TODO: Support ES imports once tsc, tap & ts-node provide better compatibility guarantees.
fn = realRequire(decodeURIComponent(filename.replace('file://', '')))
} else {
fn = (await realImport(filename))
}

// Depending on how the default export is performed, and on how the code is
// transpiled, we may find cases of two nested "default" objects.
// See https://github.com/pinojs/pino/issues/1243#issuecomment-982774762
if (typeof fn === 'object') fn = fn.default
if (typeof fn === 'object') fn = fn.default
} catch (error) {
// A yarn user that tries to start a ThreadStream for an external module
// provides a filename pointing to a zip file.
Expand All @@ -29,8 +48,8 @@ async function start () {
// More details at https://github.com/pinojs/pino/pull/1113
// The error codes may change based on the node.js version (ENOTDIR > 12, ERR_MODULE_NOT_FOUND <= 12 )
if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND') &&
workerData.filename.startsWith('file://')) {
fn = realRequire(decodeURIComponent(workerData.filename.replace('file://', '')))
filename.startsWith('file://')) {
fn = realRequire(decodeURIComponent(filename.replace('file://', '')))
} else {
throw error
}
Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
"real-require": "^0.1.0"
},
"devDependencies": {
"@types/node": "^12.0.0",
"@types/tap": "^15.0.0",
"desm": "^1.1.0",
"fastbench": "^1.0.1",
"husky": "^7.0.0",
"sonic-boom": "^2.0.1",
"standard": "^16.0.3",
"tap": "^16.0.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.0",
"why-is-node-running": "^2.2.0"
},
"scripts": {
"test": "standard && tap test/*.test.*js",
"test:ci": "standard && tap \"test/**/*.test.*js\" --no-check-coverage --coverage-report=lcovonly",
"test:yarn": "tap \"test/**/*.test.js\" --no-check-coverage",
"test": "standard && npm run transpile && tap test/*.test.*js && tap --ts test/*.test.*ts",
"test:ci": "standard && npm run transpile && npm run test:ci:js && npm run test:ci:ts",
"test:ci:js": "tap --no-check-coverage --coverage-report=lcovonly \"test/**/*.test.*js\"",
"test:ci:ts": "tap --ts --no-check-coverage --coverage-report=lcovonly \"test/**/*.test.*ts\"",
"test:yarn": "npm run transpile && tap \"test/**/*.test.js\" --no-check-coverage",
"transpile": "sh ./test/ts/transpile.sh",
"prepare": "husky install"
},
"standard": { "ignore": ["test/ts/**/*"] },
"repository": {
"type": "git",
"url": "git+https://github.com/mcollina/thread-stream.git"
Expand Down
9 changes: 9 additions & 0 deletions test/esm.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ function basic (text, filename) {

basic('esm with path', join(import.meta.url, 'to-file.mjs'))
basic('esm with file URL', pathToFileURL(join(import.meta.url, 'to-file.mjs')).href)

basic('(ts -> es6) esm with path', join(import.meta.url, 'ts', 'to-file.es6.mjs'))
basic('(ts -> es6) esm with file URL', pathToFileURL(join(import.meta.url, 'ts', 'to-file.es6.mjs')).href)

basic('(ts -> es2017) esm with path', join(import.meta.url, 'ts', 'to-file.es2017.mjs'))
basic('(ts -> es2017) esm with file URL', pathToFileURL(join(import.meta.url, 'ts', 'to-file.es2017.mjs')).href)

basic('(ts -> esnext) esm with path', join(import.meta.url, 'ts', 'to-file.esnext.mjs'))
basic('(ts -> esnext) esm with file URL', pathToFileURL(join(import.meta.url, 'ts', 'to-file.esnext.mjs')).href)
1 change: 1 addition & 0 deletions test/helper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function file(): string
30 changes: 30 additions & 0 deletions test/transpiled.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { test } = require('tap')
const { join } = require('path')
const { file } = require('./helper')
const ThreadStream = require('..')

function basic (esVersion) {
test(`transpiled-ts-to-${esVersion}`, function (t) {
t.plan(2)

const dest = file()
const stream = new ThreadStream({
filename: join(__dirname, 'ts', `to-file.${esVersion}.cjs`),
workerData: { dest },
sync: true
})

// There are arbitrary checks, the important aspect of this test is to ensure
// that we can properly load the transpiled file into our worker thread.
t.same(stream.writableEnded, false)
stream.end()
t.same(stream.writableEnded, true)
})
}

basic('es5')
basic('es6')
basic('es2017')
basic('esnext')
39 changes: 39 additions & 0 deletions test/ts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { test } from 'tap'
import { readFile } from 'fs'
import ThreadStream from '../index.js'
import { join } from 'path'
import { file } from './helper.js'


test('typescript module', function (t) {
if (process.platform === 'win32') {
// TODO: Implement .ts files loading support for Windows
t.plan(0)
return
}

t.plan(5)

const dest = file()
const stream = new ThreadStream({
filename: join(__dirname, 'ts', 'to-file.ts'),
workerData: { dest },
sync: true
})

stream.on('finish', () => {
readFile(dest, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'hello world\nsomething else\n')
})
})

stream.on('close', () => {
t.pass('close emitted')
})

t.ok(stream.write('hello world\n'))
t.ok(stream.write('something else\n'))

stream.end()
})
10 changes: 10 additions & 0 deletions test/ts/to-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type PathLike, type WriteStream, createWriteStream } from 'fs'
import { once } from 'events'

export default async function run (
opts: { dest: PathLike },
): Promise<WriteStream> {
const stream = createWriteStream(opts.dest)
await once(stream, 'open')
return stream
}
19 changes: 19 additions & 0 deletions test/ts/transpile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -e

cd ./test/ts;

if (echo "${npm_config_user_agent}" | grep "yarn"); then
export RUNNER="yarn";
else
export RUNNER="npx";
fi

test ./to-file.ts -ot ./to-file.es5.cjs || ("${RUNNER}" tsc --target es5 ./to-file.ts && mv ./to-file.js ./to-file.es5.cjs);
test ./to-file.ts -ot ./to-file.es6.mjs || ("${RUNNER}" tsc --target es6 ./to-file.ts && mv ./to-file.js ./to-file.es6.mjs);
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);
test ./to-file.ts -ot ./to-file.es2017.mjs || ("${RUNNER}" tsc --target es2017 ./to-file.ts && mv ./to-file.js ./to-file.es2017.mjs);
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);
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);
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 comments on commit 01bd72c

Please sign in to comment.