
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
golike-defer
Advanced tools
go's defer statement in JavaScript
defer()
is a function decorator which injects a $defer()
function
as a first parameter.
This injected function can be used to register deferreds, functions
which will be executed at the end of the decorated function execution,
no matter how it ended (via a return
or a throw
).
Note: the deferreds are executed in the reverse order of their declarations.
Installation of the npm package:
> npm install --save golike-defer
$defer(cb)
: cb
will be called at the end of the function$defer(cb, arg1, arg2)
: cb
will be called with arg1
and arg2
arguments$defer.call(thisArg, cb)
: cb
will be called with the context thisArg
$defer.call(thisArg, 'method')
: thisArg.method
will be called at the end of the functionimport defer from 'golike-defer'
import fs from 'fs'
const readFileSync = defer(($defer, path) => {
const fd = fs.openSync(path, 'r')
// The file will be automatically closed at the end of the function,
// whether it succeed or failed.
$defer(fs.closeSync, fd)
const { size } = fs.statSync(path)
const buffer = Buffer.allocUnsafe(size)
fs.readSync(fd, buffer, 0, buffer.length)
return buffer
})
// Helper to promisify a function call.
const fromCallback = fn => new Promise((resolve, reject) => {
fn((error, result) => error ? reject(error) : resolve(result))
})
const readFile = defer(async ($defer, path) => {
const fd = await fromCallback(cb => fs.open(path, 'r', cb))
// The file will be automatically closed at the end of the function,
// whether it succeed or failed.
$defer(() => fromCallback(cb => fs.close(fd, cb)))
const { size } = await fromCallback(cb => fs.stat(path, cb))
const buffer = Buffer.allocUnsafe((size)
await fromCallback(cb => fs.read(fd, buffer, 0, buffer.length, cb))
return buffer
})
There are also two decorators, defer.onSuccess()
and
defer.onFailure()
which run the deferreds only, respectively, in
case of success or in case of failure.
Exceptions (or rejected promises) thrown in deferred are caught and printed on the console.
This can be customized with the onError()
method:
const myDefer = defer.onError(error => {
log(error)
})
const fn = myDefer(($defer, arg1, arg2) => {
// ...
})
# Install dependencies
> npm install
# Run the tests
> npm test
# Continuously compile
> npm run dev
# Continuously run the tests
> npm run dev-test
# Build for production (automatically called by npm install)
> npm run build
Contributions are very welcomed, either on the documentation or on the code.
You may:
ISC © Julien Fontanet
FAQs
go's defer statement in JavaScript
The npm package golike-defer receives a total of 691 weekly downloads. As such, golike-defer popularity was classified as not popular.
We found that golike-defer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.