You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Promise = require("bluebird");
|
|
|
|
const through2 = require("through2");
|
|
|
|
const assureArray = require("assure-array");
|
|
|
|
|
|
|
|
function wrapStreamHandler(stream, handler, callback, item) {
|
|
|
|
Promise.try(() => {
|
|
|
|
return handler(item);
|
|
|
|
}).then((result) => {
|
|
|
|
return assureArray(result);
|
|
|
|
}).each((newItem) => {
|
|
|
|
stream.push(newItem);
|
|
|
|
}).then(() => {
|
|
|
|
// TODO: Fix the "a promise was created in a handler at [...] but was not returned from it" Bluebird warning that occurs here
|
|
|
|
callback();
|
|
|
|
}).catch((err) => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function stream(handler, flushHandler) {
|
|
|
|
let flushHandlerWrapper = (flushHandler == null)
|
|
|
|
? undefined
|
|
|
|
: function (callback) {
|
|
|
|
wrapStreamHandler(this, flushHandler, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
return through2.obj(function (item, _encoding, callback) {
|
|
|
|
wrapStreamHandler(this, handler, callback, item);
|
|
|
|
}, flushHandlerWrapper);
|
|
|
|
};
|