Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
freeloader-stream
Advanced tools
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:
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);
});
item.request
: a unirest request objectitem.response
: a Q promise for the responseitem.clone()
: returns a copy of the item, with the same request and a new response promisethis.push(item)
to forward the item to the next module.this.push(item.clone())
several times.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!');
});
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 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);
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();
};
FAQs
Base class for FreeLoader streams
The npm package freeloader-stream receives a total of 37 weekly downloads. As such, freeloader-stream popularity was classified as not popular.
We found that freeloader-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.