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.
- package/LICENSE +21 -0
- package/README.cn.md +64 -0
- package/README.md +70 -0
- package/dist/browser/buffer.d.ts +76 -0
- package/dist/browser/comm.d.ts +162 -0
- package/dist/browser/escape.d.ts +33 -0
- package/dist/browser/index.d.ts +17 -0
- package/dist/browser/options.d.ts +57 -0
- package/dist/browser/progress.d.ts +100 -0
- package/dist/browser/transfer.d.ts +259 -0
- package/dist/browser/trzsz2.js +6147 -0
- package/dist/browser/trzsz2.js.map +1 -0
- package/dist/browser/trzsz2.min.js +19 -0
- package/dist/buffer.d.ts +76 -0
- package/dist/cjs/buffer.cjs +252 -0
- package/dist/cjs/buffer.cjs.map +1 -0
- package/dist/cjs/buffer.d.ts +76 -0
- package/dist/cjs/comm.cjs +292 -0
- package/dist/cjs/comm.cjs.map +1 -0
- package/dist/cjs/comm.d.ts +162 -0
- package/dist/cjs/escape.cjs +86 -0
- package/dist/cjs/escape.cjs.map +1 -0
- package/dist/cjs/escape.d.ts +33 -0
- package/dist/cjs/index.cjs +30 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +17 -0
- package/dist/cjs/node_modules/pako/dist/pako.esm.cjs +4177 -0
- package/dist/cjs/node_modules/pako/dist/pako.esm.cjs.map +1 -0
- package/dist/cjs/node_modules/ts-md5/dist/esm/md5.cjs +353 -0
- package/dist/cjs/node_modules/ts-md5/dist/esm/md5.cjs.map +1 -0
- package/dist/cjs/options.d.ts +57 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/progress.cjs +313 -0
- package/dist/cjs/progress.cjs.map +1 -0
- package/dist/cjs/progress.d.ts +100 -0
- package/dist/cjs/transfer.cjs +706 -0
- package/dist/cjs/transfer.cjs.map +1 -0
- package/dist/cjs/transfer.d.ts +259 -0
- package/dist/cjs-full/buffer.d.ts +76 -0
- package/dist/cjs-full/comm.d.ts +162 -0
- package/dist/cjs-full/escape.d.ts +33 -0
- package/dist/cjs-full/index.cjs +6144 -0
- package/dist/cjs-full/index.cjs.map +1 -0
- package/dist/cjs-full/index.d.ts +17 -0
- package/dist/cjs-full/options.d.ts +57 -0
- package/dist/cjs-full/package.json +3 -0
- package/dist/cjs-full/progress.d.ts +100 -0
- package/dist/cjs-full/transfer.d.ts +259 -0
- package/dist/comm.d.ts +162 -0
- package/dist/escape.d.ts +33 -0
- package/dist/esm/buffer.d.ts +76 -0
- package/dist/esm/buffer.js +252 -0
- package/dist/esm/buffer.js.map +1 -0
- package/dist/esm/comm.d.ts +162 -0
- package/dist/esm/comm.js +292 -0
- package/dist/esm/comm.js.map +1 -0
- package/dist/esm/escape.d.ts +33 -0
- package/dist/esm/escape.js +86 -0
- package/dist/esm/escape.js.map +1 -0
- package/dist/esm/index.d.ts +17 -0
- package/dist/esm/index.js +30 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/node_modules/pako/dist/pako.esm.js +4177 -0
- package/dist/esm/node_modules/pako/dist/pako.esm.js.map +1 -0
- package/dist/esm/node_modules/ts-md5/dist/esm/md5.js +353 -0
- package/dist/esm/node_modules/ts-md5/dist/esm/md5.js.map +1 -0
- package/dist/esm/options.d.ts +57 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/progress.d.ts +100 -0
- package/dist/esm/progress.js +313 -0
- package/dist/esm/progress.js.map +1 -0
- package/dist/esm/transfer.d.ts +259 -0
- package/dist/esm/transfer.js +706 -0
- package/dist/esm/transfer.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/options.d.ts +57 -0
- package/dist/progress.d.ts +100 -0
- package/dist/transfer.d.ts +259 -0
- package/package.json +108 -0
package/dist/esm/comm.js
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import pako from "./node_modules/pako/dist/pako.esm.js";
|
|
2
|
+
const trzszVersion = "2.0.0";
|
|
3
|
+
function strToUint8(str) {
|
|
4
|
+
return Uint8Array.from(str, (v) => v.charCodeAt(0));
|
|
5
|
+
}
|
|
6
|
+
async function uint8ToStr(buf, encoding = "binary") {
|
|
7
|
+
if (typeof Buffer === "function") {
|
|
8
|
+
return Buffer.from(buf).toString(encoding);
|
|
9
|
+
}
|
|
10
|
+
return await new Promise((resolve) => {
|
|
11
|
+
const reader = new FileReader();
|
|
12
|
+
reader.onloadend = () => resolve(reader.result);
|
|
13
|
+
const arrayBuffer = new ArrayBuffer(buf.byteLength);
|
|
14
|
+
new Uint8Array(arrayBuffer).set(buf);
|
|
15
|
+
const blob = new Blob([arrayBuffer]);
|
|
16
|
+
if (encoding === "binary") {
|
|
17
|
+
reader.readAsBinaryString(blob);
|
|
18
|
+
} else {
|
|
19
|
+
reader.readAsText(blob, encoding);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function strToArrBuf(str) {
|
|
24
|
+
const arr = strToUint8(str);
|
|
25
|
+
const buffer = new ArrayBuffer(arr.byteLength);
|
|
26
|
+
new Uint8Array(buffer).set(arr);
|
|
27
|
+
return buffer;
|
|
28
|
+
}
|
|
29
|
+
const _hasBuffer = typeof Buffer === "function";
|
|
30
|
+
let _nodeZlib;
|
|
31
|
+
function getNodeZlib() {
|
|
32
|
+
if (_nodeZlib === void 0) {
|
|
33
|
+
try {
|
|
34
|
+
_nodeZlib = require("zlib");
|
|
35
|
+
} catch {
|
|
36
|
+
_nodeZlib = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return _nodeZlib ?? null;
|
|
40
|
+
}
|
|
41
|
+
function uint8ToBase64(buffer) {
|
|
42
|
+
if (_hasBuffer) {
|
|
43
|
+
return Buffer.from(buffer).toString("base64");
|
|
44
|
+
}
|
|
45
|
+
let binary = "";
|
|
46
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
47
|
+
binary += String.fromCharCode(buffer[i]);
|
|
48
|
+
}
|
|
49
|
+
return btoa(binary);
|
|
50
|
+
}
|
|
51
|
+
function base64ToUint8(base64) {
|
|
52
|
+
if (_hasBuffer) {
|
|
53
|
+
return Buffer.from(base64, "base64");
|
|
54
|
+
}
|
|
55
|
+
const binary = atob(base64);
|
|
56
|
+
const buffer = new Uint8Array(binary.length);
|
|
57
|
+
for (let i = 0; i < binary.length; i++) {
|
|
58
|
+
buffer[i] = binary.charCodeAt(i);
|
|
59
|
+
}
|
|
60
|
+
return buffer;
|
|
61
|
+
}
|
|
62
|
+
function deflate(data) {
|
|
63
|
+
const input = typeof data === "string" ? strToUint8(data) : data;
|
|
64
|
+
const zlib = getNodeZlib();
|
|
65
|
+
if (zlib != null) {
|
|
66
|
+
const result = zlib.deflateSync(input);
|
|
67
|
+
return new Uint8Array(result);
|
|
68
|
+
}
|
|
69
|
+
return pako.deflate(input);
|
|
70
|
+
}
|
|
71
|
+
function inflate(data) {
|
|
72
|
+
const zlib = getNodeZlib();
|
|
73
|
+
if (zlib != null) {
|
|
74
|
+
const result = zlib.inflateSync(data);
|
|
75
|
+
return new Uint8Array(result);
|
|
76
|
+
}
|
|
77
|
+
return pako.inflate(data);
|
|
78
|
+
}
|
|
79
|
+
function encodeBuffer(buf) {
|
|
80
|
+
const buffer = deflate(buf);
|
|
81
|
+
return uint8ToBase64(buffer);
|
|
82
|
+
}
|
|
83
|
+
function decodeBuffer(buf) {
|
|
84
|
+
const buffer = base64ToUint8(buf);
|
|
85
|
+
return inflate(buffer);
|
|
86
|
+
}
|
|
87
|
+
class TrzszError extends Error {
|
|
88
|
+
constructor(message, type = null, trace = false) {
|
|
89
|
+
if (type === "fail" || type === "FAIL" || type === "EXIT") {
|
|
90
|
+
try {
|
|
91
|
+
message = new TextDecoder().decode(decodeBuffer(message));
|
|
92
|
+
} catch {
|
|
93
|
+
message = `decode [${message}] error`;
|
|
94
|
+
}
|
|
95
|
+
} else if (type !== null) {
|
|
96
|
+
message = `[TrzszError] ${type}: ${message}`;
|
|
97
|
+
}
|
|
98
|
+
super(message);
|
|
99
|
+
Object.setPrototypeOf(this, TrzszError.prototype);
|
|
100
|
+
if (typeof Error.captureStackTrace === "function") {
|
|
101
|
+
Error.captureStackTrace(this, TrzszError);
|
|
102
|
+
}
|
|
103
|
+
this.name = "TrzszError";
|
|
104
|
+
this.type = type;
|
|
105
|
+
this.trace = trace;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Check if the error should include traceback.
|
|
109
|
+
* @return {boolean} True if traceback should be included.
|
|
110
|
+
*/
|
|
111
|
+
isTraceBack() {
|
|
112
|
+
if (this.type === "fail" || this.type === "EXIT") {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return this.trace;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Check if the error is a remote exit.
|
|
119
|
+
* @return {boolean} True if remote exit.
|
|
120
|
+
*/
|
|
121
|
+
isRemoteExit() {
|
|
122
|
+
return this.type === "EXIT";
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Check if the error is a remote failure.
|
|
126
|
+
* @return {boolean} True if remote failure.
|
|
127
|
+
*/
|
|
128
|
+
isRemoteFail() {
|
|
129
|
+
return this.type === "fail" || this.type === "FAIL";
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Check if the error indicates stop and delete.
|
|
133
|
+
* @return {boolean} True if stop and delete.
|
|
134
|
+
*/
|
|
135
|
+
isStopAndDelete() {
|
|
136
|
+
if (this.type !== "fail") {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
return this.message === "Stopped and deleted";
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get the error message.
|
|
143
|
+
* @param {Error} err - The error object.
|
|
144
|
+
* @return {string} The error message.
|
|
145
|
+
*/
|
|
146
|
+
static getErrorMessage(err) {
|
|
147
|
+
if (err instanceof TrzszError && !err.isTraceBack()) {
|
|
148
|
+
return err.message;
|
|
149
|
+
}
|
|
150
|
+
if (typeof err.stack === "string" && err.stack.length > 0) {
|
|
151
|
+
return err.stack.replace("TrzszError: ", "");
|
|
152
|
+
}
|
|
153
|
+
return err.toString();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function checkDuplicateNames(files) {
|
|
157
|
+
const names = /* @__PURE__ */ new Set();
|
|
158
|
+
for (const file of files) {
|
|
159
|
+
const path = file.getRelPath().join("/");
|
|
160
|
+
if (names.has(path)) {
|
|
161
|
+
throw new TrzszError(`Duplicate name: ${path}`);
|
|
162
|
+
}
|
|
163
|
+
names.add(path);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function isArrayOfType(arr, type) {
|
|
167
|
+
if (!Array.isArray(arr)) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
for (const a of arr) {
|
|
171
|
+
switch (type) {
|
|
172
|
+
case "string":
|
|
173
|
+
if (typeof a !== "string") return false;
|
|
174
|
+
break;
|
|
175
|
+
case "number":
|
|
176
|
+
if (typeof a !== "number") return false;
|
|
177
|
+
break;
|
|
178
|
+
case "boolean":
|
|
179
|
+
if (typeof a !== "boolean") return false;
|
|
180
|
+
break;
|
|
181
|
+
case "object":
|
|
182
|
+
if (typeof a !== "object") return false;
|
|
183
|
+
break;
|
|
184
|
+
case "function":
|
|
185
|
+
if (typeof a !== "function") return false;
|
|
186
|
+
break;
|
|
187
|
+
case "undefined":
|
|
188
|
+
if (typeof a !== "undefined") return false;
|
|
189
|
+
break;
|
|
190
|
+
default:
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
function isVT100End(c) {
|
|
197
|
+
if (c >= 97 && c <= 122) {
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
if (c >= 65 && c <= 90) {
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
function stripServerOutput(output) {
|
|
206
|
+
let uint8;
|
|
207
|
+
if (typeof output === "string") {
|
|
208
|
+
uint8 = strToUint8(output);
|
|
209
|
+
} else if (output instanceof ArrayBuffer) {
|
|
210
|
+
uint8 = new Uint8Array(output);
|
|
211
|
+
} else if (output instanceof Uint8Array) {
|
|
212
|
+
uint8 = output;
|
|
213
|
+
} else {
|
|
214
|
+
return output;
|
|
215
|
+
}
|
|
216
|
+
const buf = new Uint8Array(uint8.length);
|
|
217
|
+
let skipVT100 = false;
|
|
218
|
+
let idx = 0;
|
|
219
|
+
for (let i = 0; i < uint8.length; i++) {
|
|
220
|
+
const c = uint8[i];
|
|
221
|
+
if (skipVT100) {
|
|
222
|
+
if (isVT100End(c)) {
|
|
223
|
+
skipVT100 = false;
|
|
224
|
+
}
|
|
225
|
+
} else if (c === 27) {
|
|
226
|
+
skipVT100 = true;
|
|
227
|
+
} else {
|
|
228
|
+
buf[idx++] = c;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
while (idx > 0) {
|
|
232
|
+
const c = buf[idx - 1];
|
|
233
|
+
if (c !== 13 && c !== 10) {
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
idx--;
|
|
237
|
+
}
|
|
238
|
+
const result = buf.subarray(0, idx);
|
|
239
|
+
if (result.length > 100) {
|
|
240
|
+
return output;
|
|
241
|
+
}
|
|
242
|
+
return String.fromCharCode.apply(null, Array.from(result));
|
|
243
|
+
}
|
|
244
|
+
const TmuxMode = {
|
|
245
|
+
NoTmux: 0,
|
|
246
|
+
TmuxNormalMode: 1,
|
|
247
|
+
TmuxControlMode: 2
|
|
248
|
+
};
|
|
249
|
+
function formatSavedFiles(fileNames, destPath) {
|
|
250
|
+
let msg = `Saved ${fileNames.length} ${fileNames.length > 1 ? "files/directories" : "file/directory"}`;
|
|
251
|
+
if (destPath.length > 0) {
|
|
252
|
+
msg += ` to ${destPath}`;
|
|
253
|
+
}
|
|
254
|
+
return [msg].concat(fileNames).join("\r\n- ");
|
|
255
|
+
}
|
|
256
|
+
function stripTmuxStatusLine(buf) {
|
|
257
|
+
while (true) {
|
|
258
|
+
const beginIdx = buf.indexOf("\x1BP=");
|
|
259
|
+
if (beginIdx < 0) {
|
|
260
|
+
return buf;
|
|
261
|
+
}
|
|
262
|
+
let bufIdx = beginIdx + 3;
|
|
263
|
+
const midIdx = buf.substring(bufIdx).indexOf("\x1BP=");
|
|
264
|
+
if (midIdx < 0) {
|
|
265
|
+
return buf.substring(0, beginIdx);
|
|
266
|
+
}
|
|
267
|
+
bufIdx += midIdx + 3;
|
|
268
|
+
const endIdx = buf.substring(bufIdx).indexOf("\x1B\\");
|
|
269
|
+
if (endIdx < 0) {
|
|
270
|
+
return buf.substring(0, beginIdx);
|
|
271
|
+
}
|
|
272
|
+
bufIdx += endIdx + 2;
|
|
273
|
+
buf = buf.substring(0, beginIdx) + buf.substring(bufIdx);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
export {
|
|
277
|
+
TmuxMode,
|
|
278
|
+
TrzszError,
|
|
279
|
+
checkDuplicateNames,
|
|
280
|
+
decodeBuffer,
|
|
281
|
+
encodeBuffer,
|
|
282
|
+
formatSavedFiles,
|
|
283
|
+
isArrayOfType,
|
|
284
|
+
isVT100End,
|
|
285
|
+
strToArrBuf,
|
|
286
|
+
strToUint8,
|
|
287
|
+
stripServerOutput,
|
|
288
|
+
stripTmuxStatusLine,
|
|
289
|
+
trzszVersion,
|
|
290
|
+
uint8ToStr
|
|
291
|
+
};
|
|
292
|
+
//# sourceMappingURL=comm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comm.js","sources":["../../src/comm.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 Pako from 'pako'\n\n/**\n * trzsz version\n */\nexport const trzszVersion = '2.0.0'\n\n/**\n * Convert a string to Uint8Array.\n * @param {string} str - The input string.\n * @return {Uint8Array} The resulting Uint8Array.\n */\nexport function strToUint8 (str: string): Uint8Array {\n return Uint8Array.from(str, (v) => v.charCodeAt(0))\n}\n\n/**\n * Convert a Uint8Array to string.\n * @param {Uint8Array} buf - The input buffer.\n * @param {string} encoding - The encoding to use.\n * @return {Promise<string>} The resulting string.\n */\nexport async function uint8ToStr (buf: Uint8Array, encoding: string = 'binary'): Promise<string> {\n if (typeof Buffer === 'function') {\n return Buffer.from(buf).toString(encoding as BufferEncoding)\n }\n return await new Promise<string>((resolve) => {\n const reader = new FileReader()\n reader.onloadend = () => resolve(reader.result as string)\n // Create a new ArrayBuffer copy to avoid SharedArrayBuffer issues\n const arrayBuffer = new ArrayBuffer(buf.byteLength)\n new Uint8Array(arrayBuffer).set(buf)\n const blob = new Blob([arrayBuffer])\n if (encoding === 'binary') {\n reader.readAsBinaryString(blob)\n } else {\n reader.readAsText(blob, encoding)\n }\n })\n}\n\n/**\n * Convert a string to ArrayBuffer.\n * @param {string} str - The input string.\n * @return {ArrayBuffer} The resulting ArrayBuffer.\n */\nexport function strToArrBuf (str: string): ArrayBuffer {\n const arr = strToUint8(str)\n // Create a new ArrayBuffer copy to avoid SharedArrayBuffer issues\n const buffer = new ArrayBuffer(arr.byteLength)\n new Uint8Array(buffer).set(arr)\n return buffer\n}\n\nconst _hasBuffer = typeof Buffer === 'function'\n\n// Lazy load Node.js zlib module\nlet _nodeZlib: typeof import('zlib') | null | undefined\n\n/**\n * Get Node.js zlib module if available.\n * @return {typeof import('zlib') | null} The zlib module or null if not available.\n */\nfunction getNodeZlib (): typeof import('zlib') | null {\n if (_nodeZlib === undefined) {\n try {\n // Use dynamic require for Node.js environment\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n _nodeZlib = require('zlib')\n } catch {\n _nodeZlib = null\n }\n }\n return _nodeZlib ?? null\n}\n\n/**\n * Encode Uint8Array to base64 string using native functions.\n * @param {Uint8Array} buffer - The input buffer.\n * @return {string} The base64 encoded string.\n */\nfunction uint8ToBase64 (buffer: Uint8Array): string {\n if (_hasBuffer) {\n return Buffer.from(buffer).toString('base64')\n }\n // Browser: use btoa with binary string\n let binary = ''\n for (let i = 0; i < buffer.length; i++) {\n binary += String.fromCharCode(buffer[i])\n }\n return btoa(binary)\n}\n\n/**\n * Decode base64 string to Uint8Array using native functions.\n * @param {string} base64 - The base64 encoded string.\n * @return {Uint8Array} The decoded buffer.\n */\nfunction base64ToUint8 (base64: string): Uint8Array {\n if (_hasBuffer) {\n return Buffer.from(base64, 'base64')\n }\n // Browser: use atob and convert to Uint8Array\n const binary = atob(base64)\n const buffer = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i++) {\n buffer[i] = binary.charCodeAt(i)\n }\n return buffer\n}\n\n/**\n * Compress data using deflate algorithm.\n * Uses Node.js native zlib in Node.js, pako in browser.\n * @param {string | Uint8Array} data - The data to compress.\n * @return {Uint8Array} The compressed data.\n */\nfunction deflate (data: string | Uint8Array): Uint8Array {\n const input = typeof data === 'string' ? strToUint8(data) : data\n\n // Try Node.js native zlib first\n const zlib = getNodeZlib()\n if (zlib != null) {\n // Use deflateSync (zlib format) to match pako's default behavior\n const result = zlib.deflateSync(input)\n // Convert Buffer to Uint8Array for consistency\n return new Uint8Array(result)\n }\n\n // Fall back to pako for browser\n return Pako.deflate(input)\n}\n\n/**\n * Decompress data using inflate algorithm.\n * Uses Node.js native zlib in Node.js, pako in browser.\n * @param {Uint8Array} data - The compressed data.\n * @return {Uint8Array} The decompressed data.\n */\nfunction inflate (data: Uint8Array): Uint8Array {\n // Try Node.js native zlib first\n const zlib = getNodeZlib()\n if (zlib != null) {\n // Use inflateSync (zlib format) to match pako's default behavior\n const result = zlib.inflateSync(data)\n // Convert Buffer to Uint8Array for consistency\n return new Uint8Array(result)\n }\n\n // Fall back to pako for browser\n return Pako.inflate(data)\n}\n\n/**\n * Encode a buffer (compress and base64).\n * @param {string | Uint8Array} buf - The input buffer.\n * @return {string} The encoded string.\n */\nexport function encodeBuffer (buf: string | Uint8Array): string {\n const buffer = deflate(buf)\n return uint8ToBase64(buffer)\n}\n\n/**\n * Decode a buffer (base64 and decompress).\n * @param {string} buf - The encoded string.\n * @return {Uint8Array} The decoded buffer.\n */\nexport function decodeBuffer (buf: string): Uint8Array {\n const buffer = base64ToUint8(buf)\n return inflate(buffer)\n}\n\n/**\n * Custom error class for trzsz operations.\n */\nexport class TrzszError extends Error {\n private readonly type: string | null\n private readonly trace: boolean\n\n constructor (message: string, type: string | null = null, trace: boolean = false) {\n if (type === 'fail' || type === 'FAIL' || type === 'EXIT') {\n try {\n message = new TextDecoder().decode(decodeBuffer(message))\n } catch {\n message = `decode [${message}] error`\n }\n } else if (type !== null) {\n message = `[TrzszError] ${type}: ${message}`\n }\n\n super(message)\n Object.setPrototypeOf(this, TrzszError.prototype)\n if (typeof Error.captureStackTrace === 'function') {\n Error.captureStackTrace(this, TrzszError)\n }\n\n this.name = 'TrzszError'\n this.type = type\n this.trace = trace\n }\n\n /**\n * Check if the error should include traceback.\n * @return {boolean} True if traceback should be included.\n */\n public isTraceBack (): boolean {\n if (this.type === 'fail' || this.type === 'EXIT') {\n return false\n }\n return this.trace\n }\n\n /**\n * Check if the error is a remote exit.\n * @return {boolean} True if remote exit.\n */\n public isRemoteExit (): boolean {\n return this.type === 'EXIT'\n }\n\n /**\n * Check if the error is a remote failure.\n * @return {boolean} True if remote failure.\n */\n public isRemoteFail (): boolean {\n return this.type === 'fail' || this.type === 'FAIL'\n }\n\n /**\n * Check if the error indicates stop and delete.\n * @return {boolean} True if stop and delete.\n */\n public isStopAndDelete (): boolean {\n if (this.type !== 'fail') {\n return false\n }\n return this.message === 'Stopped and deleted'\n }\n\n /**\n * Get the error message.\n * @param {Error} err - The error object.\n * @return {string} The error message.\n */\n public static getErrorMessage (err: Error): string {\n if (err instanceof TrzszError && !err.isTraceBack()) {\n return err.message\n }\n if (typeof err.stack === 'string' && err.stack.length > 0) {\n return err.stack.replace('TrzszError: ', '')\n }\n return err.toString()\n }\n}\n\n/**\n * Interface for trzsz file operations.\n */\nexport interface TrzszFile {\n closeFile: () => void\n}\n\n/**\n * Interface for trzsz file reader.\n */\nexport interface TrzszFileReader extends TrzszFile {\n getPathId: () => number\n getRelPath: () => string[]\n isDir: () => boolean\n getSize: () => number\n readFile: (buf: ArrayBuffer) => Promise<Uint8Array>\n}\n\n/**\n * Interface for trzsz file writer.\n */\nexport interface TrzszFileWriter extends TrzszFile {\n getFileName: () => string\n getLocalName: () => string\n isDir: () => boolean\n writeFile: (buf: Uint8Array) => Promise<void>\n deleteFile: () => Promise<string>\n}\n\n/**\n * Type for opening save file.\n */\nexport type OpenSaveFile = (\n saveParam: any,\n fileName: string,\n directory: boolean,\n overwrite: boolean,\n) => Promise<TrzszFileWriter>\n\n/**\n * Interface for progress callback.\n */\nexport interface ProgressCallback {\n onNum: (num: number) => void\n onName: (name: string) => void\n onSize: (size: number) => void\n onStep: (step: number) => void\n onDone: () => void\n}\n\n/**\n * Check for duplicate file names.\n * @param {TrzszFileReader[]} files - The files to check.\n */\nexport function checkDuplicateNames (files: TrzszFileReader[]): void {\n const names = new Set<string>()\n for (const file of files) {\n const path = file.getRelPath().join('/')\n if (names.has(path)) {\n throw new TrzszError(`Duplicate name: ${path}`)\n }\n names.add(path)\n }\n}\n\n/**\n * Check if an array contains only elements of a specific type.\n * @param {any} arr - The array to check.\n * @param {string} type - The type to check for.\n * @return {boolean} True if all elements are of the specified type.\n */\nexport function isArrayOfType (arr: any, type: string): boolean {\n if (!Array.isArray(arr)) {\n return false\n }\n for (const a of arr) {\n switch (type) {\n case 'string':\n if (typeof a !== 'string') return false\n break\n case 'number':\n if (typeof a !== 'number') return false\n break\n case 'boolean':\n if (typeof a !== 'boolean') return false\n break\n case 'object':\n if (typeof a !== 'object') return false\n break\n case 'function':\n if (typeof a !== 'function') return false\n break\n case 'undefined':\n if (typeof a !== 'undefined') return false\n break\n default:\n return false\n }\n }\n return true\n}\n\n/**\n * Check if a character is a VT100 end character.\n * @param {number} c - The character code.\n * @return {boolean} True if it's a VT100 end character.\n */\nexport function isVT100End (c: number): boolean {\n if (c >= 0x61 && c <= 0x7a) {\n // 'a' <= c && c <= 'z'\n return true\n }\n if (c >= 0x41 && c <= 0x5a) {\n // 'A' <= c && c <= 'Z'\n return true\n }\n return false\n}\n\n/**\n * Strip VT100 escape sequences from server output.\n * @param {string | ArrayBuffer | Uint8Array | Blob} output - The server output.\n * @return {string | ArrayBuffer | Uint8Array | Blob} The stripped output.\n */\nexport function stripServerOutput (output: string | ArrayBuffer | Uint8Array | Blob): string | ArrayBuffer | Uint8Array | Blob {\n let uint8: Uint8Array\n if (typeof output === 'string') {\n uint8 = strToUint8(output)\n } else if (output instanceof ArrayBuffer) {\n uint8 = new Uint8Array(output)\n } else if (output instanceof Uint8Array) {\n uint8 = output\n } else {\n return output\n }\n const buf = new Uint8Array(uint8.length)\n let skipVT100 = false\n let idx = 0\n for (let i = 0; i < uint8.length; i++) {\n const c = uint8[i]\n if (skipVT100) {\n if (isVT100End(c)) {\n skipVT100 = false\n }\n } else if (c === 0x1b) {\n skipVT100 = true\n } else {\n buf[idx++] = c\n }\n }\n while (idx > 0) {\n const c = buf[idx - 1]\n if (c !== 0x0d && c !== 0x0a) {\n // not \\r\\n\n break\n }\n idx--\n }\n const result = buf.subarray(0, idx)\n if (result.length > 100) {\n return output\n }\n return String.fromCharCode.apply(null, Array.from(result))\n}\n\n/**\n * Tmux mode constants.\n */\nexport const TmuxMode = {\n NoTmux: 0,\n TmuxNormalMode: 1,\n TmuxControlMode: 2\n} as const\n\nexport type TmuxModeType = (typeof TmuxMode)[keyof typeof TmuxMode]\n\n/**\n * Format saved files message.\n * @param {string[]} fileNames - The file names.\n * @param {string} destPath - The destination path.\n * @return {string} The formatted message.\n */\nexport function formatSavedFiles (fileNames: string[], destPath: string): string {\n let msg = `Saved ${fileNames.length} ${fileNames.length > 1 ? 'files/directories' : 'file/directory'}`\n if (destPath.length > 0) {\n msg += ` to ${destPath}`\n }\n return [msg].concat(fileNames).join('\\r\\n- ')\n}\n\n/**\n * Strip tmux status line from buffer.\n * @param {string} buf - The input buffer.\n * @return {string} The stripped buffer.\n */\nexport function stripTmuxStatusLine (buf: string): string {\n while (true) {\n const beginIdx = buf.indexOf('\\x1bP=')\n if (beginIdx < 0) {\n return buf\n }\n let bufIdx = beginIdx + 3\n const midIdx = buf.substring(bufIdx).indexOf('\\x1bP=')\n if (midIdx < 0) {\n return buf.substring(0, beginIdx)\n }\n bufIdx += midIdx + 3\n const endIdx = buf.substring(bufIdx).indexOf('\\x1b\\\\')\n if (endIdx < 0) {\n return buf.substring(0, beginIdx)\n }\n bufIdx += endIdx + 2\n buf = buf.substring(0, beginIdx) + buf.substring(bufIdx)\n }\n}\n"],"names":["Pako"],"mappings":";AAaO,MAAM,eAAe;AAOrB,SAAS,WAAY,KAAyB;AACnD,SAAO,WAAW,KAAK,KAAK,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AACpD;AAQA,eAAsB,WAAY,KAAiB,WAAmB,UAA2B;AAC/F,MAAI,OAAO,WAAW,YAAY;AAChC,WAAO,OAAO,KAAK,GAAG,EAAE,SAAS,QAA0B;AAAA,EAC7D;AACA,SAAO,MAAM,IAAI,QAAgB,CAAC,YAAY;AAC5C,UAAM,SAAS,IAAI,WAAA;AACnB,WAAO,YAAY,MAAM,QAAQ,OAAO,MAAgB;AAExD,UAAM,cAAc,IAAI,YAAY,IAAI,UAAU;AAClD,QAAI,WAAW,WAAW,EAAE,IAAI,GAAG;AACnC,UAAM,OAAO,IAAI,KAAK,CAAC,WAAW,CAAC;AACnC,QAAI,aAAa,UAAU;AACzB,aAAO,mBAAmB,IAAI;AAAA,IAChC,OAAO;AACL,aAAO,WAAW,MAAM,QAAQ;AAAA,IAClC;AAAA,EACF,CAAC;AACH;AAOO,SAAS,YAAa,KAA0B;AACrD,QAAM,MAAM,WAAW,GAAG;AAE1B,QAAM,SAAS,IAAI,YAAY,IAAI,UAAU;AAC7C,MAAI,WAAW,MAAM,EAAE,IAAI,GAAG;AAC9B,SAAO;AACT;AAEA,MAAM,aAAa,OAAO,W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T;AACA,QAAI,SAAS,WAAW;AACxB,UAAM,SAAS,IAAI,UAAU,MAAM,EAAE,QAAQ,QAAQ;AACrD,QAAI,SAAS,GAAG;AACd,aAAO,IAAI,UAAU,GAAG,QAAQ;AAAA,IAClC;AACA,cAAU,SAAS;AACnB,UAAM,SAAS,IAAI,UAAU,MAAM,EAAE,QAAQ,QAAQ;AACrD,QAAI,SAAS,GAAG;AACd,aAAO,IAAI,UAAU,GAAG,QAAQ;AAAA,IAClC;AACA,cAAU,SAAS;AACnB,UAAM,IAAI,UAAU,GAAG,QAAQ,IAAI,IAAI,UAAU,MAAM;AAAA,EACzD;AACF;"}
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Get escape characters for trzsz protocol.
|
|
10
|
+
* @param escapeAll - Whether to escape all special characters.
|
|
11
|
+
* @return Array of escape character pairs.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getEscapeChars(escapeAll: boolean): string[][];
|
|
14
|
+
/**
|
|
15
|
+
* Convert escape characters to codes.
|
|
16
|
+
* @param escapeChars - Array of escape character pairs.
|
|
17
|
+
* @return Array of escape code triplets.
|
|
18
|
+
*/
|
|
19
|
+
export declare function escapeCharsToCodes(escapeChars: string[][]): number[][];
|
|
20
|
+
/**
|
|
21
|
+
* Escape data using escape codes.
|
|
22
|
+
* @param data - The data to escape.
|
|
23
|
+
* @param escapeCodes - Array of escape code triplets.
|
|
24
|
+
* @return The escaped data.
|
|
25
|
+
*/
|
|
26
|
+
export declare function escapeData(data: Uint8Array, escapeCodes: number[][]): Uint8Array;
|
|
27
|
+
/**
|
|
28
|
+
* Unescape data using escape codes.
|
|
29
|
+
* @param data - The data to unescape.
|
|
30
|
+
* @param escapeCodes - Array of escape code triplets.
|
|
31
|
+
* @return The unescaped data.
|
|
32
|
+
*/
|
|
33
|
+
export declare function unescapeData(data: Uint8Array, escapeCodes: number[][]): Uint8Array;
|
|
@@ -0,0 +1,86 @@
|
|
|
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 getEscapeChars(escapeAll) {
|
|
9
|
+
const escapeChars = [
|
|
10
|
+
["î", "îî"],
|
|
11
|
+
["~", "î1"]
|
|
12
|
+
];
|
|
13
|
+
if (escapeAll) {
|
|
14
|
+
const chars = "\r\x1B";
|
|
15
|
+
for (let i = 0; i < chars.length; i++) {
|
|
16
|
+
escapeChars.push([chars[i], "î" + String.fromCharCode(65 + i)]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return escapeChars;
|
|
20
|
+
}
|
|
21
|
+
function escapeCharsToCodes(escapeChars) {
|
|
22
|
+
const escapeCodes = [];
|
|
23
|
+
for (let i = 0; i < escapeChars.length; i++) {
|
|
24
|
+
escapeCodes.push([
|
|
25
|
+
escapeChars[i][0].charCodeAt(0),
|
|
26
|
+
escapeChars[i][1].charCodeAt(0),
|
|
27
|
+
escapeChars[i][1].charCodeAt(1)
|
|
28
|
+
]);
|
|
29
|
+
}
|
|
30
|
+
return escapeCodes;
|
|
31
|
+
}
|
|
32
|
+
function escapeData(data, escapeCodes) {
|
|
33
|
+
if (escapeCodes.length === 0) {
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
const buf = new Uint8Array(data.length * 2);
|
|
37
|
+
let idx = 0;
|
|
38
|
+
for (let i = 0; i < data.length; i++) {
|
|
39
|
+
let escapeIdx = -1;
|
|
40
|
+
for (let j = 0; j < escapeCodes.length; j++) {
|
|
41
|
+
if (data[i] === escapeCodes[j][0]) {
|
|
42
|
+
escapeIdx = j;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (escapeIdx < 0) {
|
|
47
|
+
buf[idx++] = data[i];
|
|
48
|
+
} else {
|
|
49
|
+
buf[idx++] = escapeCodes[escapeIdx][1];
|
|
50
|
+
buf[idx++] = escapeCodes[escapeIdx][2];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return buf.subarray(0, idx);
|
|
54
|
+
}
|
|
55
|
+
function unescapeData(data, escapeCodes) {
|
|
56
|
+
if (escapeCodes.length === 0) {
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
const buf = new Uint8Array(data.length);
|
|
60
|
+
let idx = 0;
|
|
61
|
+
for (let i = 0; i < data.length; i++) {
|
|
62
|
+
let escapeIdx = -1;
|
|
63
|
+
if (i < data.length - 1) {
|
|
64
|
+
for (let j = 0; j < escapeCodes.length; j++) {
|
|
65
|
+
if (data[i] === escapeCodes[j][1] && data[i + 1] === escapeCodes[j][2]) {
|
|
66
|
+
escapeIdx = j;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (escapeIdx < 0) {
|
|
72
|
+
buf[idx++] = data[i];
|
|
73
|
+
} else {
|
|
74
|
+
buf[idx++] = escapeCodes[escapeIdx][0];
|
|
75
|
+
i++;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return buf.subarray(0, idx);
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
escapeCharsToCodes,
|
|
82
|
+
escapeData,
|
|
83
|
+
getEscapeChars,
|
|
84
|
+
unescapeData
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=escape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"escape.js","sources":["../../src/escape.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\n/**\n * Get escape characters for trzsz protocol.\n * @param escapeAll - Whether to escape all special characters.\n * @return Array of escape character pairs.\n */\nexport function getEscapeChars (escapeAll: boolean): string[][] {\n const escapeChars: string[][] = [\n ['\\xee', '\\xee\\xee'],\n ['\\x7e', '\\xee\\x31']\n ]\n if (escapeAll) {\n const chars = '\\x02\\x0d\\x10\\x11\\x13\\x18\\x1b\\x1d\\x8d\\x90\\x91\\x93\\x9d'\n for (let i = 0; i < chars.length; i++) {\n escapeChars.push([chars[i], '\\xee' + String.fromCharCode(0x41 + i)])\n }\n }\n return escapeChars\n}\n\n/**\n * Convert escape characters to codes.\n * @param escapeChars - Array of escape character pairs.\n * @return Array of escape code triplets.\n */\nexport function escapeCharsToCodes (escapeChars: string[][]): number[][] {\n const escapeCodes: number[][] = []\n for (let i = 0; i < escapeChars.length; i++) {\n escapeCodes.push([\n escapeChars[i][0].charCodeAt(0),\n escapeChars[i][1].charCodeAt(0),\n escapeChars[i][1].charCodeAt(1)\n ])\n }\n return escapeCodes\n}\n\n/**\n * Escape data using escape codes.\n * @param data - The data to escape.\n * @param escapeCodes - Array of escape code triplets.\n * @return The escaped data.\n */\nexport function escapeData (data: Uint8Array, escapeCodes: number[][]): Uint8Array {\n if (escapeCodes.length === 0) {\n return data\n }\n\n const buf = new Uint8Array(data.length * 2)\n\n let idx = 0\n for (let i = 0; i < data.length; i++) {\n let escapeIdx = -1\n for (let j = 0; j < escapeCodes.length; j++) {\n if (data[i] === escapeCodes[j][0]) {\n escapeIdx = j\n break\n }\n }\n if (escapeIdx < 0) {\n buf[idx++] = data[i]\n } else {\n buf[idx++] = escapeCodes[escapeIdx][1]\n buf[idx++] = escapeCodes[escapeIdx][2]\n }\n }\n\n return buf.subarray(0, idx)\n}\n\n/**\n * Unescape data using escape codes.\n * @param data - The data to unescape.\n * @param escapeCodes - Array of escape code triplets.\n * @return The unescaped data.\n */\nexport function unescapeData (data: Uint8Array, escapeCodes: number[][]): Uint8Array {\n if (escapeCodes.length === 0) {\n return data\n }\n\n const buf = new Uint8Array(data.length)\n\n let idx = 0\n for (let i = 0; i < data.length; i++) {\n let escapeIdx = -1\n if (i < data.length - 1) {\n for (let j = 0; j < escapeCodes.length; j++) {\n if (data[i] === escapeCodes[j][1] && data[i + 1] === escapeCodes[j][2]) {\n escapeIdx = j\n break\n }\n }\n }\n if (escapeIdx < 0) {\n buf[idx++] = data[i]\n } else {\n buf[idx++] = escapeCodes[escapeIdx][0]\n i++\n }\n }\n\n return buf.subarray(0, idx)\n}\n"],"names":[],"mappings":"AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaO,SAAS,eAAgB,WAAgC;AAC9D,QAAM,cAA0B;AAAA,IAC9B,CAAC,KAAQ,IAAU;AAAA,IACnB,CAAC,KAAQ,IAAU;AAAA,EAAA;AAErB,MAAI,WAAW;AACb,UAAM,QAAQ;AACd,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,kBAAY,KAAK,CAAC,MAAM,CAAC,GAAG,MAAS,OAAO,aAAa,KAAO,CAAC,CAAC,CAAC;AAAA,IACrE;AAAA,EACF;AACA,SAAO;AACT;AAOO,SAAS,mBAAoB,aAAqC;AACvE,QAAM,cAA0B,CAAA;AAChC,WAAS,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAK;AAC3C,gBAAY,KAAK;AAAA,MACf,YAAY,CAAC,EAAE,CAAC,EAAE,WAAW,CAAC;AAAA,MAC9B,YAAY,CAAC,EAAE,CAAC,EAAE,WAAW,CAAC;AAAA,MAC9B,YAAY,CAAC,EAAE,CAAC,EAAE,WAAW,CAAC;AAAA,IAAA,CAC/B;AAAA,EACH;AACA,SAAO;AACT;AAQO,SAAS,WAAY,MAAkB,aAAqC;AACjF,MAAI,YAAY,WAAW,GAAG;AAC5B,WAAO;AAAA,EACT;AAEA,QAAM,MAAM,IAAI,WAAW,KAAK,SAAS,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
|
3
|
+
* Copyright(c) 2024 Lonny Wong
|
|
4
|
+
* @license MIT
|
|
5
|
+
*
|
|
6
|
+
* Pure protocol implementation without fs/browser dependencies.
|
|
7
|
+
*/
|
|
8
|
+
export { trzszVersion } from './comm';
|
|
9
|
+
export type { TrzszFile, TrzszFileReader, TrzszFileWriter, OpenSaveFile, ProgressCallback, TmuxModeType } from './comm';
|
|
10
|
+
export type { TrzszOptions } from './options';
|
|
11
|
+
export { TrzszError } from './comm';
|
|
12
|
+
export { TrzszBuffer } from './buffer';
|
|
13
|
+
export { TrzszTransfer } from './transfer';
|
|
14
|
+
export { TextProgressBar } from './progress';
|
|
15
|
+
export { strToUint8, uint8ToStr, strToArrBuf, encodeBuffer, decodeBuffer, checkDuplicateNames, isArrayOfType, isVT100End, stripServerOutput, formatSavedFiles, stripTmuxStatusLine, TmuxMode } from './comm';
|
|
16
|
+
export { getEscapeChars, escapeCharsToCodes, escapeData, unescapeData } from './escape';
|
|
17
|
+
export { getEllipsisString } from './progress';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { TmuxMode, TrzszError, checkDuplicateNames, decodeBuffer, encodeBuffer, formatSavedFiles, isArrayOfType, isVT100End, strToArrBuf, strToUint8, stripServerOutput, stripTmuxStatusLine, trzszVersion, uint8ToStr } from "./comm.js";
|
|
2
|
+
import { TrzszBuffer } from "./buffer.js";
|
|
3
|
+
import { TrzszTransfer } from "./transfer.js";
|
|
4
|
+
import { TextProgressBar, getEllipsisString } from "./progress.js";
|
|
5
|
+
import { escapeCharsToCodes, escapeData, getEscapeChars, unescapeData } from "./escape.js";
|
|
6
|
+
export {
|
|
7
|
+
TextProgressBar,
|
|
8
|
+
TmuxMode,
|
|
9
|
+
TrzszBuffer,
|
|
10
|
+
TrzszError,
|
|
11
|
+
TrzszTransfer,
|
|
12
|
+
checkDuplicateNames,
|
|
13
|
+
decodeBuffer,
|
|
14
|
+
encodeBuffer,
|
|
15
|
+
escapeCharsToCodes,
|
|
16
|
+
escapeData,
|
|
17
|
+
formatSavedFiles,
|
|
18
|
+
getEllipsisString,
|
|
19
|
+
getEscapeChars,
|
|
20
|
+
isArrayOfType,
|
|
21
|
+
isVT100End,
|
|
22
|
+
strToArrBuf,
|
|
23
|
+
strToUint8,
|
|
24
|
+
stripServerOutput,
|
|
25
|
+
stripTmuxStatusLine,
|
|
26
|
+
trzszVersion,
|
|
27
|
+
uint8ToStr,
|
|
28
|
+
unescapeData
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|