Example #1
0
module.exports = function () {
    var grouper = through(write, end);
    var current;
    
    function write (line, _, next) {
        if (line.length === 0) return next();
        var row = JSON.parse(line);
        
        if (row.type === 'genre') {
            if (current) {
                this.push(JSON.stringify(current) + '\n');
            }
            current = { name: row.name, books: [] };
        }
        else if (row.type === 'book') {
            current.books.push(row.name);
        }
        next();
    }
    function end (next) {
        if (current) {
            this.push(JSON.stringify(current) + '\n');
        }
        next();
    }
    
    return combine(split(), grouper, zlib.createGzip());
};
Example #2
0
const compileAllHbs = (templateData, dest) => {
  const Helpers = require('./helpers.js')
  const helpers = new Helpers(handlebars.Handlebars, templateData)

  // Delete `Helpers` from require cache so that next `require` gets new version
  Object.keys(require.cache).forEach((key) => {
    if (key.indexOf('helpers.js') !== -1 && key.indexOf('node_modules') === -1) {
      delete require.cache[key]
    }
  })

  // Handlebars options, `batch` is where partials (templates in wiki case) are stored
  const hbsOptions = {
    batch: [ paths.partials ],
    helpers
  }

  console.log(hbsOptions)

  // Combiner stream since series of pipes doesnt work
  return combiner(
    gulp.src(globs.hbs),
    handlebars(templateData, hbsOptions),
    rename(path => path.extname = '.html'),
    header(headerCreator('html')),
    gulp.dest(dest)
  ).on('end', () => browserSync.reload())
}
Example #3
0
    return function (req, res, m) {
        var feed = ixf.feed.createReadStream({ reverse: true });
        
        var html = template();
        var event = html.template('event');
        
        feed.pipe(through.obj(ewrite)).pipe(event);

        var input = through(), output = through();
        counts.get(function (err, c) {
            if (err) return m.error(err);
            input.pipe(hyperstream({
                '[key=collectives]': membership.collectiveNamesSentence(settings),
                '[key=collectives-list]': Object.keys(settings.collectives).map(function(collective) {
                    return '<li><a href="c/'+collective+'">'+settings.collectives[collective].name+'</a></li>';
                }).join("\n"),
                '[key=member-count]': c.member || 0,
                '[key=user-count]': c.user || 0
            })).pipe(output);
        });
        
        var modify = duplexer(input, output);
        return combine(modify, html);
        
        function ewrite (update, enc, next) {
            var self = this;
            var limit = 5;
            
            update.value.map(function (row) {
                if (self.count >= limit) return;
                if (row.type === 'put' && row.value
                && row.value.type === 'user') {
                    self.push(userUpdate(row.value));
                    self.count = (self.count || 0) + 1;
                }
            });
            if (self.count < limit) next()
            else self.push(null)
        }
        
        function userUpdate (user) {
            var name = user.name;
            var msg = user.created === user.updated
                ? 'created an account'
                : 'updated their profile'
            ;
            return {
                '[key=name]': { _text: name, href: '~' + name },
                '[key=msg]': { _text: msg },
                '[key=ago]': { _text: timeago(user.updated) },
                '[key=date]': { _text: user.updated }
            };
        }
    };
module.exports = function (file) {
  if (!/\.txt$/.test(file)) return through();
  var num = 0;
  var liner = through(function write (buf, enc, next) {
    var line = buf.toString('utf8') + '\n';
    if (num % 5 === 0) {
      this.push(sprintf('%3d %s', num, line));
    }
    else this.push('    ' + line);
    num ++;
    next();
  });
  var prefix = through();
  prefix.push('module.exports=');
  return combine([ split(), liner, quote(), prefix ]);
};
Example #5
0
module.exports = function (opts) {
  if (typeof opts === 'function') {
    opts = { keys: opts }
  }

  opts = xtend(defaults, opts)

  var keyOpts    = pick(opts, ['defaultValue', 'bare'])
    , detectOpts = pick(opts, 'phpexcel')

  return pipeline (
    detect(detectOpts),
    keys(keyOpts, opts.keys),
    coerce()
  )
}
Example #6
0
Decompress.prototype.createStream = function () {
	this.streams.unshift(vinylAssign({extract: true}));
	this.streams.unshift(this.getFiles());

	if (this.streams.length === 2) {
		this.use(Decompress.tar(this.opts));
		this.use(Decompress.tarbz2(this.opts));
		this.use(Decompress.targz(this.opts));
		this.use(Decompress.zip(this.opts));
	}

	if (this.dest()) {
		this.streams.push(vinylFs.dest(this.dest()));
	}

	return streamCombiner(this.streams);
};
Example #7
0
module.exports = function argosyClient () {
    var sequence    = 0,
        outstanding = [],
        input       = split(),
        parse       = objectify(function (chunk, enc, cb) { cb(null, JSON.parse(chunk)) }),
        output      = objectify.deobj(function (msg, enc, cb) { cb(null, JSON.stringify(msg) + '\n') }),
        responses   = filter.obj(function (msg) {
            return (msg.type === 'response' && msg.headers.client.id === client.id)
        })

    var processMessage = through2.obj(function parse(msg, enc, cb) {
        outstanding.filter(function (pending) {
            return pending.seq === msg.headers.client.seq
        }).forEach(function (pending) {
            if (msg.error) pending.reject(assign(new Error(msg.error.message), { remoteStack: msg.error.stack }))
            else pending.resolve(msg.body)
        })
        cb()
    })

    var client = pipeline(input, parse, responses, processMessage, output)
    client.id = uuid()
    client.invoke = function invoke (msgBody, cb) {
        var request = { type: 'request', headers: { client: { id: client.id, seq: sequence++ } }, body: msgBody },
            cb      = cb || function () {}

        var done = new Promise(function (resolve, reject) {
            outstanding.push({ seq: request.headers.client.seq, resolve: resolve, reject: reject })
            output.write(request)
        })
        done.then(function (body) {
            setImmediate(cb.bind(undefined, null, body))
        })
        done.catch(function (err) {
            setImmediate(cb.bind(undefined, err))
        })
        return done
    }
    client.invoke.partial = function invokePartial (partialBody) {
        return function partialInvoke (msgBody, cb) {
            return client.invoke(assign({}, partialBody, msgBody), cb)
        }
    }

    return client
}
Example #8
0
WikiDB.prototype.byTag = function (opts) {
    if (!opts) opts = {};
    if (typeof opts === 'string') opts = { tag: opts };
    var r = this.db.createReadStream({
        gt: [ 'wiki-tag', opts.tag, defined(opts.gt, null) ],
        lt: [ 'wiki-tag', opts.tag, defined(opts.lt, undefined) ],
        limit: opts.limit
    });
    return combine([ r, through.obj(write) ]);
    
    function write (row, enc, next) {
        this.push({
            key: row.key[2],
            hash: row.key[3]
        });
        next();
    }
};
Example #9
0
Download.prototype.createStream = function (file, dest) {
	var stream = new PassThrough({objectMode: true});
	var streams = [stream];

	stream.end(file);

	if (this.opts.extract) {
		streams.push(decompress(this.opts));
	}

	if (this.rename()) {
		streams.push(rename(this.rename()));
	}

	if (dest) {
		streams.push(vinylFs.dest(dest, this.opts));
	}

	return streamCombiner(streams);
};
Example #10
0
module.exports = function () {
    return combine(
            split(),
            parse(),
            through.obj(function (req, enc, cb) {
                var response = {};
                //care about the str
                if(req.error){
                    response.ok = false;
                    response.error = req.error;
                }
                else if(req.str){
                    response.upperCased = req.str.toUpperCase();
                    response.ok = true;
                    response.original = req.str;
                }
                this.push(response);
                cb();
            }),
            encode()
            );
}
Example #11
0
module.exports = function (opts) {
  opts || (opts = {})

  var input    = pick(opts, ['defaultValue', 'keys', 'phpexcel'])
    , cfg      = formats[opts.output]
    , keys     = cfg && cfg[0]
    , override = cfg && cfg[1]

  if (typeof keys === 'string') {
    // It's a shorthand
    override = xtend(override, formats[keys][1])
    keys = formats[keys][0]
  }

  var output = xtend(
    keys ? pick(opts, keys) : null,
    { format: opts.output },
    override
  )

  return pipeline( tabular(input), formatter(output) )
}
Example #12
0
module.exports = function(opts, selector) {
  if (isString(opts)) {
    return module.exports({}, opts);
  }

  if (!isObject(opts)) {
    return module.exports({}, selector);
  }

  var parser = sax.createStream(Boolean(opts.strict), defaults(opts, {
    trim: true,
    normalize: true,
    lowercase: true,
    xmlns: false,
    position: true
  }));

  var stream = new XMLStream(defaults({
    parser: parser
  }, opts), selector);

  return combine(parser, stream);
};
Example #13
0
 import: function (indexName) {
     console.log("importing data to " + indexName);
     return combine(cleaner, bulker(indexName, 1024), writer(indexName));
 }
function parserFactory(){
  return bun([ osm(), expander() ]);
}
 function concatAndUglify() {
     return combiner(
         concat(finalName),
         uglify()
     )
 }
Example #16
0
module.exports = function () {
  return combine(tokenize(), parse());
};
Example #17
0
module.exports = function argosy (options) {
    options = assign({ id: uuid() }, options)

    var requestSeq     = 0,
        localServices  = [],
        remoteServices = [],
        outstanding    = [],
        input          = split(),
        parse          = objectify(function (chunk, enc, cb) { cb(null, JSON.parse(chunk)) }),
        output         = objectify.deobj(function (msg, enc, cb) { cb(null, JSON.stringify(msg) + '\n') })

    var processMessage = through2.obj(function parseMessage (msg, enc, cb) {
        switch (msg.type) {
            case 'request':
                queue(msg.body, function done (err, result) {
                    var reply = { type: 'response', headers: assign(msg.headers, { servicer: { id: options.id } }), body: result }
                    if (err) reply.error = { message: err.message, stack: err.stack }
                    processMessage.push(reply)
                })
                break
            case 'response':
                outstanding.filter(function (pending) {
                    return (msg.headers.consumer.id === options.id && pending.seq === msg.headers.consumer.seq)
                }).forEach(function (pending) {
                    if (msg.error) pending.reject(assign(new Error(msg.error.message), { remoteStack: msg.error.stack }))
                    else pending.resolve(msg.body)
                })
                break
            case 'subscribe':
                var syncMessage = { id: options.id }
                if (~msg.body.indexOf('services')) {
                    stream.localServiceAdded.removeConsumer(announceService)
                    stream.localServiceAdded(announceService)
                    localServices.forEach(announceService)
                    syncMessage.services = localServices.length
                }
                output.write({ type: 'synced', body: syncMessage })
                break
            case 'announce-service':
                remoteServices.push({ provider: msg.body.provider, pattern: pattern.decode(msg.body.pattern) })
                stream.serviceAdded.produce({ remote: true, provider: msg.body.provider, pattern: pattern.decode(msg.body.pattern) })
                break
            case 'synced':
                stream.synced.produce(msg.body)
                break
        }
        cb()
    })

    var stream = assign(pipeline(input, parse, processMessage, output), { id: options.id })

    stream.accept = function accept (rules) {
        var p = pattern(rules),
            q = cq()

        localServices.push({ pattern: p, queue: q })
        stream.serviceAdded.produce({ local: true, provider: { id: options.id }, pattern: p })
        return q
    }
    stream.invoke = function invoke (msgBody, cb) {
        var done = (serviceable(msgBody, localServices))
            ? queue(msgBody) // if we implement it ourself, stay in-process
            : stream.invoke.remote(msgBody) // otherwise, head out to sea
        return after(done, cb)
    }
    stream.invoke.remote = function invokeRemote (msgBody, cb) {
        var request = { type: 'request', headers: { consumer: { id: options.id, seq: requestSeq++ } }, body: msgBody }

        var done = new Promise(function (resolve, reject) {
            outstanding.push({ seq: request.headers.consumer.seq, resolve: resolve, reject: reject })
            output.write(request)
        })
        return after(done, cb)
    }
    stream.invoke.partial = function invokePartial (partialBody) {
        return function partialInvoke (msgBody, cb) {
            if (typeof msgBody === 'function') {
                cb = msgBody
                msgBody = {}
            }
            return stream.invoke(assign({}, partialBody, msgBody), cb)
        }
    }
    stream.subscribeRemote = function subscribeRemote (msgBody, cb) {
        assert(Array.isArray(msgBody), 'subscribeRemote requires an array of subscriptions')
        output.write({ type: 'subscribe', body: msgBody })
        return after(once(stream.synced), cb)
    }
    stream.synced = eventuate()
    stream.serviceAdded = eventuate()
    stream.remoteServiceAdded = filter(stream.serviceAdded, function (svc) { return svc.remote })
    stream.localServiceAdded = filter(stream.serviceAdded, function (svc) { return svc.local })
    stream.pattern = pattern

    Object.defineProperties(stream, {
        services: { get: function () {
            return localServices.map(function (svc) {
                return { local: true, provider: { id: options.id }, pattern: svc.pattern }
            }).concat(remoteServices.map(function (svc) {
                return { remote: true, provider: svc.provider, pattern: svc.pattern }
            }))
        }}
    })

    // can the message be routed to a service
    function serviceable (msgBody, services) {
        return (services.length && services.some(function acceptsMessage (svc) {
            return svc.pattern.matches(msgBody)
        }))
    }

    // find the right queue
    function queue (msgBody, cb) {
        var service = find(localServices, function (svc) {
            return svc.pattern.matches(msgBody)
        })
        if (!service) return cb(new Error('not implemented: ' + JSON.stringify(msgBody)))
        return service.queue(msgBody, cb)
    }

    function announceService (svc) {
        output.write({ type: 'announce-service', body: { provider: { id: options.id }, pattern: svc.pattern.encode() } })
    }

    return stream
}
Example #18
0
var phpexcel = php && function() {
  return pipeline( php(), csv() )
}
Example #19
0
function encoder () {
    return combine(u8(), through(function (buf, enc, next) {
        this.push(ent.encode(buf.toString('utf8')));
        next();
    }));
}