'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var http = _interopDefault(require('http'));
var util = _interopDefault(require('util'));
var events = _interopDefault(require('events'));
var string_decoder = _interopDefault(require('string_decoder'));
var cluster = _interopDefault(require('cluster'));
var fs = require('fs');

var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};





function createCommonjsModule(fn, module) {
	return module = { exports: {} }, fn(module, module.exports), module.exports;
}

function Queue() {
    this._in = [];
    this._out = [];
    this.length = 0;
}
Queue.prototype.enqueueAll = function enqueueList(list) {
    this._in = this._in.concat(list);
    this.length += list.length;
};
Queue.prototype.enqueue = function enqueue(item) {
    this._in.push(item);
    this.length++;
};
Queue.prototype.dequeue = function dequeue() {
    if (this._out.length) {
        this.length--;
        return this._out.pop();
    } else if (this._in.length) {
        this._out = this._in.reverse();
        this._in = [];
        this.length--;
        return this._out.pop();
    } else {
        return undefined;
    }
};
Queue.prototype.clear = function clear() {
    this._out = [];
    this._in = [];
    this.length = 0;
};
Queue.prototype.toArray = function toArray() {
    var res = [];
    res = res.concat(this._out);
    res.reverse();
    res = res.concat(this._in);
    return res;
};
Queue.prototype.toString = function toString() {
    return this.toArray().toString();
};
var queue = Queue;

var intMap = createCommonjsModule(function (module) {
var hasOwn = Object.prototype.hasOwnProperty;
function IntMap() {
    this.map = {};
    this.size = 0;
}
IntMap.prototype.set = function set(key, value) {
    if (this.has(key)) {
        this.map[key] = value;
    } else {
        this.map[key] = value;
        this.size++;
    }
    return this;
};
IntMap.prototype.get = function get(key) {
    return this.map[key];
};
IntMap.prototype.delete = function intMapDelete(key) {
    var deleted = this.has(key);
    if (deleted) {
        delete this.map[key];
        this.size--;
    }
    return deleted;
};
IntMap.prototype.has = function has(key) {
    return hasOwn.call(this.map, key);
};
IntMap.prototype.forEach = function forEach(f, thisArg) {
    for (var key in this.map) {
        f.call(thisArg, this.map[key], key, this);
    }
};
if (commonjsGlobal.Map === void 0) {
    module.exports = IntMap;
} else {
    module.exports = commonjsGlobal.Map;
}
});

var index = createCommonjsModule(function (module, exports) {
var inherits = util.inherits;
var EventEmitter = events.EventEmitter;
var Decoder = string_decoder.StringDecoder;
var slice = Array.prototype.slice;
var hasOwn = Object.prototype.hasOwnProperty;
var _global = commonjsGlobal;
if (typeof commonjsGlobal !== 'undefined') {
    _global = commonjsGlobal;
} else if (typeof window !== 'undefined') {
    _global = window;
}
var _ = exports = module.exports = __(Stream);
function __(StreamCtor) {
    return function (             xs,             secondArg,             mappingHint) {
        var s = null;
        if (_.isUndefined(xs)) {
            s = new StreamCtor();
            s.writable = true;
        } else if (_.isStream(xs)) {
            if (!(xs instanceof StreamCtor)) {
                var ret = new StreamCtor();
                xs.on('error', ret.write.bind(ret));
                s = xs.pipe(ret);
            } else {
                s = xs;
            }
        } else if (_.isArray(xs)) {
            s = new StreamCtor();
            s._outgoing.enqueueAll(xs);
            s._outgoing.enqueue(_.nil);
        } else if (_.isFunction(xs)) {
            s = new StreamCtor(xs);
        } else if (_.isObject(xs)) {
            if (_.isFunction(xs.on) && _.isFunction(xs.pipe)) {
                var onFinish = _.isFunction(secondArg) ? secondArg : defaultReadableOnFinish;
                s = new StreamCtor();
                s.writable = true;
                pipeReadable(xs, onFinish, s);
                return s.map(function (x) {
                    return x;
                });
            } else if (_.isFunction(xs.then)) {
                s = promiseStream(StreamCtor, xs);
            }
            else if (_.isFunction(xs.next)) {
                    return iteratorStream(StreamCtor, xs);
                } else if (!_.isUndefined(_global.Symbol) && xs[_global.Symbol.iterator]) {
                    return iteratorStream(StreamCtor, xs[_global.Symbol.iterator]());
                } else {
                    throw new Error('Object was not a stream, promise, iterator or iterable: ' + typeof xs);
                }
        } else if (_.isString(xs)) {
            var mapper = hintMapper(mappingHint);
            s = new StreamCtor();
            var callback_func = function () {
                var ctx = mapper.apply(this, arguments);
                s.write(ctx);
            };
            secondArg.on(xs, callback_func);
            var removeMethod = secondArg.removeListener
            || secondArg.unbind;
            if (removeMethod) {
                s._destructors.push(function () {
                    removeMethod.call(secondArg, xs, callback_func);
                });
            }
        } else {
            throw new Error('Unexpected argument type to Stream constructor: ' + typeof xs);
        }
        return s;
    };
}
var isES5 = function () {
    'use strict';
    return Function.prototype.bind && !this;
}();
function checkIsNumber(parameter, paramName) {
    if (typeof parameter != 'number') {
        throw new TypeError('Parameter "' + paramName + '" is not a number.');
    }
}
function checkRange(predicate, message) {
    if (!predicate) {
        throw new RangeError(message);
    }
}
_.isUndefined = function (x) {
    return typeof x === 'undefined';
};
_.isFunction = function (x) {
    return typeof x === 'function';
};
_.isObject = function (x) {
    return typeof x === 'object' && x !== null;
};
_.isString = function (x) {
    return typeof x === 'string';
};
_.isArray = Array.isArray || function (x) {
    return Object.prototype.toString.call(x) === '[object Array]';
};
if (typeof setImmediate === 'undefined') {
    _.setImmediate = function (fn) {
        setTimeout(fn, 0);
    };
} else {
    _.setImmediate = function (fn) {
        setImmediate(fn);
    };
}
if (!_global.nil) {
    _global.nil = {};
}
var nil = _.nil = _global.nil;
_.curry = function (fn              ) {
    var args = slice.call(arguments);
    return _.ncurry.apply(this, [fn.length].concat(args));
};
_.ncurry = function (n, fn              ) {
    var largs = slice.call(arguments, 2);
    if (largs.length >= n) {
        return fn.apply(this, largs.slice(0, n));
    }
    return _.partial.apply(this, [_.ncurry, n, fn].concat(largs));
};
_.partial = function (f              ) {
    var args = slice.call(arguments, 1);
    return function () {
        return f.apply(this, args.concat(slice.call(arguments)));
    };
};
_.flip = _.curry(function (fn, x, y) {
    return fn(y, x);
});
_.compose = function ()                 {
    var fns = slice.call(arguments).reverse();
    return _.seq.apply(null, fns);
};
_.seq = function () {
    var fns = slice.call(arguments);
    return function () {
        if (!fns.length) {
            return null;
        }
        var r = fns[0].apply(this, arguments);
        for (var i = 1; i < fns.length; i++) {
            r = fns[i].call(this, r);
        }
        return r;
    };
};
function nop() {
}
function defaultReadableOnFinish(readable, callback) {
    readable.once('error', callback);
    return function () {
        readable.removeListener('error', callback);
    };
}
function pipeReadable(xs, onFinish, stream) {
    var response = onFinish(xs, streamEndCb);
    var unbound = false;
    var cleanup = null;
    var endOnError = true;
    if (_.isFunction(response)) {
        cleanup = response;
    } else if (response != null) {
        cleanup = response.onDestroy;
        endOnError = !response.continueOnError;
    }
    xs.pipe(stream);
    stream.onDestroy(unbind);
    function streamEndCb(error) {
        if (stream._nil_pushed) {
            return;
        }
        if (error) {
            stream.write(new StreamError(error));
        }
        if (error == null || endOnError) {
            unbind();
            stream.end();
        }
    }
    function unbind() {
        if (unbound) {
            return;
        }
        unbound = true;
        if (cleanup) {
            cleanup();
        }
        if (xs.unpipe) {
            xs.unpipe(stream);
        }
    }
}
function newPullFunction(xs) {
    return function pull(cb) {
        xs.pull(cb);
    };
}
function newDelegateGenerator(pull) {
    return function delegateGenerator(push, next) {
        var self = this;
        pull(function (err, x) {
            var old = self._defer_run_generator;
            self._defer_run_generator = true;
            push(err, x);
            if (x !== nil) {
                next();
            }
            self._defer_run_generator = old;
            if (!old && self._run_generator_deferred) {
                self._runGenerator();
            }
        });
    };
}
function promiseStream(StreamCtor, promise) {
    var nilScheduled = false;
    return new StreamCtor(function (push) {
        promise = promise.then(function (value) {
            nilScheduled = true;
            _.setImmediate(function () {
                push(null, value);
                push(null, nil);
            });
            return null;
        }, function (err) {
            nilScheduled = true;
            _.setImmediate(function () {
                push(err);
                push(null, nil);
            });
            return null;
        });
        if (_.isFunction(promise['finally'])) {
            promise['finally'](function () {
                if (!nilScheduled) {
                    _.setImmediate(function () {
                        push(null, nil);
                    });
                }
                return null;
            });
        }
    });
}
function iteratorStream(StreamCtor, it) {
    return new StreamCtor(function (push, next) {
        var iterElem, iterErr;
        try {
            iterElem = it.next();
        } catch (err) {
            iterErr = err;
        }
        if (iterErr) {
            push(iterErr);
            push(null, _.nil);
        } else if (iterElem.done) {
            if (!_.isUndefined(iterElem.value)) {
                push(null, iterElem.value);
            }
            push(null, _.nil);
        } else {
            push(null, iterElem.value);
            next();
        }
    });
}
function hintMapper(mappingHint) {
    var mappingHintType = typeof mappingHint;
    var mapper;
    if (mappingHintType === 'function') {
        mapper = mappingHint;
    } else if (mappingHintType === 'number') {
        mapper = function () {
            return slice.call(arguments, 0, mappingHint);
        };
    } else if (_.isArray(mappingHint)) {
        mapper = function () {
            var args = arguments;
            return mappingHint.reduce(function (ctx, hint, idx) {
                ctx[hint] = args[idx];
                return ctx;
            }, {});
        };
    } else {
        mapper = function (x) {
            return x;
        };
    }
    return mapper;
}
function pipeStream(src, dest, write, end, passAlongErrors) {
    var resume = null;
    var s = src.consume(function (err, x, push, next) {
        var canContinue;
        if (err) {
            if (passAlongErrors) {
                canContinue = write.call(dest, new StreamError(err));
            } else {
                src.emit('error', err);
                canContinue = true;
            }
        } else if (x === nil) {
            end.call(dest);
            return;
        } else {
            canContinue = write.call(dest, x);
        }
        if (canContinue !== false) {
            next();
        } else {
            resume = next;
        }
    });
    dest.on('drain', onConsumerDrain);
    src.onDestroy(function () {
        dest.removeListener('drain', onConsumerDrain);
    });
    dest.emit('pipe', src);
    s.resume();
    return dest;
    function onConsumerDrain() {
        if (resume) {
            resume();
            resume = null;
        }
    }
}
function Stream(generator) {
    EventEmitter.call(this);
    this.__HighlandStream__ = true;
    this.id = ('' + Math.random()).substr(2, 6);
    this.paused = true;
    this.ended = false;
    this.readable = true;
    this.writable = false;
    this._outgoing = new queue();
    this._observers = [];
    this._destructors = [];
    this._send_events = false;
    this._nil_pushed = false;
    this._explicitly_destroyed = false;
    this._request = null;
    this._multiplexer = null;
    this._consumer = null;
    this._generator = generator;
    this._generator_requested = true;
    this._defer_run_generator = false;
    this._run_generator_deferred = false;
    var self = this;
    this._push_fn = function (err, x) {
        if (x === nil) {
            self._run_generator_deferred = false;
        }
        self._writeOutgoing(err ? new StreamError(err) : x);
    };
    this._next_fn = function (xs) {
        if (self._explicitly_destroyed) {
            return;
        }
        if (self._nil_pushed) {
            throw new Error('Cannot call next after nil');
        }
        self._generator_requested = true;
        if (xs) {
            xs = self.create(xs);
            var pull = newPullFunction(xs);
            self._generator = newDelegateGenerator(pull);
        }
        if (!self.paused) {
            if (self._defer_run_generator) {
                self._run_generator_deferred = true;
            } else {
                _.setImmediate(function () {
                    self._runGenerator();
                });
            }
        }
    };
    this.on('newListener', function (ev) {
        if (ev === 'data') {
            self._send_events = true;
            _.setImmediate(self.resume.bind(self));
        } else if (ev === 'end') {
            self._send_events = true;
        }
    });
    this.on('removeListener', function (ev) {
        if (ev === 'end' || ev === 'data') {
            var end_listeners = self.listeners('end').length;
            var data_listeners = self.listeners('data').length;
            if (end_listeners + data_listeners === 0) {
                self._send_events = false;
            }
        }
    });
}
inherits(Stream, EventEmitter);
function _addMethod(proto, topLevel) {
    return function (name, f) {
        proto[name] = f;
        var n = f.length;
        function relevel(coerce) {
            return _.ncurry(n + 1, function () {
                var args = slice.call(arguments);
                var s = coerce(args.pop());
                return f.apply(s, args);
            });
        }
        topLevel[name] = relevel(topLevel);
        topLevel[name]._relevel = relevel;
    };
}
var addMethod = _addMethod(Stream.prototype, _);
function _addMethods(proto, topLevel, methods) {
    for (var p in methods) {
        if (hasOwn.call(methods, p)) {
            _addMethod(proto, topLevel)(p, methods[p]);
        }
    }
}
function _addToplevelMethod(topLevel) {
    return function (name, fn) {
        function relevel(_topLevel) {
            var bound = fn.bind(_topLevel);
            bound._relevel = relevel;
            return bound;
        }
        topLevel[name] = relevel(topLevel);
    };
}
var addToplevelMethod = _addToplevelMethod(_);
function _addToplevelMethods(topLevel, methods) {
    for (var p in methods) {
        if (hasOwn.call(methods, p)) {
            _addToplevelMethod(topLevel)(p, methods[p]);
        }
    }
}
function use(Super, originalTopLevel) {
    return function (methods, toplevelMethods) {
        function Sub() {
            Stream.apply(this, arguments);
        }
        inherits(Sub, Super);
        function topLevel() {
            return __(Sub).apply(null, arguments);
        }
        for (var p in originalTopLevel) {
            if (hasOwn.call(originalTopLevel, p)) {
                var fn = originalTopLevel[p];
                topLevel[p] = typeof fn._relevel === 'function' ? fn._relevel(topLevel) : fn;
            }
        }
        _addMethods(Sub.prototype, topLevel, methods || {});
        _addToplevelMethods(topLevel, toplevelMethods || {});
        topLevel.use = use(Sub, topLevel);
        return topLevel;
    };
}
_.use = use(Stream, _);
addToplevelMethod('of', function (x) {
    return _([x]);
});
addToplevelMethod('fromError', function (error) {
    return _(function (push) {
        push(error);
        push(null, _.nil);
    });
});
Stream.prototype.create = function () {
    return __(this.constructor).apply(null, arguments);
};
Stream.prototype.createChild = function createChild()            {
    var child = this.create.apply(this, arguments);
    child.onDestroy(this.destroy.bind(this));
    return child;
};
function StreamError(err) {
    this.__HighlandStreamError__ = true;
    this.error = err;
}
_.isStream = function (x) {
    return _.isObject(x) && !!x.__HighlandStream__;
};
_._isStreamError = function (x) {
    return _.isObject(x) && !!x.__HighlandStreamError__;
};
_._isStreamRedirect = function (x) {
    return _.isObject(x) && !!x.__HighlandStreamRedirect__;
};
Stream.prototype._send = function (token) {
    var err = null,
        x;
    if (_._isStreamError(token)) {
        err = token.error;
        this.readable = false;
    } else {
        x = token;
    }
    if (x === nil) {
        this.readable = false;
    }
    if (this._request) {
        var cb = this._request;
        this._request = null;
        this.pause();
        cb(err, x);
    }
    var observers = this._observers;
    for (var j = 0, len2 = observers.length; j < len2; j++) {
        observers[j].write(token);
    }
    if (this._send_events) {
        if (err) {
            this.emit('error', err);
        } else if (x === nil) {
            this.emit('end');
        } else {
            this.emit('data', x);
        }
    }
    if (x === nil) {
        this._onEnd();
    }
};
Stream.prototype._onEnd = function _onEnd() {
    if (this.ended) {
        return;
    }
    this.pause();
    this.ended = true;
    var i, len, observer;
    for (i = 0, len = this._observers.length; i < len; i++) {
        observer = this._observers[i];
        if (observer.source === this) {
            observer.source = null;
        }
    }
    for (i = 0, len = this._destructors.length; i < len; i++) {
        this._destructors[i].call(this);
    }
    this._outgoing.clear();
    this._observers = [];
    this._destructors = [];
    this._request = null;
    this._multiplexer = null;
    this._consumer = null;
    this._generator = null;
    this._generator_requested = false;
    this._defer_run_generator = false;
    this._run_generator_deferred = false;
};
addMethod('pause', function () {
    this.paused = true;
});
Stream.prototype._emitNext = function (num) {
    var emitted = 0;
    while ((num == null || emitted < num) && this._outgoing.length && !this.paused) {
        this._send(this._outgoing.dequeue());
        emitted++;
    }
};
addMethod('resume', function () {
    if (!this.paused || this._consumer && this._consumer.paused || this._multiplexer && this._multiplexer.paused) {
        return;
    }
    this.paused = false;
    if (this._outgoing.length) {
        this._emitNext();
        if (this.paused) {
            return;
        }
    }
    if (this._generator) {
        this._runGenerator();
    } else {
        this.emit('drain');
    }
});
addMethod('end', function () {
    if (this._nil_pushed) {
        return;
    }
    this.write(nil);
});
addMethod('pipe', function (dest, options) {
    options = options || {};
    var canClose = dest !== process.stdout && dest !== process.stderr && options.end !== false;
    var end;
    if (canClose) {
        end = dest.end;
    } else {
        end = nop;
    }
    return pipeStream(this, dest, dest.write, end, false);
});
addMethod('destroy', function () {
    this._explicitly_destroyed = true;
    if (this.ended) {
        return;
    }
    this.readable = this.writable = false;
    if (!this._nil_pushed) {
        this.end();
    }
    this._onEnd();
});
Stream.prototype._writeOutgoing = function _writeOutgoing(token) {
    if (this._explicitly_destroyed) {
        return;
    }
    if (this._nil_pushed) {
        throw new Error('Cannot write to stream after nil');
    }
    if (token === _.nil) {
        this._nil_pushed = true;
    }
    if (token === nil || _._isStreamError(token)) {
        this.writable = false;
    }
    if (this.paused) {
        this._outgoing.enqueue(token);
    } else {
        this._send(token);
    }
};
Stream.prototype._runGenerator = function () {
    if (!this._generator_requested) {
        return;
    }
    if (this._defer_run_generator) {
        this._run_generator_deferred = true;
        return;
    }
    this._defer_run_generator = true;
    do {
        this._run_generator_deferred = false;
        this._generator_requested = false;
        var gen = this._generator;
        if (gen.__ConsumeGenerator__) {
            var source = gen.source;
            if (source._outgoing.length) {
                var token = source._outgoing.dequeue();
                source._request = gen.pullCb;
                source.paused = false;
                source._send(token);
            } else {
                source._request = gen.pullCb;
                source.resume();
            }
        } else {
            gen.call(this, this._push_fn, this._next_fn);
        }
    } while (this._run_generator_deferred);
    this._defer_run_generator = false;
};
Stream.prototype._removeObserver = function (s) {
    this._observers = this._observers.filter(function (o) {
        return o !== s;
    });
    if (s.source === this) {
        s.source = null;
    }
};
addMethod('consume', function (f) {
    var source = this,
        consumer;
    if (this._consumer) {
        throw new Error('Stream already being consumed, you must either fork() or observe()');
    }
    if (this._multiplexer) {
        throw new Error('Stream has been forked. You must either fork() or observe().');
    }
    var gen = function () {
        source.pull(pullCb);
    };
    gen.__ConsumeGenerator__ = true;
    gen.pullCb = pullCb;
    gen.source = source;
    consumer = this._consumer = this.createChild(gen);
    consumer.id = 'consume:' + consumer.id;
    return consumer;
    function pullCb(err, x) {
        if (consumer._defer_run_generator) {
            f(err, x, consumer._push_fn, consumer._next_fn);
        } else {
            consumer._defer_run_generator = true;
            f(err, x, consumer._push_fn, consumer._next_fn);
            consumer._defer_run_generator = false;
            if (consumer._run_generator_deferred) {
                consumer._runGenerator();
            }
        }
    }
});
addMethod('pull', function (f) {
    if (f == null) {
        throw new Error('Cannot pull() with a null callback.');
    }
    if (this._request) {
        f(new Error('Cannot service a second pull() request while one is in progress.'));
    }
    this._request = f;
    this.resume();
});
addMethod('write', function (x) {
    this._writeOutgoing(x);
    return !this.paused;
});
addMethod('fork', function () {
    if (this._requests) {
        throw new Error('Cannot fork a stream with an outstanding pull() request.');
    }
    if (this._consumer) {
        throw new Error('Cannot fork a stream that has already been consumed().');
    }
    if (!this._multiplexer) {
        this._multiplexer = new StreamMultiplexer(this);
    }
    var s = this._multiplexer.newStream();
    s.id = 'fork:' + s.id;
    return s;
});
addMethod('observe', function () {
    var s = this.create();
    s.id = 'observe:' + s.id;
    s.writable = false;
    s.onDestroy(this._removeObserver.bind(this, s));
    this._observers.push(s);
    return s;
});
addMethod('errors', function (f) {
    return this.consume(function (err, x, push, next) {
        if (err) {
            f(err, push);
            next();
        } else if (x === nil) {
            push(null, nil);
        } else {
            push(null, x);
            next();
        }
    });
});
addMethod('stopOnError', function (f) {
    return this.consume(function (err, x, push, next) {
        if (err) {
            f(err, push);
            push(null, nil);
        } else if (x === nil) {
            push(null, nil);
        } else {
            push(null, x);
            next();
        }
    });
});
addMethod('each', function (f) {
    var self = this;
    var s = this.consume(function (err, x, push, next) {
        if (err) {
            self.emit('error', err);
        } else if (x === nil) {
            push(null, nil);
        } else {
            f(x);
            next();
        }
    });
    s.resume();
    return s;
});
addMethod('apply', function (f) {
    return this.toArray(function (args) {
        f.apply(null, args);
    });
});
addMethod('toArray', function (f) {
    var self = this;
    return this.collect().pull(function (err, x) {
        if (err) {
            self.emit('error', err);
        } else {
            f(x);
        }
    });
});
addMethod('done', function (f) {
    if (this.ended) {
        f();
        return null;
    }
    var self = this;
    return this.consume(function (err, x, push, next) {
        if (err) {
            self.emit('error', err);
        } else if (x === nil) {
            f();
        } else {
            next();
        }
    }).resume();
});
Stream.prototype.onDestroy = function onDestroy(f) {
    this._destructors.push(f);
    return this;
};
addMethod('toCallback', function (cb) {
    var value;
    var hasValue = false;
    this.consume(function (err, x, push, next) {
        if (err) {
            push(null, nil);
            if (hasValue) {
                cb(new Error('toCallback called on stream emitting multiple values'));
            } else {
                cb(err);
            }
        } else if (x === nil) {
            if (hasValue) {
                cb(null, value);
            } else {
                cb();
            }
        } else {
            if (hasValue) {
                push(null, nil);
                cb(new Error('toCallback called on stream emitting multiple values'));
            } else {
                value = x;
                hasValue = true;
                next();
            }
        }
    }).resume();
});
addMethod('map', function (f) {
    if (!_.isFunction(f)) {
        throw new Error('map expects a function as its only argument.');
    }
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(err, x);
        } else {
            var fnVal, fnErr;
            try {
                fnVal = f(x);
            } catch (e) {
                fnErr = e;
            }
            push(fnErr, fnVal);
            next();
        }
    });
});
addMethod('doto', function (f) {
    return this.map(function (x) {
        f(x);
        return x;
    });
});
Stream.prototype.tap = Stream.prototype.doto;
_.tap = _.doto;
addMethod('ratelimit', function (num, ms) {
    if (num < 1) {
        throw new Error('Invalid number of operations per ms: ' + num);
    }
    var sent = 0;
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(null, nil);
        } else {
            if (sent < num) {
                sent++;
                push(null, x);
                next();
            } else {
                setTimeout(function () {
                    sent = 1;
                    push(null, x);
                    next();
                }, ms);
            }
        }
    });
});
addMethod('flatMap', function (f) {
    return this.map(f).sequence();
});
addMethod('pluck', function (prop) {
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(err, x);
        } else if (_.isObject(x)) {
            push(null, x[prop]);
            next();
        } else {
            push(new Error('Expected Object, got ' + typeof x));
            next();
        }
    });
});
var objectOnly = _.curry(function (strategy, x) {
    if (_.isObject(x)) {
        return strategy(x);
    } else {
        throw new Error('Expected Object, got ' + typeof x);
    }
});
addMethod('pickBy', function (f) {
    return this.map(objectOnly(function (x) {
        var out = {};
        var seen = isES5 ? Object.create(null) : {};
        var obj = x;
        function testAndAdd(prop) {
            if (seen[prop] !== true && f(x[prop], prop)) {
                out[prop] = x[prop];
                seen[prop] = true;
            }
        }
        if (isES5) {
            do {
                Object.getOwnPropertyNames(obj).forEach(testAndAdd);
                obj = Object.getPrototypeOf(obj);
            } while (obj);
        } else {
            for (var k in x) {
                testAndAdd(k);
            }
        }
        return out;
    }));
});
addMethod('pick', function (properties) {
    return this.map(objectOnly(function (x) {
        var out = {};
        for (var i = 0, length = properties.length; i < length; i++) {
            var p = properties[i];
            if (p in x) {
                out[p] = x[p];
            }
        }
        return out;
    }));
});
addMethod('filter', function (f) {
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(err, x);
        } else {
            var fnVal, fnErr;
            try {
                fnVal = f(x);
            } catch (e) {
                fnErr = e;
            }
            if (fnErr) {
                push(fnErr);
            } else if (fnVal) {
                push(null, x);
            }
            next();
        }
    });
});
addMethod('flatFilter', function (f) {
    return this.flatMap(function (x) {
        return f(x).take(1).otherwise(errorStream()).flatMap(function (bool) {
            return _(bool ? [x] : []);
        });
    });
    function errorStream() {
        return _(function (push) {
            push(new Error('Stream returned by function was empty.'));
            push(null, _.nil);
        });
    }
});
addMethod('reject', function (f) {
    return this.filter(_.compose(_.not, f));
});
addMethod('find', function (f) {
    return this.filter(f).take(1);
});
addMethod('findWhere', function (props) {
    return this.where(props).take(1);
});
addMethod('group', function (f) {
    var lambda = _.isString(f) ? _.get(f) : f;
    return this.reduce(function (m, o) {
        var key = lambda(o);
        if (!hasOwn.call(m, key)) {
            m[key] = [];
        }
        m[key].push(o);
        return m;
    }, {});
});
addMethod('compact', function () {
    return this.filter(function (x) {
        return x;
    });
});
addMethod('where', function (props) {
    return this.filter(function (x) {
        for (var k in props) {
            if (x[k] !== props[k]) {
                return false;
            }
        }
        return true;
    });
});
addMethod('uniqBy', function (compare) {
    var uniques = [];
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(err, x);
        } else {
            var seen = false;
            var hasErr;
            for (var i = 0, len = uniques.length; i < len; i++) {
                try {
                    seen = compare(x, uniques[i]);
                } catch (e) {
                    hasErr = e;
                    seen = true;
                }
                if (seen) {
                    break;
                }
            }
            if (!seen) {
                uniques.push(x);
                push(null, x);
            }
            if (hasErr) {
                push(hasErr);
            }
            next();
        }
    });
});
addMethod('uniq', function () {
    if (!_.isUndefined(_global.Set)) {
        var uniques = new _global.Set(),
            size = uniques.size;
        return this.consume(function (err, x, push, next) {
            if (err) {
                push(err);
                next();
            } else if (x === nil) {
                push(err, x);
            }
            else if (x !== x) {
                    push(null, x);
                    next();
                } else {
                    uniques.add(x);
                    if (uniques.size > size) {
                        size = uniques.size;
                        push(null, x);
                    }
                    next();
                }
        });
    }
    return this.uniqBy(function (a, b) {
        return a === b;
    });
});
addMethod('zipAll', function () {
    var returned = 0;
    var z = [];
    var finished = false;
    function nextValue(index, max, src, push, next) {
        src.pull(function (err, x) {
            if (err) {
                push(err);
                nextValue(index, max, src, push, next);
            } else if (x === _.nil) {
                if (!finished) {
                    finished = true;
                    push(null, nil);
                }
            } else {
                returned++;
                z[index] = x;
                if (returned === max) {
                    push(null, z);
                    next();
                }
            }
        });
    }
    var self = this;
    return self.collect().flatMap(function (array) {
        if (!array.length) {
            return self.create([]);
        }
        return self.create(function (push, next) {
            returned = 0;
            z = [];
            for (var i = 0, length = array.length; i < length; i++) {
                nextValue(i, length, array[i], push, next);
            }
        });
    });
});
addMethod('zipEach', function (ys) {
    return this.create([this]).concat(this.create(ys).map(this.create.bind(this))).zipAll();
});
addMethod('zip', function (ys) {
    return this.create([this, this.create(ys)]).zipAll();
});
addMethod('batch', function (n) {
    return this.batchWithTimeOrCount(-1, n);
});
addMethod('batchWithTimeOrCount', function (ms, n) {
    var batched = [],
        timeout;
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            if (batched.length > 0) {
                push(null, batched);
                clearTimeout(timeout);
            }
            push(null, nil);
        } else {
            batched.push(x);
            if (batched.length === n) {
                push(null, batched);
                batched = [];
                clearTimeout(timeout);
            } else if (batched.length === 1 && ms >= 0) {
                timeout = setTimeout(function () {
                    push(null, batched);
                    batched = [];
                }, ms);
            }
            next();
        }
    });
});
addMethod('intersperse', function (separator) {
    var started = false;
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(null, nil);
        } else {
            if (started) {
                push(null, separator);
            } else {
                started = true;
            }
            push(null, x);
            next();
        }
    });
});
addMethod('splitBy', function (sep) {
    var decoder = new Decoder();
    var buffer = false;
    function drain(x, push) {
        buffer = (buffer || '') + decoder.write(x);
        var pieces = buffer.split(sep);
        buffer = pieces.pop();
        pieces.forEach(function (piece) {
            push(null, piece);
        });
    }
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            if (_.isString(buffer)) {
                drain(decoder.end(), push);
                push(null, buffer);
            }
            push(null, nil);
        } else {
            drain(x, push);
            next();
        }
    });
});
addMethod('split', function () {
    return this.splitBy(/\r?\n/);
});
addMethod('slice', function (start, end) {
    if (start == null) {
        start = 0;
    }
    if (end == null) {
        end = Infinity;
    }
    checkIsNumber(start, 'start');
    checkIsNumber(end, 'end');
    checkRange(start >= 0, 'start cannot be negative.');
    checkRange(end >= 0, 'end cannot be negative.');
    var index = 0;
    if (start === 0 && end === Infinity) {
        return this;
    } else if (start >= end) {
        return _([]);
    }
    var s = this.consume(function (err, x, push, next) {
        var done = x === nil;
        if (err) {
            push(err);
        } else if (!done && index++ >= start) {
            push(null, x);
        }
        if (!done && index < end) {
            next();
        } else {
            push(null, nil);
        }
    });
    s.id = 'slice:' + s.id;
    return s;
});
addMethod('take', function (n) {
    var s = this.slice(0, n);
    s.id = 'take:' + s.id;
    return s;
});
addMethod('drop', function (n) {
    return this.slice(n, Infinity);
});
addMethod('head', function () {
    return this.take(1);
});
addMethod('last', function () {
    var nothing = {};
    var prev = nothing;
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            if (prev !== nothing) {
                push(null, prev);
            }
            push(null, nil);
        } else {
            prev = x;
            next();
        }
    });
});
addMethod('sortBy', function (f) {
    return this.collect().invoke('sort', [f]).sequence();
});
addMethod('sort', function () {
    return this.sortBy();
});
addMethod('through', function (target) {
    var output;
    if (_.isFunction(target)) {
        return target(this);
    } else {
        target.pause();
        output = this.createChild();
        this.on('error', writeErr);
        target.on('error', writeErr);
        return this.pipe(target).pipe(output);
    }
    function writeErr(err) {
        output.write(new StreamError(err));
    }
});
addToplevelMethod('pipeline', function ()               {
    if (!arguments.length) {
        return this();
    }
    var start = arguments[0],
        rest,
        startHighland;
    if (!_.isStream(start) && !_.isFunction(start.resume)) {
        start = this();
        startHighland = start;
        rest = slice.call(arguments);
    } else {
        startHighland = this(start);
        rest = slice.call(arguments, 1);
    }
    var end = rest.reduce(function (src, dest) {
        return src.through(dest);
    }, startHighland);
    var wrapper = this(function (push, next) {
        end.pull(function (err, x) {
            push(err, x);
            if (x !== nil) {
                next();
            }
        });
    });
    wrapper.writable = true;
    wrapper.write = function (x) {
        return start.write(x);
    };
    wrapper.end = function () {
        wrapper.writable = false;
        return start.end();
    };
    start.on('drain', function () {
        wrapper.emit('drain');
    });
    return wrapper;
});
addMethod('sequence', function () {
    var original = this;
    var curr = this;
    return this.create(function (push, next) {
        curr.pull(function (err, x) {
            if (err) {
                push(err);
                next();
            } else if (_.isArray(x)) {
                if (onOriginalStream()) {
                    x.forEach(function (y) {
                        push(null, y);
                    });
                } else {
                    push(null, x);
                }
                next();
            } else if (_.isStream(x)) {
                if (onOriginalStream()) {
                    curr = x;
                    next();
                } else {
                    push(null, x);
                    next();
                }
            } else if (x === nil) {
                if (onOriginalStream()) {
                    push(null, nil);
                } else {
                    curr = original;
                    next();
                }
            } else {
                if (onOriginalStream()) {
                    push(new Error('Expected Stream, got ' + typeof x));
                    next();
                } else {
                    push(null, x);
                    next();
                }
            }
        });
    });
    function onOriginalStream() {
        return curr === original;
    }
});
Stream.prototype.series = Stream.prototype.sequence;
_.series = _.sequence;
addMethod('flatten', function () {
    var curr = this;
    var stack = [];
    return this.createChild(function (push, next) {
        curr.pull(function (err, x) {
            if (err) {
                push(err);
                next();
                return;
            }
            if (_.isArray(x)) {
                x = _(x);
            }
            if (_.isStream(x)) {
                stack.push(curr);
                curr = x;
                next();
            } else if (x === nil) {
                if (stack.length) {
                    curr = stack.pop();
                    next();
                } else {
                    push(null, nil);
                }
            } else {
                push(null, x);
                next();
            }
        });
    });
});
addMethod('parallel', function (n) {
    var source = this;
    var running = [];
    var ended = false;
    var reading_source = false;
    if (typeof n !== 'number') {
        throw new Error('Must specify a number to parallel().');
    }
    if (n <= 0) {
        throw new Error('The parallelism factor must be positive');
    }
    return this.create(function (push, next) {
        if (running.length < n && !ended && !reading_source) {
            reading_source = true;
            source.pull(function (err, x) {
                reading_source = false;
                if (err) {
                    push(err);
                } else if (x === nil) {
                    ended = true;
                } else if (!_.isStream(x)) {
                    push(new Error('Expected Stream, got ' + typeof x));
                } else {
                    var run = { stream: x, buffer: [] };
                    running.push(run);
                    x.consume(function (_err, y, _push, _next) {
                        if (running[0] === run) {
                            if (y === nil) {
                                running.shift();
                                flushBuffer();
                                next();
                            } else {
                                push(_err, y);
                            }
                        } else {
                            run.buffer.push([_err, y]);
                        }
                        if (y !== nil) {
                            _next();
                        }
                    }).resume();
                }
                return next();
            });
        } else if (!running.length && ended) {
            push(null, nil);
        }
        function flushBuffer() {
            while (running.length && running[0].buffer.length) {
                var buf = running[0].buffer;
                for (var i = 0; i < buf.length; i++) {
                    if (buf[i][1] === nil) {
                        running.shift();
                        break;
                    } else {
                        push.apply(null, buf[i]);
                    }
                }
                buf.length = 0;
            }
        }
    });
});
addMethod('otherwise', function (ys) {
    var xs = this;
    return xs.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            if (_.isFunction(ys)) {
                next(ys());
            } else {
                next(ys);
            }
        } else {
            push(null, x);
            next(xs);
        }
    });
});
addMethod('append', function (y) {
    return this.consume(function (err, x, push, next) {
        if (x === nil) {
            push(null, y);
            push(null, _.nil);
        } else {
            push(err, x);
            next();
        }
    });
});
addMethod('reduce', function (f, z) {
    return this.consume(function (err, x, push, next) {
        if (x === nil) {
            push(null, z);
            push(null, _.nil);
        } else if (err) {
            push(err);
            next();
        } else {
            try {
                z = f(z, x);
            } catch (e) {
                push(e);
                push(null, _.nil);
                return;
            }
            next();
        }
    });
});
addMethod('reduce1', function (f) {
    var self = this;
    return self.createChild(function (push, next) {
        self.pull(function (err, x) {
            if (err) {
                push(err);
                next();
            } else if (x === nil) {
                push(null, nil);
            } else {
                next(self.reduce(f, x));
            }
        });
    });
});
addMethod('collect', function () {
    var xs = [];
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(null, xs);
            push(null, nil);
        } else {
            xs.push(x);
            next();
        }
    });
});
addMethod('scan', function (f, z) {
    var self = this;
    return this.create([z]).concat(self.consume(function (err, x, push, next) {
        if (x === nil) {
            push(null, _.nil);
        } else if (err) {
            push(err);
            next();
        } else {
            try {
                z = f(z, x);
            } catch (e) {
                push(e);
                push(null, _.nil);
                return;
            }
            push(null, z);
            next();
        }
    }));
});
addMethod('scan1', function (f) {
    var self = this;
    return self.createChild(function (push, next) {
        self.pull(function (err, x) {
            if (err) {
                push(err);
                next();
            } else if (x === nil) {
                push(null, nil);
            } else {
                next(self.scan(f, x));
            }
        });
    });
});
function HighlandTransform(push) {
    this.push = push;
}
HighlandTransform.prototype['@@transducer/init'] = function () {
    return this.push;
};
HighlandTransform.prototype['@@transducer/result'] = function (push) {
    return push;
};
HighlandTransform.prototype['@@transducer/step'] = function (push, input) {
    push(null, input);
    return push;
};
addMethod('transduce', function transduce(xf) {
    var transform = null,
        memo = null;
    return this.consume(function (err, x, push, next) {
        if (transform == null) {
            transform = xf(new HighlandTransform(push));
            memo = transform['@@transducer/init']();
        }
        if (err) {
            push(err);
            next();
        } else if (x === _.nil) {
            runResult(push, memo);
        } else {
            var res = runStep(push, memo, x);
            if (!res) {
                return;
            }
            memo = res;
            if (memo['@@transducer/reduced']) {
                runResult(memo['@@transducer/value']);
            } else {
                next();
            }
        }
    });
    function runResult(push, _memo) {
        try {
            transform['@@transducer/result'](_memo);
        } catch (e) {
            push(e);
        }
        push(null, _.nil);
    }
    function runStep(push, _memo, x) {
        try {
            return transform['@@transducer/step'](_memo, x);
        } catch (e) {
            push(e);
            push(null, _.nil);
            return null;
        }
    }
});
addMethod('concat', function (ys) {
    ys = _(ys);
    return this.consume(function (err, x, push, next) {
        if (x === nil) {
            next(ys);
        } else {
            push(err, x);
            next();
        }
    });
});
addMethod('merge', function () {
    var self = this;
    var srcs = [];
    var srcsNeedPull = [],
        first = true,
        async = false;
    return this.create(function (push, next) {
        if (first) {
            first = false;
            getSourcesSync(push, next);
        }
        if (srcs.length === 0) {
            push(null, nil);
        } else if (srcsNeedPull.length) {
            pullFromAllSources(push, next);
            next();
        } else {
            async = true;
        }
    });
    function srcPullHandler(push, next, src) {
        return function (err, x) {
            if (err) {
                push(err);
                srcsNeedPull.push(src);
            } else if (x === nil) {
                srcs = srcs.filter(function (s) {
                    return s !== src;
                });
            } else {
                if (src === self) {
                    srcs.push(x);
                    srcsNeedPull.push(x);
                    srcsNeedPull.unshift(self);
                } else {
                    push(null, x);
                    srcsNeedPull.push(src);
                }
            }
            if (async) {
                async = false;
                next();
            }
        };
    }
    function pullFromAllSources(push, next) {
        var _srcs = srcsNeedPull;
        srcsNeedPull = [];
        _srcs.forEach(function (src) {
            src.pull(srcPullHandler(push, next, src));
        });
    }
    function getSourcesSync(push, next) {
        var asynchronous;
        var done = false;
        var pull_cb = function (err, x) {
            asynchronous = false;
            if (done) {
                srcPullHandler(push, next, self)(err, x);
            } else {
                if (err) {
                    push(err);
                } else if (x === nil) {
                    done = true;
                } else {
                    srcs.push(x);
                    srcsNeedPull.push(x);
                }
            }
        };
        while (!done) {
            asynchronous = true;
            self.pull(pull_cb);
            if (asynchronous) {
                done = true;
                srcs.unshift(self);
            }
        }
    }
});
addMethod('mergeWithLimit', function mergeWithLimit(n) {
    var self = this;
    var processCount = 0;
    var waiting = false;
    if (typeof n !== 'number' || n < 1) {
        throw new Error('mergeWithLimit expects a positive number, but got: ' + n);
    }
    if (n === Infinity) {
        return this.merge();
    }
    return this.create(function (push, next) {
        self.pull(function (err, x) {
            var done = x === nil;
            if (err) {
                push(err);
                next();
            } else if (x === nil) {
                push(null, nil);
            } else {
                processCount++;
                push(err, x);
                x.onDestroy(function () {
                    processCount--;
                    if (waiting) {
                        waiting = false;
                        next();
                    }
                });
                if (!done && processCount < n) {
                    next();
                } else {
                    waiting = true;
                }
            }
        });
    }).merge();
});
addMethod('invoke', function (method, args) {
    return this.map(function (x) {
        return x[method].apply(x, args);
    });
});
addMethod('nfcall', function (args) {
    var wrap = _.wrapCallback._relevel(this.create.bind(this));
    return this.map(function (x) {
        return wrap(x).apply(x, args);
    });
});
addMethod('throttle', function (ms) {
    var last = 0 - ms;
    return this.consume(function (err, x, push, next) {
        var now = new Date().getTime();
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            push(null, nil);
        } else if (now - ms >= last) {
            last = now;
            push(null, x);
            next();
        } else {
            next();
        }
    });
});
addMethod('debounce', function (ms) {
    var t = null;
    var nothing = {};
    var last = nothing;
    return this.consume(function (err, x, push, next) {
        if (err) {
            push(err);
            next();
        } else if (x === nil) {
            if (t) {
                clearTimeout(t);
            }
            if (last !== nothing) {
                push(null, last);
            }
            push(null, nil);
        } else {
            last = x;
            if (t) {
                clearTimeout(t);
            }
            t = setTimeout(push.bind(this, null, x), ms);
            next();
        }
    });
});
addMethod('latest', function () {
    var nothing = {},
        latest = nothing,
        errors = [],
        ended = false,
        onValue = null;
    this.consume(function (err, x, push, next) {
        if (onValue != null) {
            var cb = onValue;
            onValue = null;
            cb(err, x);
        }
        if (err) {
            errors.push(err);
            next();
        } else if (x === nil) {
            ended = true;
        } else {
            latest = x;
            next();
        }
    }).resume();
    return this.create(function (push, next) {
        var oldErrors = errors;
        errors = [];
        if (!oldErrors.length && latest === nothing && !ended) {
            onValue = function (err, x) {
                push(err, x);
                if (x !== nil) {
                    next();
                }
            };
        } else {
            oldErrors.forEach(push);
            if (latest !== nothing) {
                push(null, latest);
            }
            if (ended) {
                push(null, nil);
            } else {
                next();
            }
        }
    });
});
function StreamMultiplexer(stream) {
    this._stream = stream;
    this._consumers = new intMap();
    this._requests = new intMap();
    this._nonce = 0;
    this._emitting = false;
    this._repeatEmit = false;
    this._cached_value = null;
    this.paused = true;
    var self = this;
    this._pullCb = function _pullCb(err, x) {
        self.paused = true;
        if (self._requests.size === self._consumers.size) {
            self._send(err, x);
        } else {
            self._saved_token = [err, x];
        }
    };
}
StreamMultiplexer.prototype._send = function _send(err, x) {
    var requests = this._requests;
    this._requests = new intMap();
    requests.forEach(function (requestCb) {
        requestCb(err, x);
    });
};
StreamMultiplexer.prototype._resume = function _resume() {
    if (this._emitting) {
        this._repeatEmit = true;
        return;
    }
    this._emitting = true;
    do {
        this._repeatEmit = false;
        if (this._requests.size === this._consumers.size) {
            if (this._saved_token) {
                var token = this._saved_token;
                this._saved_token = null;
                this.paused = true;
                this._send(token[0], token[1]);
            } else if (this.paused) {
                this.paused = false;
                this._stream.pull(this._pullCb);
            }
        }
    } while (this._repeatEmit);
    this._emitting = false;
};
StreamMultiplexer.prototype.pull = function pull(id, cb) {
    if (!cb) {
        return;
    }
    if (this._requests.has(id)) {
        cb(new Error('Cannot service a second pull() request while one is in progress.'));
        return;
    }
    this._requests.set(id, cb);
    this._resume();
};
StreamMultiplexer.prototype.newStream = function newStream() {
    var self = this,
        id = self._nonce++;
    self._consumers.set(id, true);
    var pull = this.pull.bind(this, id);
    return new this._stream.constructor(newDelegateGenerator(pull)).onDestroy(function () {
        self.removeConsumer(id);
    });
};
StreamMultiplexer.prototype.removeConsumer = function removeConsumer(id) {
    if (!this._consumers.has(id)) {
        return;
    }
    this._consumers.delete(id);
    this._requests.delete(id);
    this._resume();
};
_.values = function (obj) {
    return _.keys(obj).map(function (k) {
        return obj[k];
    });
};
function keys(obj) {
    var keysArray = [];
    for (var k in obj) {
        if (hasOwn.call(obj, k)) {
            keysArray.push(k);
        }
    }
    return keysArray;
}
_.keys = function (obj) {
    return _(keys(obj));
};
_.pairs = function (obj) {
    return _.keys(obj).map(function (k) {
        return [k, obj[k]];
    });
};
_.extend = _.curry(function (extensions, target) {
    for (var k in extensions) {
        if (hasOwn.call(extensions, k)) {
            target[k] = extensions[k];
        }
    }
    return target;
});
_.get = _.curry(function (prop, obj) {
    return obj[prop];
});
_.set = _.curry(function (prop, val, obj) {
    obj[prop] = val;
    return obj;
});
_.log = function () {
    console.log.apply(console, arguments);
};
addToplevelMethod('wrapCallback', function (f,             mappingHint) {
    var stream = this;
    var mapper = hintMapper(mappingHint);
    return function () {
        var self = this;
        var args = slice.call(arguments);
        return stream(function (push) {
            var cb = function (err) {
                if (err) {
                    push(err);
                } else {
                    var cbArgs = slice.call(arguments, 1);
                    var v = mapper.apply(this, cbArgs);
                    push(null, v);
                }
                push(null, nil);
            };
            f.apply(self, args.concat([cb]));
        });
    };
});
addToplevelMethod('wrapAsync', function (f) {
    var stream = this;
    return function () {
        var promise;
        try {
            promise = f.apply(this, arguments);
            if (!_.isObject(promise) || !_.isFunction(promise.then)) {
                return _.fromError(new Error('Wrapped function did not return a promise'));
            }
            return stream(promise);
        } catch (e) {
            return _.fromError(e);
        }
    };
});
function isClass(fn) {
    if (!(typeof fn === 'function' && fn.prototype)) {
        return false;
    }
    var getKeys = isES5 ? Object.getOwnPropertyNames : keys;
    var allKeys = getKeys(fn.prototype);
    return allKeys.length > 0 && !(allKeys.length === 1 && allKeys[0] === 'constructor');
}
function inheritedKeys(obj) {
    var allProps = {};
    var curr = obj;
    var handleProp = function (prop) {
        allProps[prop] = true;
    };
    while (Object.getPrototypeOf(curr)) {
        var props = Object.getOwnPropertyNames(curr);
        props.forEach(handleProp);
        curr = Object.getPrototypeOf(curr);
    }
    return keys(allProps);
}
function streamifyAll(wrapCallback, inp, suffix) {
    var getKeys = isES5 ? inheritedKeys : keys;
    var allKeys = getKeys(inp);
    for (var i = 0, len = allKeys.length; i < len; i++) {
        var key = allKeys[i];
        var val;
        try {
            val = inp[key];
        } catch (e) {
        }
        if (val && typeof val === 'function' && !isClass(val) && !val.__HighlandStreamifiedFunction__) {
            var streamified = wrapCallback(val);
            streamified.__HighlandStreamifiedFunction__ = true;
            inp[key + suffix] = streamified;
        }
    }
    return inp;
}
addToplevelMethod('streamifyAll', function (arg) {
    if (typeof arg !== 'function' && typeof arg !== 'object') {
        throw new TypeError('takes an object or a constructor function');
    }
    var suffix = 'Stream';
    var ret = streamifyAll(this.wrapCallback, arg, suffix);
    if (isClass(arg)) {
        ret.prototype = streamifyAll(this.wrapCallback, arg.prototype, suffix);
    }
    return ret;
});
_.add = _.curry(function (a, b) {
    return a + b;
});
_.not = function (x) {
    return !x;
};
});

var reverseInParallel = (({
  trace,
  srcmapFile
}) => {
  const lines = trace.split('\n');
  return index(lines).map(line => [line, cluster.fork()]).map(([line, worker]) => index(push => {
    worker.on('message', msg => {
      if (msg.error) {
        console.error(msg.error, 'Reversing source map');
        push(null, Buffer.from(line, 'utf8'));
      } else if (msg.line) {
        push(null, msg.line);
      }
      push(null, index.nil);
      worker.kill();
    });
    worker.send({
      srcmapFile,
      line
    });
  })).parallel(lines.length).collect().map(x => x.join('\n').trim());
});

var intToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
var encode$1 = function (number) {
  if (0 <= number && number < intToCharMap.length) {
    return intToCharMap[number];
  }
  throw new TypeError("Must be between 0 and 63: " + number);
};
var decode$1 = function (charCode) {
  var bigA = 65;
  var bigZ = 90;
  var littleA = 97;
  var littleZ = 122;
  var zero = 48;
  var nine = 57;
  var plus = 43;
  var slash = 47;
  var littleOffset = 26;
  var numberOffset = 52;
  if (bigA <= charCode && charCode <= bigZ) {
    return charCode - bigA;
  }
  if (littleA <= charCode && charCode <= littleZ) {
    return charCode - littleA + littleOffset;
  }
  if (zero <= charCode && charCode <= nine) {
    return charCode - zero + numberOffset;
  }
  if (charCode == plus) {
    return 62;
  }
  if (charCode == slash) {
    return 63;
  }
  return -1;
};
var base64 = {
	encode: encode$1,
	decode: decode$1
};

var VLQ_BASE_SHIFT = 5;
var VLQ_BASE = 1 << VLQ_BASE_SHIFT;
var VLQ_BASE_MASK = VLQ_BASE - 1;
var VLQ_CONTINUATION_BIT = VLQ_BASE;
function toVLQSigned(aValue) {
  return aValue < 0 ? (-aValue << 1) + 1 : (aValue << 1) + 0;
}
function fromVLQSigned(aValue) {
  var isNegative = (aValue & 1) === 1;
  var shifted = aValue >> 1;
  return isNegative ? -shifted : shifted;
}
var encode = function base64VLQ_encode(aValue) {
  var encoded = "";
  var digit;
  var vlq = toVLQSigned(aValue);
  do {
    digit = vlq & VLQ_BASE_MASK;
    vlq >>>= VLQ_BASE_SHIFT;
    if (vlq > 0) {
      digit |= VLQ_CONTINUATION_BIT;
    }
    encoded += base64.encode(digit);
  } while (vlq > 0);
  return encoded;
};
var decode = function base64VLQ_decode(aStr, aIndex, aOutParam) {
  var strLen = aStr.length;
  var result = 0;
  var shift = 0;
  var continuation, digit;
  do {
    if (aIndex >= strLen) {
      throw new Error("Expected more digits in base 64 VLQ value.");
    }
    digit = base64.decode(aStr.charCodeAt(aIndex++));
    if (digit === -1) {
      throw new Error("Invalid base64 digit: " + aStr.charAt(aIndex - 1));
    }
    continuation = !!(digit & VLQ_CONTINUATION_BIT);
    digit &= VLQ_BASE_MASK;
    result = result + (digit << shift);
    shift += VLQ_BASE_SHIFT;
  } while (continuation);
  aOutParam.value = fromVLQSigned(result);
  aOutParam.rest = aIndex;
};
var base64Vlq = {
	encode: encode,
	decode: decode
};

var util$1 = createCommonjsModule(function (module, exports) {
function getArg(aArgs, aName, aDefaultValue) {
  if (aName in aArgs) {
    return aArgs[aName];
  } else if (arguments.length === 3) {
    return aDefaultValue;
  } else {
    throw new Error('"' + aName + '" is a required argument.');
  }
}
exports.getArg = getArg;
var urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.]*)(?::(\d+))?(\S*)$/;
var dataUrlRegexp = /^data:.+\,.+$/;
function urlParse(aUrl) {
  var match = aUrl.match(urlRegexp);
  if (!match) {
    return null;
  }
  return {
    scheme: match[1],
    auth: match[2],
    host: match[3],
    port: match[4],
    path: match[5]
  };
}
exports.urlParse = urlParse;
function urlGenerate(aParsedUrl) {
  var url = '';
  if (aParsedUrl.scheme) {
    url += aParsedUrl.scheme + ':';
  }
  url += '//';
  if (aParsedUrl.auth) {
    url += aParsedUrl.auth + '@';
  }
  if (aParsedUrl.host) {
    url += aParsedUrl.host;
  }
  if (aParsedUrl.port) {
    url += ":" + aParsedUrl.port;
  }
  if (aParsedUrl.path) {
    url += aParsedUrl.path;
  }
  return url;
}
exports.urlGenerate = urlGenerate;
function normalize(aPath) {
  var path = aPath;
  var url = urlParse(aPath);
  if (url) {
    if (!url.path) {
      return aPath;
    }
    path = url.path;
  }
  var isAbsolute = exports.isAbsolute(path);
  var parts = path.split(/\/+/);
  for (var part, up = 0, i = parts.length - 1; i >= 0; i--) {
    part = parts[i];
    if (part === '.') {
      parts.splice(i, 1);
    } else if (part === '..') {
      up++;
    } else if (up > 0) {
      if (part === '') {
        parts.splice(i + 1, up);
        up = 0;
      } else {
        parts.splice(i, 2);
        up--;
      }
    }
  }
  path = parts.join('/');
  if (path === '') {
    path = isAbsolute ? '/' : '.';
  }
  if (url) {
    url.path = path;
    return urlGenerate(url);
  }
  return path;
}
exports.normalize = normalize;
function join(aRoot, aPath) {
  if (aRoot === "") {
    aRoot = ".";
  }
  if (aPath === "") {
    aPath = ".";
  }
  var aPathUrl = urlParse(aPath);
  var aRootUrl = urlParse(aRoot);
  if (aRootUrl) {
    aRoot = aRootUrl.path || '/';
  }
  if (aPathUrl && !aPathUrl.scheme) {
    if (aRootUrl) {
      aPathUrl.scheme = aRootUrl.scheme;
    }
    return urlGenerate(aPathUrl);
  }
  if (aPathUrl || aPath.match(dataUrlRegexp)) {
    return aPath;
  }
  if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {
    aRootUrl.host = aPath;
    return urlGenerate(aRootUrl);
  }
  var joined = aPath.charAt(0) === '/' ? aPath : normalize(aRoot.replace(/\/+$/, '') + '/' + aPath);
  if (aRootUrl) {
    aRootUrl.path = joined;
    return urlGenerate(aRootUrl);
  }
  return joined;
}
exports.join = join;
exports.isAbsolute = function (aPath) {
  return aPath.charAt(0) === '/' || !!aPath.match(urlRegexp);
};
function relative(aRoot, aPath) {
  if (aRoot === "") {
    aRoot = ".";
  }
  aRoot = aRoot.replace(/\/$/, '');
  var level = 0;
  while (aPath.indexOf(aRoot + '/') !== 0) {
    var index = aRoot.lastIndexOf("/");
    if (index < 0) {
      return aPath;
    }
    aRoot = aRoot.slice(0, index);
    if (aRoot.match(/^([^\/]+:\/)?\/*$/)) {
      return aPath;
    }
    ++level;
  }
  return Array(level + 1).join("../") + aPath.substr(aRoot.length + 1);
}
exports.relative = relative;
var supportsNullProto = function () {
  var obj = Object.create(null);
  return !('__proto__' in obj);
}();
function identity(s) {
  return s;
}
function toSetString(aStr) {
  if (isProtoString(aStr)) {
    return '$' + aStr;
  }
  return aStr;
}
exports.toSetString = supportsNullProto ? identity : toSetString;
function fromSetString(aStr) {
  if (isProtoString(aStr)) {
    return aStr.slice(1);
  }
  return aStr;
}
exports.fromSetString = supportsNullProto ? identity : fromSetString;
function isProtoString(s) {
  if (!s) {
    return false;
  }
  var length = s.length;
  if (length < 9                         ) {
      return false;
    }
  if (s.charCodeAt(length - 1) !== 95           || s.charCodeAt(length - 2) !== 95           || s.charCodeAt(length - 3) !== 111           || s.charCodeAt(length - 4) !== 116           || s.charCodeAt(length - 5) !== 111           || s.charCodeAt(length - 6) !== 114           || s.charCodeAt(length - 7) !== 112           || s.charCodeAt(length - 8) !== 95           || s.charCodeAt(length - 9) !== 95          ) {
      return false;
    }
  for (var i = length - 10; i >= 0; i--) {
    if (s.charCodeAt(i) !== 36          ) {
        return false;
      }
  }
  return true;
}
function compareByOriginalPositions(mappingA, mappingB, onlyCompareOriginal) {
  var cmp = mappingA.source - mappingB.source;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.originalLine - mappingB.originalLine;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.originalColumn - mappingB.originalColumn;
  if (cmp !== 0 || onlyCompareOriginal) {
    return cmp;
  }
  cmp = mappingA.generatedColumn - mappingB.generatedColumn;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.generatedLine - mappingB.generatedLine;
  if (cmp !== 0) {
    return cmp;
  }
  return mappingA.name - mappingB.name;
}
exports.compareByOriginalPositions = compareByOriginalPositions;
function compareByGeneratedPositionsDeflated(mappingA, mappingB, onlyCompareGenerated) {
  var cmp = mappingA.generatedLine - mappingB.generatedLine;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.generatedColumn - mappingB.generatedColumn;
  if (cmp !== 0 || onlyCompareGenerated) {
    return cmp;
  }
  cmp = mappingA.source - mappingB.source;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.originalLine - mappingB.originalLine;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.originalColumn - mappingB.originalColumn;
  if (cmp !== 0) {
    return cmp;
  }
  return mappingA.name - mappingB.name;
}
exports.compareByGeneratedPositionsDeflated = compareByGeneratedPositionsDeflated;
function strcmp(aStr1, aStr2) {
  if (aStr1 === aStr2) {
    return 0;
  }
  if (aStr1 > aStr2) {
    return 1;
  }
  return -1;
}
function compareByGeneratedPositionsInflated(mappingA, mappingB) {
  var cmp = mappingA.generatedLine - mappingB.generatedLine;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.generatedColumn - mappingB.generatedColumn;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = strcmp(mappingA.source, mappingB.source);
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.originalLine - mappingB.originalLine;
  if (cmp !== 0) {
    return cmp;
  }
  cmp = mappingA.originalColumn - mappingB.originalColumn;
  if (cmp !== 0) {
    return cmp;
  }
  return strcmp(mappingA.name, mappingB.name);
}
exports.compareByGeneratedPositionsInflated = compareByGeneratedPositionsInflated;
});

var has = Object.prototype.hasOwnProperty;
function ArraySet$1() {
  this._array = [];
  this._set = Object.create(null);
}
ArraySet$1.fromArray = function ArraySet_fromArray(aArray, aAllowDuplicates) {
  var set = new ArraySet$1();
  for (var i = 0, len = aArray.length; i < len; i++) {
    set.add(aArray[i], aAllowDuplicates);
  }
  return set;
};
ArraySet$1.prototype.size = function ArraySet_size() {
  return Object.getOwnPropertyNames(this._set).length;
};
ArraySet$1.prototype.add = function ArraySet_add(aStr, aAllowDuplicates) {
  var sStr = util$1.toSetString(aStr);
  var isDuplicate = has.call(this._set, sStr);
  var idx = this._array.length;
  if (!isDuplicate || aAllowDuplicates) {
    this._array.push(aStr);
  }
  if (!isDuplicate) {
    this._set[sStr] = idx;
  }
};
ArraySet$1.prototype.has = function ArraySet_has(aStr) {
  var sStr = util$1.toSetString(aStr);
  return has.call(this._set, sStr);
};
ArraySet$1.prototype.indexOf = function ArraySet_indexOf(aStr) {
  var sStr = util$1.toSetString(aStr);
  if (has.call(this._set, sStr)) {
    return this._set[sStr];
  }
  throw new Error('"' + aStr + '" is not in the set.');
};
ArraySet$1.prototype.at = function ArraySet_at(aIdx) {
  if (aIdx >= 0 && aIdx < this._array.length) {
    return this._array[aIdx];
  }
  throw new Error('No element indexed by ' + aIdx);
};
ArraySet$1.prototype.toArray = function ArraySet_toArray() {
  return this._array.slice();
};
var ArraySet_1 = ArraySet$1;
var arraySet = {
	ArraySet: ArraySet_1
};

var binarySearch = createCommonjsModule(function (module, exports) {
exports.GREATEST_LOWER_BOUND = 1;
exports.LEAST_UPPER_BOUND = 2;
function recursiveSearch(aLow, aHigh, aNeedle, aHaystack, aCompare, aBias) {
  var mid = Math.floor((aHigh - aLow) / 2) + aLow;
  var cmp = aCompare(aNeedle, aHaystack[mid], true);
  if (cmp === 0) {
    return mid;
  } else if (cmp > 0) {
    if (aHigh - mid > 1) {
      return recursiveSearch(mid, aHigh, aNeedle, aHaystack, aCompare, aBias);
    }
    if (aBias == exports.LEAST_UPPER_BOUND) {
      return aHigh < aHaystack.length ? aHigh : -1;
    } else {
      return mid;
    }
  } else {
    if (mid - aLow > 1) {
      return recursiveSearch(aLow, mid, aNeedle, aHaystack, aCompare, aBias);
    }
    if (aBias == exports.LEAST_UPPER_BOUND) {
      return mid;
    } else {
      return aLow < 0 ? -1 : aLow;
    }
  }
}
exports.search = function search(aNeedle, aHaystack, aCompare, aBias) {
  if (aHaystack.length === 0) {
    return -1;
  }
  var index = recursiveSearch(-1, aHaystack.length, aNeedle, aHaystack, aCompare, aBias || exports.GREATEST_LOWER_BOUND);
  if (index < 0) {
    return -1;
  }
  while (index - 1 >= 0) {
    if (aCompare(aHaystack[index], aHaystack[index - 1], true) !== 0) {
      break;
    }
    --index;
  }
  return index;
};
});

function swap(ary, x, y) {
  var temp = ary[x];
  ary[x] = ary[y];
  ary[y] = temp;
}
function randomIntInRange(low, high) {
  return Math.round(low + Math.random() * (high - low));
}
function doQuickSort(ary, comparator, p, r) {
  if (p < r) {
    var pivotIndex = randomIntInRange(p, r);
    var i = p - 1;
    swap(ary, pivotIndex, r);
    var pivot = ary[r];
    for (var j = p; j < r; j++) {
      if (comparator(ary[j], pivot) <= 0) {
        i += 1;
        swap(ary, i, j);
      }
    }
    swap(ary, i + 1, j);
    var q = i + 1;
    doQuickSort(ary, comparator, p, q - 1);
    doQuickSort(ary, comparator, q + 1, r);
  }
}
var quickSort_1 = function (ary, comparator) {
  doQuickSort(ary, comparator, 0, ary.length - 1);
};
var quickSort$1 = {
	quickSort: quickSort_1
};

var ArraySet$2 = arraySet.ArraySet;
var quickSort = quickSort$1.quickSort;
function SourceMapConsumer$1(aSourceMap) {
  var sourceMap = aSourceMap;
  if (typeof aSourceMap === 'string') {
    sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));
  }
  return sourceMap.sections != null ? new IndexedSourceMapConsumer(sourceMap) : new BasicSourceMapConsumer(sourceMap);
}
SourceMapConsumer$1.fromSourceMap = function (aSourceMap) {
  return BasicSourceMapConsumer.fromSourceMap(aSourceMap);
};
SourceMapConsumer$1.prototype._version = 3;
SourceMapConsumer$1.prototype.__generatedMappings = null;
Object.defineProperty(SourceMapConsumer$1.prototype, '_generatedMappings', {
  get: function () {
    if (!this.__generatedMappings) {
      this._parseMappings(this._mappings, this.sourceRoot);
    }
    return this.__generatedMappings;
  }
});
SourceMapConsumer$1.prototype.__originalMappings = null;
Object.defineProperty(SourceMapConsumer$1.prototype, '_originalMappings', {
  get: function () {
    if (!this.__originalMappings) {
      this._parseMappings(this._mappings, this.sourceRoot);
    }
    return this.__originalMappings;
  }
});
SourceMapConsumer$1.prototype._charIsMappingSeparator = function SourceMapConsumer_charIsMappingSeparator(aStr, index) {
  var c = aStr.charAt(index);
  return c === ";" || c === ",";
};
SourceMapConsumer$1.prototype._parseMappings = function SourceMapConsumer_parseMappings(aStr, aSourceRoot) {
  throw new Error("Subclasses must implement _parseMappings");
};
SourceMapConsumer$1.GENERATED_ORDER = 1;
SourceMapConsumer$1.ORIGINAL_ORDER = 2;
SourceMapConsumer$1.GREATEST_LOWER_BOUND = 1;
SourceMapConsumer$1.LEAST_UPPER_BOUND = 2;
SourceMapConsumer$1.prototype.eachMapping = function SourceMapConsumer_eachMapping(aCallback, aContext, aOrder) {
  var context = aContext || null;
  var order = aOrder || SourceMapConsumer$1.GENERATED_ORDER;
  var mappings;
  switch (order) {
    case SourceMapConsumer$1.GENERATED_ORDER:
      mappings = this._generatedMappings;
      break;
    case SourceMapConsumer$1.ORIGINAL_ORDER:
      mappings = this._originalMappings;
      break;
    default:
      throw new Error("Unknown order of iteration.");
  }
  var sourceRoot = this.sourceRoot;
  mappings.map(function (mapping) {
    var source = mapping.source === null ? null : this._sources.at(mapping.source);
    if (source != null && sourceRoot != null) {
      source = util$1.join(sourceRoot, source);
    }
    return {
      source: source,
      generatedLine: mapping.generatedLine,
      generatedColumn: mapping.generatedColumn,
      originalLine: mapping.originalLine,
      originalColumn: mapping.originalColumn,
      name: mapping.name === null ? null : this._names.at(mapping.name)
    };
  }, this).forEach(aCallback, context);
};
SourceMapConsumer$1.prototype.allGeneratedPositionsFor = function SourceMapConsumer_allGeneratedPositionsFor(aArgs) {
  var line = util$1.getArg(aArgs, 'line');
  var needle = {
    source: util$1.getArg(aArgs, 'source'),
    originalLine: line,
    originalColumn: util$1.getArg(aArgs, 'column', 0)
  };
  if (this.sourceRoot != null) {
    needle.source = util$1.relative(this.sourceRoot, needle.source);
  }
  if (!this._sources.has(needle.source)) {
    return [];
  }
  needle.source = this._sources.indexOf(needle.source);
  var mappings = [];
  var index = this._findMapping(needle, this._originalMappings, "originalLine", "originalColumn", util$1.compareByOriginalPositions, binarySearch.LEAST_UPPER_BOUND);
  if (index >= 0) {
    var mapping = this._originalMappings[index];
    if (aArgs.column === undefined) {
      var originalLine = mapping.originalLine;
      while (mapping && mapping.originalLine === originalLine) {
        mappings.push({
          line: util$1.getArg(mapping, 'generatedLine', null),
          column: util$1.getArg(mapping, 'generatedColumn', null),
          lastColumn: util$1.getArg(mapping, 'lastGeneratedColumn', null)
        });
        mapping = this._originalMappings[++index];
      }
    } else {
      var originalColumn = mapping.originalColumn;
      while (mapping && mapping.originalLine === line && mapping.originalColumn == originalColumn) {
        mappings.push({
          line: util$1.getArg(mapping, 'generatedLine', null),
          column: util$1.getArg(mapping, 'generatedColumn', null),
          lastColumn: util$1.getArg(mapping, 'lastGeneratedColumn', null)
        });
        mapping = this._originalMappings[++index];
      }
    }
  }
  return mappings;
};
var SourceMapConsumer_1 = SourceMapConsumer$1;
function BasicSourceMapConsumer(aSourceMap) {
  var sourceMap = aSourceMap;
  if (typeof aSourceMap === 'string') {
    sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));
  }
  var version = util$1.getArg(sourceMap, 'version');
  var sources = util$1.getArg(sourceMap, 'sources');
  var names = util$1.getArg(sourceMap, 'names', []);
  var sourceRoot = util$1.getArg(sourceMap, 'sourceRoot', null);
  var sourcesContent = util$1.getArg(sourceMap, 'sourcesContent', null);
  var mappings = util$1.getArg(sourceMap, 'mappings');
  var file = util$1.getArg(sourceMap, 'file', null);
  if (version != this._version) {
    throw new Error('Unsupported version: ' + version);
  }
  sources = sources.map(String)
  .map(util$1.normalize)
  .map(function (source) {
    return sourceRoot && util$1.isAbsolute(sourceRoot) && util$1.isAbsolute(source) ? util$1.relative(sourceRoot, source) : source;
  });
  this._names = ArraySet$2.fromArray(names.map(String), true);
  this._sources = ArraySet$2.fromArray(sources, true);
  this.sourceRoot = sourceRoot;
  this.sourcesContent = sourcesContent;
  this._mappings = mappings;
  this.file = file;
}
BasicSourceMapConsumer.prototype = Object.create(SourceMapConsumer$1.prototype);
BasicSourceMapConsumer.prototype.consumer = SourceMapConsumer$1;
BasicSourceMapConsumer.fromSourceMap = function SourceMapConsumer_fromSourceMap(aSourceMap) {
  var smc = Object.create(BasicSourceMapConsumer.prototype);
  var names = smc._names = ArraySet$2.fromArray(aSourceMap._names.toArray(), true);
  var sources = smc._sources = ArraySet$2.fromArray(aSourceMap._sources.toArray(), true);
  smc.sourceRoot = aSourceMap._sourceRoot;
  smc.sourcesContent = aSourceMap._generateSourcesContent(smc._sources.toArray(), smc.sourceRoot);
  smc.file = aSourceMap._file;
  var generatedMappings = aSourceMap._mappings.toArray().slice();
  var destGeneratedMappings = smc.__generatedMappings = [];
  var destOriginalMappings = smc.__originalMappings = [];
  for (var i = 0, length = generatedMappings.length; i < length; i++) {
    var srcMapping = generatedMappings[i];
    var destMapping = new Mapping();
    destMapping.generatedLine = srcMapping.generatedLine;
    destMapping.generatedColumn = srcMapping.generatedColumn;
    if (srcMapping.source) {
      destMapping.source = sources.indexOf(srcMapping.source);
      destMapping.originalLine = srcMapping.originalLine;
      destMapping.originalColumn = srcMapping.originalColumn;
      if (srcMapping.name) {
        destMapping.name = names.indexOf(srcMapping.name);
      }
      destOriginalMappings.push(destMapping);
    }
    destGeneratedMappings.push(destMapping);
  }
  quickSort(smc.__originalMappings, util$1.compareByOriginalPositions);
  return smc;
};
BasicSourceMapConsumer.prototype._version = 3;
Object.defineProperty(BasicSourceMapConsumer.prototype, 'sources', {
  get: function () {
    return this._sources.toArray().map(function (s) {
      return this.sourceRoot != null ? util$1.join(this.sourceRoot, s) : s;
    }, this);
  }
});
function Mapping() {
  this.generatedLine = 0;
  this.generatedColumn = 0;
  this.source = null;
  this.originalLine = null;
  this.originalColumn = null;
  this.name = null;
}
BasicSourceMapConsumer.prototype._parseMappings = function SourceMapConsumer_parseMappings(aStr, aSourceRoot) {
  var generatedLine = 1;
  var previousGeneratedColumn = 0;
  var previousOriginalLine = 0;
  var previousOriginalColumn = 0;
  var previousSource = 0;
  var previousName = 0;
  var length = aStr.length;
  var index = 0;
  var cachedSegments = {};
  var temp = {};
  var originalMappings = [];
  var generatedMappings = [];
  var mapping, str, segment, end, value;
  while (index < length) {
    if (aStr.charAt(index) === ';') {
      generatedLine++;
      index++;
      previousGeneratedColumn = 0;
    } else if (aStr.charAt(index) === ',') {
      index++;
    } else {
      mapping = new Mapping();
      mapping.generatedLine = generatedLine;
      for (end = index; end < length; end++) {
        if (this._charIsMappingSeparator(aStr, end)) {
          break;
        }
      }
      str = aStr.slice(index, end);
      segment = cachedSegments[str];
      if (segment) {
        index += str.length;
      } else {
        segment = [];
        while (index < end) {
          base64Vlq.decode(aStr, index, temp);
          value = temp.value;
          index = temp.rest;
          segment.push(value);
        }
        if (segment.length === 2) {
          throw new Error('Found a source, but no line and column');
        }
        if (segment.length === 3) {
          throw new Error('Found a source and line, but no column');
        }
        cachedSegments[str] = segment;
      }
      mapping.generatedColumn = previousGeneratedColumn + segment[0];
      previousGeneratedColumn = mapping.generatedColumn;
      if (segment.length > 1) {
        mapping.source = previousSource + segment[1];
        previousSource += segment[1];
        mapping.originalLine = previousOriginalLine + segment[2];
        previousOriginalLine = mapping.originalLine;
        mapping.originalLine += 1;
        mapping.originalColumn = previousOriginalColumn + segment[3];
        previousOriginalColumn = mapping.originalColumn;
        if (segment.length > 4) {
          mapping.name = previousName + segment[4];
          previousName += segment[4];
        }
      }
      generatedMappings.push(mapping);
      if (typeof mapping.originalLine === 'number') {
        originalMappings.push(mapping);
      }
    }
  }
  quickSort(generatedMappings, util$1.compareByGeneratedPositionsDeflated);
  this.__generatedMappings = generatedMappings;
  quickSort(originalMappings, util$1.compareByOriginalPositions);
  this.__originalMappings = originalMappings;
};
BasicSourceMapConsumer.prototype._findMapping = function SourceMapConsumer_findMapping(aNeedle, aMappings, aLineName, aColumnName, aComparator, aBias) {
  if (aNeedle[aLineName] <= 0) {
    throw new TypeError('Line must be greater than or equal to 1, got ' + aNeedle[aLineName]);
  }
  if (aNeedle[aColumnName] < 0) {
    throw new TypeError('Column must be greater than or equal to 0, got ' + aNeedle[aColumnName]);
  }
  return binarySearch.search(aNeedle, aMappings, aComparator, aBias);
};
BasicSourceMapConsumer.prototype.computeColumnSpans = function SourceMapConsumer_computeColumnSpans() {
  for (var index = 0; index < this._generatedMappings.length; ++index) {
    var mapping = this._generatedMappings[index];
    if (index + 1 < this._generatedMappings.length) {
      var nextMapping = this._generatedMappings[index + 1];
      if (mapping.generatedLine === nextMapping.generatedLine) {
        mapping.lastGeneratedColumn = nextMapping.generatedColumn - 1;
        continue;
      }
    }
    mapping.lastGeneratedColumn = Infinity;
  }
};
BasicSourceMapConsumer.prototype.originalPositionFor = function SourceMapConsumer_originalPositionFor(aArgs) {
  var needle = {
    generatedLine: util$1.getArg(aArgs, 'line'),
    generatedColumn: util$1.getArg(aArgs, 'column')
  };
  var index = this._findMapping(needle, this._generatedMappings, "generatedLine", "generatedColumn", util$1.compareByGeneratedPositionsDeflated, util$1.getArg(aArgs, 'bias', SourceMapConsumer$1.GREATEST_LOWER_BOUND));
  if (index >= 0) {
    var mapping = this._generatedMappings[index];
    if (mapping.generatedLine === needle.generatedLine) {
      var source = util$1.getArg(mapping, 'source', null);
      if (source !== null) {
        source = this._sources.at(source);
        if (this.sourceRoot != null) {
          source = util$1.join(this.sourceRoot, source);
        }
      }
      var name = util$1.getArg(mapping, 'name', null);
      if (name !== null) {
        name = this._names.at(name);
      }
      return {
        source: source,
        line: util$1.getArg(mapping, 'originalLine', null),
        column: util$1.getArg(mapping, 'originalColumn', null),
        name: name
      };
    }
  }
  return {
    source: null,
    line: null,
    column: null,
    name: null
  };
};
BasicSourceMapConsumer.prototype.hasContentsOfAllSources = function BasicSourceMapConsumer_hasContentsOfAllSources() {
  if (!this.sourcesContent) {
    return false;
  }
  return this.sourcesContent.length >= this._sources.size() && !this.sourcesContent.some(function (sc) {
    return sc == null;
  });
};
BasicSourceMapConsumer.prototype.sourceContentFor = function SourceMapConsumer_sourceContentFor(aSource, nullOnMissing) {
  if (!this.sourcesContent) {
    return null;
  }
  if (this.sourceRoot != null) {
    aSource = util$1.relative(this.sourceRoot, aSource);
  }
  if (this._sources.has(aSource)) {
    return this.sourcesContent[this._sources.indexOf(aSource)];
  }
  var url;
  if (this.sourceRoot != null && (url = util$1.urlParse(this.sourceRoot))) {
    var fileUriAbsPath = aSource.replace(/^file:\/\//, "");
    if (url.scheme == "file" && this._sources.has(fileUriAbsPath)) {
      return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)];
    }
    if ((!url.path || url.path == "/") && this._sources.has("/" + aSource)) {
      return this.sourcesContent[this._sources.indexOf("/" + aSource)];
    }
  }
  if (nullOnMissing) {
    return null;
  } else {
    throw new Error('"' + aSource + '" is not in the SourceMap.');
  }
};
BasicSourceMapConsumer.prototype.generatedPositionFor = function SourceMapConsumer_generatedPositionFor(aArgs) {
  var source = util$1.getArg(aArgs, 'source');
  if (this.sourceRoot != null) {
    source = util$1.relative(this.sourceRoot, source);
  }
  if (!this._sources.has(source)) {
    return {
      line: null,
      column: null,
      lastColumn: null
    };
  }
  source = this._sources.indexOf(source);
  var needle = {
    source: source,
    originalLine: util$1.getArg(aArgs, 'line'),
    originalColumn: util$1.getArg(aArgs, 'column')
  };
  var index = this._findMapping(needle, this._originalMappings, "originalLine", "originalColumn", util$1.compareByOriginalPositions, util$1.getArg(aArgs, 'bias', SourceMapConsumer$1.GREATEST_LOWER_BOUND));
  if (index >= 0) {
    var mapping = this._originalMappings[index];
    if (mapping.source === needle.source) {
      return {
        line: util$1.getArg(mapping, 'generatedLine', null),
        column: util$1.getArg(mapping, 'generatedColumn', null),
        lastColumn: util$1.getArg(mapping, 'lastGeneratedColumn', null)
      };
    }
  }
  return {
    line: null,
    column: null,
    lastColumn: null
  };
};
var BasicSourceMapConsumer_1 = BasicSourceMapConsumer;
function IndexedSourceMapConsumer(aSourceMap) {
  var sourceMap = aSourceMap;
  if (typeof aSourceMap === 'string') {
    sourceMap = JSON.parse(aSourceMap.replace(/^\)\]\}'/, ''));
  }
  var version = util$1.getArg(sourceMap, 'version');
  var sections = util$1.getArg(sourceMap, 'sections');
  if (version != this._version) {
    throw new Error('Unsupported version: ' + version);
  }
  this._sources = new ArraySet$2();
  this._names = new ArraySet$2();
  var lastOffset = {
    line: -1,
    column: 0
  };
  this._sections = sections.map(function (s) {
    if (s.url) {
      throw new Error('Support for url field in sections not implemented.');
    }
    var offset = util$1.getArg(s, 'offset');
    var offsetLine = util$1.getArg(offset, 'line');
    var offsetColumn = util$1.getArg(offset, 'column');
    if (offsetLine < lastOffset.line || offsetLine === lastOffset.line && offsetColumn < lastOffset.column) {
      throw new Error('Section offsets must be ordered and non-overlapping.');
    }
    lastOffset = offset;
    return {
      generatedOffset: {
        generatedLine: offsetLine + 1,
        generatedColumn: offsetColumn + 1
      },
      consumer: new SourceMapConsumer$1(util$1.getArg(s, 'map'))
    };
  });
}
IndexedSourceMapConsumer.prototype = Object.create(SourceMapConsumer$1.prototype);
IndexedSourceMapConsumer.prototype.constructor = SourceMapConsumer$1;
IndexedSourceMapConsumer.prototype._version = 3;
Object.defineProperty(IndexedSourceMapConsumer.prototype, 'sources', {
  get: function () {
    var sources = [];
    for (var i = 0; i < this._sections.length; i++) {
      for (var j = 0; j < this._sections[i].consumer.sources.length; j++) {
        sources.push(this._sections[i].consumer.sources[j]);
      }
    }
    return sources;
  }
});
IndexedSourceMapConsumer.prototype.originalPositionFor = function IndexedSourceMapConsumer_originalPositionFor(aArgs) {
  var needle = {
    generatedLine: util$1.getArg(aArgs, 'line'),
    generatedColumn: util$1.getArg(aArgs, 'column')
  };
  var sectionIndex = binarySearch.search(needle, this._sections, function (needle, section) {
    var cmp = needle.generatedLine - section.generatedOffset.generatedLine;
    if (cmp) {
      return cmp;
    }
    return needle.generatedColumn - section.generatedOffset.generatedColumn;
  });
  var section = this._sections[sectionIndex];
  if (!section) {
    return {
      source: null,
      line: null,
      column: null,
      name: null
    };
  }
  return section.consumer.originalPositionFor({
    line: needle.generatedLine - (section.generatedOffset.generatedLine - 1),
    column: needle.generatedColumn - (section.generatedOffset.generatedLine === needle.generatedLine ? section.generatedOffset.generatedColumn - 1 : 0),
    bias: aArgs.bias
  });
};
IndexedSourceMapConsumer.prototype.hasContentsOfAllSources = function IndexedSourceMapConsumer_hasContentsOfAllSources() {
  return this._sections.every(function (s) {
    return s.consumer.hasContentsOfAllSources();
  });
};
IndexedSourceMapConsumer.prototype.sourceContentFor = function IndexedSourceMapConsumer_sourceContentFor(aSource, nullOnMissing) {
  for (var i = 0; i < this._sections.length; i++) {
    var section = this._sections[i];
    var content = section.consumer.sourceContentFor(aSource, true);
    if (content) {
      return content;
    }
  }
  if (nullOnMissing) {
    return null;
  } else {
    throw new Error('"' + aSource + '" is not in the SourceMap.');
  }
};
IndexedSourceMapConsumer.prototype.generatedPositionFor = function IndexedSourceMapConsumer_generatedPositionFor(aArgs) {
  for (var i = 0; i < this._sections.length; i++) {
    var section = this._sections[i];
    if (section.consumer.sources.indexOf(util$1.getArg(aArgs, 'source')) === -1) {
      continue;
    }
    var generatedPosition = section.consumer.generatedPositionFor(aArgs);
    if (generatedPosition) {
      var ret = {
        line: generatedPosition.line + (section.generatedOffset.generatedLine - 1),
        column: generatedPosition.column + (section.generatedOffset.generatedLine === generatedPosition.line ? section.generatedOffset.generatedColumn - 1 : 0)
      };
      return ret;
    }
  }
  return {
    line: null,
    column: null
  };
};
IndexedSourceMapConsumer.prototype._parseMappings = function IndexedSourceMapConsumer_parseMappings(aStr, aSourceRoot) {
  this.__generatedMappings = [];
  this.__originalMappings = [];
  for (var i = 0; i < this._sections.length; i++) {
    var section = this._sections[i];
    var sectionMappings = section.consumer._generatedMappings;
    for (var j = 0; j < sectionMappings.length; j++) {
      var mapping = sectionMappings[j];
      var source = section.consumer._sources.at(mapping.source);
      if (section.consumer.sourceRoot !== null) {
        source = util$1.join(section.consumer.sourceRoot, source);
      }
      this._sources.add(source);
      source = this._sources.indexOf(source);
      var name = section.consumer._names.at(mapping.name);
      this._names.add(name);
      name = this._names.indexOf(name);
      var adjustedMapping = {
        source: source,
        generatedLine: mapping.generatedLine + (section.generatedOffset.generatedLine - 1),
        generatedColumn: mapping.generatedColumn + (section.generatedOffset.generatedLine === mapping.generatedLine ? section.generatedOffset.generatedColumn - 1 : 0),
        originalLine: mapping.originalLine,
        originalColumn: mapping.originalColumn,
        name: name
      };
      this.__generatedMappings.push(adjustedMapping);
      if (typeof adjustedMapping.originalLine === 'number') {
        this.__originalMappings.push(adjustedMapping);
      }
    }
  }
  quickSort(this.__generatedMappings, util$1.compareByGeneratedPositionsDeflated);
  quickSort(this.__originalMappings, util$1.compareByOriginalPositions);
};
var IndexedSourceMapConsumer_1 = IndexedSourceMapConsumer;
var sourceMapConsumer = {
	SourceMapConsumer: SourceMapConsumer_1,
	BasicSourceMapConsumer: BasicSourceMapConsumer_1,
	IndexedSourceMapConsumer: IndexedSourceMapConsumer_1
};

var isSourceNode = "$$$isSourceNode$$$";

var SourceMapConsumer = sourceMapConsumer.SourceMapConsumer;

const traceItemRegex = /(http.+):(\d+):(\d+)/;
var buildTraceCollection = (xs => {
  return xs.split('\n').map(x => traceItemRegex.exec(x)).filter(x => x != null).map(([compiledLine, url, line, column]) => ({
    compiledLine,
    url,
    line: parseInt(line, 10),
    column: parseInt(column, 10)
  }));
});

var srcmapReverse = ((srcMap, trace) => {
  const smc = new SourceMapConsumer(srcMap);
  return buildTraceCollection(trace).map(smc.originalPositionFor.bind(smc)).map(x => {
    x.name = x.name ? 'at ' + x.name + ' ' : 'at ';
    return x;
  }).map(x => `${x.name}${x.source}:${x.line}:${x.column}`).join('\n');
});

var reverser = (srcmapFile => s => {
  srcmapFile = srcmapFile || 'main.js.map';
  const sourceMapStream = index(fs.createReadStream(srcmapFile));
  return index([sourceMapStream, s]).flatMap(s => s.map(x => {
    return x.toString('utf8');
  }).collect().map(x => x.join(''))).collect().map(([srcMap, traceLine]) => srcmapReverse(srcMap, traceLine));
});

if (cluster.isMaster) {
  const server = http.createServer((request, response) => {
    const r = index(request);
    r.map(x => x.toString('utf-8')).collect().map(x => x.join('')).map(JSON.parse).flatMap(reverseInParallel).map(xs => JSON.stringify(xs)).pipe(response);
  });
  server.listen({ fd: parseInt(process.env.SRCMAP_REVERSE_FD, 10) });
} else {
  process.on('message', ({ line, srcmapFile }) => {
    index([line]).otherwise(index([''])).through(reverser(srcmapFile)).collect().errors(e => {
      if (process.send) process.send({ error: e });
    }).each(line => {
      if (process.send) process.send({ line });
    });
  });
}
//# sourceMappingURL=srcmap-reverse.js.map
