npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

which-stream

v1.2.1

Published

A small Node.JS library to determine which stream to use.

Downloads

598

Readme

which-stream

npm version

which-stream is a small Node.JS library to pipe an input stream to an output one. It can create filesystem's read and write streams, or use provided ones, as well as piping output to the stdout.

yarn add which-stream

Table Of Contents

API

The package is available by importing its default function:

import whichStream from 'which-stream'

The types and externs for Google Closure Compiler via Depack are defined in the _whichStream namespace.

async whichStream(  config: Config,): void

The whichStream function will determine which streams to use by creating readable and writable streams when source and/or destination are passed as strings, pipe the input to the output, and wait for the output to finish.

_whichStream.Config: The configuration object.

| Name | Type | Description | | ----------- | -------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | source | string | The path to a source file from which to read data. | | readable | !stream.Readable | An optional input stream, if the source is not given. | | destination | string | The path to an output file. If - is given, process.stdout will be used. If the path of the input stream is the same as of the output one, the result will be first written to the memory, and only then to the destination file. Moreover, when used with the readable specified to overwrite the file from which data is originally read from, the source should also be passed. | | writable | !stream.Writable | A stream into which to pipe the input stream, if destination is not given. |

Use Cases

Below is the list of possible use cases when which-stream package could be used.

Source to Destination

When the source and destination are passed, a file is be copied as it is.

/* yarn example/source-destination.js */
import whichStream from 'which-stream'
import { createReadStream } from 'fs'

(async () => {
  const source = 'example/millet.txt'
  const destination = 'example/millet-out.txt'
  await whichStream({
    source,
    destination,
  })
  // verify
  const rs = createReadStream(destination)
  rs.pipe(process.stdout)
})()
Millet (gluten-free):
An excellet source of manganese, magnesioum, and phosphorus.

Source to Writable

When the source and writable are supplied, a stream pushing input text from the source file will be piped into the given output writable stream.

/* yarn example/source-writable.js */
import whichStream from 'which-stream'
import { Transform } from 'stream';

(async () => {
  const source = 'example/brown-rice.txt'
  const writable = new Transform({
    transform(data, encoding, next) {
      const d = `${data}`.toUpperCase()
      this.push(d)
      next()
    },
  })
  writable.pipe(process.stdout) // to verify
  await whichStream({
    source,
    writable,
  })
})()
BROWN RICE (GLUTEN-FREE):
UNLIKE WHITE RICE, BROWN RICE IS REACH WITH VITAMINS,
MINERALS, FIBRE AND FATTY ACIDS.

Source to Stdout

To print a file to stdout, the destination should be set to - .

/* yarn example/source-stdout.js */
import whichStream from 'which-stream'

(async () => {
  await whichStream({
    source: 'example/zinc.txt',
    destination: '-',
  })
})()
ZINC: an often-overlooked essential mineral, zinc is the
most common mineral found in the body after iron.

Readable to Destination

Passing both the readable and destination properties will ensure that the input stream is written to the destination on the disk.

/* yarn example/readable-destination.js */
import whichStream from 'which-stream'
import { createReadStream } from 'fs'
import { Readable } from 'stream';

(async () => {
  const destination = 'example/thiamine-out.txt'
  const readable = new Readable({
    read() {
      this.push(`
Vitamin B1 (Thiamine): essential for proper functioning of
the heart, muscles and nervous system.
`.trim()
      )
      this.push(null)
    },
  })
  await whichStream({
    readable,
    destination,
  })
  // verify
  const rs = createReadStream(destination)
  rs.pipe(process.stdout)
})()
Vitamin B1 (Thiamine): essential for proper functioning of
the heart, muscles and nervous system.

Readable to Destination (Overwriting)

If readable's data initially comes from the same source as the destination to which it will be written, the source property must also be set to make sure that the file is overwritten properly. The stream's data will first be buffered in memory, and upon the readable stream's end it will be released to the destination. This is useful when using transform streams which don't necessary read from the source themselves, but are being piped into by another readable.

/* yarn example/readable-destination-overwrite.js */
import whichStream from 'which-stream'
import { createReadStream } from 'fs'
import { Transform } from 'stream'

(async () => {
  const source = 'example/onions.txt'
  const readable = new Transform({
    transform(data, encoding, next) {
      const d = `${data}`.replace(
        /Modified: (.+)/m,
        `Modified: ${new Date().toDateString()}`,
      )
      this.push(d)
      next()
    },
  })
  const rs = createReadStream(source)
  rs.pipe(readable)
  await whichStream({
    source,
    readable,
    destination: source,
  })
  // verify
  const vrs = createReadStream(source)
  vrs.pipe(process.stdout)
})()
Buy a bag of onions, chop in food processor, toss into
plastic zip bag, and stow in freezer.
Modified: Sat Aug 03 2019

In case the source is not passed, the file will become empty.

Readable to Writable

In the scenario when the readable and writable are specified, the former will be piped into the latter, and the function's promise will be resolved when the writable finishes.

/* yarn example/readable-writable.js */
import whichStream from 'which-stream'
import { Readable, Transform } from 'stream';

(async () => {
  const readable = new Readable({
    read() {
      this.push(`
Omega-3 fatty acids boost heart health, lower
triglycerides, and may help in the treatment
and prevention of depression.
`.trim()
      )
      this.push(null)
    },
  })
  const writable = new Transform({
    transform(data, encoding, next) {
      const d = `*${data}*`
      this.push(d)
      next()
    },
  })
  writable.pipe(process.stdout) // to verify
  await whichStream({
    readable,
    writable,
  })
})()
*Omega-3 fatty acids boost heart health, lower
triglycerides, and may help in the treatment
and prevention of depression.*

Readable to Stdout

When a Readable stream needs to be output to the stdout, the destination should be set to -.

/* yarn example/readable-stdout.js */
import whichStream from 'which-stream'
import { Readable } from 'stream';

(async () => {
  const readable = new Readable({
    read() {
      this.push(`
> Use microwave to quickly steam your veggies:
place in a bowl, add a few tablespoons of water,
cover and cook in 3 to 5 minutes increments.
`.trim()
      )
      this.push(null)
    },
  })
  await whichStream({
    readable,
    destination: '-',
  })
})()
> Use microwave to quickly steam your veggies:
place in a bowl, add a few tablespoons of water,
cover and cook in 3 to 5 minutes increments.

Copyright