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

list-stream

v2.1.0

Published

Collect chunks / objects from a readable stream, write obejcts / chunks to a writable stream

Downloads

19,587

Readme

ListStream

Build Status

Collect chunks / objects from a readable stream, write obejcts / chunks to a writable stream

NPM

Built on a DuplexStream, ListStream can collect the chunks as they come in so you can use them later as an array, or pipe them on to another stream.

Using the same callback style as bl and a similar style to concat-stream, you can use ListStream as a terminal stream collecting the chunks or objects for use once the stream has ended. This is particularly helpful for object streams where each chunk is a discrete object.

var ListStream = require('list-stream')
  , db = require('level')('/path/to/db', { valueEncoding: 'json' }) // stream from LevelDB!

db.createValueStream().pipe(ListStream.obj(function (err, data) {
  if (err)
    throw err

  console.log('Values in the database:')
  // `data` is an array of objects from the database, serialised from JSON strings
  data.forEach(function (value, i) {
    console.log(i, JSON.stringify(value))
  })
}))

Or emulate fs.readFile():

var ListStream = require('list-stream')
  , fs = require('fs')

fs.createReadStream('/path/to/file.dat').pipe(ListStream(function (err, data) {
  if (err)
    throw err

  console.log('Contents of /path/to/file.dat:')
  // `data` is an array of Buffer objects
  console.log(Buffer.concat(data).toString('utf8'))
}))

See bl for nicer Buffer stream handling

Or use as a store to stream from later

var list = require('list-stream').obj()
  , db = require('level')('/path/to/db')

list.write({ key: 'name', value: 'Yuri Irsenovich Kim' })
list.write({ key: 'dob', value: '16 February 1941' })
list.write({ key: 'spouse', value: 'Kim Young-sook' })
list.write({ key: 'occupation', value: 'Clown' })

list.pipe(db.createWriteStream()) // write all of the stored entries to a database

API

  • [new ]ListStream([ options, ][ callback ])
  • ListStream.obj([ options, ][ callback ])
  • listStream.length
  • listStream.append(obj)
  • listStream.get(index)
  • listStream.end()
  • listStream.duplicate()

[new ]ListStream([ options, ][ callback ])

Create a new ListStream with the given options which will be passed up to the parent DuplexStream.

The optional callback will be called when the stream reaches an end event. You will receive both an error argument and a data argument where the error will come off 'error' events emitted by any piped stream and the data will be an Array of chunks or objects fed in to the stream.


ListStream.obj([ options, ][ callback ])

Same as the standard constructor but shorthand for new ListStream({ objectMode:true }, callback).

Use this for when you're not dealing with Buffers or Strings in your streams.


listStream.length

The number of chunks currently being held. (Not available in pre-ES5 environments as it uses a getter)


listStream.append(obj)

Append a chunk / object to the list.


listStream.get(index)

Get the chunk / object from the list at index index.


listStream.end()

End the stream. A standard WritableStream method, can be used when piping to another stream:

listStream.pipe(fs.createWriteStream(os.tmpDir() + '/randombytes.dat'))

for (var i = 0; i < 100)
  listStream.append(crypto.randomBytes(32))

listStream.end()

listStream.duplicate()

Create a full duplicate of this ListStream. Each item in the list will be .append()ed to the new copy. The copy will be returned.

License

ListStream is Copyright (c) 2014 Rod Vagg @rvagg and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.