Skip to content

Commit

Permalink
feat: add custom execArgv support
Browse files Browse the repository at this point in the history
close #55
  • Loading branch information
JounQin committed Oct 15, 2021
1 parent b9bf237 commit 18106a5
Show file tree
Hide file tree
Showing 5 changed files with 1,463 additions and 1,243 deletions.
7 changes: 7 additions & 0 deletions .changeset/late-years-float.md
@@ -0,0 +1,7 @@
---
'synckit': minor
---

feat: add custom `execArgv` support

close #55
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -70,6 +70,7 @@ You must make sure, the `result` is serialized by [`Structured Clone Algorithm`]

1. `SYNCKIT_BUFFER_SIZE`: `bufferSize` to create `SharedArrayBuffer` for `worker_threads` (default as `1024`)
2. `SYNCKIT_TIMEOUT`: `timeout` for performing the async job (no default)
3. `SYNCKIT_EXEC_ARGV`: List of node CLI options passed to the worker, split with comma `,`. (default as `[]`), see also [`node` docs](https://nodejs.org/api/worker_threads.html)

### TypeScript

Expand Down
28 changes: 14 additions & 14 deletions package.json
Expand Up @@ -9,6 +9,8 @@
"engines": {
"node": ">=12.3"
},
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs"
Expand Down Expand Up @@ -53,25 +55,23 @@
"tslib": "^2.3.1"
},
"devDependencies": {
"@1stg/lib-config": "^4.0.0",
"@changesets/changelog-github": "^0.4.0",
"@changesets/cli": "^2.16.0",
"@types/jest": "^27.0.1",
"@types/node": "^16.7.5",
"@1stg/lib-config": "^4.1.2",
"@changesets/changelog-github": "^0.4.1",
"@changesets/cli": "^2.17.0",
"@types/jest": "^27.0.2",
"@types/node": "^16.11.0",
"@types/uuid": "^8.3.1",
"clean-publish": "^2.1.1",
"clean-publish": "^3.4.1",
"deasync": "^0.1.23",
"enhanced-resolve": "^5.8.2",
"postcss": "^8.3.6",
"sync-threads": "^1.0.1",
"ts-expect": "^1.3.0",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"type-coverage": "^2.18.1",
"typescript": "^4.4.2"
"ts-jest": "^27.0.6",
"ts-node": "^10.3.0",
"type-coverage": "^2.18.2",
"typescript": "^4.4.4"
},
"resolutions": {
"prettier": "^2.3.2",
"prettier": "^2.4.1",
"tslib": "^2.3.1"
},
"commitlint": {
Expand Down Expand Up @@ -104,7 +104,7 @@
]
},
"typeCoverage": {
"atLeast": 99.42,
"atLeast": 99.46,
"cache": true,
"detail": true,
"ignoreAsAssertion": true,
Expand Down
39 changes: 32 additions & 7 deletions src/index.ts
Expand Up @@ -18,7 +18,12 @@ import {

export * from './types.js'

const { SYNCKIT_BUFFER_SIZE, SYNCKIT_TIMEOUT, SYNCKIT_TS_ESM } = process.env
const {
SYNCKIT_BUFFER_SIZE,
SYNCKIT_TIMEOUT,
SYNCKIT_TS_ESM,
SYNCKIT_EXEC_ARV,
} = process.env

const TS_USE_ESM = !!SYNCKIT_TS_ESM && ['1', 'true'].includes(SYNCKIT_TS_ESM)

Expand All @@ -30,17 +35,29 @@ export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : undefined

export const DEFAULT_WORKER_BUFFER_SIZE = DEFAULT_BUFFER_SIZE || 1024

export const DEFAULT_EXEC_ARGV = SYNCKIT_EXEC_ARV?.split(',') ?? []

const syncFnCache = new Map<string, AnyFn>()

export interface SynckitOptions {
bufferSize?: number
timeout?: number
execArgv?: string[]
}

export function createSyncFn<T extends AnyAsyncFn>(
workerPath: string,
bufferSize?: number,
timeout?: number,
): Syncify<T>
export function createSyncFn<T extends AnyAsyncFn>(
workerPath: string,
options?: SynckitOptions,
): Syncify<T>
export function createSyncFn<R, T extends AnyAsyncFn<R>>(
workerPath: string,
bufferSize?: number,
timeout = DEFAULT_TIMEOUT,
bufferSizeOrOptions?: SynckitOptions | number,
timeout?: number,
) {
if (!path.isAbsolute(workerPath)) {
throw new Error('`workerPath` must be absolute')
Expand All @@ -52,7 +69,12 @@ export function createSyncFn<R, T extends AnyAsyncFn<R>>(
return cachedSyncFn
}

const syncFn = startWorkerThread<R, T>(workerPath, bufferSize, timeout)
const syncFn = startWorkerThread<R, T>(
workerPath,
typeof bufferSizeOrOptions === 'number'
? { bufferSize: bufferSizeOrOptions, timeout }
: bufferSizeOrOptions,
)

syncFnCache.set(workerPath, syncFn)

Expand All @@ -65,8 +87,11 @@ const throwError = (msg: string) => {

function startWorkerThread<R, T extends AnyAsyncFn<R>>(
workerPath: string,
bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
timeout?: number,
{
bufferSize = DEFAULT_WORKER_BUFFER_SIZE,
timeout = DEFAULT_TIMEOUT,
execArgv = DEFAULT_EXEC_ARGV,
}: SynckitOptions = {},
) {
const { port1: mainPort, port2: workerPort } = new MessageChannel()

Expand All @@ -84,7 +109,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
eval: isTs,
workerData: { workerPort },
transferList: [workerPort],
execArgv: [],
execArgv,
},
)

Expand Down

0 comments on commit 18106a5

Please sign in to comment.