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/README.md | 104 +++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 node_modules/peek-readable/README.md (limited to 'node_modules/peek-readable/README.md') 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); +})(); +``` + -- cgit