UNPKG

@awayjs/graphics

Version:
83 lines (82 loc) 2.71 kB
var NativeDeflate = /** @class */ (function () { function NativeDeflate(_verHeader, _size) { this._verHeader = _verHeader; this._size = _size; this.isDone = false; this._isRunned = false; this._blockPosition = 0; this.onError = function (_e) { }; if (_verHeader) { this._size -= 8; } else { //throw 'Native not support deflate without header'; } if (!NativeDeflate.isSupported) { throw 'DecompressionStream is not supported!'; } var decoder = new self.DecompressionStream('deflate'); this._writer = decoder.writable.getWriter(); this._reader = decoder.readable.getReader(); this._buffer = new Uint8Array(_size); this._processBlocks = this._processBlocks.bind(this); } Object.defineProperty(NativeDeflate.prototype, "closed", { get: function () { return !this._isRunned && this.isDone; }, enumerable: false, configurable: true }); Object.defineProperty(NativeDeflate, "isSupported", { // check that decoding stream is supported get: function () { return ('DecompressionStream' in self); }, enumerable: false, configurable: true }); NativeDeflate.prototype._processBlocks = function (_a) { var done = _a.done, value = _a.value; var reader = this._reader; if (value) { if (value.length >= this._size) { this._buffer = value; this._blockPosition = this._size; } else { this._buffer.set(value, this._blockPosition); this._blockPosition += value.length; } } if (done || this._blockPosition >= this._size) { this.isDone = true; this.onData && this.onData(this._buffer); return this._buffer; } return reader.read().then(this._processBlocks); }; NativeDeflate.prototype.push = function (data) { // header if (data.length === 8) { // header this.onData && this.onData(data); return; } this._writer.write(data); if (!this._isRunned) { this._isRunned = true; this._reader.read() .then(this._processBlocks) .catch(this.onError); } }; NativeDeflate.prototype.close = function () { if (this._isRunned && this.isDone) { this._writer.close(); this._isRunned = false; } }; return NativeDeflate; }()); export { NativeDeflate };