Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
callback-to-async-iterator
Advanced tools
callback-to-async-iterator
Turn any callback-based listener into an async iterator.
We needed this module to turn our database listeners into async iterators, which is what GraphQL subscriptions expect to be passed. It might be useful for you too!
npm install callback-to-async-iterator
Imagine a standard callback-based listener like this:
// callback will be called with each new message added to the database
const listenToNewMessages = (callback) => {
return db.messages.listen(message => callback(message));
}
The problem is that callbacks are push based, they push values to the listener whenever a new value is availabe. Async Iterators on the other hand are pull based, they request a new value and wait until it is available.
This module reconciliates that difference so you can turn your standard callback-based listener into an async iterator:
import asyncify from 'callback-to-async-iterator';
const messages = asyncify(listenToNewMessages);
// Wait until the first message is sent
const firstMessage = await messages.next();
// Asynchronously iterate over new messages and log them as they come in
for await (let message of messages) {
console.log(message);
}
console.log('Done!')
This module will automatically buffer incoming data if .next
hasn't been called yet.
onClose
: A function that's called with whatever you resolve from the listener after the async iterator is done, perfect to do cleanuponError
: A function that's called with any error that happens in the listener or async iteratorbuffering
: (default: true
) Whether incoming values should be buffered before the async iterator starts listening or notasyncify(listenToNewMessages, {
// Close the database connection when the async iterator is done
// NOTE: This is passed whatever the listener resolves the returned promise with, in this case listenToNewMessages resolves with the database connection but it could be whatever you desire
onClose: (connection) => { connection.close(); },
// Log errors to your error tracking system
onError: (err) => {
errorTracking.capture(err);
},
// Don't buffer incoming messages before the async iterator starts listening
buffering: false
})
This module is heavily based on the event emitter to async iterator utility used in graphql-js
. Also big shoutout to @ForbesLindesay who helped a ton with the initial implementation and understanding the problem.
Licensed under the MIT License, Copyright ©️ 2017 Maximilian Stoiber. See LICENSE.md for more information.
FAQs
Turn any callback-based listener into an async iterator.
The npm package callback-to-async-iterator receives a total of 336 weekly downloads. As such, callback-to-async-iterator popularity was classified as not popular.
We found that callback-to-async-iterator 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.