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.
async-iterator-to-stream
Advanced tools
Convert an async iterator/iterable to a readable stream
Even though this library is dedicated to async iterators, it is fully compatible with synchronous ones.
Furthermore, generators can be used to create readable stream factories!
Installation of the npm package:
> npm install --save async-iterator-to-stream
const asyncIteratorToStream = require('async-iterator-to-stream')
// sync/async iterators
asyncIteratorToStream(new Set(['foo', 'bar']).values())
.pipe(output)
// sync/async iterables
asyncIteratorToStream.obj([1, 2, 3])
.pipe(output)
// if you pass a sync/async generator, it will return a factory instead of a
// stream
const createRangeStream = asyncIteratorToStream.obj(function * (n) {
for (let i = 0; i < n; ++i) {
yield i
}
})
createRangeStream(10)
.pipe(output)
Let's implement a simpler fs.createReadStream
to illustrate the usage of this
library.
const asyncIteratorToStream = require('async-iterator-to-stream')
// promisified fs
const fs = require('mz/fs')
const createReadStream = asyncIteratorToStream(async function * (file) {
const fd = await fs.open(file, 'r')
try {
let size = yield
while (true) {
const buf = Buffer.alloc(size)
const [n] = await fs.read(fd, buf, 0, size, null)
if (n < size) {
yield buf.slice(0, n)
return
}
size = yield buf
}
} finally {
await fs.close(fd)
}
})
createReadStream('foo.txt').pipe(process.stdout)
If your environment does not support async generators, you may use a sync generator instead and
yield
promises to wait for them.
# Install dependencies
> yarn
# Run the tests
> yarn test
# Continuously compile
> yarn dev
# Continuously run the tests
> yarn dev-test
# Build for production
> yarn build
Contributions are very welcomed, either on the documentation or on the code.
You may:
ISC © Julien Fontanet
FAQs
Convert an async iterator/iterable to a readable stream
The npm package async-iterator-to-stream receives a total of 773 weekly downloads. As such, async-iterator-to-stream popularity was classified as not popular.
We found that async-iterator-to-stream 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.
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.