Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-readable

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-readable - npm Package Compare versions

Comparing version 3.1.5 to 4.0.0

src/promise-readable.ts

6

CHANGELOG.md
# Changelog
## v4.0.0 2019-05-09
* Rewritten in Typescript.
* `PromiseReadable` accepts `NodeJS.ReadableStream`.
* Dropped support for Node < 6.
## v3.1.5 2018-03-12

@@ -4,0 +10,0 @@

37

lib/promise-readable.d.ts

@@ -1,21 +0,18 @@

/// <reference types="node" />
import { Readable } from 'stream'
export declare class PromiseReadable<TReadable extends Readable> {
readonly stream: TReadable
constructor (stream: TReadable)
read (size?: number): Promise<Buffer | string | undefined>
readAll (): Promise<Buffer | string | undefined>
setEncoding (encoding: string): this
destroy (): void
once (event: 'close' | 'end' | 'error'): Promise<void>
once (event: 'open'): Promise<number>
interface ReadableStream extends NodeJS.ReadableStream {
closed?: boolean;
destroyed?: boolean;
destroy?(): void;
}
export default PromiseReadable
export declare class PromiseReadable<TReadable extends ReadableStream> {
readonly stream: TReadable;
private errorHandler;
private errored?;
constructor(stream: TReadable);
read(size?: number): Promise<Buffer | string | undefined>;
readAll(): Promise<Buffer | string | undefined>;
setEncoding(encoding: string): this;
destroy(): void;
once(event: 'close' | 'end' | 'error'): Promise<void>;
once(event: 'open'): Promise<number>;
}
export default PromiseReadable;

@@ -1,209 +0,190 @@

'use strict'
"use strict";
/// <reference types="node" />
Object.defineProperty(exports, "__esModule", { value: true });
class PromiseReadable {
constructor (stream) {
this.stream = stream
this._errorHandler = (err) => {
this._errored = err
constructor(stream) {
this.stream = stream;
this.errorHandler = (err) => {
this.errored = err;
};
stream.on('error', this.errorHandler);
}
stream.on('error', this._errorHandler)
}
read (size) {
const stream = this.stream
return new Promise((resolve, reject) => {
if (this._errored) {
const err = this._errored
delete this._errored
return reject(err)
}
if (!stream.readable || stream.closed || stream.destroyed) {
return resolve()
}
const readableHandler = () => {
let chunk = stream.read(size)
if (chunk != null) {
removeListeners()
resolve(chunk)
read(size) {
const stream = this.stream;
return new Promise((resolve, reject) => {
if (this.errored) {
const err = this.errored;
this.errored = undefined;
return reject(err);
}
if (!stream.readable || stream.closed || stream.destroyed) {
return resolve();
}
const readableHandler = () => {
const chunk = stream.read(size);
if (chunk) {
removeListeners();
resolve(chunk);
}
};
const closeHandler = () => {
removeListeners();
resolve();
};
const endHandler = () => {
removeListeners();
resolve();
};
const errorHandler = (err) => {
this.errored = undefined;
removeListeners();
reject(err);
};
const removeListeners = () => {
stream.removeListener('close', closeHandler);
stream.removeListener('error', errorHandler);
stream.removeListener('end', endHandler);
stream.removeListener('readable', readableHandler);
};
stream.on('close', closeHandler);
stream.on('end', endHandler);
stream.on('error', errorHandler);
stream.on('readable', readableHandler);
readableHandler();
});
}
readAll() {
const stream = this.stream;
const bufferArray = [];
let content = '';
return new Promise((resolve, reject) => {
if (this.errored) {
const err = this.errored;
this.errored = undefined;
return reject(err);
}
if (!stream.readable || stream.closed || stream.destroyed) {
return resolve();
}
const dataHandler = (chunk) => {
if (typeof chunk === 'string') {
content += chunk;
}
else {
bufferArray.push(chunk);
}
};
const closeHandler = () => {
removeListeners();
resolve();
};
const endHandler = () => {
removeListeners();
if (bufferArray.length) {
resolve(Buffer.concat(bufferArray));
}
else {
resolve(content);
}
};
const errorHandler = (err) => {
this.errored = undefined;
removeListeners();
reject(err);
};
const removeListeners = () => {
stream.removeListener('close', closeHandler);
stream.removeListener('data', dataHandler);
stream.removeListener('error', errorHandler);
stream.removeListener('end', endHandler);
};
stream.on('close', closeHandler);
stream.on('data', dataHandler);
stream.on('end', endHandler);
stream.on('error', errorHandler);
stream.resume();
});
}
setEncoding(encoding) {
this.stream.setEncoding(encoding);
return this;
}
destroy() {
if (this.stream) {
if (this.errorHandler) {
this.stream.removeListener('error', this.errorHandler);
delete this.errorHandler;
}
if (typeof this.stream.destroy === 'function') {
this.stream.destroy();
}
}
}
const closeHandler = () => {
removeListeners()
resolve(stream.bytesWritten || 0)
}
const endHandler = () => {
removeListeners()
resolve()
}
const errorHandler = (err) => {
delete this._errored
removeListeners()
reject(err)
}
const removeListeners = () => {
stream.removeListener('close', closeHandler)
stream.removeListener('error', errorHandler)
stream.removeListener('end', endHandler)
stream.removeListener('readable', readableHandler)
}
stream.on('close', closeHandler)
stream.on('end', endHandler)
stream.on('error', errorHandler)
stream.on('readable', readableHandler)
readableHandler()
})
}
readAll () {
const stream = this.stream
const bufferArray = []
return new Promise((resolve, reject) => {
if (this._errored) {
const err = this._errored
delete this._errored
return reject(err)
}
if (!stream.readable || stream.closed || stream.destroyed) {
return resolve()
}
const dataHandler = (chunk) => {
bufferArray.push(chunk)
}
const closeHandler = () => {
removeListeners()
resolve(stream.bytesWritten || 0)
}
const endHandler = () => {
removeListeners()
resolve(Buffer.concat(bufferArray))
}
const errorHandler = (err) => {
delete this._errored
removeListeners()
reject(err)
}
const removeListeners = () => {
stream.removeListener('close', closeHandler)
stream.removeListener('data', dataHandler)
stream.removeListener('error', errorHandler)
stream.removeListener('end', endHandler)
}
stream.on('close', closeHandler)
stream.on('data', dataHandler)
stream.on('end', endHandler)
stream.on('error', errorHandler)
stream.resume()
})
}
once (event) {
const stream = this.stream
return new Promise((resolve, reject) => {
if (this._errored) {
const err = this._errored
delete this._errored
return reject(err)
}
if (stream.closed) {
if (event === 'close') {
return resolve()
} else {
return reject(new Error(`once ${event} after close`))
}
} else if (stream.destroyed) {
if (event === 'close' || event === 'end') {
return resolve()
} else {
return reject(new Error(`once ${event} after destroy`))
}
}
const closeHandler = () => {
removeListeners()
resolve()
}
const eventHandler = event !== 'close' && event !== 'end' && event !== 'error' ? (argument) => {
removeListeners()
resolve(argument)
} : undefined
const endHandler = event !== 'close' ? () => {
removeListeners()
resolve()
} : undefined
const errorHandler = (err) => {
delete this._errored
removeListeners()
reject(err)
}
const removeListeners = () => {
if (eventHandler) {
stream.removeListener(event, eventHandler)
}
stream.removeListener('error', errorHandler)
if (endHandler) {
stream.removeListener('end', endHandler)
}
stream.removeListener('error', errorHandler)
}
if (eventHandler) {
stream.on(event, eventHandler)
}
stream.on('close', closeHandler)
if (endHandler) {
stream.on('end', endHandler)
}
stream.on('error', errorHandler)
})
}
setEncoding (encoding) {
this.stream.setEncoding(encoding)
return this
}
destroy () {
if (this.stream) {
if (this._errorHandler) {
this.stream.removeListener('error', this._errorHandler)
delete this._errorHandler
}
if (typeof this.stream.destroy === 'function') {
this.stream.destroy()
}
delete this.stream
}
}
once(event) {
const stream = this.stream;
return new Promise((resolve, reject) => {
if (this.errored) {
const err = this.errored;
this.errored = undefined;
return reject(err);
}
if (stream.closed) {
if (event === 'close') {
return resolve();
}
else {
return reject(new Error(`once ${event} after close`));
}
}
else if (stream.destroyed) {
if (event === 'close' || event === 'end') {
return resolve();
}
else {
return reject(new Error(`once ${event} after destroy`));
}
}
const closeHandler = () => {
removeListeners();
resolve();
};
const eventHandler = event !== 'close' && event !== 'end' && event !== 'error'
? (argument) => {
removeListeners();
resolve(argument);
}
: undefined;
const endHandler = event !== 'close'
? () => {
removeListeners();
resolve();
}
: undefined;
const errorHandler = (err) => {
this.errored = undefined;
removeListeners();
reject(err);
};
const removeListeners = () => {
if (eventHandler) {
stream.removeListener(event, eventHandler);
}
stream.removeListener('error', errorHandler);
if (endHandler) {
stream.removeListener('end', endHandler);
}
stream.removeListener('error', errorHandler);
};
if (eventHandler) {
stream.on(event, eventHandler);
}
stream.on('close', closeHandler);
if (endHandler) {
stream.on('end', endHandler);
}
stream.on('error', errorHandler);
});
}
}
PromiseReadable.PromiseReadable = PromiseReadable
PromiseReadable.default = PromiseReadable
module.exports = PromiseReadable
exports.PromiseReadable = PromiseReadable;
exports.default = PromiseReadable;
{
"name": "promise-readable",
"version": "3.1.5",
"version": "4.0.0",
"description": "Return promise for readable stream",

@@ -23,15 +23,23 @@ "main": "lib/promise-readable.js",

"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"dependencies": {},
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.6",
"@types/node": "^9.4.7",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"markdownlint-cli": "^0.7.1",
"snazzy": "^7.1.1",
"standard": "^11.0.0",
"tap": "^11.1.2",
"tap-given": "^0.6.0",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-node": "^9.0.1",
"markdownlint-cli": "^0.15.0",
"mocha": "^6.1.4",
"nyc": "^14.1.0",
"prettier": "^1.17.0",
"shx": "^0.3.2",
"ts-node": "^8.1.0",
"tslint": "^5.9.1",
"tslint-config-prettier": "^1.18.0",
"tslint-config-standard": "^7.0.0",

@@ -41,21 +49,22 @@ "typescript": "^2.7.2"

"scripts": {
"pretest": "standard --verbose | snazzy && tsc --noEmit --pretty && tslint -t stylish -p . && markdownlint *.md",
"test": "tap test/*.js",
"test:coverage": "npm test -- --coverage",
"test:onchange:lcovonly": "npm run -s test-coverage -- -R min --coverage-report lcovonly; onchange '**/*.js' -- npm run -s test-coverage -- -R min --coverage-report lcovonly"
"build": "npm run compile",
"compile": "tsc --pretty",
"clean": "shx rm -rf lib",
"postpublish": "git tag v$npm_package_version -a -m \"Release v$npm_package_version\" && git push --tags",
"prepublishOnly": "npm run build",
"pretest": "npm run build && 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' '**/*.ts' '**/*.yml' && markdownlint \"*.md\"",
"test": "npm run test:spec",
"test:spec": "npm run ts-mocha -- \"test/*.ts\"",
"test:coverage": "nyc --reporter json npm run test:spec && nyc report",
"ts-mocha": "mocha --use_strict --throw-deprecation --require source-map-support/register --require ts-node/register --timeout 90000"
},
"standard": {
"globals": [
"After",
"And",
"Feature",
"Given",
"Scenario",
"Then",
"When"
"nyc": {
"extension": [
".ts"
],
"exclude": [
".*.js",
"**/*.d.ts"
]
},
"nyc": {
"exclude": []
}
}

@@ -20,3 +20,3 @@ # promise-readable

This module requires Node >= 4.
This module requires Node >= 6.

@@ -29,2 +29,8 @@ ## Installation

_Additionally for Typescript:_
```shell
npm install -D @types/node
```
## Usage

@@ -44,3 +50,3 @@

const fs = require('fs')
const PromiseReadable = require('promise-readable')
const {PromiseReadable} = require('promise-readable')

@@ -181,4 +187,4 @@ const stream = fs.createReadStream('/etc/hosts')

Copyright (c) 2017-2018 Piotr Roszatycki <mailto:piotr.roszatycki@gmail.com>
Copyright (c) 2017-2019 Piotr Roszatycki <mailto:piotr.roszatycki@gmail.com>
[MIT](https://opensource.org/licenses/MIT)
{
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,
"importHelpers": true,
"lib": ["es6"],
"module": "commonjs",
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./lib",
"target": "es6",
"strict": true,
"target": "ES2017"
}
"typeRoots": ["node_modules/@types"]
},
"exclude": ["./examples/**/*", "./lib/**/*", "./test/**/*"]
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc