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

from2

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

from2

Convenience wrapper for ReadableStream, with an API lifted from "from" and "through2"

  • 2.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
2
Weekly downloads
 
Created

What is from2?

The 'from2' npm package is a utility for creating readable streams using a simple API. It is particularly useful for stream-based operations where data needs to be generated or transformed on the fly. The package simplifies the process of creating custom readable streams by handling the underlying Node.js stream mechanics.

What are from2's main functionalities?

Creating a readable stream from a data source

This feature allows you to create a readable stream that emits data from an array. The stream reads data from the array and pushes it to the consumer until there's no data left, at which point it signals the end of the stream.

const from2 = require('from2');

const data = ['hello', 'world'];
const stream = from2(function(size, next) {
  if (data.length === 0) return next(null, null);
  next(null, data.shift());
});

stream.on('data', (chunk) => {
  console.log(chunk.toString());
});
stream.on('end', () => {
  console.log('Stream ended');
});

Creating a stream from an asynchronous data source

This example demonstrates how to create a readable stream that reads data from a file asynchronously. The stream uses the 'fs.readFile' method to read data and then pushes it to the stream consumer.

const from2 = require('from2');
const fs = require('fs');

const stream = from2(function(size, next) {
  fs.readFile('/path/to/file.txt', (err, data) => {
    if (err) return next(err);
    next(null, data);
  });
});

stream.on('data', (chunk) => {
  console.log(chunk.toString());
});
stream.on('end', () => {
  console.log('Stream ended');
});

Other packages similar to from2

Keywords

FAQs

Package last updated on 17 Jun 2015

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