Socket
Socket
Sign inDemoInstall

stream

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream

Node.js streams in the browser


Version published
Weekly downloads
414K
decreased by-23.2%
Maintainers
1
Weekly downloads
 
Created

What is stream?

The 'stream' package in Node.js provides a way to handle streaming data. Streams are objects that let you read data from a source or write data to a destination in a continuous fashion. They are particularly useful for handling large amounts of data, such as reading files or handling HTTP requests and responses.

What are stream's main functionalities?

Readable Stream

A Readable stream is used to read data from a source. In this example, a custom Readable stream is created that pushes 'Hello, World!' and then signals the end of data.

const { Readable } = require('stream');

const readable = new Readable({
  read(size) {
    this.push('Hello, World!');
    this.push(null); // No more data
  }
});

readable.on('data', (chunk) => {
  console.log(`Received ${chunk.length} bytes of data.`);
});

readable.on('end', () => {
  console.log('No more data.');
});

Writable Stream

A Writable stream is used to write data to a destination. In this example, a custom Writable stream is created that logs the data it receives.

const { Writable } = require('stream');

const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(`Writing: ${chunk.toString()}`);
    callback();
  }
});

writable.write('Hello, World!');
writable.end('Goodbye, World!');

Transform Stream

A Transform stream is a type of duplex stream where the output is computed based on the input. In this example, a custom Transform stream is created that converts input data to uppercase.

const { Transform } = require('stream');

const transform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});

transform.on('data', (chunk) => {
  console.log(`Transformed: ${chunk.toString()}`);
});

transform.write('Hello, World!');
transform.end('Goodbye, World!');

Duplex Stream

A Duplex stream is both Readable and Writable. In this example, a custom Duplex stream is created that reads and writes data.

const { Duplex } = require('stream');

const duplex = new Duplex({
  read(size) {
    this.push('Hello, World!');
    this.push(null); // No more data
  },
  write(chunk, encoding, callback) {
    console.log(`Writing: ${chunk.toString()}`);
    callback();
  }
});

duplex.on('data', (chunk) => {
  console.log(`Received ${chunk.length} bytes of data.`);
});

duplex.write('Hello, World!');
duplex.end('Goodbye, World!');

Other packages similar to stream

Keywords

FAQs

Package last updated on 03 Dec 2014

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