@av-pi-studio/server 0.0.117 → 0.0.119
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/README.md +8 -6
- package/dist/agent/agent-service.d.ts +1 -1
- package/dist/agent/agent-service.js +5 -4
- package/dist/agent/session-operations.d.ts +1 -1
- package/dist/agent/session-operations.js +2 -1
- package/dist/daemon/bootstrap.js +6 -2
- package/dist/daemon/dev-bootstrap.js +30 -4
- package/dist/files/file-explorer.d.ts +0 -3
- package/dist/files/file-explorer.js +0 -2
- package/dist/files/file-transfer.d.ts +80 -16
- package/dist/files/file-transfer.js +439 -90
- package/dist/files/index.d.ts +0 -1
- package/dist/files/index.js +0 -1
- package/dist/observability/daemon-status-service.js +4 -0
- package/dist/test-support/daemon-test-client.d.ts +2 -0
- package/dist/test-support/daemon-test-client.js +7 -1
- package/dist/test-support/external-fixtures-scenario.js +106 -0
- package/dist/ws/router.d.ts +7 -0
- package/dist/ws/router.js +28 -9
- package/dist/ws/session.d.ts +21 -0
- package/dist/ws/session.js +34 -0
- package/dist/ws/ws-server.js +2 -0
- package/package.json +3 -3
- package/dist/files/download-token-store.d.ts +0 -14
- package/dist/files/download-token-store.js +0 -33
|
@@ -1,53 +1,131 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { mkdir, open, realpath } from "node:fs/promises";
|
|
1
|
+
import { mkdir, open, realpath, stat } from "node:fs/promises";
|
|
3
2
|
import { basename, dirname } from "node:path";
|
|
4
3
|
import { encodeFileTransferFrame, nextFreeSlot, SLOT_SPACE, tryDecodeFileTransferFrame, } from "@av-pi-studio/protocol";
|
|
5
|
-
import { DownloadTokenStore } from "./download-token-store.js";
|
|
6
4
|
import { mimeHintForFile } from "./file-explorer.js";
|
|
7
5
|
import { expandHome } from "./resolve-path.js";
|
|
8
6
|
/**
|
|
9
|
-
* Chunked file download (
|
|
10
|
-
* (features/file-explorer-transfer.md § Binary transfer frames, § Behavior).
|
|
7
|
+
* Chunked file download (by path, optionally a byte range) + upload over the file-transfer binary
|
|
8
|
+
* frame format (features/file-explorer-transfer.md § Binary transfer frames, § Behavior). A
|
|
9
|
+
* download is answered first (`file_download_response`, with the file's size) and then streams
|
|
11
10
|
* `Begin → Chunk* → End`; uploads consume the same frames and write to the target path.
|
|
11
|
+
* Range, response ordering and errors: features/desktop-client-protocol-followups-2.md § 1.
|
|
12
|
+
* Flow control, completion and stream ownership: features/desktop-client-protocol-followups.md § 2.
|
|
12
13
|
*/
|
|
13
14
|
const DEFAULT_CHUNK_BYTES = 32 * 1024;
|
|
15
|
+
/**
|
|
16
|
+
* A download stops reading from disk while this many bytes are queued on the session's socket (32
|
|
17
|
+
* default chunks) and resumes below the low mark. Chosen, not measured: big enough that the read
|
|
18
|
+
* never starves a fast LAN link between polls, small enough that a stalled client costs the daemon
|
|
19
|
+
* about a megabyte instead of the whole file. Injectable through `FileTransferDeps`.
|
|
20
|
+
*/
|
|
21
|
+
export const DOWNLOAD_HIGH_WATER_BYTES = 1024 * 1024;
|
|
22
|
+
export const DOWNLOAD_LOW_WATER_BYTES = 256 * 1024;
|
|
23
|
+
const DEFAULT_DRAIN_POLL_MS = 10;
|
|
24
|
+
/** Absent → `undefined`; a safe integer ≥ `min` → itself; anything else → `null` (invalid). */
|
|
25
|
+
function parseCount(value, min) {
|
|
26
|
+
if (value === undefined)
|
|
27
|
+
return undefined;
|
|
28
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= min ? value : null;
|
|
29
|
+
}
|
|
30
|
+
function isMissing(err) {
|
|
31
|
+
return err?.code === "ENOENT";
|
|
32
|
+
}
|
|
33
|
+
/** `ifUnchanged` equals the file as it is now. Anything but `{ size, mtimeMs }` never does. */
|
|
34
|
+
function matchesFile(ifUnchanged, size, mtimeMs) {
|
|
35
|
+
if (typeof ifUnchanged !== "object" || ifUnchanged === null)
|
|
36
|
+
return false;
|
|
37
|
+
const expected = ifUnchanged;
|
|
38
|
+
return expected.size === size && expected.mtimeMs === mtimeMs;
|
|
39
|
+
}
|
|
40
|
+
/** `open_failed (EACCES)`-style text: a stage plus the errno code, never a path or a stack. */
|
|
41
|
+
function failureText(stage, err) {
|
|
42
|
+
const code = err?.code;
|
|
43
|
+
return typeof code === "string" ? `${stage}_failed (${code})` : `${stage}_failed`;
|
|
44
|
+
}
|
|
14
45
|
export class FileTransferService {
|
|
15
|
-
tokenStore;
|
|
16
46
|
chunkBytes;
|
|
47
|
+
highWaterBytes;
|
|
48
|
+
lowWaterBytes;
|
|
49
|
+
drainPollMs;
|
|
17
50
|
/** Pending uploads keyed by stream id. */
|
|
18
51
|
uploads = new Map();
|
|
19
52
|
/** Rotating hand-out point in the one-byte stream id space (see `nextFreeSlot`). */
|
|
20
53
|
streamCursor = 1;
|
|
54
|
+
/** Accepted downloads by session id, then stream id: download ids are scoped to the session. */
|
|
55
|
+
downloads = new Map();
|
|
56
|
+
/** Rotating hand-out point for a download the client did not number itself. */
|
|
57
|
+
downloadCursor = 1;
|
|
21
58
|
constructor(deps = {}) {
|
|
22
|
-
this.tokenStore = deps.tokenStore ?? new DownloadTokenStore();
|
|
23
59
|
this.chunkBytes = deps.chunkBytes ?? DEFAULT_CHUNK_BYTES;
|
|
60
|
+
this.highWaterBytes = deps.highWaterBytes ?? DOWNLOAD_HIGH_WATER_BYTES;
|
|
61
|
+
this.lowWaterBytes = deps.lowWaterBytes ?? DOWNLOAD_LOW_WATER_BYTES;
|
|
62
|
+
this.drainPollMs = deps.drainPollMs ?? DEFAULT_DRAIN_POLL_MS;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Answer a `file_download_request` from session `sessionId`. `response` is the
|
|
66
|
+
* `file_download_response` to send; `run` is present only when the download was accepted, and
|
|
67
|
+
* streams the file once called (after the response has gone out). The caller must call `run`
|
|
68
|
+
* exactly once: it owns the open file.
|
|
69
|
+
*/
|
|
70
|
+
async beginDownload(sessionId, message) {
|
|
71
|
+
const opened = await this.openDownload(sessionId, message);
|
|
72
|
+
if (!opened.ok) {
|
|
73
|
+
return {
|
|
74
|
+
response: {
|
|
75
|
+
type: "file_download_response",
|
|
76
|
+
ok: false,
|
|
77
|
+
error: opened.error,
|
|
78
|
+
...(opened.stream === undefined ? {} : { stream: opened.stream }),
|
|
79
|
+
...opened.current,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const d = opened.download;
|
|
84
|
+
return {
|
|
85
|
+
response: {
|
|
86
|
+
type: "file_download_response",
|
|
87
|
+
ok: true,
|
|
88
|
+
stream: d.stream,
|
|
89
|
+
transferId: d.transferId,
|
|
90
|
+
totalBytes: d.totalBytes,
|
|
91
|
+
mtimeMs: d.mtimeMs,
|
|
92
|
+
offset: d.offset,
|
|
93
|
+
length: d.length,
|
|
94
|
+
},
|
|
95
|
+
run: (sink, pacing) => this.streamDownload(d, sink, pacing),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Stop `sessionId`'s download on `stream`: it sends `Error` `cancelled` and reads no further.
|
|
100
|
+
* False when that session has no download on `stream` (never started, or already finished).
|
|
101
|
+
*/
|
|
102
|
+
cancelDownload(sessionId, stream) {
|
|
103
|
+
const active = this.downloads.get(sessionId)?.get(stream);
|
|
104
|
+
if (!active)
|
|
105
|
+
return false;
|
|
106
|
+
active.stop ??= "cancelled";
|
|
107
|
+
active.wake?.();
|
|
108
|
+
return true;
|
|
24
109
|
}
|
|
25
|
-
/** Issue a download token for a resolved path (also used by the file explorer for binary files). */
|
|
26
|
-
issueDownloadToken = (resolvedPath) => this.tokenStore.issue(resolvedPath).token;
|
|
27
110
|
registerHandlers(registry) {
|
|
28
|
-
registry.register("file_download_token_request", async (ctx) => {
|
|
29
|
-
const path = String(ctx.message.path ?? "");
|
|
30
|
-
let resolved;
|
|
31
|
-
try {
|
|
32
|
-
resolved = await realpath(expandHome(path));
|
|
33
|
-
}
|
|
34
|
-
catch {
|
|
35
|
-
return { type: "file_download_token_response", ok: false, error: "not_found" };
|
|
36
|
-
}
|
|
37
|
-
const { token, expiresAt } = this.tokenStore.issue(resolved);
|
|
38
|
-
return { type: "file_download_token_response", ok: true, token, expiresAt };
|
|
39
|
-
});
|
|
40
111
|
registry.register("file_download_request", async (ctx) => {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
112
|
+
const { response, run } = await this.beginDownload(ctx.session.id, ctx.message);
|
|
113
|
+
// The router sends the reply first and the stream starts after it, so a client always learns
|
|
114
|
+
// the size and range before the first frame and a long transfer never holds the request open.
|
|
115
|
+
if (run) {
|
|
116
|
+
ctx.afterReply(() => void run((frame) => ctx.session.sendBinary(frame), {
|
|
117
|
+
bufferedAmount: () => ctx.session.bufferedAmount,
|
|
118
|
+
isOpen: () => ctx.session.isOpen,
|
|
119
|
+
}));
|
|
48
120
|
}
|
|
49
|
-
|
|
50
|
-
|
|
121
|
+
return response;
|
|
122
|
+
});
|
|
123
|
+
registry.register("file_download_cancel_request", (ctx) => {
|
|
124
|
+
const stream = ctx.message.stream;
|
|
125
|
+
const cancelled = typeof stream === "number" && this.cancelDownload(ctx.session.id, stream);
|
|
126
|
+
return cancelled
|
|
127
|
+
? { type: "file_download_cancel_response", ok: true }
|
|
128
|
+
: { type: "file_download_cancel_response", ok: false, error: "not_found" };
|
|
51
129
|
});
|
|
52
130
|
registry.register("file_upload_request", (ctx) => {
|
|
53
131
|
const targetPath = String(ctx.message.path ?? "");
|
|
@@ -63,91 +141,362 @@ export class FileTransferService {
|
|
|
63
141
|
this.uploads.set(stream, {
|
|
64
142
|
transferId,
|
|
65
143
|
targetPath,
|
|
144
|
+
sessionId: ctx.session.id,
|
|
145
|
+
ack: ctx.message.ack === true,
|
|
66
146
|
handle: null,
|
|
67
147
|
bytesWritten: 0,
|
|
68
148
|
chain: Promise.resolve(),
|
|
149
|
+
done: false,
|
|
69
150
|
});
|
|
70
151
|
return { type: "file_upload_response", ok: true, stream };
|
|
71
152
|
});
|
|
72
153
|
}
|
|
73
|
-
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Validate a download request, reserve its stream id on the session and open its file. Nothing is
|
|
156
|
+
* sent: the caller answers first and then calls `streamDownload`, which owns (and closes) the
|
|
157
|
+
* returned handle and releases the reservation.
|
|
158
|
+
*
|
|
159
|
+
* `offset` defaults to 0 and `length` to "to the end of the file"; `length` is clamped to it. An
|
|
160
|
+
* `offset` equal to the size is a legal empty range, one beyond it is `range_not_satisfiable`.
|
|
161
|
+
* `ifUnchanged` that does not equal the file's current size and modification time (a malformed
|
|
162
|
+
* one never does) is `changed`.
|
|
163
|
+
*/
|
|
164
|
+
async openDownload(sessionId, message) {
|
|
165
|
+
const path = message.path;
|
|
166
|
+
if (typeof path !== "string" || path === "")
|
|
167
|
+
return { ok: false, error: "missing_path" };
|
|
168
|
+
const requestedStream = message.stream;
|
|
169
|
+
if (requestedStream !== undefined &&
|
|
170
|
+
!(typeof requestedStream === "number" &&
|
|
171
|
+
Number.isInteger(requestedStream) &&
|
|
172
|
+
requestedStream >= 0 &&
|
|
173
|
+
requestedStream < SLOT_SPACE)) {
|
|
174
|
+
return { ok: false, error: "invalid_stream" };
|
|
175
|
+
}
|
|
176
|
+
const offset = parseCount(message.offset, 0);
|
|
177
|
+
const requestedLength = parseCount(message.length, 1);
|
|
178
|
+
if (offset === null || requestedLength === null)
|
|
179
|
+
return { ok: false, error: "invalid_range" };
|
|
180
|
+
// Check and reserve with no await in between: two requests for one id cannot both pass.
|
|
181
|
+
let mine = this.downloads.get(sessionId);
|
|
182
|
+
if (!mine)
|
|
183
|
+
this.downloads.set(sessionId, (mine = new Map()));
|
|
184
|
+
const stream = requestedStream ?? nextFreeSlot(mine, this.downloadCursor);
|
|
185
|
+
if (stream === null) {
|
|
186
|
+
this.releaseSession(sessionId, mine);
|
|
187
|
+
return { ok: false, error: "no_free_stream" };
|
|
188
|
+
}
|
|
189
|
+
if (mine.has(stream))
|
|
190
|
+
return { ok: false, error: "stream_in_use", stream };
|
|
191
|
+
if (requestedStream === undefined)
|
|
192
|
+
this.downloadCursor = (stream + 1) % SLOT_SPACE;
|
|
193
|
+
const active = { stop: null };
|
|
194
|
+
mine.set(stream, active);
|
|
195
|
+
const result = await this.openFile(sessionId, active, stream, path, {
|
|
196
|
+
offset: offset ?? 0,
|
|
197
|
+
requestedLength,
|
|
198
|
+
ifUnchanged: message.ifUnchanged,
|
|
199
|
+
});
|
|
200
|
+
if (!result.ok)
|
|
201
|
+
this.release(sessionId, stream, active);
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
async openFile(sessionId, active, stream, path, range) {
|
|
205
|
+
let resolved;
|
|
206
|
+
try {
|
|
207
|
+
resolved = await realpath(expandHome(path));
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
return { ok: false, error: "not_found", stream };
|
|
211
|
+
}
|
|
212
|
+
let handle;
|
|
213
|
+
try {
|
|
214
|
+
// Checked before opening: opening a FIFO or device for reading can block.
|
|
215
|
+
if (!(await stat(resolved)).isFile())
|
|
216
|
+
return { ok: false, error: "not_a_file", stream };
|
|
217
|
+
handle = await open(resolved, "r");
|
|
218
|
+
}
|
|
219
|
+
catch (err) {
|
|
220
|
+
return { ok: false, error: isMissing(err) ? "not_found" : "unreadable", stream };
|
|
221
|
+
}
|
|
222
|
+
let handedOver = false;
|
|
223
|
+
try {
|
|
224
|
+
active.handle = handle;
|
|
225
|
+
const info = await handle.stat();
|
|
226
|
+
if (!info.isFile())
|
|
227
|
+
return { ok: false, error: "not_a_file", stream };
|
|
228
|
+
if (range.offset > info.size)
|
|
229
|
+
return { ok: false, error: "range_not_satisfiable", stream };
|
|
230
|
+
const mtimeMs = Math.trunc(info.mtimeMs);
|
|
231
|
+
if (range.ifUnchanged !== undefined && !matchesFile(range.ifUnchanged, info.size, mtimeMs)) {
|
|
232
|
+
return {
|
|
233
|
+
ok: false,
|
|
234
|
+
error: "changed",
|
|
235
|
+
stream,
|
|
236
|
+
current: { totalBytes: info.size, mtimeMs },
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
const available = info.size - range.offset;
|
|
240
|
+
const download = {
|
|
241
|
+
sessionId,
|
|
242
|
+
active,
|
|
243
|
+
stream,
|
|
244
|
+
transferId: `dl-${stream}-${Date.now()}`,
|
|
245
|
+
fileName: basename(resolved),
|
|
246
|
+
mimeType: mimeHintForFile(resolved),
|
|
247
|
+
totalBytes: info.size,
|
|
248
|
+
mtimeMs,
|
|
249
|
+
offset: range.offset,
|
|
250
|
+
length: Math.min(range.requestedLength ?? available, available),
|
|
251
|
+
handle,
|
|
252
|
+
};
|
|
253
|
+
handedOver = true;
|
|
254
|
+
return { ok: true, download };
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
return { ok: false, error: "unreadable", stream };
|
|
258
|
+
}
|
|
259
|
+
finally {
|
|
260
|
+
if (!handedOver)
|
|
261
|
+
await handle.close().catch(() => { });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/** Forget a finished or rejected download. A no-op once the session's downloads were dropped. */
|
|
265
|
+
release(sessionId, stream, active) {
|
|
266
|
+
const mine = this.downloads.get(sessionId);
|
|
267
|
+
if (!mine || mine.get(stream) !== active)
|
|
268
|
+
return;
|
|
269
|
+
mine.delete(stream);
|
|
270
|
+
this.releaseSession(sessionId, mine);
|
|
271
|
+
}
|
|
272
|
+
releaseSession(sessionId, mine) {
|
|
273
|
+
if (mine.size === 0 && this.downloads.get(sessionId) === mine)
|
|
274
|
+
this.downloads.delete(sessionId);
|
|
80
275
|
}
|
|
81
276
|
/**
|
|
82
|
-
*
|
|
277
|
+
* Stream an opened download as `Begin → Chunk* → End` binary frames, then close its file.
|
|
278
|
+
*
|
|
279
|
+
* The disk read pauses while the socket's queue is above the high-water mark, so a client that
|
|
280
|
+
* reads slowly costs the daemon about that much memory, not the whole file. A read error, or a
|
|
281
|
+
* file that ended before the announced length, sends one `Error` frame and no `End`; a socket
|
|
282
|
+
* that closed mid-transfer ends it silently. Never rejects: it runs detached from the request.
|
|
83
283
|
*/
|
|
84
|
-
async
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
284
|
+
async streamDownload(d, sink, pacing) {
|
|
285
|
+
try {
|
|
286
|
+
if (d.active.stop === "closed" || !pacing.isOpen())
|
|
287
|
+
return;
|
|
288
|
+
if (d.active.stop === "cancelled") {
|
|
289
|
+
// Cancelled between the answer and the first frame: no `Begin` was promised yet.
|
|
290
|
+
sink(encodeFileTransferFrame({ opcode: "Error", stream: d.stream, message: "cancelled" }));
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
sink(encodeFileTransferFrame({
|
|
294
|
+
opcode: "Begin",
|
|
295
|
+
stream: d.stream,
|
|
296
|
+
meta: {
|
|
297
|
+
transferId: d.transferId,
|
|
298
|
+
fileName: d.fileName,
|
|
299
|
+
mimeType: d.mimeType,
|
|
300
|
+
totalBytes: d.totalBytes,
|
|
301
|
+
mtimeMs: d.mtimeMs,
|
|
302
|
+
offset: d.offset,
|
|
303
|
+
length: d.length,
|
|
304
|
+
},
|
|
305
|
+
}));
|
|
306
|
+
const outcome = await this.pipeRange(d, sink, pacing);
|
|
307
|
+
if (outcome === "closed")
|
|
308
|
+
return;
|
|
309
|
+
sink(encodeFileTransferFrame(outcome === "end"
|
|
310
|
+
? { opcode: "End", stream: d.stream, ok: true }
|
|
311
|
+
: { opcode: "Error", stream: d.stream, message: outcome }));
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
// A send to a socket that closed under us throws; there is nobody left to tell.
|
|
315
|
+
}
|
|
316
|
+
finally {
|
|
317
|
+
this.release(d.sessionId, d.stream, d.active);
|
|
318
|
+
await d.handle.close().catch(() => { });
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
pipeRange(d, sink, pacing) {
|
|
322
|
+
if (d.length === 0)
|
|
323
|
+
return Promise.resolve("end");
|
|
324
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
325
|
+
const rs = d.handle.createReadStream({
|
|
326
|
+
start: d.offset,
|
|
327
|
+
end: d.offset + d.length - 1,
|
|
328
|
+
highWaterMark: this.chunkBytes,
|
|
329
|
+
autoClose: false,
|
|
104
330
|
});
|
|
105
|
-
|
|
106
|
-
|
|
331
|
+
let sent = 0;
|
|
332
|
+
let settled = false;
|
|
333
|
+
const stopped = () => (d.active.stop === "cancelled" ? "cancelled" : "closed");
|
|
334
|
+
const finish = (outcome) => {
|
|
335
|
+
if (settled)
|
|
336
|
+
return;
|
|
337
|
+
settled = true;
|
|
338
|
+
d.active.wake = undefined;
|
|
339
|
+
rs.destroy();
|
|
340
|
+
resolve(outcome);
|
|
341
|
+
};
|
|
342
|
+
d.active.wake = () => {
|
|
343
|
+
if (d.active.stop)
|
|
344
|
+
finish(stopped());
|
|
345
|
+
};
|
|
346
|
+
rs.on("data", (chunk) => {
|
|
347
|
+
if (settled)
|
|
348
|
+
return;
|
|
349
|
+
if (d.active.stop)
|
|
350
|
+
return finish(stopped());
|
|
351
|
+
if (!pacing.isOpen())
|
|
352
|
+
return finish("closed");
|
|
353
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
354
|
+
try {
|
|
355
|
+
sink(encodeFileTransferFrame({
|
|
356
|
+
opcode: "Chunk",
|
|
357
|
+
stream: d.stream,
|
|
358
|
+
data: new Uint8Array(bytes),
|
|
359
|
+
}));
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
return finish("closed");
|
|
363
|
+
}
|
|
364
|
+
sent += bytes.length;
|
|
365
|
+
const queued = pacing.bufferedAmount();
|
|
366
|
+
if (queued !== undefined && queued > this.highWaterBytes) {
|
|
367
|
+
rs.pause();
|
|
368
|
+
void this.waitForDrain(pacing, () => settled).then((stillOpen) => {
|
|
369
|
+
if (!stillOpen)
|
|
370
|
+
finish("closed");
|
|
371
|
+
else if (!settled)
|
|
372
|
+
rs.resume();
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
// The range is bounded, so a short read means the file shrank after the size was announced.
|
|
377
|
+
rs.on("end", () => finish(sent === d.length ? "end" : "changed"));
|
|
378
|
+
rs.on("error", () => finish("read_failed"));
|
|
379
|
+
return promise;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Resolves `true` once the socket's queue is at or below the low-water mark; `false` if it closed
|
|
383
|
+
* or the download ended (`ended`) while waiting.
|
|
384
|
+
*/
|
|
385
|
+
async waitForDrain(pacing, ended) {
|
|
386
|
+
for (;;) {
|
|
387
|
+
if (ended() || !pacing.isOpen())
|
|
388
|
+
return false;
|
|
389
|
+
const queued = pacing.bufferedAmount();
|
|
390
|
+
if (queued === undefined || queued <= this.lowWaterBytes)
|
|
391
|
+
return true;
|
|
392
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
393
|
+
setTimeout(resolve, this.drainPollMs);
|
|
394
|
+
await promise;
|
|
395
|
+
}
|
|
107
396
|
}
|
|
108
397
|
/** Binary frame handler for upload streams (Begin → Chunk* → End). Frames are serialized per
|
|
109
|
-
* stream so an in-flight `Begin` (open) completes before any `Chunk` writes.
|
|
398
|
+
* stream so an in-flight `Begin` (open) completes before any `Chunk` writes. A frame is applied
|
|
399
|
+
* only if it comes from the session that opened the stream. */
|
|
110
400
|
binaryHandler() {
|
|
111
|
-
return (
|
|
401
|
+
return (session, bytes) => {
|
|
112
402
|
const frame = tryDecodeFileTransferFrame(bytes);
|
|
113
403
|
if (!frame)
|
|
114
404
|
return;
|
|
115
405
|
const upload = this.uploads.get(frame.stream);
|
|
116
406
|
if (!upload)
|
|
117
407
|
return; // unknown/closed stream
|
|
118
|
-
|
|
408
|
+
if (upload.sessionId !== session.id)
|
|
409
|
+
return; // not this session's stream
|
|
410
|
+
upload.chain = upload.chain.then(() => this.handleUploadFrame(session, upload, frame));
|
|
119
411
|
};
|
|
120
412
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
413
|
+
/**
|
|
414
|
+
* Release everything a session left open: its pending uploads lose their stream id and their file
|
|
415
|
+
* handle is closed once frames already in flight finish; its downloads stop reading and lose
|
|
416
|
+
* their stream id and file handle. Nothing is sent (the session is gone).
|
|
417
|
+
*/
|
|
418
|
+
disposeSession(session) {
|
|
419
|
+
for (const [stream, upload] of this.uploads) {
|
|
420
|
+
if (upload.sessionId !== session.id)
|
|
421
|
+
continue;
|
|
422
|
+
this.uploads.delete(stream);
|
|
423
|
+
upload.done = true;
|
|
424
|
+
upload.chain = upload.chain.then(() => upload.handle?.close().catch(() => { }));
|
|
425
|
+
}
|
|
426
|
+
const mine = this.downloads.get(session.id);
|
|
427
|
+
if (!mine)
|
|
428
|
+
return;
|
|
429
|
+
this.downloads.delete(session.id);
|
|
430
|
+
for (const active of mine.values()) {
|
|
431
|
+
active.stop = "closed";
|
|
432
|
+
active.wake?.();
|
|
433
|
+
void active.handle?.close().catch(() => { });
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
async handleUploadFrame(session, upload, frame) {
|
|
437
|
+
if (upload.done)
|
|
438
|
+
return; // released while this frame was queued
|
|
439
|
+
let stage = "write";
|
|
440
|
+
try {
|
|
441
|
+
switch (frame.opcode) {
|
|
442
|
+
case "Begin": {
|
|
443
|
+
if (frame.meta.transferId !== upload.transferId)
|
|
444
|
+
return; // mismatched transfer
|
|
445
|
+
stage = "mkdir";
|
|
446
|
+
await mkdir(dirname(upload.targetPath), { recursive: true });
|
|
447
|
+
stage = "open";
|
|
448
|
+
upload.handle = await open(upload.targetPath, "w");
|
|
135
449
|
return;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
450
|
+
}
|
|
451
|
+
case "Chunk": {
|
|
452
|
+
if (!upload.handle)
|
|
453
|
+
return;
|
|
454
|
+
await upload.handle.write(Buffer.from(frame.data));
|
|
455
|
+
upload.bytesWritten += frame.data.length;
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
case "End": {
|
|
459
|
+
if (!upload.handle)
|
|
460
|
+
return this.finishUpload(session, frame.stream, upload, "no_begin");
|
|
461
|
+
stage = "close";
|
|
462
|
+
const handle = upload.handle;
|
|
463
|
+
upload.handle = null;
|
|
464
|
+
await handle.close();
|
|
465
|
+
return this.finishUpload(session, frame.stream, upload);
|
|
466
|
+
}
|
|
467
|
+
case "Error": {
|
|
468
|
+
await upload.handle?.close();
|
|
469
|
+
upload.handle = null;
|
|
470
|
+
return this.finishUpload(session, frame.stream, upload, frame.message || "client_error");
|
|
471
|
+
}
|
|
149
472
|
}
|
|
150
473
|
}
|
|
474
|
+
catch (err) {
|
|
475
|
+
// A failed Begin/Chunk/End: report it, free the stream, and leave whatever was written on
|
|
476
|
+
// disk (deleting a partial file is a separate decision, see the spec).
|
|
477
|
+
await upload.handle?.close().catch(() => { });
|
|
478
|
+
upload.handle = null;
|
|
479
|
+
this.finishUpload(session, frame.stream, upload, failureText(stage, err));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
/** Release the stream; push `file_upload_complete` if the client asked for it. */
|
|
483
|
+
finishUpload(session, stream, upload, error) {
|
|
484
|
+
if (upload.done)
|
|
485
|
+
return;
|
|
486
|
+
upload.done = true;
|
|
487
|
+
if (this.uploads.get(stream) === upload)
|
|
488
|
+
this.uploads.delete(stream);
|
|
489
|
+
if (!upload.ack || !session.isOpen)
|
|
490
|
+
return;
|
|
491
|
+
const message = {
|
|
492
|
+
type: "file_upload_complete",
|
|
493
|
+
transferId: upload.transferId,
|
|
494
|
+
stream,
|
|
495
|
+
ok: error === undefined,
|
|
496
|
+
bytesWritten: upload.bytesWritten,
|
|
497
|
+
...(error === undefined ? {} : { error }),
|
|
498
|
+
};
|
|
499
|
+
session.send({ type: "session", message });
|
|
151
500
|
}
|
|
152
501
|
}
|
|
153
502
|
//# sourceMappingURL=file-transfer.js.map
|
package/dist/files/index.d.ts
CHANGED
package/dist/files/index.js
CHANGED
|
@@ -19,6 +19,10 @@ function toClientInfo(session) {
|
|
|
19
19
|
return {
|
|
20
20
|
clientId: session.clientId,
|
|
21
21
|
clientType: session.clientType,
|
|
22
|
+
...(session.platform !== undefined ? { platform: session.platform } : {}),
|
|
23
|
+
...(session.agentStreams !== undefined && session.agentStreams !== "all"
|
|
24
|
+
? { agentStreams: session.agentStreams }
|
|
25
|
+
: {}),
|
|
22
26
|
...(session.appVersion !== undefined ? { appVersion: session.appVersion } : {}),
|
|
23
27
|
connectedAt: session.connectedAt,
|
|
24
28
|
...(session.remoteAddress !== undefined ? { remoteAddress: session.remoteAddress } : {}),
|
|
@@ -17,6 +17,8 @@ export interface TestClient {
|
|
|
17
17
|
ws: WebSocket;
|
|
18
18
|
/** Every top-level frame received so far, in arrival order (the handshake `status` first). */
|
|
19
19
|
frames: WireFrame[];
|
|
20
|
+
/** Every binary frame received so far (file-transfer / terminal streams), in arrival order. */
|
|
21
|
+
binaryFrames: Uint8Array[];
|
|
20
22
|
/** The handshake `status` frame. */
|
|
21
23
|
status: WireFrame;
|
|
22
24
|
/** Sends a raw top-level frame as JSON. */
|
|
@@ -22,9 +22,14 @@ export async function connectTestClient(port, options = {}) {
|
|
|
22
22
|
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
|
23
23
|
const frames = [];
|
|
24
24
|
const replies = new Map();
|
|
25
|
+
const binaryFrames = [];
|
|
25
26
|
const handshake = Promise.withResolvers();
|
|
26
27
|
ws.once("error", handshake.reject);
|
|
27
|
-
ws.on("message", (data) => {
|
|
28
|
+
ws.on("message", (data, isBinary) => {
|
|
29
|
+
if (isBinary) {
|
|
30
|
+
binaryFrames.push(new Uint8Array(data));
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
28
33
|
const frame = JSON.parse(data.toString("utf8"));
|
|
29
34
|
frames.push(frame);
|
|
30
35
|
const requestId = messageOf(frame).requestId;
|
|
@@ -73,6 +78,7 @@ export async function connectTestClient(port, options = {}) {
|
|
|
73
78
|
return {
|
|
74
79
|
ws,
|
|
75
80
|
frames,
|
|
81
|
+
binaryFrames,
|
|
76
82
|
status,
|
|
77
83
|
send: (frame) => ws.send(JSON.stringify(frame)),
|
|
78
84
|
rpcFrame,
|