mirror of
https://github.com/arthur-pbty/portfolio2023.git
synced 2026-06-13 15:57:06 +02:00
modified
This commit is contained in:
-21
@@ -1,21 +0,0 @@
|
||||
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.
|
||||
-93
@@ -1,93 +0,0 @@
|
||||

|
||||
[](https://npmjs.org/package/peek-readable)
|
||||
[](https://npmcharts.com/compare/peek-readable?start=600&interval=30)
|
||||
[](https://coveralls.io/github/Borewit/peek-readable?branch=master)
|
||||
[](https://www.codacy.com/gh/Borewit/peek-readable/dashboard?utm_source=github.com&utm_medium=referral&utm_content=Borewit/peek-readable&utm_campaign=Badge_Grade)
|
||||
[](https://lgtm.com/projects/g/Borewit/peek-readable/alerts/)
|
||||
[](https://lgtm.com/projects/g/Borewit/peek-readable/context:javascript)
|
||||
[](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 2018 (ES9)](https://en.wikipedia.org/wiki/ECMAScript#9th_Edition_%E2%80%93_ECMAScript_2018).
|
||||
|
||||
## 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 readable = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');
|
||||
const streamReader = new StreamReader(readable);
|
||||
const uint8Array = new Uint8Array(16);
|
||||
const bytesRead = await streamReader.read(uint8Array, 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); // or use: new Uint8Array(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');
|
||||
}
|
||||
})();
|
||||
```
|
||||
-87
@@ -1,87 +0,0 @@
|
||||
{
|
||||
"name": "peek-readable",
|
||||
"version": "4.1.0",
|
||||
"description": "Read and peek from a readable stream",
|
||||
"author": {
|
||||
"name": "Borewit",
|
||||
"url": "https://github.com/Borewit"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/Borewit"
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "del-cli lib/**/*.js lib/**/*.js.map lib/**/*.d.ts test/**/*.js test/**/*.js.map coverage .nyc_output",
|
||||
"build": "npm run clean && npm run compile",
|
||||
"compile-src": "tsc -p lib",
|
||||
"compile-test": "tsc -p test",
|
||||
"compile": "npm run compile-src && yarn run compile-test",
|
||||
"eslint": "eslint lib test --ext .ts --ignore-pattern *.d.ts",
|
||||
"lint-md": "remark -u preset-lint-recommended .",
|
||||
"lint": "npm run lint-md && npm run eslint",
|
||||
"test": "mocha --require ts-node/register --require source-map-support/register --full-trace test/*.ts",
|
||||
"test-coverage": "nyc npm run test",
|
||||
"send-coveralls": "nyc report --reporter=text-lcov | coveralls",
|
||||
"start": "npm run compile && npm run lint && npm run cover-test"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Borewit/peek-readable"
|
||||
},
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"bugs": {
|
||||
"url": "https://github.com/Borewit/peek-readable/issues"
|
||||
},
|
||||
"files": [
|
||||
"lib/**/*.js",
|
||||
"lib/**/*.d.ts"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.3.0",
|
||||
"@types/mocha": "^9.1.0",
|
||||
"@types/node": "^17.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
||||
"@typescript-eslint/parser": "^4.33.0",
|
||||
"add": "^2.0.6",
|
||||
"chai": "^4.3.6",
|
||||
"coveralls": "^3.1.1",
|
||||
"del-cli": "^4.0.1",
|
||||
"eslint": "^7.32.0",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-import-resolver-typescript": "^2.5.0",
|
||||
"eslint-plugin-import": "^2.25.4",
|
||||
"eslint-plugin-jsdoc": "^37.7.1",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-unicorn": "^40.1.0",
|
||||
"mocha": "^9.2.0",
|
||||
"nyc": "^15.1.0",
|
||||
"remark-cli": "^10.0.1",
|
||||
"remark-preset-lint-recommended": "^6.1.2",
|
||||
"ts-node": "^10.4.0",
|
||||
"typescript": "^4.5.5"
|
||||
},
|
||||
"keywords": [
|
||||
"readable",
|
||||
"buffer",
|
||||
"stream",
|
||||
"read"
|
||||
],
|
||||
"nyc": {
|
||||
"check-coverage": true,
|
||||
"extension": [
|
||||
".ts"
|
||||
],
|
||||
"sourceMap": true,
|
||||
"instrument": true,
|
||||
"reporter": [
|
||||
"lcov",
|
||||
"text"
|
||||
],
|
||||
"report-dir": "coverage"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user