@nightowne/tas-cli 2.0.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ import path from 'path';
8
8
 
9
9
  const gzip = promisify(zlib.gzip);
10
10
  const gunzip = promisify(zlib.gunzip);
11
+ import { PassThrough } from 'stream';
11
12
 
12
13
  // File extensions that are already compressed (skip compression for these)
13
14
  const SKIP_COMPRESSION = new Set([
@@ -71,6 +72,24 @@ export class Compressor {
71
72
  }
72
73
  }
73
74
 
75
+ /**
76
+ * Get a compression transform stream
77
+ * Returns: { stream: Transform, compressed: boolean }
78
+ */
79
+ getCompressStream(filename = '') {
80
+ if (this.shouldSkip(filename)) {
81
+ return {
82
+ stream: new PassThrough(),
83
+ compressed: false
84
+ };
85
+ }
86
+
87
+ return {
88
+ stream: zlib.createGzip({ level: 6 }),
89
+ compressed: true
90
+ };
91
+ }
92
+
74
93
  /**
75
94
  * Decompress gzip data
76
95
  */
@@ -81,4 +100,15 @@ export class Compressor {
81
100
 
82
101
  return await gunzip(data);
83
102
  }
103
+
104
+ /**
105
+ * Get a decompression transform stream
106
+ */
107
+ getDecompressStream(wasCompressed) {
108
+ if (!wasCompressed) {
109
+ return new PassThrough();
110
+ }
111
+
112
+ return zlib.createGunzip();
113
+ }
84
114
  }
@@ -76,10 +76,10 @@ export class ProgressBar {
76
76
  * Format bytes to human readable
77
77
  */
78
78
  formatBytes(bytes) {
79
- if (bytes === 0) return '0 B';
79
+ if (!Number.isFinite(bytes) || bytes <= 0) return '0 B';
80
80
  const k = 1024;
81
- const sizes = ['B', 'KB', 'MB', 'GB'];
82
- const i = Math.floor(Math.log(bytes) / Math.log(k));
81
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
82
+ const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
83
83
  return (bytes / Math.pow(k, i)).toFixed(1) + ' ' + sizes[i];
84
84
  }
85
85
 
@@ -0,0 +1,26 @@
1
+ import { Transform } from 'stream';
2
+
3
+ export class Throttle extends Transform {
4
+ constructor(bytesPerSecond) {
5
+ super();
6
+ this.bytesPerSecond = bytesPerSecond;
7
+ this.passed = 0;
8
+ this.startTime = Date.now();
9
+ }
10
+
11
+ _transform(chunk, encoding, callback) {
12
+ this.passed += chunk.length;
13
+ const elapsed = Date.now() - this.startTime;
14
+ const expectedTime = (this.passed / this.bytesPerSecond) * 1000;
15
+
16
+ if (expectedTime > elapsed) {
17
+ setTimeout(() => {
18
+ this.push(chunk);
19
+ callback();
20
+ }, expectedTime - elapsed);
21
+ } else {
22
+ this.push(chunk);
23
+ callback();
24
+ }
25
+ }
26
+ }