Installations
npm install @kaciras/deasync
Developer
Kaciras
Developer Guide
Module System
CommonJS
Min. Node Version
>=20
Typescript Support
Yes
Node Version
23.0.0
NPM Version
10.9.0
Statistics
42 Stars
306 Commits
5 Forks
2 Watching
1 Branches
13 Contributors
Updated on 25 Nov 2024
Bundle Size
858.00 B
Minified
495.00 B
Minified + Gzipped
Languages
TypeScript (64.58%)
JavaScript (31.52%)
C++ (3.34%)
Python (0.57%)
Total Downloads
Cumulative downloads
Total Downloads
150,885
Last day
-61.4%
262
Compared to previous day
Last week
-33.8%
2,721
Compared to previous week
Last month
-13.9%
10,377
Compared to previous month
Last year
254.4%
108,930
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
DeAsync
DeAsync turns async code into sync, implemented with a blocking mechanism by calling Node.js event loop at JavaScript layer. The core of deasync is written in C++.
This project is forked from abbr/deasync and rewritten in modern code, adding some new features: types, Promise support, and prebuild binaries.
The benefit of this package over synckit, await-sync and others libs is that this runs your code in the current context, so parameters and the return value of your function are no need to be serializable, you are free to use Symbol
, functions, and objects with prototypes.
[!WARNING]
Due to
uv_run()
is not reentrant, functions that poll the event loop and deasynced functions only work at the top level, and calling them from asynchronous callbacks can lead to deadlocks.
Installation
1npm install @kaciras/deasync
DeAsync downloads prebuild binary from GitHub releases during installation, if download fails, try to build locally. You can skip the install phase by setting the environment variable NO_PREBUILD=1
.
DeAsync uses node-gyp to compile C++ source code, so to build Deasync you may need the compilers listed in node-gyp.
Usage
deasync(function)
Generic wrapper of async function with conventional API signature function(...args, (error, result) => {})
. Returns result
and throws error
as exception if not null.
Sleep (a wrapper of setTimeout):
1const { deasync } = require("@kaciras/deasync"); 2 3const sleep = deasync((timeout, callback) => { 4 setTimeout(() => callback(null, "wake up!"), timeout); 5}); 6 7console.log("Timestamp before: " + performance.now()); 8console.log(sleep(1000)); 9console.log("Timestamp after: " + performance.now());
awaitSync(promise)
Similar with the keyword await
but synchronously.
1const { awaitSync } = require("@kaciras/deasync"); 2 3const promise = new Promise(resolve => setTimeout(resolve, 1000)).then(() => "wake up!") 4 5console.log("Timestamp before: " + performance.now()); 6console.log(awaitSync(promise)); 7console.log("Timestamp after: " + performance.now());
uvRun()
Run pending callbacks of macro tasks in the event loop.
1const { uvRun } = require("@kaciras/deasync"); 2 3let called = false; 4setImmediate(() => called = true); 5 6uvRun(); 7console.log(`Called is ${called}`); // Called is true
runLoopOnce()
Run micro task callbacks until the queue has been exhausted, then run pending callbacks of macro tasks.
- If a new micro task callback enqueued when executing another, it will also be executed.
- Callbacks enqueued in macro task callback will not be executed.
loopWhile(predicate)
For async function with unconventional API, for instance function asyncFunction(p1,function cb(res){}), use loopWhile(predicateFunc) where predicateFunc is a function that returns boolean loop condition.
1let done = false; 2let data; 3asyncFunction(p1, res => { 4 data = res; 5 done = true; 6}); 7require('deasync').loopWhile(() => !done); 8// data is now populated
Recommendation
DeAsync changes code execution sequence and the task scheduling, which typically degrades performance. The primary use case for DeAsync is compatibility with legacy code that does not support asynchronous. If all you are facing is syntactic problem such as callback hell, using a less drastic package implemented in pure js is recommended.
No vulnerabilities found.
Reason
20 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-7q7g-4xm8-89cq
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/build.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/build.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/build.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/Kaciras/deasync/test.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 4 third-party GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
Project has not signed or included provenance with any releases.
Details
- Warn: release artifact v1.1.0 not signed: https://api.github.com/repos/Kaciras/deasync/releases/181691908
- Warn: release artifact v1.0.4 not signed: https://api.github.com/repos/Kaciras/deasync/releases/150052032
- Warn: release artifact v1.0.3 not signed: https://api.github.com/repos/Kaciras/deasync/releases/84910093
- Warn: release artifact v1.0.2 not signed: https://api.github.com/repos/Kaciras/deasync/releases/59779352
- Warn: release artifact v1.1.0 does not have provenance: https://api.github.com/repos/Kaciras/deasync/releases/181691908
- Warn: release artifact v1.0.4 does not have provenance: https://api.github.com/repos/Kaciras/deasync/releases/150052032
- Warn: release artifact v1.0.3 does not have provenance: https://api.github.com/repos/Kaciras/deasync/releases/84910093
- Warn: release artifact v1.0.2 does not have provenance: https://api.github.com/repos/Kaciras/deasync/releases/59779352
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3.8
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More