Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

streamiterator

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

streamiterator

converts ReadableStream into AsyncIterator

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-81.82%
Maintainers
1
Weekly downloads
 
Created
Source

streamiterator

Converts ReadableStream into AsyncIterator.

Using

With this module you can iterate over a nodejs stream (file, http response, etc) with a plain loop:

import streamiterator from "streamiterator"
import split from "split"

async function DoIt() {
	// iterate over lines in a file
	for await (const line of streamiterator(
		fs.createReadStream("data.txt").pipe(split())
	)) {
		console.log(`Read: ${line}`)
	}
}

As of August, 2017 you need smth like either babel or node.js 8.4.0 or higher with --harmony_async_iteration switch to be able to use for await operator.

A bit of code with iterables can be seen in tests.

It's possible to 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.

Polyfill

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.createReadStream("./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.

Contributing

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 open an issue or email me to developer@vadzim.info if you have any question.

Keywords

FAQs

Package last updated on 05 Oct 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc