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

Transform the first chunk in a stream

  • 3.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
971K
decreased by-35.01%
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 12 May 2019

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