Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-paws-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"synckit": patch
---

fix: remove buggy `module-sync` entry
1 change: 0 additions & 1 deletion .lintstagedrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .nano-staged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@1stg/nano-staged/tsc'
935 changes: 0 additions & 935 deletions .yarn/releases/yarn-4.8.1.cjs

This file was deleted.

948 changes: 948 additions & 0 deletions .yarn/releases/yarn-4.9.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ plugins:
path: .yarn/plugins/plugin-prepare-lifecycle.cjs
spec: 'https://github.com/un-es/yarn-plugin-prepare-lifecycle/releases/download/v0.0.1/index.js'

yarnPath: .yarn/releases/yarn-4.8.1.cjs
yarnPath: .yarn/releases/yarn-4.9.1.cjs
13 changes: 7 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,27 @@
````ts
export interface GlobalShim {
moduleName: string
/**
* `undefined` means side effect only
*/
/** `undefined` means side effect only */
globalName?: string
/**
* 1. `undefined` or empty string means `default`, for example:
*
* ```js
* import globalName from 'module-name'
* ```
*
* 2. `null` means namespaced, for example:
*
* ```js
* import * as globalName from 'module-name'
* ```
*
*/
named?: string | null
/**
* If not `false`, the shim will only be applied when the original `globalName` unavailable,
* for example you may only want polyfill `globalThis.fetch` when it's unavailable natively:
* If not `false`, the shim will only be applied when the original
* `globalName` unavailable, for example you may only want polyfill
* `globalThis.fetch` when it's unavailable natively:
*
* ```js
* import fetch from 'node-fetch'
*
Expand Down
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,27 @@ You must make sure, the `result` is serializable by [`Structured Clone Algorithm
````ts
export interface GlobalShim {
moduleName: string
/**
* `undefined` means side effect only
*/
/** `undefined` means side effect only */
globalName?: string
/**
* 1. `undefined` or empty string means `default`, for example:
*
* ```js
* import globalName from 'module-name'
* ```
*
* 2. `null` means namespaced, for example:
*
* ```js
* import * as globalName from 'module-name'
* ```
*
*/
named?: string | null
/**
* If not `false`, the shim will only be applied when the original `globalName` unavailable,
* for example you may only want polyfill `globalThis.fetch` when it's unavailable natively:
* If not `false`, the shim will only be applied when the original
* `globalName` unavailable, for example you may only want polyfill
* `globalThis.fetch` when it's unavailable natively:
*
* ```js
* import fetch from 'node-fetch'
*
Expand Down Expand Up @@ -207,15 +208,17 @@ See [benchmark.cjs](./benchmarks/benchmark.cjs.txt) and [benchmark.esm](./benchm

You can try it with running `yarn benchmark` by yourself. [Here](./benchmarks/benchmark.js) is the benchmark source code.

[![Sponsors](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)
## Sponsors and Backers

[![Sponsors and Backers](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)

## Sponsors
### Sponsors

| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| [![1stG Open Collective sponsors](https://opencollective.com/1stG/organizations.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective sponsors](https://opencollective.com/rxts/organizations.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective sponsors](https://opencollective.com/unts/organizations.svg)](https://opencollective.com/unts) |

## Backers
### Backers

| 1stG | RxTS | UnTS |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
Expand Down
9 changes: 4 additions & 5 deletions benchmarks/benchmark.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const RUN_TIMES = +(process.env.RUN_TIMES || 1000)

/**
* @param {string} name
* @typedef {{ loadTime: number, runTime: number, totalTime: number } | void} PerfResult
*
* @typedef {{ loadTime: number; runTime: number; totalTime: number } | void} PerfResult
* @returns {PerfResult | void} Perf result
*/
const perfCase = name => {
Expand Down Expand Up @@ -47,9 +48,7 @@ const kebabCase = text =>
text.replace(/([A-Z]+)/, (_, $1) => '-' + $1.toLowerCase())

class Benchmark {
/**
* @param { Record<string, PerfResult> } perfResults
*/
/** @param {Record<string, PerfResult>} perfResults */
constructor(perfResults) {
const keys = Object.keys(perfResults)
const _baseKey = keys[0]
Expand Down Expand Up @@ -85,7 +84,7 @@ class Benchmark {
/**
* @param {number} a
* @param {number} b
* @returns {string} perf description
* @returns {string} Perf description
*/
perf(a, b) {
return a === b
Expand Down
9 changes: 4 additions & 5 deletions benchmarks/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const __filename = fileURLToPath(import.meta.url)

/**
* @param {string} name
* @typedef {{ loadTime: number, runTime: number, totalTime: number } | void} PerfResult
*
* @typedef {{ loadTime: number; runTime: number; totalTime: number } | void} PerfResult
* @returns {Promise<PerfResult>} Perf result
*/
const perfCase = async name => {
Expand Down Expand Up @@ -54,9 +55,7 @@ const kebabCase = text =>
text.replace(/([A-Z]+)/, (_, $1) => '-' + $1.toLowerCase())

class Benchmark {
/**
* @param { Record.<string, PerfResult> } perfResults
*/
/** @param {Record<string, PerfResult>} perfResults */
constructor(perfResults) {
const keys = Object.keys(perfResults)
const _baseKey = keys[0]
Expand Down Expand Up @@ -92,7 +91,7 @@ class Benchmark {
/**
* @param {number} a
* @param {number} b
* @returns {string} perf description
* @returns {string} Perf description
*/
perf(a, b) {
return a === b
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/make-synchronized.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { makeSynchronized } = require('make-synchronized')

/**
* @param {string} filename
* @returns {() => string} syncified function
* @returns {() => string} Syncified function
*/
const syncFn = filename =>
makeSynchronized('node:fs/promises').readFile(filename, 'utf8')
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/make-synchronized.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { makeSynchronized } from 'make-synchronized'

/**
* @param {string} filename
* @returns {() => string} syncified function
* @returns {() => string} Syncified function
*/
const syncFn = filename =>
makeSynchronized('node:fs/promises').readFile(filename, 'utf8')
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/sync-threads.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { createSyncFn } = require('sync-threads')

/**
* @type {() => string}
*/
/** @type {() => string} */
const syncFn = createSyncFn(require.resolve('./sync-threads.worker.cjs'))

module.exports = syncFn
4 changes: 1 addition & 3 deletions benchmarks/synckit.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { createSyncFn } = require('../lib/index.cjs')

/**
* @type {() => string}
*/
/** @type {() => string} */
const syncFn = createSyncFn(require.resolve('./synckit.worker.cjs'))

module.exports = syncFn
4 changes: 1 addition & 3 deletions benchmarks/synckit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { createSyncFn } from '../lib/index.js'

const cjsRequire = createRequire(import.meta.url)

/**
* @type {() => string}
*/
/** @type {() => string} */
const syncFn = createSyncFn(cjsRequire.resolve('./synckit.worker'))

export default syncFn
28 changes: 12 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "JounQin <admin@1stg.me> (https://www.1stG.me)",
"funding": "https://opencollective.com/synckit",
"license": "MIT",
"packageManager": "yarn@4.8.1",
"packageManager": "yarn@4.9.1",
"engines": {
"node": "^14.18.0 || >=16.0.0"
},
Expand All @@ -19,10 +19,6 @@
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"module-sync": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
},
"require": {
"types": "./index.d.cts",
"default": "./lib/index.cjs"
Expand Down Expand Up @@ -65,31 +61,32 @@
"version": "changeset version && yarn --no-immutable"
},
"dependencies": {
"@pkgr/core": "^0.2.1",
"@pkgr/core": "^0.2.3",
"tslib": "^2.8.1"
},
"devDependencies": {
"@1stg/common-config": "^12.0.0",
"@1stg/common-config": "^13.0.1",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.28.1",
"@changesets/cli": "^2.29.0",
"@commitlint/cli": "^19.8.0",
"@oxc-node/core": "^0.0.22",
"@pkgr/rollup": "^6.0.2",
"@oxc-node/core": "^0.0.23",
"@pkgr/rollup": "^6.0.3",
"@swc-node/register": "^1.10.10",
"@swc/core": "^1.11.18",
"@swc/helpers": "^0.5.15",
"@swc/core": "^1.11.21",
"@swc/helpers": "^0.5.17",
"@types/jest": "^29.5.14",
"@types/node": "^22.14.0",
"@types/node": "^22.14.1",
"@unts/patch-package": "^8.1.1",
"clean-pkg-json": "^1.2.1",
"concurrently": "^9.1.2",
"deasync": "^0.1.30",
"esbuild-register": "^3.6.0",
"esbuild-runner": "^2.2.2",
"eslint": "^9.24.0",
"eslint-plugin-jest": "^28.11.0",
"jest": "^29.7.0",
"lint-staged": "^15.5.0",
"make-synchronized": "^0.7.2",
"nano-staged": "^0.8.0",
"node-gyp": "^11.2.0",
"prettier": "^3.5.3",
"simple-git-hooks": "^2.12.1",
Expand All @@ -99,15 +96,14 @@
"synckit": "link:.",
"tinyexec": "^1.0.1",
"ts-expect": "^1.3.0",
"ts-jest": "^29.3.1",
"ts-jest": "^29.3.2",
"ts-node": "^10.9.2",
"tsx": "^4.19.3",
"type-coverage": "^2.29.7",
"typescript": "^5.8.3",
"yarn-berry-deduplicate": "^6.1.1"
},
"resolutions": {
"es5-ext": "npm:@unes/es5-ext@^0.10.64-1",
"prettier": "^3.5.3",
"synckit": "link:."
},
Expand Down
37 changes: 21 additions & 16 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,7 @@ export const encodeImportModule = (
)
}

/**
* @internal
*/
/** @internal */
export const _generateGlobals = (
globalShims: GlobalShim[],
type: 'import' | 'require',
Expand Down Expand Up @@ -461,7 +459,8 @@ export function extractProperties<T>(object?: T): T | undefined
* Creates a shallow copy of the enumerable properties from the provided object.
*
* @param object - An optional object whose properties are to be extracted.
* @returns A new object containing the enumerable properties of the input, or undefined if no valid object is provided.
* @returns A new object containing the enumerable properties of the input, or
* undefined if no valid object is provided.
*/
export function extractProperties<T>(object?: T) {
if (object && typeof object === 'object') {
Expand All @@ -479,24 +478,30 @@ let sharedBufferView: Int32Array | undefined
/**
* Spawns a worker thread and returns a synchronous function to dispatch tasks.
*
* The function initializes a worker thread with the specified script and configuration,
* setting up a dedicated message channel for bidirectional communication. It applies TypeScript
* runner settings, execution arguments, and global shims as needed. The returned function sends
* tasks to the worker, waits synchronously for a response using shared memory synchronization, and
* The function initializes a worker thread with the specified script and
* configuration, setting up a dedicated message channel for bidirectional
* communication. It applies TypeScript runner settings, execution arguments,
* and global shims as needed. The returned function sends tasks to the worker,
* waits synchronously for a response using shared memory synchronization, and
* then returns the computed result.
*
* @param workerPath - The file path of the worker script to execute.
* @param options - An object containing configuration parameters:
* - timeout: Maximum time in milliseconds to wait for the worker's response.
* - execArgv: Array of Node.js execution arguments for the worker.
* - tsRunner: Specifies the TypeScript runner to use if the worker script is TypeScript.
* - transferList: List of additional transferable objects to pass to the worker.
* - globalShims: Modules to import as global shims; if true, a default preset is used.
*
* @returns A synchronous function that accepts task arguments intended for the worker thread and returns its result.
* - Timeout: Maximum time in milliseconds to wait for the worker's response.
* - ExecArgv: Array of Node.js execution arguments for the worker.
* - TsRunner: Specifies the TypeScript runner to use if the worker script is
* TypeScript.
* - TransferList: List of additional transferable objects to pass to the worker.
* - GlobalShims: Modules to import as global shims; if true, a default preset is
* used.
*
* @throws {Error} If a TypeScript runner is required but not specified, or if an unsupported TypeScript runner is used for the file type.
* @throws {Error} If internal synchronization fails or if the message identifier does not match the expected value.
* @returns A synchronous function that accepts task arguments intended for the
* worker thread and returns its result.
* @throws {Error} If a TypeScript runner is required but not specified, or if
* an unsupported TypeScript runner is used for the file type.
* @throws {Error} If internal synchronization fails or if the message
* identifier does not match the expected value.
*/
export function startWorkerThread<T extends AnyFn, R = Awaited<ReturnType<T>>>( // eslint-disable-line sonarjs/cognitive-complexity
workerPath: string,
Expand Down
Loading
Loading