TrinetraLabs/Placebo_AI
0
1"use strict";2var USE_TYPEDARRAY = (typeof Uint8Array !== "undefined") && (typeof Uint16Array !== "undefined") && (typeof Uint32Array !== "undefined");3 4var pako = require("pako");5var utils = require("./utils");6var GenericWorker = require("./stream/GenericWorker");7 8var ARRAY_TYPE = USE_TYPEDARRAY ? "uint8array" : "array";9 10exports.magic = "\x08\x00";11 12/**13 * Create a worker that uses pako to inflate/deflate.14 * @constructor15 * @param {String} action the name of the pako function to call : either "Deflate" or "Inflate".16 * @param {Object} options the options to use when (de)compressing.17 */18function FlateWorker(action, options) {19 GenericWorker.call(this, "FlateWorker/" + action);20 21 this._pako = null;22 this._pakoAction = action;23 this._pakoOptions = options;24 // the `meta` object from the last chunk received25 // this allow this worker to pass around metadata26 this.meta = {};27}28 29utils.inherits(FlateWorker, GenericWorker);30 31/**32 * @see GenericWorker.processChunk33 */34FlateWorker.prototype.processChunk = function (chunk) {35 this.meta = chunk.meta;36 if (this._pako === null) {37 this._createPako();38 }39 this._pako.push(utils.transformTo(ARRAY_TYPE, chunk.data), false);40};41 42/**43 * @see GenericWorker.flush44 */45FlateWorker.prototype.flush = function () {46 GenericWorker.prototype.flush.call(this);47 if (this._pako === null) {48 this._createPako();49 }50 this._pako.push([], true);51};52/**53 * @see GenericWorker.cleanUp54 */55FlateWorker.prototype.cleanUp = function () {56 GenericWorker.prototype.cleanUp.call(this);57 this._pako = null;58};59 60/**61 * Create the _pako object.62 * TODO: lazy-loading this object isn't the best solution but it's the63 * quickest. The best solution is to lazy-load the worker list. See also the64 * issue #446.65 */66FlateWorker.prototype._createPako = function () {67 this._pako = new pako[this._pakoAction]({68 raw: true,69 level: this._pakoOptions.level || -1 // default compression70 });71 var self = this;72 this._pako.onData = function(data) {73 self.push({74 data : data,75 meta : self.meta76 });77 };78};79 80exports.compressWorker = function (compressionOptions) {81 return new FlateWorker("Deflate", compressionOptions);82};83exports.uncompressWorker = function () {84 return new FlateWorker("Inflate", {});85};86 