promise-readable
Advanced tools
Comparing version 4.2.1 to 5.0.0
# Changelog | ||
## v5.0.0 2019-07-14 | ||
- `PromiseReadable` implements `AsyncIterable` so it is possible to use | ||
`for await (const chunk of promiseReadable)` loop. | ||
- New method `iterate` is provided. | ||
## v4.2.1 2019-06-04 | ||
@@ -4,0 +10,0 @@ |
/// <reference types="node" /> | ||
import "core-js/modules/es.symbol.async-iterator"; | ||
interface ReadableStream extends NodeJS.ReadableStream { | ||
@@ -7,3 +8,3 @@ closed?: boolean; | ||
} | ||
export declare class PromiseReadable<TReadable extends ReadableStream> { | ||
export declare class PromiseReadable<TReadable extends ReadableStream> implements AsyncIterable<Buffer | string> { | ||
readonly stream: TReadable; | ||
@@ -13,3 +14,2 @@ static [Symbol.hasInstance](instance: any): boolean; | ||
_errored?: Error; | ||
private readonly errorHandler; | ||
constructor(stream: TReadable); | ||
@@ -19,6 +19,9 @@ read(size?: number): Promise<Buffer | string | undefined>; | ||
setEncoding(encoding: string): this; | ||
destroy(): void; | ||
once(event: "close" | "end" | "error"): Promise<void>; | ||
once(event: "open"): Promise<number>; | ||
iterate(size?: number): AsyncIterableIterator<Buffer | string>; | ||
[Symbol.asyncIterator](): AsyncIterableIterator<Buffer | string>; | ||
destroy(): void; | ||
private readonly errorHandler; | ||
} | ||
export default PromiseReadable; |
"use strict"; | ||
/// <reference types="node" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("core-js/modules/es.symbol.async-iterator"); | ||
class PromiseReadable { | ||
@@ -116,12 +117,2 @@ constructor(stream) { | ||
} | ||
destroy() { | ||
if (this.stream) { | ||
if (this.errorHandler) { | ||
this.stream.removeListener("error", this.errorHandler); | ||
} | ||
if (typeof this.stream.destroy === "function") { | ||
this.stream.destroy(); | ||
} | ||
} | ||
} | ||
once(event) { | ||
@@ -192,2 +183,38 @@ const stream = this.stream; | ||
} | ||
iterate(size) { | ||
const promiseReadable = this; | ||
let wasEof = false; | ||
return { | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
next() { | ||
if (wasEof) { | ||
return Promise.resolve({ value: "", done: true }); | ||
} | ||
else { | ||
return promiseReadable.read(size).then(value => { | ||
if (value === undefined) { | ||
wasEof = true; | ||
return { value: "", done: true }; | ||
} | ||
else { | ||
return { value, done: false }; | ||
} | ||
}); | ||
} | ||
}, | ||
}; | ||
} | ||
[Symbol.asyncIterator]() { | ||
return this.iterate(); | ||
} | ||
destroy() { | ||
if (this.stream) { | ||
this.stream.removeListener("error", this.errorHandler); | ||
if (typeof this.stream.destroy === "function") { | ||
this.stream.destroy(); | ||
} | ||
} | ||
} | ||
} | ||
@@ -194,0 +221,0 @@ exports.PromiseReadable = PromiseReadable; |
{ | ||
"name": "promise-readable", | ||
"version": "4.2.1", | ||
"version": "5.0.0", | ||
"description": "Return promise for readable stream", | ||
@@ -25,25 +25,30 @@ "main": "lib/promise-readable.js", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"core-js": "^3.1.4" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.1.7", | ||
"@types/dirty-chai": "^2.0.0", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^12.0.4", | ||
"@types/semver": "^6.0.0", | ||
"@types/node": "^12.6.2", | ||
"@types/semver": "^6.0.1", | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.0.3", | ||
"changelog-parser": "^2.8.0", | ||
"coveralls": "^3.0.5", | ||
"cross-env": "^5.2.0", | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^4.3.0", | ||
"eslint-plugin-import": "^2.17.3", | ||
"dirty-chai": "^2.0.1", | ||
"eslint": "^6.0.1", | ||
"eslint-config-prettier": "^6.0.0", | ||
"eslint-plugin-import": "^2.18.0", | ||
"eslint-plugin-node": "^9.1.0", | ||
"markdownlint-cli": "^0.16.0", | ||
"markdownlint-cli": "^0.17.0", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1", | ||
"prettier": "^1.17.1", | ||
"semver": "^6.1.1", | ||
"prettier": "^1.18.2", | ||
"semver": "^6.2.0", | ||
"shx": "^0.3.2", | ||
"ts-node": "^8.2.0", | ||
"tslint": "^5.17.0", | ||
"ts-node": "^8.3.0", | ||
"tslint": "^5.18.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.5.1" | ||
"typescript": "^3.5.3" | ||
}, | ||
@@ -56,3 +61,3 @@ "scripts": { | ||
"lint": "npm run compile && tsc --pretty -p examples && tsc --pretty -p test && eslint . && tslint -t stylish -p . && tslint -t stylish -p examples && tslint -t stylish -p test && prettier --ignore-path .gitignore --list-different '**/*.{js,json,md,ts,yml}' && markdownlint \"*.md\"", | ||
"postpublish": "git tag v$npm_package_version -a -m \"Release v$npm_package_version\" && git push --tags", | ||
"postpublish": "node -e \"require(\\\"changelog-parser\\\")(\\\"CHANGELOG.md\\\").then(ch => console.log(ch.versions.filter(v => v.version === \\\"$npm_package_version\\\").map(v => v.body).concat(\\\"Release v$npm_package_version\\\")[0]))\" | xargs -0 git tag v$npm_package_version -a -m && git push --tags", | ||
"prepack": "npm run compile", | ||
@@ -59,0 +64,0 @@ "prepublishOnly": "npm run test", |
@@ -20,2 +20,6 @@ # promise-readable | ||
The promisified stream provides async iterator so it is possible to use it with | ||
[`for await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) | ||
statement. | ||
## Requirements | ||
@@ -182,2 +186,20 @@ | ||
### iterate | ||
```js | ||
for await (const chunk of promiseReadable.iterate(chunkSize)) {} | ||
``` | ||
This method returns async iterator which returns a content of the stream | ||
chunk-by-chunk with defined `chunkSize`. | ||
### async iterator | ||
```js | ||
for await (const chunk of promiseReadable) {} | ||
``` | ||
The `PromiseReadable` object is an async iterator which returns a content of | ||
the stream chunk-by-chunk with the default `chunkSize`. | ||
### destroy | ||
@@ -184,0 +206,0 @@ |
/// <reference types="node" /> | ||
import "core-js/modules/es.symbol.async-iterator" | ||
interface ReadableStream extends NodeJS.ReadableStream { | ||
@@ -10,3 +12,3 @@ closed?: boolean | ||
export class PromiseReadable<TReadable extends ReadableStream> { | ||
export class PromiseReadable<TReadable extends ReadableStream> implements AsyncIterable<Buffer | string> { | ||
static [Symbol.hasInstance](instance: any): boolean { | ||
@@ -20,9 +22,3 @@ return instance._isPromiseReadable | ||
private readonly errorHandler: (err: Error) => void | ||
constructor(readonly stream: TReadable) { | ||
this.errorHandler = (err: Error) => { | ||
this._errored = err | ||
} | ||
stream.on("error", this.errorHandler) | ||
@@ -152,13 +148,2 @@ } | ||
destroy(): void { | ||
if (this.stream) { | ||
if (this.errorHandler) { | ||
this.stream.removeListener("error", this.errorHandler) | ||
} | ||
if (typeof this.stream.destroy === "function") { | ||
this.stream.destroy() | ||
} | ||
} | ||
} | ||
once(event: "close" | "end" | "error"): Promise<void> | ||
@@ -245,4 +230,48 @@ once(event: "open"): Promise<number> | ||
} | ||
iterate(size?: number): AsyncIterableIterator<Buffer | string> { | ||
const promiseReadable = this | ||
let wasEof = false | ||
return { | ||
[Symbol.asyncIterator](): AsyncIterableIterator<Buffer | string> { | ||
return this | ||
}, | ||
next(): Promise<IteratorResult<Buffer | string>> { | ||
if (wasEof) { | ||
return Promise.resolve({value: "", done: true}) | ||
} else { | ||
return promiseReadable.read(size).then(value => { | ||
if (value === undefined) { | ||
wasEof = true | ||
return {value: "", done: true} | ||
} else { | ||
return {value, done: false} | ||
} | ||
}) | ||
} | ||
}, | ||
} | ||
} | ||
[Symbol.asyncIterator](): AsyncIterableIterator<Buffer | string> { | ||
return this.iterate() | ||
} | ||
destroy(): void { | ||
if (this.stream) { | ||
this.stream.removeListener("error", this.errorHandler) | ||
if (typeof this.stream.destroy === "function") { | ||
this.stream.destroy() | ||
} | ||
} | ||
} | ||
private readonly errorHandler = (err: Error): void => { | ||
this._errored = err | ||
} | ||
} | ||
export default PromiseReadable |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34005
483
223
1
24
+ Addedcore-js@^3.1.4
+ Addedcore-js@3.39.0(transitive)