Socket
Socket
Sign inDemoInstall

synckit

Package Overview
Dependencies
2
Maintainers
2
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    synckit

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


Version published
Weekly downloads
7.9M
decreased by-3.82%
Maintainers
2
Install size
184 kB
Created
Weekly downloads
 

Package description

What is synckit?

The synckit npm package allows for executing asynchronous tasks synchronously using worker threads or child processes. It is designed to offload heavy computation or I/O-bound tasks without blocking the main thread, thus enabling a synchronous-like coding style while maintaining non-blocking behavior.

What are synckit's main functionalities?

Running asynchronous tasks synchronously

This feature allows you to run an asynchronous task inside a worker thread and wait for the result synchronously. The 'worker.js' file should export an async function that will be executed with the provided arguments.

const { runAsWorkerThread } = require('synckit');
const result = runAsWorkerThread('./worker.js', ...args);

Creating a synchronous API from asynchronous functions

With this feature, you can create a synchronous version of an asynchronous function. The 'async-fn.js' file should export an async function that will be executed with the provided arguments, and the result will be returned synchronously.

const { createSyncFn } = require('synckit');
const syncFn = createSyncFn('./async-fn.js');
const result = syncFn(...args);

Other packages similar to synckit

Changelog

Source

0.2.0

Minor Changes

  • #23 6577e86 Thanks @JounQin! - feat: use worker_threads by default for performance

Readme

Source

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

Keywords

FAQs

Last updated on 09 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc