Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
then-read-stream
Advanced tools
A promise based asynchronous stream reader, which makes reading from a stream easy.
Allows to read from a Readable Stream similar as you would read from a file.
The then-read-stream
contains one class: StreamReader
, which reads from a stream.Readable.
NPM module is compliant with ECMAScript 2017 (ES8).
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.
import * as fs from 'fs';
import * as path from 'path';
import { StreamReader } from 'then-read-stream';
const readable = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');
const streamReader = new StreamReader(readable);
const buffer = Buffer.alloc(16);
return streamReader.read(buffer, 0, 16)
.then( bytesRead => {
// buf, contains bytesRead, which will be 16 if the end-of-stream has not been reached
});
With peek you can read ahead:
import * as fs from 'fs';
import * as path from 'path';
import { StreamReader } from 'then-read-stream';
const fileReadStream = fs.createReadStream('JPEG_example_JPG_RIP_001.jpg');
const streamReader = new StreamReader(fileReadStream);
const buffer = Buffer.alloc(20);
return streamReader.peek(buffer, 0, 3)
.then(bytesRead => {
if (bytesRead === 3 && buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) {
console.log('This is a JPEG file');
return streamReader.read(buffer, 0, 20); // Read JPEG header
} else {
throw Error('Expected a JPEG file');
}
})
.then(bytesRead => {
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:
return streamReader.ignore(16)
.then( bytesIgnored => {
if (bytesIgnored < 16){
console.log(`Remaining stream length was ${bytesIgnored}, expected 16`);
}
});
FAQs
Read from a readable stream just like a file
The npm package then-read-stream receives a total of 6,905 weekly downloads. As such, then-read-stream popularity was classified as popular.
We found that then-read-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.