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

streammagic

v1.0.0

Published

Convert anything to a stream

Downloads

421

Readme

Stream Magic

Build Status npm GitHub issues Known Vulnerabilities GitHub license

A simple module that can convert any variable into a Node.js stream

What it does

Stream Magic extends the prototypes of all variable types within Node.JS with a .toStream() method, which lets you easily transform objects and variables into streams.

A safe mode that doesn't extend prototypes is also available.

Installation

You can install the streammagic package using npm.

$ npm install streammagic

Usage

The module exports a single function that attaches the .toStream() method to the prototypes. To use it, simply require the module and run the function once.

// No need to assign this as the function doesn't return anything
require('streammagic')();

Once this is done, the .toStream() method should be available on all variables.

// Logs 'hello world' to stdout (the console)
let myString = 'hello world';
let myStream = myString.toStream();
myStream.pipe(process.stdout);

// Or shorter
'hello world'.toStream().pipe(process.stdout)

Safe mode

The safe mode is available as a method of the module. Simply require it like this:

const toStream = require('streammagic').toStream;

// Same as above
let myString = 'hello world';
let myStream = toStream(myString);
myStream.pipe(process.stdout);

// Short version
toStream('hello world').pipe(process.stdout);

Datatypes

Primitive datatypes

All primitive datatypes (number, boolean, string) will be pushed to the stream in one piece. This cause a slight performance loss for long strings, depending on the actions of the subsequent pipes. This is something that may be addressed later on.

// Boolean
let stream = false.toStream() // stream.on('data') will contain: false

// Number
let stream = (35).toStream() // stream.on('data') will contain: 35

// String
let stream = 'foo'.toStream() // stream.on('data') will contain: foo

Arrays

Arrays will be piped one item at a time. This can be useful for processing datasets.

let myArray = ['hello', 'world'];
let myStream = myArray.toStream();

myStream.on('data', function(data){
	// The data event will fire twice. Data will contain 'hello' the first time, 'world' the second.
});

Objects

Objects are piped one property at a time as {key: value} objects. Keep this in mind, as assembling the original object on the other end of the pipe will require some manual work.

let myObject = {
	hello: 'world',
	foo: 'bar'
};
let myStream = myObject.toStream();

myStream.on('data', function(data){
	// The data event will fire twice. Data will contain {hello: 'world'} the first time, {foo: 'bar'} the second.
});
Tip

If you need an object or an array to be piped as one instead of being split up, you can simply wrap it inside another array. Only the outermost array or object will be split.

let myArray = ['hello', 'world'];
let myStream = [myArray].toStream();

myStream.on('data', function(data){
	console.log(data); // ['hello', 'world']
});

Buffers

Node buffers will be piped as they are, just like primitive datatypes.

Issues

If you find any bugs or problems with this module, please create an issue so we can look into it. Pull requests with bugfixes are of course welcome.

License

This module is licensed under the MIT License.