streamiterator
Advanced tools
Comparing version 1.3.1 to 2.0.8
{ | ||
"name": "streamiterator", | ||
"version": "1.3.1", | ||
"description": "converts ReadableStream into AsyncIterator", | ||
"version": "2.0.8", | ||
"description": "Turns browser fetch stream into asynchronous iterator", | ||
"keywords": [ | ||
"fetch", | ||
"stream", | ||
"iterator", | ||
"iterable", | ||
"stream", | ||
"async", | ||
"asynchronous", | ||
"ReadableStream", | ||
"async", | ||
"iterator", | ||
"AsyncIterator" | ||
@@ -18,47 +20,30 @@ ], | ||
"contributors": [], | ||
"main": "distr/streamiterator.js", | ||
"author": "Vadzim Zieńka <developer@vadzim.info>", | ||
"author": "Vadzim Zieńka <vadzim@vadzim.info>", | ||
"source": "src/index.js", | ||
"types": "dist/types/index.d.ts", | ||
"main": "dist/commonjs/index.js", | ||
"module": "dist/module/index.js", | ||
"esnext": "dist/esnext/index.js", | ||
"scripts": { | ||
"build": "flow && (rm distr/*; true) && babel source --out-dir distr && cd distr && (for f in $(find | grep -E '.js$'); do if grep @flow ../source/$f >/dev/null; then cp ../source/$f $f.flow; fi; done) && cd ..", | ||
"prettier": "prettier --write 'source/**/*.js' 'example/**/*.js' '*.js'", | ||
"prepare": "npm run build", | ||
"test": "jest", | ||
"go": "npm run prettier && npm run build && npm run test" | ||
"compile": "module-scripts compile", | ||
"clean": "module-scripts clean", | ||
"types": "module-scripts types", | ||
"build": "module-scripts build", | ||
"lint": "module-scripts lint", | ||
"lint:fix": "module-scripts lint:fix", | ||
"lint:ci": "module-scripts lint:ci", | ||
"test": "module-scripts test", | ||
"test:debug": "module-scripts test:debug", | ||
"coverage": "module-scripts coverage", | ||
"allchecks": "module-scripts allchecks", | ||
"prepack": "module-scripts prepack", | ||
"prepublishOnly": "module-scripts prepublishOnly", | ||
"postpublish": "module-scripts postpublish" | ||
}, | ||
"jest": { | ||
"transform": { | ||
"\\.jsx?$": "babel-7-jest" | ||
} | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0-beta.37", | ||
"@babel/core": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-async-generator-functions": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-decorators": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-export-default-from": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-numeric-separator": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-optional-chaining": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-pipeline-operator": "^7.0.0-beta.37", | ||
"@babel/plugin-proposal-throw-expressions": "^7.0.0-beta.37", | ||
"@babel/plugin-transform-runtime": "^7.0.0-beta.37", | ||
"@babel/preset-env": "^7.0.0-beta.37", | ||
"@babel/preset-flow": "^7.0.0-beta.37", | ||
"babel-7-jest": "^21.3.3", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-plugin-transform-strict-mode": "^6.24.1", | ||
"babylon": "^7.0.0-beta.37", | ||
"flow-bin": "^0.63.1", | ||
"jest": "^22.1.1", | ||
"prettier": "^1.10.2" | ||
"module-scripts": "^0.0.34", | ||
"node-fetch": "^2.6.0" | ||
}, | ||
"dependencies": { | ||
"@vadzim/callback-to-iterator": "^0.2.1", | ||
"@vadzim/get-async-iterable": "^0.0.1", | ||
"@babel/runtime": "^7.0.0-beta.37" | ||
} | ||
"dependencies": {} | ||
} |
# streamiterator | ||
Converts ReadableStream into AsyncIterator. | ||
### Using ### | ||
Turns browser streams into asynchronous iterator. This works with `fetch` responses, `fetch` body, files, blobs, readable streams. | ||
For compatibility reasons works with `node-fetch` too. | ||
With this module you can [iterate](https://github.com/tc39/proposal-async-iteration) over a nodejs stream (file, http response, etc) with a plain loop: | ||
```js | ||
import streamiterator from "streamiterator" | ||
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}`) | ||
for await (const chunk of streamiterator(fetch('https://api/data'))) { | ||
console.log('Read chunk:', chunk) | ||
} | ||
} | ||
``` | ||
As of August, 2017 you need smth like either [babel](http://babeljs.io/) or [node.js 8.4.0 or higher](https://nodejs.org/) with `--harmony_async_iteration` switch to be able to use `for await` operator. | ||
// or in more verbose but more explicit way: | ||
A bit of code with iterables can be seen in [tests](https://github.com/vadzim/streamiterator/blob/master/source/streamiterator.test.js). | ||
It's possible to iterate without `for await`, though it is not so nice as using syntactic suger: | ||
```js | ||
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: | ||
```js | ||
import "streamiterator/polyfill" | ||
import fs from "fs" | ||
async function DoIt() { | ||
for await (const data of fs.createReadStream("./data.txt")) { | ||
console.log(data) | ||
const response = await fetch('https://api/data') | ||
for await (const chunk of streamiterator(response.body.getReader())) { | ||
console.log('Read chunk:', chunk) | ||
} | ||
} | ||
``` | ||
Note that you don't need to import `streamiterator/polyfill` in every file of your project. Just in the `main.js` or similar. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
0
2
15
383
16597
25
2
- Removed@babel/runtime@^7.0.0-beta.37
- Removed@vadzim/callback-to-iterator@^0.2.1
- Removed@vadzim/get-async-iterable@^0.0.1
- Removed@babel/runtime@7.26.9(transitive)
- Removed@vadzim/callback-to-iterator@0.2.1(transitive)
- Removed@vadzim/get-async-iterable@0.0.1(transitive)
- Removedbabel-runtime@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedregenerator-runtime@0.11.10.14.1(transitive)