
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
The byline npm package is designed to help with reading lines from a stream, such as a file or stdin, efficiently. It wraps Node.js streams and allows for easy line-by-line processing without having to manage buffering or line splitting manually. This can be particularly useful for reading large files or processing real-time data feeds where handling data line-by-line is necessary.
Line-by-line Stream Reading
This feature allows you to read from a stream line-by-line. The code sample demonstrates how to create a readable stream from a file and use byline to process each line as it becomes available.
const byline = require('byline');
const fs = require('fs');
let stream = byline(fs.createReadStream('file.txt', { encoding: 'utf8' }));
stream.on('data', function(line) {
console.log(line);
});
The readline package is a core Node.js module that provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time. It differs from byline in that it's built into Node.js and offers more complex features for input processing, such as prompting and history. However, byline is simpler for basic line-by-line stream reading.
Split is an npm package that allows you to split a stream of data into lines or any other arbitrary chunks based on a delimiter. It's similar to byline in its basic functionality but offers more flexibility in terms of customizing the delimiter and handling edge cases. Byline, on the other hand, is more focused on line-by-line processing with less configuration.
Through2 is a tiny wrapper around Node.js streams.Transform (Streams2/3) to make defining custom transform streams easier. While not specifically designed for line-by-line processing like byline, it can be used in combination with other modules to achieve similar functionality. It provides a lower-level API for stream transformation, making it more versatile but also requiring more boilerplate code for simple tasks.
byline
is a simple module providing a LineStream
.
streams2
(transform stream)pipe
stream = byline(stream);
npm install byline
or from source:
git clone git://github.com/jahewson/node-byline.git
cd node-byline
npm link
The byline
module can be used as a function to quickly wrap a readable stream:
var fs = require('fs'),
byline = require('byline');
var stream = byline(fs.createReadStream('sample.txt', { encoding: 'utf8' }));
The data
event then emits lines:
stream.on('data', function(line) {
console.log(line);
});
You just need to add one line to wrap your readable Stream
with a LineStream
.
var fs = require('fs'),
byline = require('byline');
var stream = fs.createReadStream('sample.txt');
stream = byline.createStream(stream);
stream.on('data', function(line) {
console.log(line);
});
byline
supports pipe
(though it strips the line endings, of course).
var stream = fs.createReadStream('sample.txt');
stream = byline.createStream(stream);
stream.pipe(fs.createWriteStream('nolines.txt'));
Alternatively, you can create a readable/writable "through-stream" which doesn't wrap any specific stream:
var stream = fs.createReadStream('sample.txt');
stream = byline.createStream(stream);
stream.pipe(fs.createWriteStream('nolines.txt'));
var input = fs.createReadStream('LICENSE');
var lineStream = byline.createStream();
input.pipe(lineStream);
var output = fs.createWriteStream('test.txt');
lineStream.pipe(output);
Node v0.10 added a new streams2 API. This allows the stream to be used in non-flowing mode and is preferred over the legacy pause() and resume() methods.
var stream = fs.createReadStream('sample.txt');
stream = byline.createStream(stream);
stream.on('readable', function() {
var line;
while (null !== (line = stream.read())) {
console.log(line);
}
});
The byline
transform stream can be directly manipulated like so:
var LineStream = require('byline').LineStream;
var input = fs.createReadStream('sample.txt');
var output = fs.createWriteStream('nolines.txt');
var lineStream = new LineStream();
input.pipe(lineStream);
lineStream.pipe(output);
By default byline skips empty lines, if you want to keep them, pass the keepEmptyLines
option in
the call to byline.createStream(stream, options)
or byline(stream, options)
.
npm test
If you want to use node-byline
with node v0.8 then you can use the 2.1.x series. Simply use the
following in your package.json
:
"dependencies": {
"byline": ">=2.1.0 <3.0.0"
},
Unlike other modules (of which there are many), byline
contains no:
pipe
FAQs
simple line-by-line stream reader
The npm package byline receives a total of 2,221,364 weekly downloads. As such, byline popularity was classified as popular.
We found that byline 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.