Skip to content

Commit

Permalink
Minor tweaks for documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Mar 16, 2017
1 parent 5b9fa20 commit b4b8412
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## v0.4.2 2017-03-16

* Minor tweaks for documentation.

## v0.4.1 2017-03-14

* `once('error')` is the same as `once('end')`.
Expand Down
64 changes: 42 additions & 22 deletions README.md
Expand Up @@ -25,72 +25,92 @@ npm install promise-readable

### Usage

#### constructor(stream)
#### constructor

`PromiseReadable` object requires `Readable` object to work:
```js
const promiseReadable = new PromiseReadable(stream)
```

`PromiseReadable` object requires `Readable` object to work.

_Example:_

```js
const PromiseReadable = require('promise-readable')

const rstream = require('fs').createReadStream('/etc/hosts')
const stream = require('fs').createReadStream('/etc/hosts')

const promiseRstream = new PromiseReadable(rstream)
const promiseReadable = new PromiseReadable(stream)
```

Original stream is available with `stream` property:
#### stream

Original stream is available with `stream` property.

_Example:_

```js
console.log(promiseRstream.stream.flags)
console.log(promiseReadable.stream.flags)
```

#### read([chunkSize])

This method returns `Promise` which is fulfilled when stream can return one
chunk (by `read` method or `data` event) or stream is ended (`end` event).
#### read

```js
const chunk = await promiseRstream.read()
const chunk = await promiseReadable.read([chunkSize])
```

This method returns `Promise` which is fulfilled when stream can return one
chunk (by `read` method or `data` event) or stream is ended (`end` event).

If stream2 API is available then additional argument `size` is accepted.

_Example:_

```js
const chunk = await promiseRstream.read(1024)
const chunk = await promiseReadable.read(1024)
```

Promise returns chunk data if something has been read or `null` value if it is
an end of the stream.

_Example:_

```js
for (let chunk; (chunk = await promiseRstream.read()) !== null;) {
for (let chunk; (chunk = await promiseReadable.read()) !== null;) {
console.log(chunk.length)
}
console.log('stream is ended')
```

#### readAll()
#### readAll

```js
const content = await promiseReadable.readAll()
```

This method returns `Promise` which is fulfilled when stream is ended. The
content from the stream is buffered and then Promise returns this concatenated
content.

#### once

```js
const content = await promiseRstream.readAll()
const result = await promiseReadable.once(event)
```

#### once(event)

This method returns `Promise` which is fulfilled when stream emits `event`. The
result of this event is returned or `null` value if stream is already ended.

_Example:_

```js
const fd = await promiseRstream.once('open')
promiseRstream.stream.pipe(process.stdout)
const fd = await promiseReadable.once('open')
promiseReadable.stream.pipe(process.stdout)

await promiseRstream.once('close')
await promiseReadable.once('close')

promiseRstream.stream.on('data', chunk => console.log(chunk.length))
await promiseRstream.once('end')
promiseReadable.stream.on('data', chunk => console.log(chunk.length))
await promiseReadable.once('end')
```

### Promise
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "promise-readable",
"version": "0.4.1",
"version": "0.4.2",
"description": "Return promise for readable stream",
"main": "lib/promise-readable.js",
"repository": {
Expand Down

0 comments on commit b4b8412

Please sign in to comment.