then-read-stream
Advanced tools
Comparing version 1.2.1 to 1.3.0
/// <reference types="node" /> | ||
import { Promise } from "es6-promise"; | ||
import * as stream from "stream"; | ||
@@ -4,0 +3,0 @@ /** |
112
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var assert = require("assert"); | ||
var es6_promise_1 = require("es6-promise"); | ||
var Deferred = /** @class */ (function () { | ||
function Deferred() { | ||
var _this = this; | ||
this.promise = new es6_promise_1.Promise(function (resolve, reject) { | ||
_this.reject = reject; | ||
_this.resolve = resolve; | ||
const assert = require("assert"); | ||
class Deferred { | ||
constructor() { | ||
this.promise = new Promise((resolve, reject) => { | ||
this.reject = reject; | ||
this.resolve = resolve; | ||
}); | ||
} | ||
return Deferred; | ||
}()); | ||
} | ||
/** | ||
@@ -19,5 +16,4 @@ * Error message | ||
exports.endOfStream = "End-Of-Stream"; | ||
var StreamReader = /** @class */ (function () { | ||
function StreamReader(s) { | ||
var _this = this; | ||
class StreamReader { | ||
constructor(s) { | ||
this.s = s; | ||
@@ -33,7 +29,7 @@ this.endOfStream = false; | ||
} | ||
this.s.once("end", function () { | ||
_this.endOfStream = true; | ||
if (_this.request) { | ||
_this.request.deferred.reject(new Error(exports.endOfStream)); | ||
_this.request = null; | ||
this.s.once("end", () => { | ||
this.endOfStream = true; | ||
if (this.request) { | ||
this.request.deferred.reject(new Error(exports.endOfStream)); | ||
this.request = null; | ||
} | ||
@@ -50,9 +46,8 @@ }); | ||
*/ | ||
StreamReader.prototype.peek = function (buffer, offset, length) { | ||
var _this = this; | ||
return this.read(buffer, offset, length).then(function (bytesRead) { | ||
_this.peekQueue.unshift(buffer.slice(offset, bytesRead)); | ||
peek(buffer, offset, length) { | ||
return this.read(buffer, offset, length).then(bytesRead => { | ||
this.peekQueue.unshift(buffer.slice(offset, bytesRead)); | ||
return bytesRead; | ||
}); | ||
}; | ||
} | ||
/** | ||
@@ -65,22 +60,22 @@ * Read chunk from stream | ||
*/ | ||
StreamReader.prototype.read = function (buffer, offset, length) { | ||
read(buffer, offset, length) { | ||
if (length === 0) { | ||
return es6_promise_1.Promise.resolve(0); | ||
return Promise.resolve(0); | ||
} | ||
if (this.peekQueue.length > 0) { | ||
var peekData_1 = this.peekQueue.shift(); | ||
if (length <= peekData_1.length) { | ||
peekData_1.copy(buffer, offset, 0, length); | ||
if (length < peekData_1.length) { | ||
this.peekQueue.unshift(peekData_1.slice(length)); | ||
const peekData = this.peekQueue.shift(); | ||
if (length <= peekData.length) { | ||
peekData.copy(buffer, offset, 0, length); | ||
if (length < peekData.length) { | ||
this.peekQueue.unshift(peekData.slice(length)); | ||
} | ||
return es6_promise_1.Promise.resolve(length); | ||
return Promise.resolve(length); | ||
} | ||
else { | ||
peekData_1.copy(buffer, offset); | ||
return this.read(buffer, offset + peekData_1.length, length - peekData_1.length).then(function (bytesRead) { | ||
return peekData_1.length + bytesRead; | ||
}).catch(function (err) { | ||
peekData.copy(buffer, offset); | ||
return this.read(buffer, offset + peekData.length, length - peekData.length).then(bytesRead => { | ||
return peekData.length + bytesRead; | ||
}).catch(err => { | ||
if (err.message === exports.endOfStream) { | ||
return peekData_1.length; // Return partial read | ||
return peekData.length; // Return partial read | ||
} | ||
@@ -95,3 +90,3 @@ else | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -104,35 +99,33 @@ * Read chunk from stream | ||
*/ | ||
StreamReader.prototype._read = function (buffer, offset, length) { | ||
var _this = this; | ||
_read(buffer, offset, length) { | ||
assert.ok(!this.request, "Concurrent read operation?"); | ||
if (this.endOfStream) { | ||
return es6_promise_1.Promise.reject(new Error(exports.endOfStream)); | ||
return Promise.reject(new Error(exports.endOfStream)); | ||
} | ||
var readBuffer = this.s.read(length); | ||
const readBuffer = this.s.read(length); | ||
if (readBuffer) { | ||
readBuffer.copy(buffer, offset); | ||
return es6_promise_1.Promise.resolve(readBuffer.length); | ||
return Promise.resolve(readBuffer.length); | ||
} | ||
else { | ||
this.request = { | ||
buffer: buffer, | ||
offset: offset, | ||
length: length, | ||
buffer, | ||
offset, | ||
length, | ||
deferred: new Deferred() | ||
}; | ||
this.s.once("readable", function () { | ||
_this.tryRead(); | ||
this.s.once("readable", () => { | ||
this.tryRead(); | ||
}); | ||
return this.request.deferred.promise.then(function (n) { | ||
_this.request = null; | ||
return this.request.deferred.promise.then(n => { | ||
this.request = null; | ||
return n; | ||
}).catch(function (err) { | ||
_this.request = null; | ||
}).catch(err => { | ||
this.request = null; | ||
throw err; | ||
}); | ||
} | ||
}; | ||
StreamReader.prototype.tryRead = function () { | ||
var _this = this; | ||
var readBuffer = this.s.read(this.request.length); | ||
} | ||
tryRead() { | ||
const readBuffer = this.s.read(this.request.length); | ||
if (readBuffer) { | ||
@@ -143,9 +136,8 @@ readBuffer.copy(this.request.buffer, this.request.offset); | ||
else { | ||
this.s.once("readable", function () { | ||
_this.tryRead(); | ||
this.s.once("readable", () => { | ||
this.tryRead(); | ||
}); | ||
} | ||
}; | ||
return StreamReader; | ||
}()); | ||
} | ||
} | ||
exports.StreamReader = StreamReader; |
{ | ||
"name": "then-read-stream", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Read from a readable stream just like a file", | ||
@@ -12,9 +12,10 @@ "author": { | ||
"compile-test": "tsc -p test", | ||
"compile": "npm run compile-src && npm run compile-test", | ||
"prepare": "npm run compile", | ||
"build": "yarn run compile", | ||
"compile": "yarn run compile-src && yarn run compile-test", | ||
"prepare": "yarn run compile", | ||
"lint": "tslint 'src/**/*.ts' --exclude 'src/**/*.d.ts' 'test/**/*.ts' --exclude 'test/**/*.d.ts'", | ||
"test": "mocha --require ts-node/register test", | ||
"cover-test": "nyc npm run test", | ||
"coveralls": "npm run cover-test && nyc report --reporter=text-lcov | coveralls", | ||
"start": "npm run compile && npm run lint && npm run cover-test" | ||
"coveralls": "yarn run cover-test && nyc report --reporter=text-lcov | coveralls", | ||
"start": "yarn run compile && yarn run lint && npm run cover-test" | ||
}, | ||
@@ -36,3 +37,3 @@ "engines": { | ||
"@types/mocha": "^5.2.3", | ||
"@types/node": "^9.6.22", | ||
"@types/node": "10.9.4", | ||
"chai": "^4.1.2", | ||
@@ -43,9 +44,6 @@ "coveralls": "^3.0.1", | ||
"nyc": "^13.0.0", | ||
"ts-node": "^6.2.0", | ||
"ts-node": "7", | ||
"tslint": "^5.10.0", | ||
"typescript": "^2.9.2" | ||
"typescript": "3.0.3" | ||
}, | ||
"dependencies": { | ||
"es6-promise": "^4.2.4" | ||
}, | ||
"keywords": [ | ||
@@ -52,0 +50,0 @@ "readable", |
@@ -15,7 +15,7 @@ [![Build Status](https://travis-ci.org/Borewit/then-read-stream.svg?branch=master)](https://travis-ci.org/Borewit/then-read-stream) | ||
The `then-read-stream` contains one class: `StreamReader`. The constructor of | ||
the `StreamReader` if provided with the [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable) | ||
The `then-read-stream` contains one class: `StreamReader`. | ||
`StreamReader` iis constructed with the [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable) | ||
you want to read from. | ||
##### Examles: | ||
##### Examples: | ||
@@ -22,0 +22,0 @@ In the following example we read the first 16 bytes from a stream and store them in our buffer. |
Sorry, the diff of this file is not supported yet
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
0
72827
5641
231
- Removedes6-promise@^4.2.4
- Removedes6-promise@4.2.8(transitive)