trzsz2 1.0.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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.cn.md +64 -0
  3. package/README.md +70 -0
  4. package/dist/browser/buffer.d.ts +76 -0
  5. package/dist/browser/comm.d.ts +162 -0
  6. package/dist/browser/escape.d.ts +33 -0
  7. package/dist/browser/index.d.ts +17 -0
  8. package/dist/browser/options.d.ts +57 -0
  9. package/dist/browser/progress.d.ts +100 -0
  10. package/dist/browser/transfer.d.ts +259 -0
  11. package/dist/browser/trzsz2.js +6147 -0
  12. package/dist/browser/trzsz2.js.map +1 -0
  13. package/dist/browser/trzsz2.min.js +19 -0
  14. package/dist/buffer.d.ts +76 -0
  15. package/dist/cjs/buffer.cjs +252 -0
  16. package/dist/cjs/buffer.cjs.map +1 -0
  17. package/dist/cjs/buffer.d.ts +76 -0
  18. package/dist/cjs/comm.cjs +292 -0
  19. package/dist/cjs/comm.cjs.map +1 -0
  20. package/dist/cjs/comm.d.ts +162 -0
  21. package/dist/cjs/escape.cjs +86 -0
  22. package/dist/cjs/escape.cjs.map +1 -0
  23. package/dist/cjs/escape.d.ts +33 -0
  24. package/dist/cjs/index.cjs +30 -0
  25. package/dist/cjs/index.cjs.map +1 -0
  26. package/dist/cjs/index.d.ts +17 -0
  27. package/dist/cjs/node_modules/pako/dist/pako.esm.cjs +4177 -0
  28. package/dist/cjs/node_modules/pako/dist/pako.esm.cjs.map +1 -0
  29. package/dist/cjs/node_modules/ts-md5/dist/esm/md5.cjs +353 -0
  30. package/dist/cjs/node_modules/ts-md5/dist/esm/md5.cjs.map +1 -0
  31. package/dist/cjs/options.d.ts +57 -0
  32. package/dist/cjs/package.json +3 -0
  33. package/dist/cjs/progress.cjs +313 -0
  34. package/dist/cjs/progress.cjs.map +1 -0
  35. package/dist/cjs/progress.d.ts +100 -0
  36. package/dist/cjs/transfer.cjs +706 -0
  37. package/dist/cjs/transfer.cjs.map +1 -0
  38. package/dist/cjs/transfer.d.ts +259 -0
  39. package/dist/cjs-full/buffer.d.ts +76 -0
  40. package/dist/cjs-full/comm.d.ts +162 -0
  41. package/dist/cjs-full/escape.d.ts +33 -0
  42. package/dist/cjs-full/index.cjs +6144 -0
  43. package/dist/cjs-full/index.cjs.map +1 -0
  44. package/dist/cjs-full/index.d.ts +17 -0
  45. package/dist/cjs-full/options.d.ts +57 -0
  46. package/dist/cjs-full/package.json +3 -0
  47. package/dist/cjs-full/progress.d.ts +100 -0
  48. package/dist/cjs-full/transfer.d.ts +259 -0
  49. package/dist/comm.d.ts +162 -0
  50. package/dist/escape.d.ts +33 -0
  51. package/dist/esm/buffer.d.ts +76 -0
  52. package/dist/esm/buffer.js +252 -0
  53. package/dist/esm/buffer.js.map +1 -0
  54. package/dist/esm/comm.d.ts +162 -0
  55. package/dist/esm/comm.js +292 -0
  56. package/dist/esm/comm.js.map +1 -0
  57. package/dist/esm/escape.d.ts +33 -0
  58. package/dist/esm/escape.js +86 -0
  59. package/dist/esm/escape.js.map +1 -0
  60. package/dist/esm/index.d.ts +17 -0
  61. package/dist/esm/index.js +30 -0
  62. package/dist/esm/index.js.map +1 -0
  63. package/dist/esm/node_modules/pako/dist/pako.esm.js +4177 -0
  64. package/dist/esm/node_modules/pako/dist/pako.esm.js.map +1 -0
  65. package/dist/esm/node_modules/ts-md5/dist/esm/md5.js +353 -0
  66. package/dist/esm/node_modules/ts-md5/dist/esm/md5.js.map +1 -0
  67. package/dist/esm/options.d.ts +57 -0
  68. package/dist/esm/package.json +3 -0
  69. package/dist/esm/progress.d.ts +100 -0
  70. package/dist/esm/progress.js +313 -0
  71. package/dist/esm/progress.js.map +1 -0
  72. package/dist/esm/transfer.d.ts +259 -0
  73. package/dist/esm/transfer.js +706 -0
  74. package/dist/esm/transfer.js.map +1 -0
  75. package/dist/index.d.ts +17 -0
  76. package/dist/options.d.ts +57 -0
  77. package/dist/progress.d.ts +100 -0
  78. package/dist/transfer.d.ts +259 -0
  79. package/package.json +108 -0
@@ -0,0 +1,313 @@
1
+ /**
2
+ * trzsz2: https://github.com/zxdong262/trzsz2
3
+ * Copyright(c) 2024 Lonny Wong
4
+ * @license MIT
5
+ *
6
+ * Pure protocol implementation without fs/browser dependencies.
7
+ */
8
+ function getLength(str) {
9
+ return str.replace(/[\u4e00-\u9fa5]/g, "**").length;
10
+ }
11
+ function getEllipsisString(str, max) {
12
+ max -= 3;
13
+ let len = 0;
14
+ let sub = "";
15
+ for (let i = 0; i < str.length; i++) {
16
+ if (str.charCodeAt(i) >= 19968 && str.charCodeAt(i) <= 40869) {
17
+ if (len + 2 > max) {
18
+ return { sub: sub + "...", len: len + 3 };
19
+ }
20
+ len += 2;
21
+ } else {
22
+ if (len + 1 > max) {
23
+ return { sub: sub + "...", len: len + 3 };
24
+ }
25
+ len += 1;
26
+ }
27
+ sub += str[i];
28
+ }
29
+ return { sub: sub + "...", len: len + 3 };
30
+ }
31
+ function convertSizeToString(size) {
32
+ let unit = "B";
33
+ do {
34
+ if (size < 1024) {
35
+ break;
36
+ }
37
+ size = size / 1024;
38
+ unit = "KB";
39
+ if (size < 1024) {
40
+ break;
41
+ }
42
+ size = size / 1024;
43
+ unit = "MB";
44
+ if (size < 1024) {
45
+ break;
46
+ }
47
+ size = size / 1024;
48
+ unit = "GB";
49
+ if (size < 1024) {
50
+ break;
51
+ }
52
+ size = size / 1024;
53
+ unit = "TB";
54
+ } while (false);
55
+ if (size >= 100) {
56
+ return `${size.toFixed(0)} ${unit}`;
57
+ } else if (size >= 10) {
58
+ return `${size.toFixed(1)} ${unit}`;
59
+ } else {
60
+ return `${size.toFixed(2)} ${unit}`;
61
+ }
62
+ }
63
+ function convertTimeToString(seconds) {
64
+ let result = "";
65
+ if (seconds >= 3600) {
66
+ result += Math.floor(seconds / 3600).toString() + ":";
67
+ seconds %= 3600;
68
+ }
69
+ const minute = Math.floor(seconds / 60);
70
+ result += minute >= 10 ? minute.toString() : "0" + minute.toString();
71
+ result += ":";
72
+ const second = Math.round(seconds % 60);
73
+ result += second >= 10 ? second.toString() : "0" + second.toString();
74
+ return result;
75
+ }
76
+ const kSpeedArraySize = 30;
77
+ class TextProgressBar {
78
+ /**
79
+ * Create a TextProgressBar.
80
+ * @param writer - The output writer function.
81
+ * @param columns - The terminal columns.
82
+ * @param tmuxPaneColumns - The tmux pane columns (optional).
83
+ */
84
+ constructor(writer, columns, tmuxPaneColumns = void 0) {
85
+ this.lastUpdateTime = 0;
86
+ this.fileCount = 0;
87
+ this.fileIdx = 0;
88
+ this.fileName = "";
89
+ this.fileSize = 0;
90
+ this.fileStep = 0;
91
+ this.startTime = 0;
92
+ this.firstWrite = true;
93
+ this.speedCnt = 0;
94
+ this.speedIdx = 0;
95
+ this.timeArray = new Array(kSpeedArraySize);
96
+ this.stepArray = new Array(kSpeedArraySize);
97
+ this.writer = writer;
98
+ this.tmuxPaneColumns = tmuxPaneColumns ?? 0;
99
+ this.columns = this.tmuxPaneColumns > 1 ? this.tmuxPaneColumns - 1 : columns;
100
+ }
101
+ /**
102
+ * Set the terminal columns.
103
+ * @param columns - The terminal columns.
104
+ */
105
+ setTerminalColumns(columns) {
106
+ this.columns = columns;
107
+ if (this.tmuxPaneColumns > 0) {
108
+ this.tmuxPaneColumns = 0;
109
+ }
110
+ }
111
+ /**
112
+ * Called when the number of files is known.
113
+ * @param num - The number of files.
114
+ */
115
+ onNum(num) {
116
+ this.fileCount = num;
117
+ this.fileIdx = 0;
118
+ }
119
+ /**
120
+ * Called when a file name is known.
121
+ * @param name - The file name.
122
+ */
123
+ onName(name) {
124
+ this.fileName = name;
125
+ this.fileIdx += 1;
126
+ this.startTime = Date.now();
127
+ this.timeArray[0] = this.startTime;
128
+ this.stepArray[0] = 0;
129
+ this.speedCnt = 1;
130
+ this.speedIdx = 1;
131
+ this.fileStep = -1;
132
+ }
133
+ /**
134
+ * Called when the file size is known.
135
+ * @param size - The file size.
136
+ */
137
+ onSize(size) {
138
+ this.fileSize = size;
139
+ }
140
+ /**
141
+ * Called when a step is completed.
142
+ * @param step - The step number.
143
+ */
144
+ onStep(step) {
145
+ if (step <= this.fileStep) {
146
+ return;
147
+ }
148
+ this.fileStep = step;
149
+ this.showProgress();
150
+ }
151
+ /**
152
+ * Hide the cursor.
153
+ */
154
+ hideCursor() {
155
+ this.writer("\x1B[?25l");
156
+ }
157
+ /**
158
+ * Show the cursor.
159
+ */
160
+ showCursor() {
161
+ this.writer("\x1B[?25h");
162
+ }
163
+ /**
164
+ * Show the progress.
165
+ */
166
+ showProgress() {
167
+ const now = Date.now();
168
+ if (now - this.lastUpdateTime < 200) {
169
+ return;
170
+ }
171
+ this.lastUpdateTime = now;
172
+ let percentage = "100%";
173
+ if (this.fileSize !== 0) {
174
+ percentage = Math.round(this.fileStep * 100 / this.fileSize).toString() + "%";
175
+ }
176
+ const total = convertSizeToString(this.fileStep);
177
+ const speed = this.getSpeed(now);
178
+ let speedStr = "--- B/s";
179
+ let etaStr = "--- ETA";
180
+ if (speed > 0) {
181
+ speedStr = convertSizeToString(speed) + "/s";
182
+ etaStr = convertTimeToString(Math.round((this.fileSize - this.fileStep) / speed)) + " ETA";
183
+ }
184
+ const progressText = this.getProgressText(percentage, total, speedStr, etaStr);
185
+ if (this.firstWrite) {
186
+ this.firstWrite = false;
187
+ this.writer(progressText);
188
+ return;
189
+ }
190
+ if (this.tmuxPaneColumns > 0) {
191
+ this.writer(`\x1B[${this.columns}D${progressText}`);
192
+ } else {
193
+ this.writer(`\r${progressText}`);
194
+ }
195
+ }
196
+ /**
197
+ * Get the current transfer speed.
198
+ * @param now - The current time.
199
+ * @return The speed in bytes per second.
200
+ */
201
+ getSpeed(now) {
202
+ let speed;
203
+ if (this.speedCnt <= kSpeedArraySize) {
204
+ this.speedCnt++;
205
+ speed = (this.fileStep - this.stepArray[0]) * 1e3 / (now - this.timeArray[0]);
206
+ } else {
207
+ speed = (this.fileStep - this.stepArray[this.speedIdx]) * 1e3 / (now - this.timeArray[this.speedIdx]);
208
+ }
209
+ this.timeArray[this.speedIdx] = now;
210
+ this.stepArray[this.speedIdx] = this.fileStep;
211
+ this.speedIdx++;
212
+ if (this.speedIdx >= kSpeedArraySize) {
213
+ this.speedIdx %= kSpeedArraySize;
214
+ }
215
+ return isFinite(speed) ? speed : -1;
216
+ }
217
+ /**
218
+ * Get the progress text.
219
+ * @param percentage - The percentage string.
220
+ * @param total - The total string.
221
+ * @param speed - The speed string.
222
+ * @param eta - The ETA string.
223
+ * @return The progress text.
224
+ */
225
+ getProgressText(percentage, total, speed, eta) {
226
+ const barMinLength = 24;
227
+ let left = this.fileCount > 1 ? `(${this.fileIdx}/${this.fileCount}) ${this.fileName}` : this.fileName;
228
+ let leftLength = getLength(left);
229
+ let right = ` ${percentage} | ${total} | ${speed} | ${eta}`;
230
+ do {
231
+ if (this.columns - leftLength - right.length >= barMinLength) {
232
+ break;
233
+ }
234
+ if (leftLength > 50) {
235
+ ({ sub: left, len: leftLength } = getEllipsisString(left, 50));
236
+ }
237
+ if (this.columns - leftLength - right.length >= barMinLength) {
238
+ break;
239
+ }
240
+ if (leftLength > 40) {
241
+ ({ sub: left, len: leftLength } = getEllipsisString(left, 40));
242
+ }
243
+ if (this.columns - leftLength - right.length >= barMinLength) {
244
+ break;
245
+ }
246
+ right = ` ${percentage} | ${speed} | ${eta}`;
247
+ if (this.columns - leftLength - right.length >= barMinLength) {
248
+ break;
249
+ }
250
+ if (leftLength > 30) {
251
+ ({ sub: left, len: leftLength } = getEllipsisString(left, 30));
252
+ }
253
+ if (this.columns - leftLength - right.length >= barMinLength) {
254
+ break;
255
+ }
256
+ right = ` ${percentage} | ${eta}`;
257
+ if (this.columns - leftLength - right.length >= barMinLength) {
258
+ break;
259
+ }
260
+ right = ` ${percentage}`;
261
+ if (this.columns - leftLength - right.length >= barMinLength) {
262
+ break;
263
+ }
264
+ if (leftLength > 20) {
265
+ ({ sub: left, len: leftLength } = getEllipsisString(left, 20));
266
+ }
267
+ if (this.columns - leftLength - right.length >= barMinLength) {
268
+ break;
269
+ }
270
+ left = "";
271
+ leftLength = 0;
272
+ } while (false);
273
+ let barLength = this.columns - right.length;
274
+ if (leftLength > 0) {
275
+ barLength -= leftLength + 1;
276
+ left += " ";
277
+ }
278
+ const bar = this.getProgressBar(barLength);
279
+ return (left + bar + right).trim();
280
+ }
281
+ /**
282
+ * Get the progress bar string.
283
+ * @param len - The length of the bar.
284
+ * @return The progress bar string.
285
+ */
286
+ getProgressBar(len) {
287
+ if (len < 12) {
288
+ return "";
289
+ }
290
+ const total = len - 2;
291
+ let complete = total;
292
+ if (this.fileSize !== 0) {
293
+ complete = Math.round(total * this.fileStep / this.fileSize);
294
+ }
295
+ return "[\x1B[36m" + "█".repeat(complete) + "░".repeat(total - complete) + "\x1B[0m]";
296
+ }
297
+ /**
298
+ * Called when the transfer is done.
299
+ */
300
+ onDone() {
301
+ if (this.fileSize === 0) {
302
+ return;
303
+ }
304
+ this.fileStep = this.fileSize;
305
+ this.lastUpdateTime = 0;
306
+ this.showProgress();
307
+ }
308
+ }
309
+ export {
310
+ TextProgressBar,
311
+ getEllipsisString
312
+ };
313
+ //# sourceMappingURL=progress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"progress.js","sources":["../../src/progress.ts"],"sourcesContent":["/**\n * trzsz2: https://github.com/zxdong262/trzsz2\n * Copyright(c) 2024 Lonny Wong\n * @license MIT\n *\n * Pure protocol implementation without fs/browser dependencies.\n */\n\nimport type { ProgressCallback } from './comm'\n\n/**\n * Get the display length of a string (Chinese characters count as 2).\n * @param str - The input string.\n * @return The display length.\n */\nfunction getLength (str: string): number {\n return str.replace(/[\\u4e00-\\u9fa5]/g, '**').length\n}\n\n/**\n * Get ellipsis string if the string is too long.\n * @param str - The input string.\n * @param max - The maximum length.\n * @return The ellipsis string and its length.\n */\nexport function getEllipsisString (str: string, max: number): { sub: string, len: number } {\n max -= 3\n let len = 0\n let sub = ''\n for (let i = 0; i < str.length; i++) {\n if (str.charCodeAt(i) >= 0x4e00 && str.charCodeAt(i) <= 0x9fa5) {\n if (len + 2 > max) {\n return { sub: sub + '...', len: len + 3 }\n }\n len += 2\n } else {\n if (len + 1 > max) {\n return { sub: sub + '...', len: len + 3 }\n }\n len += 1\n }\n sub += str[i]\n }\n return { sub: sub + '...', len: len + 3 }\n}\n\n/**\n * Convert size to human-readable string.\n * @param size - The size in bytes.\n * @return The human-readable string.\n */\nfunction convertSizeToString (size: number): string {\n let unit = 'B'\n do {\n if (size < 1024) {\n break\n }\n size = size / 1024\n unit = 'KB'\n\n if (size < 1024) {\n break\n }\n size = size / 1024\n unit = 'MB'\n\n if (size < 1024) {\n break\n }\n size = size / 1024\n unit = 'GB'\n\n if (size < 1024) {\n break\n }\n size = size / 1024\n unit = 'TB'\n } while (false)\n\n if (size >= 100) {\n return `${size.toFixed(0)} ${unit}`\n } else if (size >= 10) {\n return `${size.toFixed(1)} ${unit}`\n } else {\n return `${size.toFixed(2)} ${unit}`\n }\n}\n\n/**\n * Convert time to human-readable string.\n * @param seconds - The time in seconds.\n * @return The human-readable string.\n */\nfunction convertTimeToString (seconds: number): string {\n let result = ''\n if (seconds >= 3600) {\n result += Math.floor(seconds / 3600).toString() + ':'\n seconds %= 3600\n }\n\n const minute = Math.floor(seconds / 60)\n result += minute >= 10 ? minute.toString() : '0' + minute.toString()\n result += ':'\n\n const second = Math.round(seconds % 60)\n result += second >= 10 ? second.toString() : '0' + second.toString()\n\n return result\n}\n\nconst kSpeedArraySize: number = 30\n\n/**\n * TextProgressBar class for displaying file transfer progress.\n */\nexport class TextProgressBar implements ProgressCallback {\n private readonly writer: (output: string) => void\n private lastUpdateTime: number = 0\n private columns: number\n private fileCount: number = 0\n private fileIdx: number = 0\n private fileName: string = ''\n private fileSize: number = 0\n private fileStep: number = 0\n private startTime: number = 0\n private tmuxPaneColumns: number\n private firstWrite: boolean = true\n private speedCnt: number = 0\n private speedIdx: number = 0\n private timeArray: number[] = new Array(kSpeedArraySize)\n private stepArray: number[] = new Array(kSpeedArraySize)\n\n /**\n * Create a TextProgressBar.\n * @param writer - The output writer function.\n * @param columns - The terminal columns.\n * @param tmuxPaneColumns - The tmux pane columns (optional).\n */\n public constructor (\n writer: (output: string) => void,\n columns: number,\n tmuxPaneColumns: number | undefined = undefined\n ) {\n this.writer = writer\n this.tmuxPaneColumns = tmuxPaneColumns ?? 0\n // -1 to avoid xterm.js messing up the tmux pane\n this.columns = this.tmuxPaneColumns > 1 ? this.tmuxPaneColumns - 1 : columns\n }\n\n /**\n * Set the terminal columns.\n * @param columns - The terminal columns.\n */\n public setTerminalColumns (columns: number): void {\n this.columns = columns\n // resizing tmux panes is not supported\n if (this.tmuxPaneColumns > 0) {\n this.tmuxPaneColumns = 0\n }\n }\n\n /**\n * Called when the number of files is known.\n * @param num - The number of files.\n */\n public onNum (num: number): void {\n this.fileCount = num\n this.fileIdx = 0\n }\n\n /**\n * Called when a file name is known.\n * @param name - The file name.\n */\n public onName (name: string): void {\n this.fileName = name\n this.fileIdx += 1\n this.startTime = Date.now()\n this.timeArray[0] = this.startTime\n this.stepArray[0] = 0\n this.speedCnt = 1\n this.speedIdx = 1\n this.fileStep = -1\n }\n\n /**\n * Called when the file size is known.\n * @param size - The file size.\n */\n public onSize (size: number): void {\n this.fileSize = size\n }\n\n /**\n * Called when a step is completed.\n * @param step - The step number.\n */\n public onStep (step: number): void {\n if (step <= this.fileStep) {\n return\n }\n this.fileStep = step\n this.showProgress()\n }\n\n /**\n * Hide the cursor.\n */\n public hideCursor (): void {\n this.writer('\\x1b[?25l')\n }\n\n /**\n * Show the cursor.\n */\n public showCursor (): void {\n this.writer('\\x1b[?25h')\n }\n\n /**\n * Show the progress.\n */\n private showProgress (): void {\n const now = Date.now()\n if (now - this.lastUpdateTime < 200) {\n return\n }\n this.lastUpdateTime = now\n\n let percentage = '100%'\n if (this.fileSize !== 0) {\n percentage = Math.round((this.fileStep * 100) / this.fileSize).toString() + '%'\n }\n const total = convertSizeToString(this.fileStep)\n const speed = this.getSpeed(now)\n let speedStr = '--- B/s'\n let etaStr = '--- ETA'\n if (speed > 0) {\n speedStr = convertSizeToString(speed) + '/s'\n etaStr = convertTimeToString(Math.round((this.fileSize - this.fileStep) / speed)) + ' ETA'\n }\n\n const progressText = this.getProgressText(percentage, total, speedStr, etaStr)\n\n if (this.firstWrite) {\n this.firstWrite = false\n this.writer(progressText)\n return\n }\n\n if (this.tmuxPaneColumns > 0) {\n this.writer(`\\x1b[${this.columns}D${progressText}`)\n } else {\n this.writer(`\\r${progressText}`)\n }\n }\n\n /**\n * Get the current transfer speed.\n * @param now - The current time.\n * @return The speed in bytes per second.\n */\n private getSpeed (now: number): number {\n let speed: number\n if (this.speedCnt <= kSpeedArraySize) {\n this.speedCnt++\n speed = ((this.fileStep - this.stepArray[0]) * 1000) / (now - this.timeArray[0])\n } else {\n speed =\n ((this.fileStep - this.stepArray[this.speedIdx]) * 1000) /\n (now - this.timeArray[this.speedIdx])\n }\n\n this.timeArray[this.speedIdx] = now\n this.stepArray[this.speedIdx] = this.fileStep\n\n this.speedIdx++\n if (this.speedIdx >= kSpeedArraySize) {\n this.speedIdx %= kSpeedArraySize\n }\n\n return isFinite(speed) ? speed : -1\n }\n\n /**\n * Get the progress text.\n * @param percentage - The percentage string.\n * @param total - The total string.\n * @param speed - The speed string.\n * @param eta - The ETA string.\n * @return The progress text.\n */\n private getProgressText (\n percentage: string,\n total: string,\n speed: string,\n eta: string\n ): string {\n const barMinLength = 24\n let left =\n this.fileCount > 1 ? `(${this.fileIdx}/${this.fileCount}) ${this.fileName}` : this.fileName\n let leftLength = getLength(left)\n let right = ` ${percentage} | ${total} | ${speed} | ${eta}`\n\n do {\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n if (leftLength > 50) {\n ;({ sub: left, len: leftLength } = getEllipsisString(left, 50))\n }\n\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n if (leftLength > 40) {\n ;({ sub: left, len: leftLength } = getEllipsisString(left, 40))\n }\n\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n right = ` ${percentage} | ${speed} | ${eta}`\n\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n if (leftLength > 30) {\n ;({ sub: left, len: leftLength } = getEllipsisString(left, 30))\n }\n\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n right = ` ${percentage} | ${eta}`\n\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n right = ` ${percentage}`\n\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n if (leftLength > 20) {\n ;({ sub: left, len: leftLength } = getEllipsisString(left, 20))\n }\n\n if (this.columns - leftLength - right.length >= barMinLength) {\n break\n }\n left = ''\n leftLength = 0\n } while (false)\n\n let barLength = this.columns - right.length\n if (leftLength > 0) {\n barLength -= leftLength + 1\n left += ' '\n }\n\n const bar = this.getProgressBar(barLength)\n return (left + bar + right).trim()\n }\n\n /**\n * Get the progress bar string.\n * @param len - The length of the bar.\n * @return The progress bar string.\n */\n private getProgressBar (len: number): string {\n if (len < 12) {\n return ''\n }\n const total = len - 2\n let complete = total\n if (this.fileSize !== 0) {\n complete = Math.round((total * this.fileStep) / this.fileSize)\n }\n return '[\\u001b[36m' + '\\u2588'.repeat(complete) + '\\u2591'.repeat(total - complete) + '\\u001b[0m]'\n }\n\n /**\n * Called when the transfer is done.\n */\n public onDone (): void {\n if (this.fileSize === 0) {\n return\n }\n this.fileStep = this.fileSize\n this.lastUpdateTime = 0\n this.showProgress()\n }\n}\n"],"names":[],"mappings":"AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA,SAAS,UAAW,KAAqB;AACvC,SAAO,IAAI,QAAQ,oBAAoB,IAAI,EAAE;AAC/C;AAQO,SAAS,kBAAmB,KAAa,KAA2C;AACzF,SAAO;AACP,MAAI,MAAM;AACV,MAAI,MAAM;AACV,WAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,QAAI,IAAI,WAAW,CAAC,KAAK,SAAU,IAA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;"}
@@ -0,0 +1,259 @@
1
+ import { OpenSaveFile, TrzszFileReader, ProgressCallback } from './comm';
2
+ /**
3
+ * TrzszTransfer class for handling file transfer protocol.
4
+ */
5
+ export declare class TrzszTransfer {
6
+ private readonly buffer;
7
+ private readonly writer;
8
+ private readonly isWindowsShell;
9
+ private remoteIsWindows;
10
+ private lastInputTime;
11
+ private readonly openedFiles;
12
+ private readonly createdFiles;
13
+ private tmuxOutputJunk;
14
+ private cleanTimeoutInMilliseconds;
15
+ private transferConfig;
16
+ private stopped;
17
+ private maxChunkTimeInMilliseconds;
18
+ private protocolNewline;
19
+ /**
20
+ * Create a TrzszTransfer.
21
+ * @param writer - The output writer function.
22
+ * @param isWindowsShell - Whether the shell is Windows.
23
+ */
24
+ constructor(writer: (data: string | Uint8Array) => void, isWindowsShell?: boolean);
25
+ /**
26
+ * Cleanup resources.
27
+ */
28
+ cleanup(): void;
29
+ /**
30
+ * Add received data to the buffer.
31
+ * @param data - The received data.
32
+ */
33
+ addReceivedData(data: string | ArrayBuffer | Uint8Array | Blob): void;
34
+ /**
35
+ * Stop transferring.
36
+ */
37
+ stopTransferring(): Promise<void>;
38
+ /**
39
+ * Clean input buffer.
40
+ * @param timeoutInMilliseconds - The timeout in milliseconds.
41
+ */
42
+ private cleanInput;
43
+ /**
44
+ * Send a line.
45
+ * @param typ - The type.
46
+ * @param buf - The buffer.
47
+ */
48
+ private sendLine;
49
+ /**
50
+ * Receive a line.
51
+ * @param expectType - The expected type.
52
+ * @param mayHasJunk - Whether there may be junk.
53
+ */
54
+ private recvLine;
55
+ /**
56
+ * Receive and check a line.
57
+ * @param expectType - The expected type.
58
+ * @param mayHasJunk - Whether there may be junk.
59
+ */
60
+ private recvCheck;
61
+ /**
62
+ * Send an integer.
63
+ * @param typ - The type.
64
+ * @param val - The value.
65
+ */
66
+ private sendInteger;
67
+ /**
68
+ * Receive an integer.
69
+ * @param typ - The type.
70
+ * @param mayHasJunk - Whether there may be junk.
71
+ */
72
+ private recvInteger;
73
+ /**
74
+ * Check an integer.
75
+ * @param expect - The expected value.
76
+ */
77
+ private checkInteger;
78
+ /**
79
+ * Send a string.
80
+ * @param typ - The type.
81
+ * @param str - The string.
82
+ */
83
+ private sendString;
84
+ /**
85
+ * Receive a string.
86
+ * @param typ - The type.
87
+ * @param mayHasJunk - Whether there may be junk.
88
+ */
89
+ private recvString;
90
+ /**
91
+ * Check a string.
92
+ * @param expect - The expected string.
93
+ */
94
+ protected checkString(expect: string): Promise<void>;
95
+ /**
96
+ * Send binary data.
97
+ * @param typ - The type.
98
+ * @param buf - The buffer.
99
+ */
100
+ private sendBinary;
101
+ /**
102
+ * Receive binary data.
103
+ * @param typ - The type.
104
+ * @param mayHasJunk - Whether there may be junk.
105
+ */
106
+ private recvBinary;
107
+ /**
108
+ * Check binary data.
109
+ * @param expect - The expected buffer.
110
+ */
111
+ private checkBinary;
112
+ /**
113
+ * Send data.
114
+ * @param data - The data.
115
+ * @param binary - Whether to use binary mode.
116
+ * @param escapeCodes - The escape codes.
117
+ */
118
+ private sendData;
119
+ /**
120
+ * Receive data.
121
+ * @param binary - Whether to use binary mode.
122
+ * @param escapeCodes - The escape codes.
123
+ * @param timeoutInMilliseconds - The timeout in milliseconds.
124
+ */
125
+ private recvData;
126
+ /**
127
+ * Send action.
128
+ * @param confirm - Whether to confirm.
129
+ * @param remoteIsWindows - Whether the remote is Windows.
130
+ */
131
+ sendAction(confirm: boolean, remoteIsWindows: boolean): Promise<void>;
132
+ /**
133
+ * Receive action.
134
+ */
135
+ recvAction(): Promise<Record<string, unknown>>;
136
+ /**
137
+ * Send config.
138
+ * @param args - The config arguments.
139
+ * @param escapeChars - The escape characters.
140
+ * @param tmuxMode - The tmux mode.
141
+ * @param tmuxPaneWidth - The tmux pane width.
142
+ */
143
+ sendConfig(args: Record<string, unknown>, escapeChars: string[][], tmuxMode: number, tmuxPaneWidth: number): Promise<void>;
144
+ /**
145
+ * Receive config.
146
+ */
147
+ recvConfig(): Promise<Record<string, unknown>>;
148
+ /**
149
+ * Send client exit.
150
+ * @param msg - The message.
151
+ */
152
+ clientExit(msg: string): Promise<void>;
153
+ /**
154
+ * Receive exit.
155
+ */
156
+ recvExit(): Promise<string>;
157
+ /**
158
+ * Delete created files.
159
+ */
160
+ private deleteCreatedFiles;
161
+ /**
162
+ * Handle client error.
163
+ * @param err - The error.
164
+ */
165
+ clientError(err: Error): Promise<void>;
166
+ /**
167
+ * Handle server error.
168
+ * @param err - The error.
169
+ */
170
+ serverError(err: Error): Promise<void>;
171
+ /**
172
+ * Server exit - to be implemented by subclass or user.
173
+ * @param msg - The message.
174
+ */
175
+ serverExit(_msg: string): Promise<void>;
176
+ /**
177
+ * Send file number.
178
+ * @param num - The number of files.
179
+ * @param progressCallback - The progress callback.
180
+ */
181
+ private sendFileNum;
182
+ /**
183
+ * Send file name.
184
+ * @param file - The file reader.
185
+ * @param directory - Whether it's a directory.
186
+ * @param progressCallback - The progress callback.
187
+ */
188
+ private sendFileName;
189
+ /**
190
+ * Send file size.
191
+ * @param size - The file size.
192
+ * @param progressCallback - The progress callback.
193
+ */
194
+ private sendFileSize;
195
+ /**
196
+ * Send file data.
197
+ * @param file - The file reader.
198
+ * @param size - The file size.
199
+ * @param binary - Whether to use binary mode.
200
+ * @param escapeCodes - The escape codes.
201
+ * @param maxBufSize - The maximum buffer size.
202
+ * @param progressCallback - The progress callback.
203
+ */
204
+ private sendFileData;
205
+ /**
206
+ * Send file MD5.
207
+ * @param digest - The MD5 digest.
208
+ * @param progressCallback - The progress callback.
209
+ */
210
+ private sendFileMD5;
211
+ /**
212
+ * Send files.
213
+ * @param files - The file readers.
214
+ * @param progressCallback - The progress callback.
215
+ */
216
+ sendFiles(files: TrzszFileReader[], progressCallback: ProgressCallback | null): Promise<string[]>;
217
+ /**
218
+ * Receive file number.
219
+ * @param progressCallback - The progress callback.
220
+ */
221
+ private recvFileNum;
222
+ /**
223
+ * Receive file name.
224
+ * @param saveParam - The save parameter.
225
+ * @param openSaveFile - The open save file function.
226
+ * @param directory - Whether it's a directory.
227
+ * @param overwrite - Whether to overwrite.
228
+ * @param progressCallback - The progress callback.
229
+ */
230
+ private recvFileName;
231
+ /**
232
+ * Receive file size.
233
+ * @param progressCallback - The progress callback.
234
+ */
235
+ private recvFileSize;
236
+ /**
237
+ * Receive file data.
238
+ * @param file - The file writer.
239
+ * @param size - The file size.
240
+ * @param binary - Whether to use binary mode.
241
+ * @param escapeCodes - The escape codes.
242
+ * @param timeoutInMilliseconds - The timeout in milliseconds.
243
+ * @param progressCallback - The progress callback.
244
+ */
245
+ private recvFileData;
246
+ /**
247
+ * Receive file MD5.
248
+ * @param digest - The MD5 digest.
249
+ * @param progressCallback - The progress callback.
250
+ */
251
+ private recvFileMD5;
252
+ /**
253
+ * Receive files.
254
+ * @param saveParam - The save parameter.
255
+ * @param openSaveFile - The open save file function.
256
+ * @param progressCallback - The progress callback.
257
+ */
258
+ recvFiles(saveParam: unknown, openSaveFile: OpenSaveFile, progressCallback: ProgressCallback | null): Promise<string[]>;
259
+ }