Gathering detailed insights and metrics for peek-readable
Gathering detailed insights and metrics for peek-readable
Gathering detailed insights and metrics for peek-readable
Gathering detailed insights and metrics for peek-readable
A promise based asynchronous stream reader, which makes reading from a stream easy.
npm install peek-readable
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (100%)
Total Downloads
627,013,521
Last Day
1,268,379
Last Week
7,929,053
Last Month
31,214,559
Last Year
262,666,304
MIT License
8 Stars
887 Commits
7 Forks
2 Watchers
8 Branches
3 Contributors
Updated on May 07, 2025
Latest Version
7.0.0
Package Id
peek-readable@7.0.0
Unpacked Size
26.15 kB
Size
6.92 kB
File Count
21
NPM Version
10.9.2
Node Version
22.13.0
Published on
Mar 09, 2025
Cumulative downloads
Total Downloads
Last Day
15%
1,268,379
Compared to previous day
Last Week
9.6%
7,929,053
Compared to previous week
Last Month
17.1%
31,214,559
Compared to previous month
Last Year
51.8%
262,666,304
Compared to previous year
A promise based asynchronous stream reader, which makes reading from a stream easy.
Allows to read and peek from a Readable Stream
This module is used by strtok3
The peek-readable
contains one class: StreamReader
, which reads from a stream.Readable.
StreamReader
is used to read from Node.js stream.Readable.WebStreamByobReader
is used to read from ReadableStreamModule: version 5 migrated from CommonJS to pure ECMAScript Module (ESM). JavaScript is compliant with ECMAScript 2019 (ES10). Requires a modern browser or Node.js ≥ 18 engine.
For TypeScript CommonJs backward compatibility, you can use load-esm.
1npm install --save peek-readable
Both StreamReader
and WebStreamByobReader
implement the IStreamReader interface.
IStreamReader
InterfaceThe IStreamReader
interface defines the contract for a stream reader,
which provides methods to read and peek data from a stream into a Uint8Array
buffer.
The methods are asynchronous and return a promise that resolves with the number of bytes read.
peek
functionThis method allows you to inspect data from the stream without advancing the read pointer. It reads data into the provided Uint8Array at a specified offset but does not modify the stream's internal position, allowing you to look ahead in the stream.
1peek(buffer: Uint8Array, offset: number, length: number): Promise<number>
Parameters:
buffer
: Uint8Array
: The buffer into which the data will be peeked.
This is where the peeked data will be stored.maybeBeLess
: If true, the buffer maybe partially filled.Returns Promise<number>
:
A promise that resolves with the number of bytes actually peeked into the buffer.
This number may be less than the requested length if the end of the stream is reached.
read
function1read(buffer: Uint8Array, offset: number, length: number): Promise<number>
Parameters:
buffer
: Uint8Array
: The buffer into which the data will be read.
This is where the read data will be stored.maybeBeLess
: If true, the buffer maybe partially filled.Returns Promise<number>
:
A promise that resolves with the number of bytes actually read into the buffer.
This number may be less than the requested length if the end of the stream is reached.
abort
functionAbort active asynchronous operation (read
or peak
) before it has completed.
1abort(): Promise<void>
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.
1import fs from 'node:fs'; 2import { StreamReader } from 'peek-readable'; 3 4(async () => { 5 const readable = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg'); 6 const streamReader = new StreamReader(readable); 7 const uint8Array = new Uint8Array(16); 8 const bytesRead = await streamReader.read(uint8Array);; 9 // buffer contains 16 bytes, if the end-of-stream has not been reached 10})();
End-of-stream detection:
1(async () => { 2 3 const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg'); 4 const streamReader = new StreamReader(fileReadStream); 5 const buffer = Buffer.alloc(16); // or use: new Uint8Array(16); 6 7 try { 8 await streamReader.read(buffer); 9 // buffer contains 16 bytes, if the end-of-stream has not been reached 10 } catch(error) { 11 if (error instanceof EndOfStreamError) { 12 console.log('End-of-stream reached'); 13 } 14 } 15})();
With peek
you can read ahead:
1import fs from 'node:fs'; 2import { StreamReader } from 'peek-readable'; 3 4function closeNodeStream(stream: ReadStream): Promise<void> { 5 return new Promise<void>((resolve, reject) => { 6 stream.close(err => { 7 if(err) 8 reject(err); 9 else 10 resolve(); 11 }); 12 }) 13} 14 15(async () => { 16 17 const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg'); 18 try { 19 const streamReader = new StreamReader(fileReadStream); 20 try { 21 const buffer = Buffer.alloc(20); 22 23 let bytesRead = await streamReader.peek(buffer.subarray(0, 3)); 24 if (bytesRead === 3 && buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) { 25 console.log('This is a JPEG file'); 26 } else { 27 throw Error('Expected a JPEG file'); 28 } 29 30 bytesRead = await streamReader.read(buffer); // Read JPEG header 31 if (bytesRead === 20) { 32 console.log('Got the JPEG header'); 33 } else { 34 throw Error('Failed to read JPEG header'); 35 } 36 } finally { 37 await streamReader.close(); // Release fileReadStream 38 } 39 } finally { 40 await closeNodeStream(fileReadStream); 41 } 42 43 44})();
This project is licensed under the MIT License. Feel free to use, modify, and distribute as needed.
No vulnerabilities found.
Reason
15 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool is run on all commits
Details
Reason
0 existing vulnerabilities detected
Reason
Found 0/23 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More