Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
makepromise
Advanced tools
Make native Promise
from function with callback.
The package uses some newer language features. For your convenience, it's been transpiled to be compatible with Node 4. You can use the following snippet.
const makePromise = require('makepromise/es5/src/')
makePromise(fn:Function(...args, cb:Function(err:Error, res:any))) => Promise<any>
Create a promise from a function which accepts a callback as the last argument,
and where the callback will be called with 2 arguments: (error, result)
.
For example, you can unlink a file (here, a temp file is created with
wrote
package):
const { unlink } = require('fs')
const { createWritable } = require('wrote')
const makePromise = require('makepromise')
let file
(async () => {
// create a temp file and open a writable stream
const ws = await createWritable()
const { path: file } = ws // /var/folders/s0/tmp/T/wrote-69822.data
// here, just close the stream without makepromise, because it's a different
// kind of constructor, ie. resolve-reject and not error callback handlers
await new Promise((resolve, reject) => {
ws.once('close', resolve)
ws.once('error', reject)
ws.close()
})
await makePromise(unlink, file)
})()
In addition, errors will be updated to include the stack trace of when
makePromise
was called, rather than have node internal error stack, or no
stack at all:
const fs = require('fs')
const makePromise = require('makepromise');
(async () => {
try {
await makePromise(fs.unlink, 'error-test-file')
} catch ({ stack }){
console.log(stack)
/*
Error: ENOENT: no such file or directory, unlink 'error-test-file'
at cb (makepromise/src/index.js:28:31)
at makePromise (makepromise/src/index.js:18:16)
at Object.<anonymous> (makepromise/examples/error-stack.js:4:1)
*/
// without this feature:
/*
Error: ENOENT: no such file or directory, unlink 'error-test-file'
*/
}
})()
makePromise(fn:Function(...args, cb:Function(err:Error, res:any), resolveValue:any)) => Promise<any>
Pass resolveValue
as second argument to make promise be resolved with it.
const { unlink } = require('fs')
const { createWritable } = require('wrote')
const makePromise = require('makepromise');
(async () => {
// create a temp file and open a writable stream
const ws = await createWritable()
await closeWritable(ws)
const { path: file } = ws
console.log(file) // /var/folders/s0/tmp/T/wrote-69822.data
// pass 3rd argument as the return value
const resolvedFile = await makePromise(unlink, file, file)
console.log(resolvedFile === file) // true
})()
Check test/spec/integration
for the following tests:
lockfile
packagefs.unlink
fs.stat
fs.stat
with non-existent file)Licence: MIT
(c) Sobesednik-Media 2017
FAQs
Make a Promise from a function with a callback and preserve its error stack.
The npm package makepromise receives a total of 2,691 weekly downloads. As such, makepromise popularity was classified as popular.
We found that makepromise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.