Skip to content

Commit

Permalink
No support for streams v1; new method destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Feb 3, 2018
1 parent 383c5ca commit 7d80dbe
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 370 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v3.0.0 2018-02-03

* No support for streams v1.
* New method `destroy`.
* Bugfix when `PromiseReadable` could ignore `error` event.

## v2.1.1 2018-01-18

* `readAll` resumes the stream.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Piotr Roszatycki <piotr.roszatycki@gmail.com>
Copyright (c) 2017-2018 Piotr Roszatycki <piotr.roszatycki@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,16 @@ await promiseReadable.once('end')
await promiseReadable.once('error') // undefined if already ended or throws error
```

#### destroy

```js
promiseReadable.destroy()
```

This method calls `destroy` method on stream and cleans up all own handlers.

### License

Copyright (c) 2017 Piotr Roszatycki <piotr.roszatycki@gmail.com>
Copyright (c) 2017-2018 Piotr Roszatycki <piotr.roszatycki@gmail.com>

[MIT](https://opensource.org/licenses/MIT)
1 change: 1 addition & 0 deletions examples/read-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async function main () {
const data = await rstream.readAll()
if (data != null) {
console.log(`Read ${data.length} bytes in total`)
rstream.destroy()
}
}

Expand Down
6 changes: 4 additions & 2 deletions examples/read-by-chunks-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { PromiseReadable } from '../lib/promise-readable'

import { createReadStream } from 'fs'

type Maybe<T> = T | undefined
type Chunk = Buffer | undefined

async function main () {
const rstream = new PromiseReadable(createReadStream(process.argv[2] || '/etc/hosts'))

let total = 0

for (let chunk: Maybe<Buffer>; (chunk = await rstream.read());) {
for (let chunk: Chunk; (chunk = await rstream.read());) {
console.info(`Read ${chunk.length} bytes chunk`)
total += chunk.length
}

console.info(`Read ${total} bytes in total`)

rstream.destroy()
}

main().catch(console.error)
5 changes: 5 additions & 0 deletions examples/read-by-chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ const { createReadStream } = require('fs')

async function main () {
const rstream = new PromiseReadable(createReadStream(process.argv[2] || '/etc/hosts'))

let total = 0

for (let chunk; (chunk = await rstream.read());) {
console.log(`Read ${chunk.length} bytes chunk`)
total += chunk.length
}

console.log(`Read ${total} bytes in total`)

rstream.destroy()
}

main().catch(console.error)
9 changes: 3 additions & 6 deletions examples/read-on-data-event.js → examples/read-once-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ var PromiseReadable = require('../lib/promise-readable').PromiseReadable
var createReadStream = require('fs').createReadStream

var rstream = new PromiseReadable(createReadStream(process.argv[2] || '/etc/hosts'))
var total = 0

rstream.stream.on('data', function (chunk) {
console.log('Read', chunk.length, 'bytes chunk')
total += chunk.length
})
rstream.stream.pipe(process.stdout)

rstream.once('end')
.then(function () {
console.log('Read', total, 'bytes in total')
console.log('-- End of file')
rstream.destroy()
})
2 changes: 2 additions & 0 deletions lib/promise-readable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export declare class PromiseReadable<TReadable extends Readable> {
read (size?: number): Promise<Buffer | undefined>
readAll (): Promise<Buffer | undefined>

destroy (): void

once (event: 'close' | 'end' | 'error'): Promise<void>
once (event: 'open'): Promise<number>
}
Expand Down
Loading

0 comments on commit 7d80dbe

Please sign in to comment.