From 0f79e708bf07721b73ea41e5d341be08e8ea4dce Mon Sep 17 00:00:00 2001 From: Minteck Date: Sun, 7 Mar 2021 18:29:17 +0100 Subject: Initial commit --- node_modules/peek-readable/LICENSE | 21 ++++ node_modules/peek-readable/README.md | 104 ++++++++++++++++ .../peek-readable/lib/EndOfFileStream.d.ts | 7 ++ node_modules/peek-readable/lib/EndOfFileStream.js | 13 ++ node_modules/peek-readable/lib/index.d.ts | 43 +++++++ node_modules/peek-readable/lib/index.js | 137 +++++++++++++++++++++ node_modules/peek-readable/package.json | 106 ++++++++++++++++ 7 files changed, 431 insertions(+) create mode 100644 node_modules/peek-readable/LICENSE create mode 100644 node_modules/peek-readable/README.md create mode 100644 node_modules/peek-readable/lib/EndOfFileStream.d.ts create mode 100644 node_modules/peek-readable/lib/EndOfFileStream.js create mode 100644 node_modules/peek-readable/lib/index.d.ts create mode 100644 node_modules/peek-readable/lib/index.js create mode 100644 node_modules/peek-readable/package.json (limited to 'node_modules/peek-readable') diff --git a/node_modules/peek-readable/LICENSE b/node_modules/peek-readable/LICENSE new file mode 100644 index 0000000..9ba5f03 --- /dev/null +++ b/node_modules/peek-readable/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2010-2017 Borewit + +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 the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/node_modules/peek-readable/README.md b/node_modules/peek-readable/README.md new file mode 100644 index 0000000..6996d05 --- /dev/null +++ b/node_modules/peek-readable/README.md @@ -0,0 +1,104 @@ +![Node.js CI](https://github.com/Borewit/peek-readable/workflows/Node.js%20CI/badge.svg) +[![NPM version](https://badge.fury.io/js/peek-readable.svg)](https://npmjs.org/package/peek-readable) +[![npm downloads](http://img.shields.io/npm/dm/peek-readable.svg)](https://npmcharts.com/compare/peek-readable?start=600&interval=30) +[![Coverage Status](https://coveralls.io/repos/github/Borewit/peek-readable/badge.svg?branch=master)](https://coveralls.io/github/Borewit/peek-readable?branch=master) +[![Codacy Badge](https://api.codacy.com/project/badge/Grade/8a89b90858734a6da07570eaf2e89849)](https://www.codacy.com/app/Borewit/peek-readable?utm_source=github.com&utm_medium=referral&utm_content=Borewit/peek-readable&utm_campaign=Badge_Grade) +[![Total alerts](https://img.shields.io/lgtm/alerts/g/Borewit/peek-readable.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borewit/peek-readable/alerts/) +[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Borewit/peek-readable.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Borewit/peek-readable/context:javascript) +[![Known Vulnerabilities](https://snyk.io/test/github/Borewit/peek-readable/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Borewit/peek-readable?targetFile=package.json) + +# peek-readable + +A promise based asynchronous stream reader, which makes reading from a stream easy. + +Allows to read and peek from a [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams) + +Note that [peek-readable](https://github.com/Borewit/peek-readable) was formally released as [then-read-stream](https://github.com/Borewit/peek-readable). + +## Usage + +### Installation + +```shell script +npm install --save peek-readable +``` + +The `peek-readable` contains one class: `StreamReader`, which reads from a [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable). + +### Compatibility + +NPM module is compliant with [ECMAScript 2017 (ES8)](https://en.wikipedia.org/wiki/ECMAScript#8th_Edition_-_ECMAScript_2017). + +## Examples + +In the following example we read the first 16 bytes from a stream and store them in our buffer. +Source code of examples can be found [here](test/examples.ts). + +```js +const fs = require('fs'); +const { StreamReader } = require('peek-readable'); + +(async () => { + + const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg'); + const streamReader = new StreamReader(fileReadStream); + const buffer = Buffer.alloc(16); + + const bytesRead = await streamReader.read(buffer, 0, 16); + // buffer contains 16 bytes, if the end-of-stream has not been reached +})(); +``` + +End-of-stream detection: +```js +(async () => { + + const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg'); + const streamReader = new StreamReader(fileReadStream); + const buffer = Buffer.alloc(16); + + try { + await streamReader.read(buffer, 0, 16); + // buffer contains 16 bytes, if the end-of-stream has not been reached + } catch(error) { + if (error instanceof EndOfStreamError) { + console.log('End-of-stream reached'); + } + } +})(); +``` + +With peek you can read ahead: +```js +const fs = require('fs'); +const { StreamReader } = require('peek-readable'); + +const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg'); +const streamReader = new StreamReader(fileReadStream); +const buffer = Buffer.alloc(20); + +(async () => { + let bytesRead = await streamReader.peek(buffer, 0, 3); + if (bytesRead === 3 && buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) { + console.log('This is a JPEG file'); + } else { + throw Error('Expected a JPEG file'); + } + + bytesRead = await streamReader.read(buffer, 0, 20); // Read JPEG header + if (bytesRead === 20) { + console.log('Got the JPEG header'); + } else { + throw Error('Failed to read JPEG header'); + } +})(); +``` + +If you have to skip a part of the data, you can use ignore: +```js +(async () => { + //... + await streamReader.ignore(16); +})(); +``` + diff --git a/node_modules/peek-readable/lib/EndOfFileStream.d.ts b/node_modules/peek-readable/lib/EndOfFileStream.d.ts new file mode 100644 index 0000000..1081cb0 --- /dev/null +++ b/node_modules/peek-readable/lib/EndOfFileStream.d.ts @@ -0,0 +1,7 @@ +export declare const defaultMessages = "End-Of-Stream"; +/** + * Thrown on read operation of the end of file or stream has been reached + */ +export declare class EndOfStreamError extends Error { + constructor(); +} diff --git a/node_modules/peek-readable/lib/EndOfFileStream.js b/node_modules/peek-readable/lib/EndOfFileStream.js new file mode 100644 index 0000000..6ae832e --- /dev/null +++ b/node_modules/peek-readable/lib/EndOfFileStream.js @@ -0,0 +1,13 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.EndOfStreamError = exports.defaultMessages = void 0; +exports.defaultMessages = 'End-Of-Stream'; +/** + * Thrown on read operation of the end of file or stream has been reached + */ +class EndOfStreamError extends Error { + constructor() { + super(exports.defaultMessages); + } +} +exports.EndOfStreamError = EndOfStreamError; diff --git a/node_modules/peek-readable/lib/index.d.ts b/node_modules/peek-readable/lib/index.d.ts new file mode 100644 index 0000000..a313ba8 --- /dev/null +++ b/node_modules/peek-readable/lib/index.d.ts @@ -0,0 +1,43 @@ +/// +import * as stream from 'stream'; +export { EndOfStreamError } from './EndOfFileStream'; +export declare class StreamReader { + private s; + /** + * Deferred read request + */ + private request; + private endOfStream; + /** + * Store peeked data + * @type {Array} + */ + private peekQueue; + constructor(s: stream.Readable); + /** + * Read ahead (peek) from stream. Subsequent read or peeks will return the same data + * @param buffer - Buffer to store data read from stream in + * @param offset - Offset buffer + * @param length - Number of bytes to read + * @returns Number of bytes peeked + */ + peek(buffer: Buffer | Uint8Array, offset: number, length: number): Promise; + /** + * Read chunk from stream + * @param buffer - Target buffer to store data read from stream in + * @param offset - Offset of target buffer + * @param length - Number of bytes to read + * @returns Number of bytes read + */ + read(buffer: Buffer | Uint8Array, offset: number, length: number): Promise; + /** + * Read chunk from stream + * @param buffer Buffer to store data read from stream in + * @param offset Offset buffer + * @param length Number of bytes to read + * @returns Number of bytes read + */ + private _read; + private tryRead; + private reject; +} diff --git a/node_modules/peek-readable/lib/index.js b/node_modules/peek-readable/lib/index.js new file mode 100644 index 0000000..00dced4 --- /dev/null +++ b/node_modules/peek-readable/lib/index.js @@ -0,0 +1,137 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.StreamReader = exports.EndOfStreamError = void 0; +const EndOfFileStream_1 = require("./EndOfFileStream"); +var EndOfFileStream_2 = require("./EndOfFileStream"); +Object.defineProperty(exports, "EndOfStreamError", { enumerable: true, get: function () { return EndOfFileStream_2.EndOfStreamError; } }); +class Deferred { + constructor() { + this.promise = new Promise((resolve, reject) => { + this.reject = reject; + this.resolve = resolve; + }); + } +} +const maxStreamReadSize = 1 * 1024 * 1024; // Maximum request length on read-stream operation +class StreamReader { + constructor(s) { + this.s = s; + this.endOfStream = false; + /** + * Store peeked data + * @type {Array} + */ + this.peekQueue = []; + if (!s.read || !s.once) { + throw new Error('Expected an instance of stream.Readable'); + } + this.s.once('end', () => this.reject(new EndOfFileStream_1.EndOfStreamError())); + this.s.once('error', err => this.reject(err)); + this.s.once('close', () => this.reject(new Error('Stream closed'))); + } + /** + * Read ahead (peek) from stream. Subsequent read or peeks will return the same data + * @param buffer - Buffer to store data read from stream in + * @param offset - Offset buffer + * @param length - Number of bytes to read + * @returns Number of bytes peeked + */ + async peek(buffer, offset, length) { + const bytesRead = await this.read(buffer, offset, length); + this.peekQueue.push(buffer.slice(offset, offset + bytesRead)); // Put read data back to peek buffer + return bytesRead; + } + /** + * Read chunk from stream + * @param buffer - Target buffer to store data read from stream in + * @param offset - Offset of target buffer + * @param length - Number of bytes to read + * @returns Number of bytes read + */ + async read(buffer, offset, length) { + if (length === 0) { + return 0; + } + if (this.peekQueue.length === 0 && this.endOfStream) { + throw new EndOfFileStream_1.EndOfStreamError(); + } + let remaining = length; + let bytesRead = 0; + // consume peeked data first + while (this.peekQueue.length > 0 && remaining > 0) { + const peekData = this.peekQueue.pop(); // Front of queue + const lenCopy = Math.min(peekData.length, remaining); + peekData.copy(buffer, offset + bytesRead, 0, lenCopy); + bytesRead += lenCopy; + remaining -= lenCopy; + if (lenCopy < peekData.length) { + // remainder back to queue + this.peekQueue.push(peekData.slice(lenCopy)); + } + } + // continue reading from stream if required + while (remaining > 0 && !this.endOfStream) { + const reqLen = Math.min(remaining, maxStreamReadSize); + const chunkLen = await this._read(buffer, offset + bytesRead, reqLen); + bytesRead += chunkLen; + if (chunkLen < reqLen) + break; + remaining -= chunkLen; + } + return bytesRead; + } + /** + * Read chunk from stream + * @param buffer Buffer to store data read from stream in + * @param offset Offset buffer + * @param length Number of bytes to read + * @returns Number of bytes read + */ + async _read(buffer, offset, length) { + if (this.request) + throw new Error('Concurrent read operation?'); + const readBuffer = this.s.read(length); + if (readBuffer) { + readBuffer.copy(buffer, offset); + return readBuffer.length; + } + else { + this.request = { + buffer, + offset, + length, + deferred: new Deferred() + }; + this.s.once('readable', () => { + this.tryRead(); + }); + return this.request.deferred.promise.then(n => { + this.request = null; + return n; + }, err => { + this.request = null; + throw err; + }); + } + } + tryRead() { + const readBuffer = this.s.read(this.request.length); + if (readBuffer) { + readBuffer.copy(this.request.buffer, this.request.offset); + this.request.deferred.resolve(readBuffer.length); + } + else { + this.s.once('readable', () => { + this.tryRead(); + }); + } + } + reject(err) { + this.endOfStream = true; + if (this.request) { + this.request.deferred.reject(err); + this.request = null; + } + } +} +exports.StreamReader = StreamReader; diff --git a/node_modules/peek-readable/package.json b/node_modules/peek-readable/package.json new file mode 100644 index 0000000..7c3c1e8 --- /dev/null +++ b/node_modules/peek-readable/package.json @@ -0,0 +1,106 @@ +{ + "_from": "peek-readable@^3.1.3", + "_id": "peek-readable@3.1.3", + "_inBundle": false, + "_integrity": "sha512-mpAcysyRJxmICBcBa5IXH7SZPvWkcghm6Fk8RekoS3v+BpbSzlZzuWbMx+GXrlUwESi9qHar4nVEZNMKylIHvg==", + "_location": "/peek-readable", + "_phantomChildren": {}, + "_requested": { + "type": "range", + "registry": true, + "raw": "peek-readable@^3.1.3", + "name": "peek-readable", + "escapedName": "peek-readable", + "rawSpec": "^3.1.3", + "saveSpec": null, + "fetchSpec": "^3.1.3" + }, + "_requiredBy": [ + "/strtok3" + ], + "_resolved": "https://registry.npmjs.org/peek-readable/-/peek-readable-3.1.3.tgz", + "_shasum": "932480d46cf6aa553c46c68566c4fb69a82cd2b1", + "_spec": "peek-readable@^3.1.3", + "_where": "/data/dev/Projets/FNS Electrode/Projets/FNS Electrode/node_modules/strtok3", + "author": { + "name": "Borewit", + "url": "https://github.com/Borewit" + }, + "bugs": { + "url": "https://github.com/Borewit/peek-readable/issues" + }, + "bundleDependencies": false, + "deprecated": false, + "description": "Read and peek from a readable stream", + "devDependencies": { + "@types/mocha": "^8.2.0", + "@types/node": "^14.14.20", + "@typescript-eslint/eslint-plugin": "^2.34.0", + "@typescript-eslint/parser": "^2.34.0", + "add": "^2.0.6", + "chai": "^4.2.0", + "coveralls": "^3.1.0", + "del-cli": "^3.0.1", + "eslint": "^6.8.0", + "mocha": "^7.2.0", + "nyc": "^15.1.0", + "remark-cli": "^9.0.0", + "remark-preset-lint-recommended": "^5.0.0", + "ts-node": "^9.1.1", + "typescript": "^4.1.3" + }, + "engines": { + "node": ">=8" + }, + "files": [ + "lib/**/*.js", + "lib/**/*.d.ts" + ], + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Borewit" + }, + "homepage": "https://github.com/Borewit/peek-readable#readme", + "keywords": [ + "readable", + "buffer", + "stream", + "read" + ], + "license": "MIT", + "main": "lib/index.js", + "name": "peek-readable", + "nyc": { + "check-coverage": true, + "extension": [ + ".ts" + ], + "sourceMap": true, + "instrument": true, + "reporter": [ + "lcov", + "text" + ], + "report-dir": "coverage" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Borewit/peek-readable.git" + }, + "scripts": { + "build": "npm run clean && npm run compile", + "clean": "del-cli lib/**/*.js lib/**/*.js.map lib/**/*.d.ts test/**/*.js test/**/*.js.map coverage .nyc_output", + "compile": "npm run compile-src && yarn run compile-test", + "compile-src": "tsc -p lib", + "compile-test": "tsc -p test", + "eslint": "eslint lib test --ext .ts --ignore-pattern *.d.ts", + "lint": "npm run lint-md && npm run eslint", + "lint-md": "remark -u preset-lint-recommended .", + "send-coveralls": "nyc report --reporter=text-lcov | coveralls", + "start": "npm run compile && npm run lint && npm run cover-test", + "test": "mocha --require ts-node/register --require source-map-support/register --full-trace test/*.ts", + "test-coverage": "nyc npm run test" + }, + "types": "lib/index.d.ts", + "version": "3.1.3" +} -- cgit