Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@mangosteen/line-by-line
Advanced tools
Read stream line by line using async iterator or object-mode stream transfrom.
Read or parse node.js streams line by line without loading the entire file to memory.
Use async iterator in a for await
loop, or object-mode stream transform.
readline
package?Because it does not offer Transform
stream, and because its async iterator does not propagate stream errors. So if you are parsing a file or network stream and an error
happens, you better manually catch .on('error')
and additionally use that to break from the for await
iterator loop.
Our package internally uses readline
, but wraps it in a way to fix the above shortcomings.
With npm do:
$ npm install @mangosteen/line-by-line
import fs from 'fs';
import { lineByLine } from '@mangosteen/line-by-line';
(async () => {
const inputStream = fs.createReadStream('./shakespeare.txt');
for await (const line of lineByLine(inputStream)) {
console.log('Line:', line);
console.log(typeof line); // 'string'
}
})();
The iterator automatically closes and destroys the input stream, and fully propagates input stream errors.
When you break
, return
or throw
from within the for await
loop, everything gets cleaned up automatically.
Errors thrown by the stream work the same way.
You cannot reuse the same input stream for multiple for await
loops or multiple lineByLine
iterators,
because everything gets cleaned up automatically.
import fs from 'fs';
import stream from 'stream';
import { promisify } from 'util';
import { createLineByLineStream } from '@mangosteen/line-by-line';
const pipeline = promisify(stream.pipeline);
(async () => {
await pipeline(
fs.createReadStream('./shakespeare.txt'),
createLineByLineStream(),
createSinkStream(),
);
})();
function createSinkStream(): stream.Writable {
return new stream.Writable({
objectMode: true,
highWaterMark: 0,
write(chunk, _encoding, callback): void {
console.log('Line:', chunk);
console.log(typeof chunk); // 'string'
callback();
},
});
}
The createLineByLineStream
transform stream Writable
side expects a standard non-objectMode
stream.
The Readable
side runs in an objectMode
, where each object is a line string
.
FAQs
Read stream line by line using async iterator or object-mode stream transform.
The npm package @mangosteen/line-by-line receives a total of 3 weekly downloads. As such, @mangosteen/line-by-line popularity was classified as not popular.
We found that @mangosteen/line-by-line 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.