HarshvardhanCn01/Voice-Assistant
0
1[](https://github.com/Borewit/peek-readable/actions/workflows/nodejs-ci.yml)2[](https://github.com/Borewit/peek-readable/actions/workflows/github-code-scanning/codeql)[](https://npmjs.org/package/peek-readable)3[](https://npmcharts.com/compare/peek-readable?start=600&interval=30)4[](https://coveralls.io/github/Borewit/peek-readable?branch=master)5[](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)6[](https://snyk.io/test/github/Borewit/peek-readable?targetFile=package.json)7 8# peek-readable9 10A promise based asynchronous stream reader, which makes reading from a stream easy.11 12Allows to read and peek from a [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams)13 14This module is used by [strtok3](https://github.com/Borewit/strtok3)15 16The `peek-readable` contains one class: `StreamReader`, which reads from a [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable).17 18- Class `StreamReader` is used to read from Node.js [stream.Readable](https://nodejs.org/api/stream.html#stream_class_stream_readable).19- Class `WebStreamReader` is used to read from [ReadableStream<Uint8Array>](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)20 21## Compatibility22 23Module: version 5 migrated from [CommonJS](https://en.wikipedia.org/wiki/CommonJS) to [pure ECMAScript Module (ESM)](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).24JavaScript is compliant with [ECMAScript 2019 (ES10)](https://en.wikipedia.org/wiki/ECMAScript#10th_Edition_%E2%80%93_ECMAScript_2019).25Requires Node.js ≥ 14.16 engine.26 27## Usage28 29### Installation30 31```shell script32npm install --save peek-readable33```34 35## API Documentation36 37Both `StreamReader` and `WebStreamReader` implement the [IStreamReader interface](#istreamreader-interface).38 39### `IStreamReader` Interface40 41The `IStreamReader` interface defines the contract for a stream reader,42which provides methods to read and peek data from a stream into a `Uint8Array` buffer.43The methods are asynchronous and return a promise that resolves with the number of bytes read.44 45#### Methods46 47##### `peek` function48This method allows you to inspect data from the stream without advancing the read pointer.49It reads data into the provided Uint8Array at a specified offset but does not modify the stream's internal position, 50allowing you to look ahead in the stream.51 52```ts 53peek(uint8Array: Uint8Array, offset: number, length: number): Promise<number>54```55 56Parameters:57- `uint8Array`: `Uint8Array`: The buffer into which the data will be peeked.58 This is where the peeked data will be stored.59- `offset`: `number`: The offset in the Uint8Array where the peeked data should start being written.60- `length`: `number`: The number of bytes to peek from the stream.61 62Returns `Promise<number>`: 63A promise that resolves with the number of bytes actually peeked into the buffer. 64This number may be less than the requested length if the end of the stream is reached.65 66##### `read` function67```ts 68read(buffer: Uint8Array, offset: number, length: number): Promise<number>69```70 71Parameters:72- `uint8Array`: `Uint8Array`: The buffer into which the data will be read.73 This is where the read data will be stored.74- `offset`: `number`: The offset in the Uint8Array where the read data should start being written.75- `length`: `number`: The number of bytes to read from the stream.76 77Returns `Promise<number>`:78A promise that resolves with the number of bytes actually read into the buffer.79This number may be less than the requested length if the end of the stream is reached.80 81##### `abort` function82 83Abort active asynchronous operation (`read` or `peak`) before it has completed.84 85```ts 86abort(): Promise<void>87```88 89## Examples90 91In the following example we read the first 16 bytes from a stream and store them in our buffer.92Source code of examples can be found [here](test/examples.ts).93 94```js95import fs from 'node:fs';96import { StreamReader } from 'peek-readable';97 98(async () => {99 const readable = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');100 const streamReader = new StreamReader(readable);101 const uint8Array = new Uint8Array(16);102 const bytesRead = await streamReader.read(uint8Array, 0, 16);;103 // buffer contains 16 bytes, if the end-of-stream has not been reached104})();105```106 107End-of-stream detection:108```js109(async () => {110 111 const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');112 const streamReader = new StreamReader(fileReadStream);113 const buffer = Buffer.alloc(16); // or use: new Uint8Array(16);114 115 try {116 await streamReader.read(buffer, 0, 16);117 // buffer contains 16 bytes, if the end-of-stream has not been reached118 } catch(error) {119 if (error instanceof EndOfStreamError) {120 console.log('End-of-stream reached');121 }122 }123})();124```125 126With `peek` you can read ahead:127```js128import fs from 'node:fs';129import { StreamReader } from 'peek-readable';130 131const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');132const streamReader = new StreamReader(fileReadStream);133const buffer = Buffer.alloc(20);134 135(async () => {136 let bytesRead = await streamReader.peek(buffer, 0, 3);137 if (bytesRead === 3 && buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) {138 console.log('This is a JPEG file');139 } else {140 throw Error('Expected a JPEG file');141 }142 143 bytesRead = await streamReader.read(buffer, 0, 20); // Read JPEG header144 if (bytesRead === 20) {145 console.log('Got the JPEG header');146 } else {147 throw Error('Failed to read JPEG header');148 }149})();150```151 