New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

node-syncify

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-syncify

Convert async methods in worker to sync

latest
npmnpm
Version
1.0.2
Version published
Weekly downloads
3
200%
Maintainers
1
Weekly downloads
 
Created
Source

Converts async methods to sync using worker_threads. Library related to this blog post. Does not require compilation steps to work, can run anywhere in Node.js, has zero dependencies.

This'll allow you to call async methods as if they were sync, but you have to move the async methods into their own file (and they'll run inside a Worker).

Usage

Currently ESM for now, but could be made CJS. This fork fixes some issue and convert it to CJS module.

Here's a demo which artificially loads a file using the async API of the built-in FS library, but makes those calls sync to the main thread.

In your main file:

// main.js
const build = require('node-syncify');
const method = build(require.resolve('./method.js'));
const result = method('foo.json');

console.info('did something sync!', result);

In your helper file (called "method.js" here), do this:

// method.js
const fs = require('fs').promises;

export default async function(filename) {
  // do something async just for fun
  await new Promise((r) => setTimeout(r, 1000));
  const data = await fs.readFile(filename);
  return data;
};

Great! 🥳

FAQs

Package last updated on 20 Mar 2024

Did you know?

Socket

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