Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
first-chunk-stream
Advanced tools
The 'first-chunk-stream' npm package is a Node.js module that allows you to transform the first chunk of a stream. This can be particularly useful for tasks that require modifying or inspecting the beginning of a stream before processing the rest, such as adjusting headers, parsing initial metadata, or prepending data to a stream.
Modify the first chunk of a stream
This code sample demonstrates how to use first-chunk-stream to modify the first 10 bytes of data from a file stream. It prepends the text 'Modified: ' to the beginning of the first chunk.
const firstChunkStream = require('first-chunk-stream');
const fs = require('fs');
const modifyFirstChunk = firstChunkStream({ chunkSize: 10 }, (chunk, enc, callback) => {
const modifiedChunk = Buffer.from(`Modified: ${chunk.toString()}`);
callback(null, modifiedChunk);
});
fs.createReadStream('example.txt')
.pipe(modifyFirstChunk)
.pipe(fs.createWriteStream('modifiedExample.txt'));
Inspect and conditionally modify the first chunk
This example shows how to inspect the first 15 bytes of a stream and modify it only if it contains the word 'Important'. If the condition is met, it prepends 'Alert: ' to the chunk.
const firstChunkStream = require('first-chunk-stream');
const fs = require('fs');
const inspectFirstChunk = firstChunkStream({ chunkSize: 15 }, (chunk, enc, callback) => {
if (chunk.toString().includes('Important')) {
callback(null, Buffer.from(`Alert: ${chunk.toString()}`));
} else {
callback(null, chunk);
}
});
fs.createReadStream('example.txt')
.pipe(inspectFirstChunk)
.pipe(fs.createWriteStream('inspectedExample.txt'));
Through2 is a tiny wrapper around Node streams.Transform, making it easier to create transform streams. It is similar to first-chunk-stream but offers more general functionality for transforming streams, not limited to just the first chunk.
Peek-stream allows you to peek at the first few bytes of a stream to determine how to parse it, then lets the stream continue unmodified or modified. It is similar to first-chunk-stream but focuses on peeking rather than modifying.
Transform the first chunk in a stream
Useful if you want to do something to the first chunk.
You can also set the minimum size of that chunk.
$ npm install --save first-chunk-stream
var fs = require('fs');
var concat = require('concat-stream');
var firstChunk = require('first-chunk-stream');
// unicorn.txt => unicorn rainbow
// `highWaterMark: 1` means it will only read 1 byte at the time
fs.createReadStream('unicorn.txt', {highWaterMark: 1})
.pipe(firstChunk({minSize: 7}, function (chunk, enc, cb) {
this.push(chunk.toUpperCase());
cb();
}))
.pipe(concat(function (data) {
console.log(data);
//=> UNICORN rainbow
}));
Type: number
The minimum size of the first chunk.
Required
Type: function
The function that gets the first chunk.
Instead of returning a stream.Transform instance, firstChunk.ctor()
returns a constructor for a custom Transform. This is useful when you want to use the same transform logic in multiple instances.
MIT © Sindre Sorhus
FAQs
Buffer and transform the n first bytes of a stream
The npm package first-chunk-stream receives a total of 763,708 weekly downloads. As such, first-chunk-stream popularity was classified as popular.
We found that first-chunk-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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.