Skip to content

Commit b8bd815

Browse files
committed
Change package name to , add some docs
1 parent 1fa2d3e commit b8bd815

13 files changed

+85
-24
lines changed

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/node-stream-to-async-iterator.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/

History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# History
2+
3+
## 0.1.0
4+
5+
- Info: Initial Release

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# hack to get osx to recognize a chane in PATH
1+
# hack to get osx to recognize a change in PATH
22
SHELL := /usr/bin/env sh
33
PATH := $(shell pwd)/node_modules/.bin:${PATH}
44

@@ -21,7 +21,7 @@ lib_asset_files := $(shell find lib -type f ! -name "*.js" ! -name "*.js.flow" !
2121

2222
build_files := $(lib_files:lib/%=build/%)
2323
build_asset_files := $(lib_asset_files:lib/%=build/%)
24-
build_package_files := build/README.md build/package.json build/license
24+
build_package_files := build/README.md build/package.json build/license build/.npmignore
2525

2626

2727
# Build

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
11
# Stream Async Iterator
22

3+
4+
## Overview
5+
6+
`stream-async-iterator` provides a wrapper that implements `Symbol.asyncIterator`. This will allow streams to be usable
7+
as async iterables that can be used in for-await-of loops.
8+
39
## Installation
410

511
```
6-
$ npm install stream-async-iterator
12+
$ npm install stream-async-to-iterator
713
```
814

15+
The examples provides use async/await syntax for for-of loops. This assumes you are in an environment that natively
16+
supports this new syntax, or that you use a tool such as Babel. In addition, for async iterators to work properly,
17+
the `Symbol.asyncIterator` symbol must be defined. Core-js or `babel-polyfill` can both help with that.
918

10-
## Overview
1119

12-
`stream-async-iterator` provides a wrapper that implements `Symbol.asyncIterator`.
20+
## Usage
21+
22+
Import the StreamToAsyncIterator class and pass the stream to its constructor. The iterator instance can be directly
23+
used in for-of contexts.
24+
25+
If the stream is in object mode, each iteration will produce the next object. See the
26+
[node documentation](https://nodejs.org/dist/latest-v6.x/docs/api/stream.html#stream_types_of_streams) for more
27+
information.
28+
29+
```js
30+
#!/usr/bin/env node
31+
'use strict';
32+
require('core-js/es7/symbol');
33+
const fs = require('fs');
34+
const S2A = require('stream-to-async-iterator');
35+
36+
37+
(async function() {
38+
const readStream = fs.createReadStream(__filename);
39+
for await (const chunk of new S2A(readStream)) {
40+
console.log(chunk);
41+
}
42+
})();
43+
```
1344

1445

1546
## References

examples/fs-async.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
require('core-js/es7/symbol');
4+
const fs = require('fs');
5+
const S2A = require('..');
6+
7+
8+
(async function() {
9+
const readStream = fs.createReadStream(__filename);
10+
for await (const chunk of new S2A(readStream)) {
11+
console.log(chunk);
12+
}
13+
})();

lib/stream-async-iterator.js renamed to lib/stream-async-to-iterator.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ export const states = {
1212
};
1313

1414
/**
15-
* @typedef {Object} StreamAsyncIterator~Options
15+
* @typedef {Object} StreamAsyncToIterator~Options
1616
* @property {number} [size] - the size of each read from the stream for each iteration
1717
*/
18-
type StreamAsyncIteratorOptions = {
18+
type StreamAsyncToIteratorOptions = {
1919
size?: number;
2020
}
2121

2222
/**
23-
* @typedef {Object} StreamAsyncIterator~Iteration
23+
* @typedef {Object} StreamAsyncToIterator~Iteration
2424
* @property {boolean} done
2525
* @property {*} value
2626
*/
@@ -38,12 +38,12 @@ type Reject = (err: any) => void;
3838
* iteration. A size can be supplied to set an explicit call to `stream.read([size])` in
3939
* the options for each iteration.
4040
*/
41-
export default class StreamAsyncIterator {
41+
export default class StreamAsyncToIterator {
4242
/**
4343
* @param {Readable} stream
44-
* @param {StreamAsyncIterator~Options} [options]
44+
* @param {StreamAsyncToIterator~Options} [options]
4545
*/
46-
constructor(stream: Readable, options: StreamAsyncIteratorOptions={}) {
46+
constructor(stream: Readable, options: StreamAsyncToIteratorOptions={}) {
4747
/**
4848
* The underlying readable stream
4949
* @private
@@ -102,7 +102,7 @@ export default class StreamAsyncIterator {
102102

103103
/**
104104
* Returns the next iteration of data. Rejects if the stream errored out.
105-
* @returns {Promise<StreamAsyncIterator~Iteration>}
105+
* @returns {Promise<StreamAsyncToIterator~Iteration>}
106106
*/
107107
async next(): Promise<Iteration> {
108108
if (this._state === states.notReadable) {
@@ -164,7 +164,7 @@ export default class StreamAsyncIterator {
164164
}
165165
}
166166

167-
Object.defineProperty(StreamAsyncIterator.prototype, (Symbol: any).asyncIterator, {
167+
Object.defineProperty(StreamAsyncToIterator.prototype, (Symbol: any).asyncIterator, {
168168
configurable: true,
169169
value: function() {return this;}
170170
});
File renamed without changes.

0 commit comments

Comments
 (0)