UNPKG

hackpro-sdk

Version:
66 lines 2.37 kB
"use strict"; /* * Copyright 2018 balena.io * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const readable_stream_1 = require("readable-stream"); class BlockTransformStream extends readable_stream_1.Transform { constructor(chunkSize) { super(); this.chunkSize = chunkSize; this.bytesRead = 0; this.bytesWritten = 0; this._buffers = []; this._bytes = 0; } writeBuffers(flush = false) { if (flush || this._bytes >= this.chunkSize) { let block = Buffer.concat(this._buffers); const length = flush ? block.length : Math.floor(block.length / this.chunkSize) * this.chunkSize; this._buffers.length = 0; this._bytes = 0; if (block.length !== length) { this._buffers.push(block.slice(length)); this._bytes += block.length - length; block = block.slice(0, length); } this.bytesWritten += block.length; this.push(block); } } _transform(chunk, _encoding, callback) { this.bytesRead += chunk.length; if (this._bytes === 0 && chunk.length >= this.chunkSize && chunk.length % this.chunkSize === 0) { this.bytesWritten += chunk.length; this.push(chunk); } else { this._buffers.push(chunk); this._bytes += chunk.length; this.writeBuffers(); } callback(); } _flush(callback) { this.writeBuffers(true); callback(); } } exports.BlockTransformStream = BlockTransformStream; //# sourceMappingURL=block-transform-stream.js.map