8000 GitHub - un-ts/synckit at v0.2.0
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
/ synckit Public

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

License

Notifications You must be signed in to change notification settings

un-ts/synckit

Repository files navigation

synckit

GitHub Actions Codecov Codacy Grade type-coverage npm GitHub Release

David Peer David David Dev

Conventional Commits Renovate enabled JavaScript Style Guide Code Style: Prettier

Perform async work synchronously in Node.js using a separate process with first-class TypeScript support

TOC

Usage

Install

# yarn
yarn add synckit

# npm
npm i synckit

API

worker_threads is used by default for performance, if you have any problem with it, you can set env SYNCKIT_WORKER_THREADS=0 to disable it and fallback to previously child_process solution, and please raise an issue here so that we can improve it.

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

// the worker path must be absolute
const syncFn = createSyncFn(require.resolve('./worker'))

// 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:

  1. if worker_threads is enabled (by default), the result is serialized by Structured Clone Algorithm
  2. if child_process is used, the result is serialized by JSON.stringify

TypeScript

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.

Changelog

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

License

MIT © JounQin@1stG.me

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 16

0