Skip to content

Commit

Permalink
fix: only fallback to ts-node when it's available (#141)
Browse files Browse the repository at this point in the history
close #128
  • Loading branch information
JounQin committed Dec 1, 2023
1 parent b7be102 commit 608c9d9
Show file tree
Hide file tree
Showing 12 changed files with 5,496 additions and 5,385 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-parents-learn.md
@@ -0,0 +1,5 @@
---
"synckit": patch
---

fix: only fallback to ts-node when it's available
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
@@ -1,4 +1,4 @@
{
"node": "16",
"node": "18",
"sandboxes": []
}
11 changes: 4 additions & 7 deletions .github/workflows/ci.yml
Expand Up @@ -10,29 +10,26 @@ jobs:
strategy:
matrix:
node:
- 14
- 16
- 18
- 20
os:
- macos-latest
- ubuntu-latest
- windows-latest
exclude:
- node: 18
os: windows-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn

- name: Install Dependencies
run: yarn --frozen-lockfile
run: yarn --frozen-lockfile --ignore-engines

- name: Build, Lint and Test
run: yarn run-s build lint test
Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/codeql.yml
Expand Up @@ -2,9 +2,11 @@ name: 'CodeQL'

on:
push:
branches: ['main']
branches:
- main
pull_request:
branches: ['main']
branches:
- main
schedule:
- cron: '3 2 * * 6'

Expand All @@ -20,11 +22,12 @@ jobs:
strategy:
fail-fast: false
matrix:
language: [javascript]
language:
- javascript

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pkg-size.yml
Expand Up @@ -12,7 +12,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Package Size Report
uses: pkg-size/action@v1
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Expand Up @@ -11,15 +11,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0

- name: Setup Node.js 16
uses: actions/setup-node@v3
- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 16
node-version: --lts
cache: yarn

- name: Install Dependencies
Expand Down
57 changes: 30 additions & 27 deletions package.json
Expand Up @@ -7,7 +7,7 @@
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
"funding": "https://opencollective.com/unts",
"license": "MIT",
"packageManager": "yarn@1.22.19",
"packageManager": "yarn@1.22.21",
"engines": {
"node": "^14.18.0 || >=16.0.0"
},
Expand Down Expand Up @@ -52,34 +52,34 @@
"typecov": "type-coverage"
},
"dependencies": {
"@pkgr/utils": "^2.3.1",
"tslib": "^2.5.0"
"@pkgr/utils": "^2.4.2",
"tslib": "^2.6.2"
},
"devDependencies": {
"@1stg/lib-config": "^10.2.1",
"@changesets/changelog-github": "^0.4.6",
"@changesets/cli": "^2.26.0",
"@swc-node/register": "^1.5.5",
"@types/jest": "^28.1.8",
"@types/node": "^18.7.13",
"@1stg/lib-config": "^12.0.0",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@swc-node/register": "^1.6.8",
"@types/jest": "^29.5.10",
"@types/node": "^20.10.1",
"clean-pkg-json": "^1.2.0",
"deasync": "^0.1.28",
"esbuild-register": "^3.3.3",
"esbuild-runner": "^2.2.1",
"execa": "^6.1.0",
"jest": "^29.0.1",
"patch-package": "^6.4.7",
"deasync": "^0.1.29",
"esbuild-register": "^3.5.0",
"esbuild-runner": "^2.2.2",
"execa": "^8.0.1",
"jest": "^29.7.0",
"patch-package": "^8.0.0",
"sync-threads": "^1.0.1",
"ts-expect": "^1.3.0",
"ts-jest": "^28.0.8",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsx": "^3.12.2",
"type-coverage": "^2.22.0",
"typescript": "^4.9.4",
"yarn-deduplicate": "^6.0.0"
"tsx": "^4.6.1",
"type-coverage": "^2.27.0",
"typescript": "^5.3.2",
"yarn-deduplicate": "^6.0.2"
},
"resolutions": {
"prettier": "^2.7.1",
"prettier": "^2.8.8",
"synckit": "link:."
},
"commitlint": {
Expand All @@ -96,13 +96,16 @@
"^(\\.{1,2}/.*)\\.js$": "$1",
"^synckit$": "<rootDir>/src"
},
"globals": {
"ts-jest": {
"useESM": true,
"tsconfig": {
"importHelpers": false
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"useESM": true,
"tsconfig": {
"importHelpers": false
}
}
}
]
}
},
"prettier": "@1stg/prettier-config",
Expand Down
95 changes: 42 additions & 53 deletions src/index.ts
Expand Up @@ -11,7 +11,7 @@ import {
parentPort,
} from 'node:worker_threads'

import { findUp, tryExtensions } from '@pkgr/utils'
import { findUp, isPkgAvailable, tryExtensions } from '@pkgr/utils'

import {
AnyAsyncFn,
Expand Down Expand Up @@ -59,8 +59,7 @@ export const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024
/* istanbul ignore next */
export const DEFAULT_EXEC_ARGV = SYNCKIT_EXEC_ARGV?.split(',') || []

export const DEFAULT_TS_RUNNER = (SYNCKIT_TS_RUNNER ||
TsRunner.TsNode) as TsRunner
export const DEFAULT_TS_RUNNER = SYNCKIT_TS_RUNNER as TsRunner | undefined

export const MTS_SUPPORTED_NODE_VERSION = 16

Expand Down Expand Up @@ -134,15 +133,16 @@ const dataUrl = (code: string) =>

export const isFile = (path: string) => {
try {
return fs.statSync(path).isFile()
return !!fs.statSync(path, { throwIfNoEntry: false })?.isFile()
} catch {
/* istanbul ignore next */
return false
}
}

const setupTsRunner = (
workerPath: string,
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
{ execArgv, tsRunner }: { execArgv: string[]; tsRunner?: TsRunner }, // eslint-disable-next-line sonarjs/cognitive-complexity
) => {
let ext = path.extname(workerPath)

Expand All @@ -155,15 +155,18 @@ const setupTsRunner = (
: workerPath
let extensions: string[]
switch (ext) {
case '.cjs':
case '.cjs': {
extensions = ['.cts', '.cjs']
break
case '.mjs':
}
case '.mjs': {
extensions = ['.mts', '.mjs']
break
default:
}
default: {
extensions = ['.ts', '.js']
break
}
}
const found = tryExtensions(workPathWithoutExt, extensions)
let differentExt: boolean | undefined
Expand All @@ -188,6 +191,11 @@ const setupTsRunner = (
'module'
}
}

if (tsRunner == null && isPkgAvailable(TsRunner.TsNode)) {
tsRunner = TsRunner.TsNode
}

switch (tsRunner) {
case TsRunner.TsNode: {
if (tsUseEsm) {
Expand Down Expand Up @@ -261,6 +269,7 @@ const setupTsRunner = (
return {
ext,
isTs,
tsRunner,
tsUseEsm,
workerPath,
execArgv,
Expand All @@ -283,6 +292,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
isTs,
ext,
tsUseEsm,
tsRunner: finalTsRunner,
workerPath: finalWorkerPath,
execArgv: finalExecArgv,
} = setupTsRunner(workerPath, { execArgv, tsRunner })
Expand All @@ -294,7 +304,9 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
!tsUseEsm ||
Number.parseFloat(process.versions.node) >= MTS_SUPPORTED_NODE_VERSION
/* istanbul ignore if */
if (
if (!finalTsRunner) {
throw new Error('No ts runner specified, ts worker path is not supported')
} /* istanbul ignore if */ else if (
(
[
// https://github.com/egoist/esbuild-register/issues/79
Expand All @@ -305,11 +317,11 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
TsRunner.SWC,
.../* istanbul ignore next */ (isTsxSupported ? [] : [TsRunner.TSX]),
] as TsRunner[]
).includes(tsRunner)
).includes(finalTsRunner)
) {
throw new Error(
`${tsRunner} is not supported for ${ext} files yet` +
(isTsxSupported
`${finalTsRunner} is not supported for ${ext} files yet` +
/* istanbul ignore next */ (isTsxSupported
? ', you can try [tsx](https://github.com/esbuild-kit/tsx) instead'
: ''),
)
Expand All @@ -319,10 +331,11 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
const useEval = isTs && !tsUseEsm

const worker = new Worker(
tsUseEsm && tsRunner === TsRunner.TsNode
tsUseEsm && finalTsRunner === TsRunner.TsNode
? dataUrl(`import '${String(workerPathUrl)}'`)
: useEval
? `require('${finalWorkerPath.replace(/\\/g, '\\\\')}')`
? // eslint-disable-next-line unicorn/prefer-string-replace-all -- compatibility
`require('${finalWorkerPath.replace(/\\/g, '\\\\')}')`
: workerPathUrl,
{
eval: useEval,
Expand Down Expand Up @@ -387,46 +400,22 @@ export function runAsWorker<

const { workerPort } = workerData as WorkerData

try {
parentPort!.on(
'message',
({ sharedBuffer, id, args }: MainToWorkerMessage<Parameters<T>>) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
const sharedBufferView = new Int32Array(sharedBuffer)
let msg: WorkerToMainMessage<R>
try {
msg = { id, result: await fn(...args) }
} catch (error: unknown) {
msg = { id, error, properties: extractProperties(error) }
}
workerPort.postMessage(msg)
Atomics.add(sharedBufferView, 0, 1)
Atomics.notify(sharedBufferView, 0)
})()
},
)

/**
* @see https://github.com/un-ts/synckit/issues/94
*
* Starting the worker can fail, due to syntax error, for example. In that case
* we just fail all incoming messages with whatever error message we got.
* Otherwise incoming messages will hang forever waiting for a reply.
*/
} catch (error) {
parentPort!.on(
'message',
({ sharedBuffer, id }: MainToWorkerMessage<Parameters<T>>) => {
parentPort!.on(
'message',
({ sharedBuffer, id, args }: MainToWorkerMessage<Parameters<T>>) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
const sharedBufferView = new Int32Array(sharedBuffer)
workerPort.postMessage({
id,
error,
properties: extractProperties(error),
})
let msg: WorkerToMainMessage<R>
try {
msg = { id, result: await fn(...args) }
} catch (error: unknown) {
msg = { id, error, properties: extractProperties(error) }
}
workerPort.postMessage(msg)
Atomics.add(sharedBufferView, 0, 1)
Atomics.notify(sharedBufferView, 0)
},
)
}
})()
},
)
}
8 changes: 7 additions & 1 deletion test/fixtures/yarn-pnp.spec.ts
Expand Up @@ -7,5 +7,11 @@ test('yarn-pnp', async () => {
nodeOptions: ['-r', './.pnp.cjs'],
cwd: resolve('test/fixtures/yarn-pnp'),
})
expect(stdout).toBe([1, 2, 5].join(' '))

/**
* @see https://github.com/sindresorhus/execa/issues/587
*/
expect(stdout).toContain('1')
expect(stdout).toContain('2')
expect(stdout).toContain('5')
})

0 comments on commit 608c9d9

Please sign in to comment.