synckit
Perform async work synchronously in Node.js using `worker_threads`, or `child_process` as fallback, with first-class TypeScript support.
Last updated 3 years ago by i1stg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install synckit 
SYNC missed versions from official npm registry.

synckit

GitHub Actions Codecov type-coverage npm GitHub Release

Conventional Commits Renovate enabled JavaScript Style Guide Code Style: Prettier

Perform async work synchronously in Node.js using worker_threads with first-class TypeScript and Yarn P'n'P support.

TOC

Usage

Install

# yarn
yarn add synckit

# npm
npm i synckit

API

// runner.js
import { createSyncFn } from 'synckit'

// the worker path must be absolute
const syncFn = createSyncFn(require.resolve('./worker'), {
  tsRunner: 'tsx', // optional, can be `'ts-node' | 'esbuild-register' | 'esbuild-runner' | 'tsx'`
})

// do whatever you want, you will get the result synchronously!
const result = syncFn(...args)
// worker.js
import { runAsWorker } from 'synckit'

runAsWorker(async (...args) => {
  // do expensive work
  return result
})

You must make sure, the result is serializable by Structured Clone Algorithm

Types

export interface GlobalShim {
  moduleName: string
  /**
   * `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:
   * ```js
   * import fetch from 'node-fetch'
   *
   * if (!globalThis.fetch) {
   *   globalThis.fetch = fetch
   * }
   * ```
   */
  conditional?: boolean
}

Options

  1. execArgv same as env SYNCKIT_EXEC_ARGV
  2. globalShims: Similar like env SYNCKIT_GLOBAL_SHIMS but much more flexible which can be a GlobalShim Array, see GlobalShim's definition for more details
  3. timeout same as env SYNCKIT_TIMEOUT
  4. transferList: Please refer Node.js worker_threads documentation
  5. tsRunner same as env SYNCKIT_TS_RUNNER

Envs

  1. SYNCKIT_EXEC_ARGV: List of node CLI options passed to the worker, split with comma ,. (default as []), see also node docs
  2. SYNCKIT_GLOBAL_SHIMS: Whether to enable the default DEFAULT_GLOBAL_SHIMS_PRESET as globalShims
  3. SYNCKIT_TIMEOUT: timeout for performing the async job (no default)
  4. SYNCKIT_TS_RUNNER: Which TypeScript runner to be used, it could be very useful for development, could be 'ts-node' | 'esbuild-register' | 'esbuild-runner' | 'swc' | 'tsx', 'ts-node' is used by default, make sure you have installed them already

TypeScript

ts-node

If you want to use ts-node for worker file (a .ts file), it is supported out of box!

If you want to use a custom tsconfig as project instead of default tsconfig.json, use TS_NODE_PROJECT env. Please view ts-node for more details.

If you want to integrate with tsconfig-paths, please view ts-node for more details.

esbuild-register

Please view esbuild-register for its document

esbuild-runner

Please view esbuild-runner for its document

swc

Please view @swc-node/register for its document

tsx

Please view tsx for its document

Benchmark

It is about 50x faster than sync-threads but 10x slower than native for reading the file content itself 1000 times during runtime, and 40x faster than sync-threads but 10x slower than native for total time on my personal MacBook Pro with 64G M1 Max.

And it's almost 5x faster than deasync but requires no native bindings or node-gyp.

See benchmark.cjs and benchmark.esm for more details.

You can try it with running yarn benchmark by yourself. Here is the benchmark source code.

Sponsors

1stG RxTS UnTS
1stG Open Collective backers and sponsors RxTS Open Collective backers and sponsors UnTS Open Collective backers and sponsors

Backers

1stG RxTS UnTS
1stG Open Collective backers and sponsors RxTS Open Collective backers and sponsors UnTS Open Collective backers and sponsors

Changelog

Detailed changes for each release are documented in CHANGELOG.md.

License

MIT © JounQin@1stG.me

Current Tags

  • 0.9.2                                ...           latest (3 months ago)

35 Versions

  • 0.9.2                                ...           3 months ago
  • 0.9.1                                ...           5 months ago
  • 0.9.0                                ...           a year ago
  • 0.8.8                                ...           a year ago
  • 0.8.7                                ...           a year ago
  • 0.8.6                                ...           a year ago
  • 0.8.5                                ...           2 years ago
  • 0.8.4                                ...           2 years ago
  • 0.8.3                                ...           2 years ago
  • 0.8.2                                ...           2 years ago
  • 0.8.1                                ...           2 years ago
  • 0.8.0                                ...           2 years ago
  • 0.7.3                                ...           2 years ago
  • 0.7.2                                ...           2 years ago
  • 0.7.1                                ...           3 years ago
  • 0.7.0                                ...           3 years ago
  • 0.6.2                                ...           3 years ago
  • 0.6.1                                ...           3 years ago
  • 0.6.0                                ...           3 years ago
  • 0.4.1                                ...           3 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.4                                ...           3 years ago
  • 0.3.3                                ...           3 years ago
  • 0.3.2                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.6                                ...           3 years ago
  • 0.1.5                                ...           4 years ago
  • 0.1.4                                ...           4 years ago
  • 0.1.3                                ...           4 years ago
  • 0.1.2                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (2)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE