{“ast”:null,“code”:“'use strict';nnvar utils = require('../utils');nnvar ConvertWorker = require('./ConvertWorker');nnvar GenericWorker = require('./GenericWorker');nnvar base64 = require('../base64');nnvar support = require("../support");nnvar external = require("../external");nnvar NodejsStreamOutputAdapter = null;nnif (support.nodestream) {n try {n NodejsStreamOutputAdapter = require('../nodejs/NodejsStreamOutputAdapter');n } catch (e) {}n}n/**n * Apply the final transformation of the data. If the user wants a Blob forn * example, it's easier to work with an U8intArray and finally do then * ArrayBuffer/Blob conversion.n * @param {String} type the name of the final typen * @param {String|Uint8Array|Buffer} content the content to transformn * @param {String} mimeType the mime type of the content, if applicable.n * @return {String|Uint8Array|ArrayBuffer|Buffer|Blob} the content in the right format.n */nnnfunction transformZipOutput(type, content, mimeType) {n switch (type) {n case "blob":n return utils.newBlob(utils.transformTo("arraybuffer", content), mimeType);nn case "base64":n return base64.encode(content);nn default:n return utils.transformTo(type, content);n }n}n/**n * Concatenate an array of data of the given type.n * @param {String} type the type of the data in the given array.n * @param {Array} dataArray the array containing the data chunks to concatenaten * @return {String|Uint8Array|Buffer} the concatenated datan * @throws Error if the asked type is unsupportedn */nnnfunction concat(type, dataArray) {n var i,n index = 0,n res = null,n totalLength = 0;nn for (i = 0; i < dataArray.length; i++) {n totalLength += dataArray.length;n }nn switch (type) {n case "string":n return dataArray.join("");nn case "array":n return Array.prototype.concat.apply([], dataArray);nn case "uint8array":n res = new Uint8Array(totalLength);nn for (i = 0; i < dataArray.length; i++) {n res.set(dataArray, index);n index += dataArray.length;n }nn return res;nn case "nodebuffer":n return Buffer.concat(dataArray);nn default:n throw new Error("concat : unsupported type '" + type + "'");n }n}n/**n * Listen a StreamHelper, accumulate its content and concatenate it into an * complete block.n * @param {StreamHelper} helper the helper to use.n * @param {Function} updateCallback a callback called on each update. Calledn * with one arg :n * - the metadata linked to the update received.n * @return Promise the promise for the accumulation.n */nnnfunction _accumulate(helper, updateCallback) {n return new external.Promise(function (resolve, reject) {n var dataArray = [];n var chunkType = helper._internalType,n resultType = helper._outputType,n mimeType = helper._mimeType;n helper.on('data', function (data, meta) {n dataArray.push(data);nn if (updateCallback) {n updateCallback(meta);n }n }).on('error', function (err) {n dataArray = [];n reject(err);n }).on('end', function () {n try {n var result = transformZipOutput(resultType, concat(chunkType, dataArray), mimeType);n resolve(result);n } catch (e) {n reject(e);n }nn dataArray = [];n }).resume();n });n}n/**n * An helper to easily use workers outside of JSZip.n * @constructorn * @param {Worker} worker the worker to wrapn * @param {String} outputType the type of data expected by the usen * @param {String} mimeType the mime type of the content, if applicable.n */nnnfunction StreamHelper(worker, outputType, mimeType) {n var internalType = outputType;nn switch (outputType) {n case "blob":n case "arraybuffer":n internalType = "uint8array";n break;nn case "base64":n internalType = "string";n break;n }nn try {n // the type used internallyn this._internalType = internalType; // the type used to output resultsnn this._outputType = outputType; // the mime typenn this._mimeType = mimeType;n utils.checkSupport(internalType);n this._worker = worker.pipe(new ConvertWorker(internalType)); // the last workers can be rewired without issues but we need ton // prevent any updates on previous workers.nn worker.lock();n } catch (e) {n this._worker = new GenericWorker("error");nn this._worker.error(e);n }n}nnStreamHelper.prototype = {n /**n * Listen a StreamHelper, accumulate its content and concatenate it into an * complete block.n * @param {Function} updateCb the update callback.n * @return Promise the promise for the accumulation.n */n accumulate: function accumulate(updateCb) {n return _accumulate(this, updateCb);n },nn /**n * Add a listener on an event triggered on a stream.n * @param {String} evt the name of the eventn * @param {Function} fn the listenern * @return {StreamHelper} the current helper.n */n on: function on(evt, fn) {n var self = this;nn if (evt === "data") {n this._worker.on(evt, function (chunk) {n fn.call(self, chunk.data, chunk.meta);n });n } else {n this._worker.on(evt, function () {n utils.delay(fn, arguments, self);n });n }nn return this;n },nn /**n * Resume the flow of chunks.n * @return {StreamHelper} the current helper.n */n resume: function resume() {n utils.delay(this._worker.resume, [], this._worker);n return this;n },nn /**n * Pause the flow of chunks.n * @return {StreamHelper} the current helper.n */n pause: function pause() {n this._worker.pause();nn return this;n },nn /**n * Return a nodejs stream for this helper.n * @param {Function} updateCb the update callback.n * @return {NodejsStreamOutputAdapter} the nodejs stream.n */n toNodejsStream: function toNodejsStream(updateCb) {n utils.checkSupport("nodestream");nn if (this._outputType !== "nodebuffer") {n // an object stream containing blob/arraybuffer/uint8array/stringn // is strange and I don't know if it would be useful.n // I you find this comment and have a good usecase, please open an // bug report !n throw new Error(this._outputType + " is not supported by this method");n }nn return new NodejsStreamOutputAdapter(this, {n objectMode: this._outputType !== "nodebuffer"n }, updateCb);n }n};nmodule.exports = StreamHelper;”,“map”:null,“metadata”:{},“sourceType”:“module”}