{“ast”:null,“code”:“'use strict';n/**n * A worker that does nothing but passing chunks to the next one. This is liken * a nodejs stream but with some differences. On the good side :n * - it works on IE 6-9 without any issue / polyfilln * - it weights less than the full dependencies bundled with browserifyn * - it forwards errors (no need to declare an error handler EVERYWHERE)n *n * A chunk is an object with 2 attributes : `meta` and `data`. The former is ann * object containing anything (`percent` for example), see each worker for moren * details. The latter is the real data (String, Uint8Array, etc).n *n * @constructorn * @param {String} name the name of the stream (mainly used for debugging purposes)n */nnfunction GenericWorker(name) {n // the name of the workern this.name = name || "default"; // an object containing metadata about the workers chainnn this.streamInfo = {}; // an error which happened when the worker was pausednn this.generatedError = null; // an object containing metadata to be merged by this worker into the general metadatann this.extraStreamInfo = {}; // true if the stream is paused (and should not do anything), false otherwisenn this.isPaused = true; // true if the stream is finished (and should not do anything), false otherwisenn this.isFinished = false; // true if the stream is locked to prevent further structure updates (pipe), false otherwisenn this.isLocked = false; // the event listenersnn this._listeners = {n 'data': [],n 'end': [],n 'error': []n }; // the previous worker, if anynn this.previous = null;n}nnGenericWorker.prototype = {n /**n * Push a chunk to the next workers.n * @param {Object} chunk the chunk to pushn */n push: function push(chunk) {n this.emit("data", chunk);n },nn /**n * End the stream.n * @return {Boolean} true if this call ended the worker, false otherwise.n */n end: function end() {n if (this.isFinished) {n return false;n }nn this.flush();nn try {n this.emit("end");n this.cleanUp();n this.isFinished = true;n } catch (e) {n this.emit("error", e);n }nn return true;n },nn /**n * End the stream with an error.n * @param {Error} e the error which caused the premature end.n * @return {Boolean} true if this call ended the worker with an error, false otherwise.n */n error: function error(e) {n if (this.isFinished) {n return false;n }nn if (this.isPaused) {n this.generatedError = e;n } else {n this.isFinished = true;n this.emit("error", e); // in the workers chain exploded in the middle of the chain,n // the error event will go downward but we also need to notifyn // workers upward that there has been an error.nn if (this.previous) {n this.previous.error(e);n }nn this.cleanUp();n }nn return true;n },nn /**n * Add a callback on an event.n * @param {String} name the name of the event (data, end, error)n * @param {Function} listener the function to call when the event is triggeredn * @return {GenericWorker} the current object for chainabilityn */n on: function on(name, listener) {n this._listeners.push(listener);nn return this;n },nn /**n * Clean any references when a worker is ending.n */n cleanUp: function cleanUp() {n this.streamInfo = this.generatedError = this.extraStreamInfo = null;n this._listeners = [];n },nn /**n * Trigger an event. This will call registered callback with the provided arg.n * @param {String} name the name of the event (data, end, error)n * @param {Object} arg the argument to call the callback with.n */n emit: function emit(name, arg) {n if (this._listeners) {n for (var i = 0; i < this._listeners.length; i++) {n this._listeners[i].call(this, arg);n }n }n },nn /**n * Chain a worker with an other.n * @param {Worker} next the worker receiving events from the current one.n * @return {worker} the next worker for chainabilityn */n pipe: function pipe(next) {n return next.registerPrevious(this);n },nn /**n * Same as `pipe` in the other direction.n * Using an API with `pipe(next)` is very easy.n * Implementing the API with the point of view of the next one registeringn * a source is easier, see the ZipFileWorker.n * @param {Worker} previous the previous worker, sending events to this onen * @return {Worker} the current worker for chainabilityn */n registerPrevious: function registerPrevious(previous) {n if (this.isLocked) {n throw new Error("The stream '" + this + "' has already been used.");n } // sharing the streamInfo…nnn this.streamInfo = previous.streamInfo; // … and adding our own bitsnn this.mergeStreamInfo();n this.previous = previous;n var self = this;n previous.on('data', function (chunk) {n self.processChunk(chunk);n });n previous.on('end', function () {n self.end();n });n previous.on('error', function (e) {n self.error(e);n });n return this;n },nn /**n * Pause the stream so it doesn't send events anymore.n * @return {Boolean} true if this call paused the worker, false otherwise.n */n pause: function pause() {n if (this.isPaused || this.isFinished) {n return false;n }nn this.isPaused = true;nn if (this.previous) {n this.previous.pause();n }nn return true;n },nn /**n * Resume a paused stream.n * @return {Boolean} true if this call resumed the worker, false otherwise.n */n resume: function resume() {n if (!this.isPaused || this.isFinished) {n return false;n }nn this.isPaused = false; // if true, the worker tried to resume but failednn var withError = false;nn if (this.generatedError) {n this.error(this.generatedError);n withError = true;n }nn if (this.previous) {n this.previous.resume();n }nn return !withError;n },nn /**n * Flush any remaining bytes as the stream is ending.n */n flush: function flush() {},nn /**n * Process a chunk. This is usually the method overridden.n * @param {Object} chunk the chunk to process.n */n processChunk: function processChunk(chunk) {n this.push(chunk);n },nn /**n * Add a key/value to be added in the workers chain streamInfo once activated.n * @param {String} key the key to usen * @param {Object} value the associated valuen * @return {Worker} the current worker for chainabilityn */n withStreamInfo: function withStreamInfo(key, value) {n this.extraStreamInfo = value;n this.mergeStreamInfo();n return this;n },nn /**n * Merge this worker's streamInfo into the chain's streamInfo.n */n mergeStreamInfo: function mergeStreamInfo() {n for (var key in this.extraStreamInfo) {n if (!this.extraStreamInfo.hasOwnProperty(key)) {n continue;n }nn this.streamInfo = this.extraStreamInfo;n }n },nn /**n * Lock the stream to prevent further updates on the workers chain.n * After calling this method, all calls to pipe will fail.n */n lock: function lock() {n if (this.isLocked) {n throw new Error("The stream '" + this + "' has already been used.");n }nn this.isLocked = true;nn if (this.previous) {n this.previous.lock();n }n },nn /**n *n * Pretty print the workers chain.n */n toString: function toString() {n var me = "Worker " + this.name;nn if (this.previous) {n return this.previous + " -> " + me;n } else {n return me;n }n }n};nmodule.exports = GenericWorker;”,“map”:null,“metadata”:{},“sourceType”:“module”}