| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.ProgressCallbackTransform = void 0;
- function _stream() {
- const data = require("stream");
- _stream = function () {
- return data;
- };
- return data;
- }
- class ProgressCallbackTransform extends _stream().Transform {
- constructor(total, cancellationToken, onProgress) {
- super();
- this.total = total;
- this.cancellationToken = cancellationToken;
- this.onProgress = onProgress;
- this.start = Date.now();
- this.transferred = 0;
- this.delta = 0;
- this.nextUpdate = this.start + 1000;
- }
- _transform(chunk, encoding, callback) {
- if (this.cancellationToken.cancelled) {
- callback(new Error("Cancelled"), null);
- return;
- }
- this.transferred += chunk.length;
- this.delta += chunk.length;
- const now = Date.now();
- if (now >= this.nextUpdate && this.transferred !== this.total
- /* will be emitted on _flush */
- ) {
- this.nextUpdate = now + 1000;
- this.onProgress({
- total: this.total,
- delta: this.delta,
- transferred: this.transferred,
- percent: this.transferred / this.total * 100,
- bytesPerSecond: Math.round(this.transferred / ((now - this.start) / 1000))
- });
- this.delta = 0;
- }
- callback(null, chunk);
- }
- _flush(callback) {
- if (this.cancellationToken.cancelled) {
- callback(new Error("Cancelled"));
- return;
- }
- this.onProgress({
- total: this.total,
- delta: this.delta,
- transferred: this.total,
- percent: 100,
- bytesPerSecond: Math.round(this.transferred / ((Date.now() - this.start) / 1000))
- });
- this.delta = 0;
- callback(null);
- }
- } exports.ProgressCallbackTransform = ProgressCallbackTransform;
- //# sourceMappingURL=ProgressCallbackTransform.js.map
|