@yanzc/claude-pro 0.7.1 → 0.7.3
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/.env.example +24 -36
- package/README.md +317 -372
- package/package.json +2 -2
- package/src/agent.js +14 -2
- package/src/api.js +40 -173
- package/src/attach.js +14 -0
- package/src/background.js +102 -12
- package/src/checkpoints.js +4 -2
- package/src/commands.js +20 -85
- package/src/config.js +40 -151
- package/src/gateway.js +9 -3
- package/src/history.js +69 -0
- package/src/host.js +71 -14
- package/src/main.js +19 -2
- package/src/peers.js +3 -6
- package/src/print.js +1 -1
- package/src/prompts.js +1 -4
- package/src/ratelimit.js +6 -19
- package/src/remote.js +6 -40
- package/src/runtime.js +4 -2
- package/src/tools/peers.js +5 -9
- package/src/ui/repl.js +43 -215
- package/src/claude-code.js +0 -60
- package/src/codex/client.js +0 -310
- package/src/codex/translate-request.js +0 -239
- package/src/codex/translate-stream.js +0 -328
- package/src/oauth.js +0 -287
- package/src/transfer.js +0 -260
- package/src/utils/downloads.js +0 -83
package/src/transfer.js
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
// Files between the sessions of a gateway account (see remote.js): resumable uploads in chunks, and downloads that
|
|
2
|
-
// continue with Range requests, so that a 2 GB video survives a network outage. Nothing holds a whole file in
|
|
3
|
-
// memory: uploads read a chunk at a time, downloads stream to a .part file in the target folder.
|
|
4
|
-
//
|
|
5
|
-
// Upload: POST /v1/remote/uploads {name, size, sha256} -> {id, received, chunkSize} (the same unfinished upload
|
|
6
|
-
// again for the same file: sending it again later continues it), then PUT /v1/remote/uploads/:id with
|
|
7
|
-
// x-upload-offset = received, one chunk at a time, until {complete: true, file}. A 409 says where the gateway is.
|
|
8
|
-
// Download: GET /v1/remote/files/:id, with Range: bytes=N- to continue; checked against its sha256 at the end.
|
|
9
|
-
|
|
10
|
-
import fs from 'node:fs';
|
|
11
|
-
import path from 'node:path';
|
|
12
|
-
import crypto from 'node:crypto';
|
|
13
|
-
import { Readable, Transform } from 'node:stream';
|
|
14
|
-
import { pipeline } from 'node:stream/promises';
|
|
15
|
-
import { safeFileName } from './utils/downloads.js';
|
|
16
|
-
|
|
17
|
-
const CHUNK = 8 * 1024 * 1024;
|
|
18
|
-
const MIN_CHUNK = 256 * 1024;
|
|
19
|
-
const MAX_CHUNK = 16 * 1024 * 1024;
|
|
20
|
-
export const RETRY_FOR_MS = 60 * 60 * 1000; // keep trying through a network outage this long
|
|
21
|
-
const IDLE_MS = 60_000; // a download that receives nothing this long is cut, and continued
|
|
22
|
-
|
|
23
|
-
const abortError = () => Object.assign(new Error('stopped'), { name: 'AbortError' });
|
|
24
|
-
|
|
25
|
-
function sleep(ms, signal) {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
if (signal?.aborted) { reject(abortError()); return; }
|
|
28
|
-
const timer = setTimeout(() => { signal?.removeEventListener('abort', onAbort); resolve(); }, ms);
|
|
29
|
-
const onAbort = () => { clearTimeout(timer); reject(abortError()); };
|
|
30
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// What is worth waiting out: no answer at all (the network), a timeout, the gateway overloaded or restarting. Not
|
|
35
|
-
// a gateway out of disk space (507) or one that lacks the feature (501): waiting an hour would not change that.
|
|
36
|
-
export const transient = (e) => e?.name !== 'AbortError' && (!e?.status || (e.status >= 500 && e.status !== 501 && e.status !== 507) || e.status === 429 || e.status === 408);
|
|
37
|
-
|
|
38
|
-
const signalWith = (signal, ms) => (signal ? AbortSignal.any([signal, AbortSignal.timeout(ms)]) : AbortSignal.timeout(ms));
|
|
39
|
-
|
|
40
|
-
// One request to the gateway; a failure carries {status, data} (data: the JSON of the error, when there is one).
|
|
41
|
-
async function api(client, method, route, { json, body, headers = {}, timeout = 30_000, signal } = {}) {
|
|
42
|
-
let res;
|
|
43
|
-
try {
|
|
44
|
-
res = await client.fetch(`${client.base}${route}`, {
|
|
45
|
-
method,
|
|
46
|
-
headers: { authorization: `Bearer ${client.apiKey}`, ...(json !== undefined ? { 'content-type': 'application/json' } : {}), ...headers },
|
|
47
|
-
body: json !== undefined ? JSON.stringify(json) : body,
|
|
48
|
-
signal: signalWith(signal, timeout),
|
|
49
|
-
});
|
|
50
|
-
} catch (e) {
|
|
51
|
-
if (signal?.aborted) throw abortError();
|
|
52
|
-
throw e;
|
|
53
|
-
}
|
|
54
|
-
const data = await res.json().catch(() => ({}));
|
|
55
|
-
if (!res.ok) {
|
|
56
|
-
const message = data?.error?.message || (typeof data?.error === 'string' ? data.error : `HTTP ${res.status}`);
|
|
57
|
-
const missing = res.status === 404 && /\/uploads$/.test(route) ? `the gateway ${client.base} does not take files (yet)` : null;
|
|
58
|
-
throw Object.assign(new Error(missing || String(message)), { status: res.status, data });
|
|
59
|
-
}
|
|
60
|
-
return data;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export async function hashFile(file, { signal, onProgress } = {}) {
|
|
64
|
-
const hash = crypto.createHash('sha256');
|
|
65
|
-
let done = 0;
|
|
66
|
-
for await (const chunk of fs.createReadStream(file, { highWaterMark: 1 << 20, signal })) {
|
|
67
|
-
hash.update(chunk);
|
|
68
|
-
done += chunk.length;
|
|
69
|
-
onProgress?.(done);
|
|
70
|
-
}
|
|
71
|
-
return hash.digest('hex');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Put the file `{path, name, size}` on the gateway: resolves with {id, name, size, sha256}, the id a message's
|
|
75
|
-
// files refer to. onProgress({phase: 'hashing'|'sending'|'waiting', done, total, error, retryIn}).
|
|
76
|
-
export async function uploadFile(client, file, { signal, onProgress = () => {}, retryForMs = RETRY_FOR_MS } = {}) {
|
|
77
|
-
const { size } = file;
|
|
78
|
-
onProgress({ phase: 'hashing', done: 0, total: size });
|
|
79
|
-
const sha256 = await hashFile(file.path, { signal, onProgress: (done) => onProgress({ phase: 'hashing', done, total: size }) });
|
|
80
|
-
let failingSince = 0;
|
|
81
|
-
let wait = 1000;
|
|
82
|
-
// A failure worth waiting out: wait (1 s, 2 s... 30 s), for an hour at most since the last thing that worked.
|
|
83
|
-
const waitOut = async (e, done) => {
|
|
84
|
-
if (signal?.aborted) throw abortError();
|
|
85
|
-
if (!transient(e)) throw e;
|
|
86
|
-
failingSince ||= Date.now();
|
|
87
|
-
if (Date.now() - failingSince > retryForMs) throw e;
|
|
88
|
-
onProgress({ phase: 'waiting', done, total: size, error: e.message, retryIn: wait });
|
|
89
|
-
await sleep(wait, signal);
|
|
90
|
-
wait = Math.min(wait * 2, 30_000);
|
|
91
|
-
};
|
|
92
|
-
const start = async () => {
|
|
93
|
-
for (;;) {
|
|
94
|
-
try { return await api(client, 'POST', '/v1/remote/uploads', { json: { name: file.name, size, sha256 }, signal }); } catch (e) { await waitOut(e, 0); }
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
let up = await start();
|
|
98
|
-
if (up.complete && up.file) return up.file;
|
|
99
|
-
const chunkSize = Math.max(MIN_CHUNK, Math.min(MAX_CHUNK, Number(up.chunkSize) || CHUNK));
|
|
100
|
-
let received = Number(up.received) || 0;
|
|
101
|
-
let restarts = 0;
|
|
102
|
-
const fd = fs.openSync(file.path, 'r');
|
|
103
|
-
try {
|
|
104
|
-
for (;;) {
|
|
105
|
-
if (signal?.aborted) throw abortError();
|
|
106
|
-
try {
|
|
107
|
-
const len = Math.max(0, Math.min(chunkSize, size - received));
|
|
108
|
-
const chunk = Buffer.alloc(len);
|
|
109
|
-
if (len) fs.readSync(fd, chunk, 0, len, received);
|
|
110
|
-
const r = await api(client, 'PUT', `/v1/remote/uploads/${encodeURIComponent(up.id)}`, {
|
|
111
|
-
body: chunk, headers: { 'content-type': 'application/octet-stream', 'x-upload-offset': String(received) },
|
|
112
|
-
timeout: 60_000 + Math.ceil(len / 50), signal, // at least 50 KB/s
|
|
113
|
-
});
|
|
114
|
-
received = Number(r.received) || received + len;
|
|
115
|
-
failingSince = 0;
|
|
116
|
-
wait = 1000;
|
|
117
|
-
onProgress({ phase: 'sending', done: received, total: size });
|
|
118
|
-
if (r.complete && r.file) return r.file;
|
|
119
|
-
} catch (e) {
|
|
120
|
-
if (e.status === 409 && Number.isFinite(Number(e.data?.received))) {
|
|
121
|
-
// the gateway has more (or less) than was thought: a chunk cut off midway counted in part
|
|
122
|
-
received = Number(e.data.received);
|
|
123
|
-
if (e.data.complete && e.data.file) return e.data.file;
|
|
124
|
-
if (received >= size) {
|
|
125
|
-
const st = await api(client, 'GET', `/v1/remote/uploads/${encodeURIComponent(up.id)}`, { signal });
|
|
126
|
-
if (st.complete && st.file) return st.file;
|
|
127
|
-
}
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
if (e.status === 404 && restarts < 3) { // the gateway dropped it (expired): start over
|
|
131
|
-
restarts++;
|
|
132
|
-
up = await start();
|
|
133
|
-
if (up.complete && up.file) return up.file;
|
|
134
|
-
received = Number(up.received) || 0;
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
if (e.status === 422) throw new Error(`${file.name} changed while it was being sent (checksum): send it again`);
|
|
138
|
-
await waitOut(e, received);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
} finally {
|
|
142
|
-
fs.closeSync(fd);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Move `part` into `dir` as `name`, or "name (1).ext"... when that is taken: never over an existing file.
|
|
147
|
-
export function moveUnique(part, dir, name) {
|
|
148
|
-
const safe = safeFileName(name);
|
|
149
|
-
const ext = path.extname(safe);
|
|
150
|
-
const stem = safe.slice(0, safe.length - ext.length);
|
|
151
|
-
for (let i = 0; i < 1000; i++) {
|
|
152
|
-
const target = path.join(dir, i ? `${stem} (${i})${ext}` : safe);
|
|
153
|
-
try {
|
|
154
|
-
fs.linkSync(part, target); // fails when the name is taken, unlike a rename
|
|
155
|
-
fs.unlinkSync(part);
|
|
156
|
-
return target;
|
|
157
|
-
} catch (e) {
|
|
158
|
-
if (e.code === 'EEXIST') continue;
|
|
159
|
-
if (fs.existsSync(target)) continue;
|
|
160
|
-
fs.renameSync(part, target); // a file system without hard links
|
|
161
|
-
return target;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
throw new Error(`too many files called ${safe} in ${dir}`);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Copy a file of this computer into `dir` (a session on the same computer sent it), never over an existing file.
|
|
168
|
-
export async function copyUnique(src, dir, name) {
|
|
169
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
170
|
-
const safe = safeFileName(name);
|
|
171
|
-
const ext = path.extname(safe);
|
|
172
|
-
const stem = safe.slice(0, safe.length - ext.length);
|
|
173
|
-
for (let i = 0; i < 1000; i++) {
|
|
174
|
-
const target = path.join(dir, i ? `${stem} (${i})${ext}` : safe);
|
|
175
|
-
try {
|
|
176
|
-
await fs.promises.copyFile(src, target, fs.constants.COPYFILE_EXCL);
|
|
177
|
-
return target;
|
|
178
|
-
} catch (e) {
|
|
179
|
-
if (e.code !== 'EEXIST') throw e;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
throw new Error(`too many files called ${safe} in ${dir}`);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// The file `{id, name, size, sha256}` from the gateway into `dir`: resolves with the path it was saved at. It
|
|
186
|
-
// streams into a .part file named after the file's id, which a later attempt (after a restart too) continues.
|
|
187
|
-
export async function downloadFile(client, file, dir, { signal, onProgress = () => {}, retryForMs = RETRY_FOR_MS } = {}) {
|
|
188
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
189
|
-
const part = path.join(dir, `.${String(file.id).replace(/[^\w-]/g, '_')}.claude-pro-part`);
|
|
190
|
-
const total = Number(file.size) || 0;
|
|
191
|
-
try {
|
|
192
|
-
const { bavail, bsize } = fs.statfsSync(dir);
|
|
193
|
-
const have = fs.existsSync(part) ? fs.statSync(part).size : 0;
|
|
194
|
-
if (total - have > bavail * bsize) throw new Error(`not enough free space in ${dir} (${Math.round(total / 1048576)} MB needed)`);
|
|
195
|
-
} catch (e) {
|
|
196
|
-
if (/free space/.test(e.message)) throw e; // statfs itself not available: just try
|
|
197
|
-
}
|
|
198
|
-
let failingSince = 0;
|
|
199
|
-
let wait = 1000;
|
|
200
|
-
let fresh = false; // started over once after a checksum failure
|
|
201
|
-
for (;;) {
|
|
202
|
-
if (signal?.aborted) throw abortError();
|
|
203
|
-
let have = fs.existsSync(part) ? fs.statSync(part).size : 0;
|
|
204
|
-
if (have > total && total) { fs.truncateSync(part, 0); have = 0; }
|
|
205
|
-
try {
|
|
206
|
-
if (have < total || !fs.existsSync(part)) {
|
|
207
|
-
const idle = new AbortController();
|
|
208
|
-
let timer = setTimeout(() => idle.abort(), IDLE_MS);
|
|
209
|
-
const res = await client.fetch(`${client.base}/v1/remote/files/${encodeURIComponent(file.id)}`, {
|
|
210
|
-
headers: { authorization: `Bearer ${client.apiKey}`, ...(have ? { range: `bytes=${have}-` } : {}) },
|
|
211
|
-
signal: signal ? AbortSignal.any([signal, idle.signal]) : idle.signal,
|
|
212
|
-
});
|
|
213
|
-
if (!res.ok && res.status !== 206) {
|
|
214
|
-
const data = await res.json().catch(() => ({}));
|
|
215
|
-
throw Object.assign(new Error(data?.error?.message || `HTTP ${res.status}`), { status: res.status });
|
|
216
|
-
}
|
|
217
|
-
if (have && res.status !== 206) have = 0; // the range was not honoured: from the start
|
|
218
|
-
let done = have;
|
|
219
|
-
const count = new Transform({
|
|
220
|
-
transform(chunk, _enc, cb) {
|
|
221
|
-
clearTimeout(timer);
|
|
222
|
-
timer = setTimeout(() => idle.abort(), IDLE_MS);
|
|
223
|
-
done += chunk.length;
|
|
224
|
-
onProgress({ phase: 'receiving', done, total });
|
|
225
|
-
cb(null, chunk);
|
|
226
|
-
},
|
|
227
|
-
});
|
|
228
|
-
try {
|
|
229
|
-
await pipeline(Readable.fromWeb(res.body), count, fs.createWriteStream(part, { flags: have ? 'a' : 'w' }));
|
|
230
|
-
} finally {
|
|
231
|
-
clearTimeout(timer);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
const got = fs.statSync(part).size;
|
|
235
|
-
if (total && got < total) throw Object.assign(new Error('the download stopped early'), { status: 0 });
|
|
236
|
-
if (file.sha256) {
|
|
237
|
-
onProgress({ phase: 'checking', done: got, total });
|
|
238
|
-
if ((await hashFile(part, { signal })) !== String(file.sha256).toLowerCase()) {
|
|
239
|
-
fs.rmSync(part, { force: true });
|
|
240
|
-
if (fresh) throw new Error(`${file.name} arrived damaged twice (checksum)`);
|
|
241
|
-
fresh = true;
|
|
242
|
-
continue;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
return moveUnique(part, dir, file.name);
|
|
246
|
-
} catch (caught) {
|
|
247
|
-
if (signal?.aborted) throw abortError();
|
|
248
|
-
// cut for receiving nothing for a while: the network, not the user
|
|
249
|
-
const e = caught?.name === 'AbortError' ? Object.assign(new Error('nothing arrived for a minute'), { status: 0 }) : caught;
|
|
250
|
-
if (!transient(e)) throw e;
|
|
251
|
-
const now = fs.existsSync(part) ? fs.statSync(part).size : 0;
|
|
252
|
-
if (now > have) { failingSince = 0; wait = 1000; } // it got further: the hour starts again
|
|
253
|
-
failingSince ||= Date.now();
|
|
254
|
-
if (Date.now() - failingSince > retryForMs) throw e;
|
|
255
|
-
onProgress({ phase: 'waiting', done: now, total, error: e.message, retryIn: wait });
|
|
256
|
-
await sleep(wait, signal);
|
|
257
|
-
wait = Math.min(wait * 2, 30_000);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
package/src/utils/downloads.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
// Files that other sessions send this one (/sendfile, MessagePeer's files) land in the user's Downloads folder:
|
|
2
|
-
// on Windows the folder the shell knows as Downloads (it can have been moved), else ~/Downloads (or what
|
|
3
|
-
// xdg-user-dir says). CLAUDE_PRO_DOWNLOAD_DIR replaces it.
|
|
4
|
-
|
|
5
|
-
import fs from 'node:fs';
|
|
6
|
-
import os from 'node:os';
|
|
7
|
-
import path from 'node:path';
|
|
8
|
-
import { spawn } from 'node:child_process';
|
|
9
|
-
|
|
10
|
-
export const MAX_SEND_BYTES = 5 * 1024 ** 3; // a file; the gateway takes no more
|
|
11
|
-
export const MAX_SEND_FILES = 20; // a message
|
|
12
|
-
|
|
13
|
-
const DOWNLOADS_GUID = '{374DE290-123F-4565-9164-39C4925E467B}';
|
|
14
|
-
const PS_DOWNLOADS = `
|
|
15
|
-
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
16
|
-
$p = (Get-ItemProperty -Path 'HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders' -Name '${DOWNLOADS_GUID}' -ErrorAction Stop).'${DOWNLOADS_GUID}'
|
|
17
|
-
[Console]::Out.Write($p)
|
|
18
|
-
`;
|
|
19
|
-
|
|
20
|
-
function output(cmd, args) {
|
|
21
|
-
return new Promise((resolve) => {
|
|
22
|
-
let out = '';
|
|
23
|
-
let child;
|
|
24
|
-
try {
|
|
25
|
-
child = spawn(cmd, args, { windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
26
|
-
} catch {
|
|
27
|
-
resolve('');
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const timer = setTimeout(() => child.kill(), 10_000);
|
|
31
|
-
child.stdout.setEncoding('utf8');
|
|
32
|
-
child.stdout.on('data', (d) => { out += d; });
|
|
33
|
-
child.on('error', () => { clearTimeout(timer); resolve(''); });
|
|
34
|
-
child.on('close', (code) => { clearTimeout(timer); resolve(code === 0 ? out.trim() : ''); });
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
let cached = null;
|
|
39
|
-
|
|
40
|
-
export function downloadsDir(env = process.env) {
|
|
41
|
-
if (env.CLAUDE_PRO_DOWNLOAD_DIR) return Promise.resolve(path.resolve(env.CLAUDE_PRO_DOWNLOAD_DIR));
|
|
42
|
-
cached ??= (async () => {
|
|
43
|
-
const home = os.homedir();
|
|
44
|
-
let dir = '';
|
|
45
|
-
if (process.platform === 'win32') {
|
|
46
|
-
dir = await output('powershell.exe', ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', Buffer.from(PS_DOWNLOADS, 'utf16le').toString('base64')]);
|
|
47
|
-
} else if (process.platform !== 'darwin') {
|
|
48
|
-
dir = await output('xdg-user-dir', ['DOWNLOAD']);
|
|
49
|
-
if (path.resolve(dir || home) === path.resolve(home)) dir = ''; // xdg's answer when it has none
|
|
50
|
-
}
|
|
51
|
-
return dir && path.isAbsolute(dir) ? dir : path.join(home, 'Downloads');
|
|
52
|
-
})();
|
|
53
|
-
return cached;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const RESERVED = /^(con|prn|aux|nul|com\d|lpt\d)$/i;
|
|
57
|
-
|
|
58
|
-
// A name a file can be saved under, on any system, from the name another computer gave it.
|
|
59
|
-
export function safeFileName(name) {
|
|
60
|
-
let base = String(name || '').split(/[\\/]/).pop();
|
|
61
|
-
base = base.replace(/[\u0000-\u001f\u007f<>:"|?*]/g, '_').replace(/[. ]+$/g, '').trim();
|
|
62
|
-
if (!base || base === '.' || base === '..') base = 'file';
|
|
63
|
-
const ext = path.extname(base);
|
|
64
|
-
if (RESERVED.test(base.slice(0, base.length - ext.length))) base = `_${base}`;
|
|
65
|
-
if (base.length > 200) base = base.slice(0, 200 - ext.length) + ext;
|
|
66
|
-
return base;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// The files to send, checked: [{path, name, size}], or throws with what is wrong with which.
|
|
70
|
-
export function filesToSend(paths) {
|
|
71
|
-
if (!paths.length) throw new Error('No file to send.');
|
|
72
|
-
if (paths.length > MAX_SEND_FILES) throw new Error(`At most ${MAX_SEND_FILES} files at a time (${paths.length} given).`);
|
|
73
|
-
return paths.map((p) => {
|
|
74
|
-
let st;
|
|
75
|
-
try { st = fs.statSync(p); } catch { throw new Error(`File not found: ${p}`); }
|
|
76
|
-
if (st.isDirectory()) throw new Error(`${p} is a folder: send the files in it (or zip it first).`);
|
|
77
|
-
if (!st.isFile()) throw new Error(`Not a file: ${p}`);
|
|
78
|
-
if (st.size > MAX_SEND_BYTES) throw new Error(`${path.basename(p)} is ${formatBytes(st.size)}: at most ${formatBytes(MAX_SEND_BYTES)} a file.`);
|
|
79
|
-
return { path: p, name: path.basename(p), size: st.size };
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export const formatBytes = (n) => (n >= 1024 ** 3 ? `${(n / 1024 ** 3).toFixed(1)} GB` : n >= 1048576 ? `${(n / 1048576).toFixed(1)} MB` : n >= 1024 ? `${Math.round(n / 1024)} KB` : `${n} B`);
|