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

freeloader-stream

v0.0.4

Published

Base class for FreeLoader streams

Downloads

78

Readme

freeloader-stream

Base class for freeloader modules.

Freeloader modules are just normal Transform streams in flowing mode. This base class provides the core functionality to make it easier.

Most modules should only worry about 4 things:

on('request')

The request event is emitted for every request going through. Always assume there will be more than 1 request, since an upstream module could be emitting several requests on a timer for example.

this.on('request', function(item) {
  // for each request
  console.log('Request: ', item.request.options.url);
  // for each response
  item.response.then(function(res) {
    console.log('Response: ', res.body);
  });
  // and pass along
  this.push(item);
});

The request item

  • item.request: a unirest request object
  • item.response: a Q promise for the response
  • item.clone(): returns a copy of the item, with the same request and a new response promise

Passing down requests

  • You can call this.push(item) to forward the item to the next module.
  • To multiply the number of requests, you can call this.push(item.clone()) several times.
  • Note that you don't have to forward every request.

this.end()

Being a writable stream, this function will be called when the upstream module pushes null, i.e. is no longer emitting requests. You can decide to forward the end with this.push(null), or ignore it to keep emitting requests yourself.

This is good place to generate reports. Note that this event will trigger while requests are still in flight. You can decide to wait for all pending reponses by calling q.all() on the response promises.

this.on('end', function() {
  console.log('No more upstream requests!');
});

on('pause')

This is called if a downstream module asked the pipeline to stop. Modules that decided to ignore the upstream end event must respond to pause. You must not emit any new requests after this.

this.on('pause', function() {
  console.log('Not sending any more requests after this');
});

this.pause()

This notifies upstream modules that you don't want to get any more requests. They should honor this, which will terminate the pipeline once the event loop is clear.

var myStream = this;
setTimeout(function() {
  myStream.pause();
}, 1000);

Full example

Here's a full example that prints every request and its response. You can find more examples at freeloader-bundle.

var util = require('util');
var FLS  = require('freeloader-stream');

function MyStream() {
  FLS.call(this);
  this.on('request', this.request);
}

util.inherits(MyStream, FLS);
MyStream.prototype.name = 'MyStream';

MyStream.prototype.request = function(item) {
  console.log('Req: ', item.request.options.url);
  item.response.then(function(res) {
    console.log('Res: ', res.body);
  });
  this.push(item);
};

MyStream.prototype.end = function() {
  console.log('Done!');
  this.push(null);
};

module.exports = function() {
  return new MyStream();
};