https://github.com/alanshaw/stream-to-it
🚰 Convert Node.js streams to streaming iterables
https://github.com/alanshaw/stream-to-it
convert-node-streams readable-stream stream streaming-iterables
Last synced: about 1 month ago
JSON representation
🚰 Convert Node.js streams to streaming iterables
- Host: GitHub
- URL: https://github.com/alanshaw/stream-to-it
- Owner: alanshaw
- License: other
- Created: 2019-08-19T12:03:42.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-10T10:38:47.000Z (11 months ago)
- Last Synced: 2024-12-30T12:03:41.011Z (5 months ago)
- Topics: convert-node-streams, readable-stream, stream, streaming-iterables
- Language: TypeScript
- Homepage:
- Size: 989 KB
- Stars: 18
- Watchers: 3
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# stream-to-it
[](https://codecov.io/gh/alanshaw/stream-to-it)
[](https://github.com/alanshaw/stream-to-it/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)> Convert Node.js streams to streaming iterables
# About
Seamlessly use Node.js streams with `it-pipe` and friends.
## Example - Convert readable stream to source iterable
```TypeScript
import fs from 'node:fs'
import * as toIterable from 'stream-to-it'const readable = fs.createReadStream('/path/to/file')
// Node.js streams are already async iterable so this is just s => s
const source = toIterable.source(readable)for await (const chunk of source) {
console.log(chunk.toString())
}
```Also works with browser [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream):
```TypeScript
import * as toIterable from 'stream-to-it'const res = await fetch('http://example.org/file.jpg')
if (res.body == null) {
throw new Error('Body was not set')
}for await (const chunk of toIterable.source(res.body)) {
console.log(chunk.toString())
}
```## Example - Convert writable stream to sink iterable
```TypeScript
import fs from 'node:fs'
import { pipe } from 'it-pipe'
import * as toIterable from 'stream-to-it'const source = [Buffer.from('Hello '), Buffer.from('World!')]
const sink = toIterable.sink(fs.createWriteStream('/path/to/file'))await pipe(source, sink)
```## Example - Convert transform stream to transform iterable
```TypeScript
import fs from 'node:fs'
import { Transform } from 'node:stream'
import { pipe } from 'it-pipe'
import * as toIterable from 'stream-to-it'const output = await pipe(
[true, false, true, true],
toIterable.transform(new Transform({ // Inverter transform :)
transform (chunk, enc, cb) {
cb(null, !chunk)
}
})),
// Collect and return the chunks
async source => {
const chunks = []
for await (const chunk of source) chunks.push(chunk)
return chunks
}
)console.log(output) // [ false, true, false, false ]
```## Related
- [`it-to-stream`](https://www.npmjs.com/package/it-to-stream) Convert streaming iterables to Node.js streams
- [`it-pipe`](https://www.npmjs.com/package/it-pipe) Utility to "pipe" async iterables together# Install
```console
$ npm i stream-to-it
```# API Docs
-
# License
Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / )
- MIT ([LICENSE-MIT](LICENSE-MIT) / )# Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.