Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

first-chunk-stream

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

first-chunk-stream

Buffer and transform the n first bytes of a stream

  • 6.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5M
decreased by-20.21%
Maintainers
1
Weekly downloads
 
Created

What is first-chunk-stream?

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.

What are first-chunk-stream's main functionalities?

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'));

Other packages similar to first-chunk-stream

Keywords

FAQs

Package last updated on 01 Nov 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc