GithubHelp home page GithubHelp logo

cuicui-coding / from2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hughsk/from2

0.0 0.0 0.0 23 KB

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

License: MIT License

JavaScript 100.00%

from2's Introduction

from2 Flattr this!experimental

from2 is a high-level module for creating readable streams that properly handle backpressure.

Convience wrapper for readable-stream's ReadableStream base class, with an API lifted from from and through2.

Usage

from2

stream = from2([opts], read)

Where opts are the options to pass on to the ReadableStream constructor, and read(size, next) is called when data is requested from the stream.

  • size is the recommended amount of data (in bytes) to retrieve.
  • next(err) should be called when you're ready to emit more data.

For example, here's a readable stream that emits the contents of a given string:

var from = require('from2')

function fromString(string) {
  return from(function(size, next) {
    // if there's no more content
    // left in the string, close the stream.
    if (string.length <= 0) return next(null, null)

    // Pull in a new chunk of text,
    // removing it from the string.
    var chunk = string.slice(0, size)
    string = string.slice(size)

    // Emit "chunk" from the stream.
    next(null, chunk)
  })
}

// pipe "hello world" out
// to stdout.
fromString('hello world').pipe(process.stdout)

stream = from2.obj([opts], read)

Shorthand for from2({ objectMode: true }, read).

createStream = from2.ctor([opts], read)

If you're creating similar streams in quick succession you can improve performance by generating a stream constructor that you can reuse instead of creating one-off streams on each call.

Takes the same options as from2, instead returning a constructor which you can use to create new streams.

See Also

  • from2-array - Create a from2 stream based on an array of source values.
  • from2-string - Create a stream from a string. Sugary wrapper around from2.

License

MIT. See LICENSE.md for details.

from2's People

Contributors

mafintosh avatar hughsk avatar bcomnes avatar ryanramage avatar max-mapper avatar grncdr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.