Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
streamiterator
Advanced tools
Converts ReadableStream into AsyncIterator.
With this module you can over a stream with a plain loop:
import streamIterator from "streamiterator"
// or
// const streamIterator = require("streamiterator")
async function DoIt(stream) {
for await (const value of streamIterator(stream)) {
console.log(`Read: ${value}`)
}
}
Yu can use here any stream you are able to get from nodejs - file streams like fs.createReadableStream(...)
, http responses and so no.
As of August, 2017 you need smth like either or with --harmony_async_iteration
switch to be able to use for await
operator.
Of course, you can iterate without for await
, though it is not so nice as using syntactic suger:
import streamIterator from "streamiterator"
async function DoIt(stream) {
for (let done, value, iterator = streamIterator(stream); {done, value} = await iterator.next(), !done;) {
console.log(`Read: ${value}`)
}
}
If the stream emits an error, it will be thrown while looping. Wrap your loop in try..catch
to deal with it.
If eventually streams will support async iteration natively then this module will just redirect iteration to that native mechanism. No overhead will be added.
But if you believe that writing streamIterator(...)
everywhere is a bullshit and in your world streams have to be iterable from the scratch right now, then you can import streamiterator/polyfill
in the root of your project and iterate just on streams:
import "streamiterator/polyfill"
import fs from "fs"
async function DoIt() {
for await (const data of fs.createReadableStream("./data.txt")) {
console.log(data)
}
}
Note that you don't need to import streamiterator/polyfill
in every file of your project. Just in the main.js
or similar.
Please contribute! All contributions are greatly appreciated no matter how small or large the contribution is. Whether it's a small grammar fix in the README, a huge bug fix, or just an issue report, you will be recognized as a 'Contributor' to this project.
Please, feel free to if you have any question.
FAQs
Turns browser fetch stream into asynchronous iterator
The npm package streamiterator receives a total of 2 weekly downloads. As such, streamiterator popularity was classified as not popular.
We found that streamiterator 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
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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.