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/spicy-walls-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"synckit": patch
---

refactor: split into chunk files, add `module-sync` entry
16 changes: 8 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
root = true
root=true

[*]
indent_style = space
indent_size = 2
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style=space
indent_size=2
tab_width=2
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
insert_final_newline=true
36 changes: 36 additions & 0 deletions .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: autofix.ci # For security reasons, the workflow in which the autofix.ci action is used must be named "autofix.ci".

on:
pull_request:
types:
- opened
- reopened
- synchronize

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
autofix:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

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

- name: Install dependencies
run: yarn --immutable

- name: Format Codes
run: yarn format

- name: Apply autofix.ci
uses: autofix-ci/action@2891949f3779a1cafafae1523058501de3d4e944 # v1
with:
fail-fast: false
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ jobs:
node-version: ${{ matrix.node }}
cache: yarn

- uses: oven-sh/setup-bun@4bc047ad259df6fc24a6c9b0f9a0cb08cf17fbe5 # v2
- name: Setup Bun ${{ matrix.bun }}
uses: oven-sh/setup-bun@4bc047ad259df6fc24a6c9b0f9a0cb08cf17fbe5 # v2
with:
bun-version: ${{ matrix.bun }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pkg-pr-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: Build
run: yarn build

- run: yarn dlx pkg-pr-new publish
- run: yarn dlx pkg-pr-new publish --compact
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: write
id-token: write
pull-requests: write

jobs:
release:
name: Release
Expand Down Expand Up @@ -42,4 +47,5 @@ jobs:
publish: yarn release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_CONFIG_PROVENANCE: true
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
36 changes: 36 additions & 0 deletions .github/workflows/size-limit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Size Limit

on:
pull_request:
branches:
- master
- v3.x

permissions:
contents: read
pull-requests: write

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
size-limit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

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

- name: Install Dependencies
run: yarn --immutable

- uses: andresz1/size-limit-action@v1.8.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
skip_step: install
script: yarn size-limit --json
6 changes: 6 additions & 0 deletions .size-limit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"path": "./lib/index.js",
"limit": "1kB"
}
]
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ You can try it with running `yarn benchmark` by yourself. [Here](./benchmarks/be

## Sponsors

| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| [![1stG Open Collective backers and sponsors](https://opencollective.com/1stG/organizations.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers and sponsors](https://opencollective.com/rxts/organizations.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers and sponsors](https://opencollective.com/unts/organizations.svg)](https://opencollective.com/unts) |
| 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

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

## Who are using `synckit`

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmark.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const RUN_TIMES = +(process.env.RUN_TIMES || 1000)
/**
* @param {string} name
* @typedef {{ loadTime: number, runTime: number, totalTime: number } | void} PerfResult
* @returns {PerfResult} Perf result
* @returns {PerfResult | void} Perf result
*/
const perfCase = name => {
const loadStartTime = performance.now()
Expand Down
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.10.3",
"type": "module",
"description": "Perform async work synchronously in Node.js using `worker_threads` with first-class TypeScript support.",
"repository": "git+https://github.com/un-ts/synckit.git",
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
"funding": "https://opencollective.com/unts",
"repository": "https://github.com/un-ts/synckit.git",
"author": "JounQin <admin@1stg.me> (https://www.1stG.me)",
"funding": "https://opencollective.com/synckit",
"license": "MIT",
"packageManager": "yarn@4.8.1",
"engines": {
Expand All @@ -18,6 +18,10 @@
"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 @@ -50,6 +54,7 @@
"build": "concurrently -r 'yarn:build:*'",
"build:r": "r -f cjs",
"build:ts": "tsc -p src",
"format": "prettier --write .",
"lint": "concurrently -r 'yarn:lint:*'",
"lint:es": "eslint . --cache --max-warnings 10",
"lint:tsc": "tsc --noEmit",
Expand All @@ -64,14 +69,13 @@
"tslib": "^2.8.1"
},
"devDependencies": {
"@1stg/common-config": "^11.0.5",
"@1stg/common-config": "^11.1.1",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.28.1",
"@commitlint/cli": "^19.8.0",
"@pkgr/rollup": "^6.0.1",
"@swc-node/register": "^1.10.10",
"@types/jest": "^29.5.14",
"@types/node": "^22.13.11",
"@types/node": "^22.13.17",
"@unts/patch-package": "^8.1.1",
"clean-pkg-json": "^1.2.1",
"concurrently": "^9.1.2",
Expand All @@ -81,15 +85,17 @@
"eslint": "^9.23.0",
"jest": "^29.7.0",
"lint-staged": "^15.5.0",
"make-synchronized": "^0.7.0",
"node-gyp": "^11.1.0",
"make-synchronized": "^0.7.2",
"node-gyp": "^11.2.0",
"prettier": "^3.5.3",
"simple-git-hooks": "^2.12.1",
"size-limit": "^11.2.0",
"size-limit-preset-node-lib": "^0.3.0",
"sync-threads": "^1.0.1",
"synckit": "link:.",
"tinyexec": "^1.0.0",
"tinyexec": "^1.0.1",
"ts-expect": "^1.3.0",
"ts-jest": "^29.2.6",
"ts-jest": "^29.3.1",
"ts-node": "^10.9.2",
"tsx": "^4.19.3",
"type-coverage": "^2.29.7",
Expand All @@ -116,16 +122,13 @@
],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1",
"^synckit$": "<rootDir>/src"
"^synckit$": "<rootDir>/src/index.ts"
},
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"useESM": true,
"tsconfig": {
"importHelpers": false
}
"useESM": true
}
]
}
Expand Down
25 changes: 25 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const NODE_OPTIONS = process.env.NODE_OPTIONS?.split(/\s+/)

export const hasFlag = (flag: string) =>
NODE_OPTIONS?.includes(flag) || process.argv.includes(flag)

export const parseVersion = (version: string) =>
version.split('.').map(Number.parseFloat)

// A naive implementation of semver comparison
export const compareVersion = (version1: string, version2: string) => {
const versions1 = parseVersion(version1)
const versions2 = parseVersion(version2)
const length = Math.max(versions1.length, versions2.length)
for (let i = 0; i < length; i++) {
const v1 = versions1[i] || 0
const v2 = versions2[i] || 0
if (v1 > v2) {
return 1
}
if (v1 < v2) {
return -1
}
}
return 0
}
83 changes: 83 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { compareVersion, hasFlag } from './common.js'
import type { GlobalShim, ValueOf } from './types.js'

export const TsRunner = {
// https://nodejs.org/docs/latest/api/typescript.html#type-stripping
Node: 'node',
// https://bun.sh/docs/typescript
Bun: 'bun',
// https://github.com/TypeStrong/ts-node
TsNode: 'ts-node',
// https://github.com/egoist/esbuild-register
EsbuildRegister: 'esbuild-register',
// https://github.com/folke/esbuild-runner
EsbuildRunner: 'esbuild-runner',
// https://github.com/swc-project/swc-node/tree/master/packages/register
SWC: 'swc',
// https://github.com/esbuild-kit/tsx
TSX: 'tsx',
} as const

export type TsRunner = ValueOf<typeof TsRunner>

const {
NODE_OPTIONS: NODE_OPTIONS_ = '',
SYNCKIT_EXEC_ARGV = '',
SYNCKIT_GLOBAL_SHIMS,
SYNCKIT_TIMEOUT,
SYNCKIT_TS_RUNNER,
} = process.env

export const MTS_SUPPORTED_NODE_VERSION = '16'
export const LOADER_SUPPORTED_NODE_VERSION = '20'

// https://nodejs.org/docs/latest-v22.x/api/typescript.html#type-stripping
export const STRIP_TYPES_NODE_VERSION = '22.6'

// https://nodejs.org/docs/latest-v23.x/api/typescript.html#modules-typescript
export const TRANSFORM_TYPES_NODE_VERSION = '22.7'

// https://nodejs.org/docs/latest-v22.x/api/process.html#processfeaturestypescript
export const FEATURE_TYPESCRIPT_NODE_VERSION = '22.10'

// https://nodejs.org/docs/latest-v23.x/api/typescript.html#type-stripping
export const DEFAULT_TYPES_NODE_VERSION = '23.6'

export const STRIP_TYPES_FLAG = '--experimental-strip-types'
export const TRANSFORM_TYPES_FLAG = '--experimental-transform-types'
export const NO_STRIP_TYPES_FLAG = '--no-experimental-strip-types'

export const NODE_OPTIONS = NODE_OPTIONS_.split(/\s+/)

export const NODE_VERSION = process.versions.node

export const NO_STRIP_TYPES = // >=
compareVersion(NODE_VERSION, FEATURE_TYPESCRIPT_NODE_VERSION) >= 0
? process.features.typescript === false
: hasFlag(NO_STRIP_TYPES_FLAG) &&
!hasFlag(STRIP_TYPES_FLAG) &&
!hasFlag(TRANSFORM_TYPES_FLAG)

Check warning on line 59 in src/constants.ts

View check run for this annotation

Codecov / codecov/patch

src/constants.ts#L58-L59

Added lines #L58 - L59 were not covered by tests

export const DEFAULT_TIMEOUT = SYNCKIT_TIMEOUT ? +SYNCKIT_TIMEOUT : undefined

export const DEFAULT_EXEC_ARGV = SYNCKIT_EXEC_ARGV.split(',')

export const DEFAULT_TS_RUNNER = SYNCKIT_TS_RUNNER as TsRunner | undefined

export const DEFAULT_GLOBAL_SHIMS = ['1', 'true'].includes(
SYNCKIT_GLOBAL_SHIMS!,
)

export const DEFAULT_GLOBAL_SHIMS_PRESET: GlobalShim[] = [
{
moduleName: 'node-fetch',
globalName: 'fetch',
},
{
moduleName: 'node:perf_hooks',
globalName: 'performance',
named: 'performance',
},
]

export const INT32_BYTES = 4
Loading
Loading