
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
stream-from-factory
Advanced tools

Create streams from sync and async factories.
npm install stream-from-factory --save
A factory is simply a function creating a product (i.e. a JavaScript value, like numbers, objects, etc.) and sends it back to its caller.
(The term factory refers to the idea of Joshua Blochs static factory methods, not to abstract factories, nor to factory methods.)
Synchronous Factories are functions returning a single product - other than undefined - or throwing an arbitrary JavaScript value:
function syncFactory() {
if (...) {
throw new Error('sth. went wrong...'); // typically Errors are thrown
}
return null; // that's ok (typeof null !== 'undefined'), but see below (API)
}
Asynchronous Factories working with callbacks (in node style), but don't return a value;
function asyncFactory(done) {
if (...) {
// return an error:
done(new Error('sth. went wrong...'));
return; // that's ok (!)
}
// return a result:
done(null, ['a', 'string', 'array']);
}
String | Buffersvar StreamFromFactory = require('stream-from-factory');
function syncFactory() {
return new Buffer('buff!');
}
function asyncFactory(done) {
setTimeout(function() {
done(null, 'strrrring!');
}, 1000);
}
StreamFromFactory(syncFactory)
.pipe(process.stdout); // output: buff!
StreamFromFactory(asyncFactory)
.pipe(process.stdout); // output: strrrring!
var StreamFromFactory = require('stream-from-factory');
function logFunc(){
console.log('func!?!');
};
function asyncFactory(done) {
setTimeout(function() {
done(null, logFunc);
}, 1000);
}
StreamFromFactory.obj(asyncFactory)
.on('data', function(fn){
fn(); // output: func!?!
});
var StreamFromFactory = require('stream-from-factory');
function syncFactory() {
throw new Error('sth. went wrong ;-)');
}
function asyncFactory(done) {
setTimeout(function() {
done(new Error('sth. went wrong ;-)'));
}, 1000);
}
StreamFromFactory(syncFactory)
.on('error', function(err){
console.log(err); // output: [Error: sth. went wrong ;-)]
})
.on('data', function(data){
// do something awsome
});
Gulp files are vinyl files:
npm install vinyl
Test some awsome Gulp plugin:
var StreamFromFactory = require('stream-from-factory'),
File = require('vinyl');
function creatTestFile(){
return new File({
cwd: '/',
base: '/hello/',
path: '/hello/hello.js',
contents: new Buffer('console.log("Hello");')
});
}
StreamFromFactory.obj(creatTestFile)
.pipe(someAwsomeGulpPlugin())
.on('data', function(file){
console.log(file.contents.toString()); // dunno what someAwsomeGulpPlugin does :)
});
See also stream-recorder for testing gulp plugins.
StreamFromFactorys are Readable streams.
Function Async or sync factory.Object passed through new Readable([options])Notes:
new operator can be omitted.null is special for streams (signals end-of-stream). Using a factory returning null will result in an empty stream.A convenience wrapper for new StreamFromFactory(factory, {objectMode: true, ...}).
Copyright (c) 2014 Michael Mayer
Licensed under the MIT license.
FAQs
Create streams from sync and async factories
We found that stream-from-factory demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.