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

stream-spigot

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-spigot

A readable stream generator, useful for testing or converting simple functions into Readable streams.

  • 2.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.9K
increased by22.42%
Maintainers
1
Weekly downloads
 
Created
Source

Stream Spigot

A generator for (streams2) Readable streams, useful for testing or converting simple lazy functions into Readable streams.

npm install stream-spigot

Examples:

var spigot = require("stream-spigot")

spigot(["ABCDEFG"]).pipe(process.stdout)
// ABCDEFG

spigot(["ABC", "DEF", "G"]).pipe(process.stdout)
// ABCDEFG

var count = 0
function gen() {
  if (count++ < 5) {
    return {val: count}
  }
}

spigot({objectMode: true}, gen).pipe(...)
/*
{val: 1}
{val: 2}
{val: 3}
{val: 4}
{val: 5}
*/

// Do it asynchronously: (next expects fn(err, data))
function get(next) {
  some_async_op(/*..*/, next)
}
spigot(get).pipe(...)

Usage

var spigot = require("stream-spigot")

var stream = spigot(some_function)
// or
var stream = spigot(array)
// or
var stream = spigot({objectMode: true}, )

spigot([options,] fn)

Until I settle on a better API, spigot will sniff the arity of the generator function you pass it. If your function expects no arguments, it will call it synchronously and buffer each reply as a chunk. Return null to end the stream.

If the generator function expects a single argument, it expects it to be asynchronous. It will call your function like so: generator(next) where it proivides next as a callback expecting to be called like next(err, data). If an error is encountered, spigot will emit an "error" event with your error. If there is no error, data will be buffered into the stream. The stream will be ended if data is ever null.

spigot([options,] array)

The spigot will consecutively write each element from the specified array as a buffered chunk until the array has been consumed. If the array contains contents other than Strings or Buffers, you should consider using {objectMode: true} to create this as an objectMode stream.

If your array contains a null value, the stream will end at that value instead of consuming the entire array.

Sending an array is equivalent to doing the following:

var a = ["my", "array"]
function fn() {
  return a.shift()
}

var stream = spigot(fn)
// will be equivalent to
var stream = spigot(a)

Options

Accepts standard readable-stream options.

LICENSE

MIT

Keywords

FAQs

Package last updated on 21 Sep 2013

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