@rome-os/node-core 0.1.4 → 0.1.6
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 +10 -1
- package/dist/audit.d.ts +112 -0
- package/dist/audit.js +506 -0
- package/dist/cloud.d.ts +1 -1
- package/dist/cloud.js +16 -5
- package/dist/connect.d.ts +2 -0
- package/dist/connect.js +9 -5
- package/dist/connector.d.ts +2 -0
- package/dist/connector.js +16 -0
- package/dist/daemon-client.d.ts +2 -0
- package/dist/daemon-client.js +34 -0
- package/dist/daemon.js +23 -0
- package/dist/devices-status.d.ts +5 -0
- package/dist/devices-status.js +7 -0
- package/dist/executor.d.ts +8 -4
- package/dist/executor.js +259 -91
- package/dist/host.d.ts +1 -0
- package/dist/host.js +1 -0
- package/dist/storage.d.ts +1 -0
- package/dist/storage.js +4 -2
- package/dist/transfer.d.ts +15 -1
- package/dist/transfer.js +44 -16
- package/package.json +1 -1
package/dist/executor.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import {
|
|
2
|
+
import { readdir, realpath, stat } from "node:fs/promises";
|
|
3
|
+
import { homedir, platform } from "node:os";
|
|
4
|
+
import { dirname, resolve } from "node:path";
|
|
3
5
|
import { actionError, isRecord } from "./actions.js";
|
|
6
|
+
import { AUDIT_PUSH_BYTES } from "./audit.js";
|
|
4
7
|
import { decodeMeta, encodeMeta, FRAME_TYPE, frameFits, MAX_FRAME_BYTES, } from "./frame.js";
|
|
5
8
|
import { parseTransferMessage, TRANSFER_VERSION, TransferHost } from "./transfer.js";
|
|
6
9
|
function execInput(input) {
|
|
@@ -19,6 +22,57 @@ function execInput(input) {
|
|
|
19
22
|
cwd: input.cwd,
|
|
20
23
|
};
|
|
21
24
|
}
|
|
25
|
+
export const FS_LIST_LIMIT = 1000;
|
|
26
|
+
const typeOrder = { dir: 0, symlink: 1, file: 2, other: 3 };
|
|
27
|
+
function fsError(error) {
|
|
28
|
+
const code = isRecord(error) ? error.code : undefined;
|
|
29
|
+
if (code === "ENOENT")
|
|
30
|
+
return actionError("not_found", "The folder does not exist.");
|
|
31
|
+
if (code === "EACCES" || code === "EPERM")
|
|
32
|
+
return actionError("permission_denied", "The computer's user cannot read this folder.");
|
|
33
|
+
if (code === "ENOTDIR")
|
|
34
|
+
return actionError("not_a_directory", "The path is not a folder.");
|
|
35
|
+
return actionError("fs_failed", "Could not read the folder.");
|
|
36
|
+
}
|
|
37
|
+
async function listFolder(input) {
|
|
38
|
+
const raw = input === undefined || input === null ? {} : input;
|
|
39
|
+
if (!isRecord(raw) ||
|
|
40
|
+
(raw.path !== undefined &&
|
|
41
|
+
(typeof raw.path !== "string" || !raw.path || raw.path.includes("\0"))))
|
|
42
|
+
return actionError("invalid_args", "fs.list takes an optional non-empty string path.");
|
|
43
|
+
let path;
|
|
44
|
+
let entries;
|
|
45
|
+
try {
|
|
46
|
+
path = await realpath(resolve(homedir(), raw.path ?? "."));
|
|
47
|
+
if (!(await stat(path)).isDirectory())
|
|
48
|
+
return actionError("not_a_directory", "The path is not a folder.");
|
|
49
|
+
entries = (await readdir(path, { withFileTypes: true })).map((entry) => ({
|
|
50
|
+
name: entry.name,
|
|
51
|
+
type: entry.isDirectory()
|
|
52
|
+
? "dir"
|
|
53
|
+
: entry.isFile()
|
|
54
|
+
? "file"
|
|
55
|
+
: entry.isSymbolicLink()
|
|
56
|
+
? "symlink"
|
|
57
|
+
: "other",
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
return fsError(error);
|
|
62
|
+
}
|
|
63
|
+
entries.sort((a, b) => typeOrder[a.type] - typeOrder[b.type] || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
64
|
+
const parent = dirname(path);
|
|
65
|
+
return {
|
|
66
|
+
type: "response",
|
|
67
|
+
ok: true,
|
|
68
|
+
result: {
|
|
69
|
+
path,
|
|
70
|
+
parent: parent === path ? null : parent,
|
|
71
|
+
entries: entries.slice(0, FS_LIST_LIMIT),
|
|
72
|
+
truncated: entries.length > FS_LIST_LIMIT,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
22
76
|
export function redactBytes(data, secrets) {
|
|
23
77
|
for (const secret of secrets) {
|
|
24
78
|
const needle = Buffer.from(secret);
|
|
@@ -29,32 +83,68 @@ export function redactBytes(data, secrets) {
|
|
|
29
83
|
}
|
|
30
84
|
return data;
|
|
31
85
|
}
|
|
32
|
-
|
|
86
|
+
function childEnvironment() {
|
|
87
|
+
return Object.fromEntries(Object.entries(process.env).filter(([key]) => !/^(?:ROME_.*(?:TOKEN|SECRET|CREDENTIAL)|NODE_OPTIONS)$/i.test(key)));
|
|
88
|
+
}
|
|
89
|
+
function outcomeOf(response, interrupted) {
|
|
90
|
+
if (interrupted)
|
|
91
|
+
return "interrupted";
|
|
92
|
+
if (!response.ok)
|
|
93
|
+
return "error";
|
|
94
|
+
const result = response.result;
|
|
95
|
+
return isRecord(result) &&
|
|
96
|
+
Object.hasOwn(result, "exitCode") &&
|
|
97
|
+
(result.exitCode !== 0 || result.signal !== null)
|
|
98
|
+
? "exit_nonzero"
|
|
99
|
+
: "ok";
|
|
100
|
+
}
|
|
101
|
+
export function createExecutor(name, audit, send, secrets = [], sendFrame = () => false) {
|
|
33
102
|
const children = new Set();
|
|
34
|
-
const transfers = new TransferHost();
|
|
103
|
+
const transfers = new TransferHost({ keepBytes: AUDIT_PUSH_BYTES });
|
|
35
104
|
let generation = 0;
|
|
36
|
-
const info = () => ({
|
|
105
|
+
const info = async () => ({
|
|
37
106
|
type: "response",
|
|
38
107
|
ok: true,
|
|
39
108
|
result: {
|
|
40
109
|
name,
|
|
41
110
|
platform: platform() === "darwin" ? "macos" : platform() === "win32" ? "windows" : platform(),
|
|
42
|
-
actions: ["system.info", "exec"],
|
|
111
|
+
actions: ["system.info", "exec", "fs.list"],
|
|
43
112
|
frameVersion: 1,
|
|
44
113
|
transferVersion: TRANSFER_VERSION,
|
|
114
|
+
audit: await audit.status(),
|
|
45
115
|
},
|
|
46
116
|
});
|
|
117
|
+
const refused = () => actionError("audit_unavailable", `This computer could not record the request in its audit log, so it did not run it. ${audit.hint}`);
|
|
118
|
+
const replaced = () => actionError("connection_replaced", "The connection was replaced before the request ran.");
|
|
119
|
+
const startRecord = (action, args, extra) => ({
|
|
120
|
+
...extra,
|
|
121
|
+
args,
|
|
122
|
+
...(action === "exec" ? { envKeys: Object.keys(childEnvironment()).sort() } : {}),
|
|
123
|
+
});
|
|
124
|
+
function endRecord(action, startSeq, started, response, interrupted, notes, replyBytes) {
|
|
125
|
+
const fields = {
|
|
126
|
+
durationMs: Date.now() - started,
|
|
127
|
+
...notes.fields,
|
|
128
|
+
...(response.ok ? {} : { errorCode: response.error.code }),
|
|
129
|
+
replyBytes: interrupted ? 0 : replyBytes,
|
|
130
|
+
};
|
|
131
|
+
if (action === "fs.list" && response.ok && isRecord(response.result)) {
|
|
132
|
+
fields.path = response.result.path;
|
|
133
|
+
fields.entries = response.result.entries;
|
|
134
|
+
fields.truncated = response.result.truncated;
|
|
135
|
+
}
|
|
136
|
+
audit.end(startSeq, action, outcomeOf(response, interrupted), fields, notes.content, secrets);
|
|
137
|
+
}
|
|
47
138
|
const invalidExec = () => actionError("invalid_args", "exec requires command, optional string args[], and optional cwd.");
|
|
48
|
-
function runProgram({ command, args, cwd }, stdin) {
|
|
139
|
+
function runProgram({ command, args, cwd }, notes, stdin) {
|
|
49
140
|
if (children.size >= 8)
|
|
50
141
|
return Promise.resolve(actionError("busy", "The computer is already running eight programs."));
|
|
51
142
|
return new Promise((resolve) => {
|
|
52
|
-
const environment = Object.fromEntries(Object.entries(process.env).filter(([key]) => !/^(?:ROME_.*(?:TOKEN|SECRET|CREDENTIAL)|NODE_OPTIONS)$/i.test(key)));
|
|
53
143
|
let child;
|
|
54
144
|
try {
|
|
55
145
|
child = spawn(command, args, {
|
|
56
146
|
cwd,
|
|
57
|
-
env:
|
|
147
|
+
env: childEnvironment(),
|
|
58
148
|
shell: false,
|
|
59
149
|
windowsHide: true,
|
|
60
150
|
stdio: [stdin ? "pipe" : "ignore", "pipe", "pipe"],
|
|
@@ -65,6 +155,7 @@ export function createExecutor(name, send, secrets = [], sendFrame = () => false
|
|
|
65
155
|
return;
|
|
66
156
|
}
|
|
67
157
|
children.add(child);
|
|
158
|
+
notes.fields.pid = child.pid ?? null;
|
|
68
159
|
if (stdin && child.stdin) {
|
|
69
160
|
child.stdin.on("error", () => { });
|
|
70
161
|
child.stdin.end(stdin);
|
|
@@ -81,24 +172,28 @@ export function createExecutor(name, send, secrets = [], sendFrame = () => false
|
|
|
81
172
|
});
|
|
82
173
|
child.once("close", (exitCode, signal) => {
|
|
83
174
|
children.delete(child);
|
|
84
|
-
|
|
175
|
+
const result = {
|
|
85
176
|
exitCode,
|
|
86
177
|
signal,
|
|
87
178
|
stdout: Buffer.concat(output.stdout),
|
|
88
179
|
stderr: Buffer.concat(output.stderr),
|
|
89
|
-
}
|
|
180
|
+
};
|
|
181
|
+
notes.fields.exitCode = exitCode;
|
|
182
|
+
notes.fields.signal = signal;
|
|
183
|
+
notes.content = { stdout: result.stdout, stderr: result.stderr };
|
|
184
|
+
resolve(result);
|
|
90
185
|
});
|
|
91
186
|
});
|
|
92
187
|
}
|
|
93
188
|
const redactText = (data) => secrets.reduce((text, secret) => text.replaceAll(secret, "[redacted]"), data.toString("utf8"));
|
|
94
189
|
const isOutput = (value) => !("type" in value);
|
|
95
190
|
const handlers = {
|
|
96
|
-
"system.info":
|
|
97
|
-
exec: async (input) => {
|
|
191
|
+
"system.info": () => info(),
|
|
192
|
+
exec: async (input, notes) => {
|
|
98
193
|
const parsed = execInput(input);
|
|
99
194
|
if (!parsed)
|
|
100
195
|
return invalidExec();
|
|
101
|
-
const output = await runProgram(parsed);
|
|
196
|
+
const output = await runProgram(parsed, notes);
|
|
102
197
|
if (!isOutput(output))
|
|
103
198
|
return output;
|
|
104
199
|
return {
|
|
@@ -113,15 +208,17 @@ export function createExecutor(name, send, secrets = [], sendFrame = () => false
|
|
|
113
208
|
},
|
|
114
209
|
};
|
|
115
210
|
},
|
|
211
|
+
"fs.list": listFolder,
|
|
116
212
|
};
|
|
117
213
|
const empty = new Uint8Array();
|
|
118
214
|
const frameHandlers = {
|
|
119
|
-
"system.info": async () => ({ response: info(), body: empty }),
|
|
120
|
-
|
|
215
|
+
"system.info": async () => ({ response: await info(), body: empty }),
|
|
216
|
+
"fs.list": async (input) => ({ response: await listFolder(input), body: empty }),
|
|
217
|
+
exec: async (input, body, notes) => {
|
|
121
218
|
const parsed = execInput(input);
|
|
122
219
|
if (!parsed)
|
|
123
220
|
return { response: invalidExec(), body: empty };
|
|
124
|
-
const output = await runProgram(parsed, body);
|
|
221
|
+
const output = await runProgram(parsed, notes, body);
|
|
125
222
|
if (!isOutput(output))
|
|
126
223
|
return { response: output, body: empty };
|
|
127
224
|
return {
|
|
@@ -139,81 +236,152 @@ export function createExecutor(name, send, secrets = [], sendFrame = () => false
|
|
|
139
236
|
};
|
|
140
237
|
},
|
|
141
238
|
};
|
|
239
|
+
let running = 0;
|
|
240
|
+
const drained = [];
|
|
241
|
+
async function track(work) {
|
|
242
|
+
running++;
|
|
243
|
+
try {
|
|
244
|
+
await work();
|
|
245
|
+
}
|
|
246
|
+
finally {
|
|
247
|
+
if (--running === 0)
|
|
248
|
+
for (const resolve of drained.splice(0))
|
|
249
|
+
resolve();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
142
252
|
return {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const current = generation;
|
|
148
|
-
const handler = Object.hasOwn(handlers, request.action)
|
|
149
|
-
? handlers[request.action]
|
|
150
|
-
: undefined;
|
|
151
|
-
const payload = handler
|
|
152
|
-
? await handler(request.args).catch(() => actionError("exec_failed", "The action failed."))
|
|
153
|
-
: actionError("unsupported_action", "The computer does not support this action.");
|
|
154
|
-
const reply = { id: message.id, to: message.from, payload };
|
|
155
|
-
if (current === generation)
|
|
156
|
-
send(reply);
|
|
157
|
-
},
|
|
158
|
-
async receiveFrame(frame) {
|
|
159
|
-
if (frame.type !== FRAME_TYPE.request)
|
|
160
|
-
return;
|
|
161
|
-
const request = decodeMeta(frame.meta);
|
|
162
|
-
const transfer = parseTransferMessage(request);
|
|
163
|
-
if (transfer) {
|
|
164
|
-
transfers.receive(frame.id, frame.peer, transfer, frame.body);
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
if (!isRecord(request) || request.type !== "request" || typeof request.action !== "string")
|
|
168
|
-
return;
|
|
169
|
-
const current = generation;
|
|
170
|
-
if (request.action === "transfer.open") {
|
|
171
|
-
void transfers.open(frame.id, frame.peer, request.args, (meta, body = empty) => {
|
|
172
|
-
if (current !== generation)
|
|
173
|
-
return false;
|
|
174
|
-
return sendFrame({
|
|
175
|
-
type: FRAME_TYPE.response,
|
|
176
|
-
id: frame.id,
|
|
177
|
-
peer: frame.peer,
|
|
178
|
-
meta: encodeMeta(meta),
|
|
179
|
-
body,
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
const handler = Object.hasOwn(frameHandlers, request.action)
|
|
185
|
-
? frameHandlers[request.action]
|
|
186
|
-
: undefined;
|
|
187
|
-
const result = handler
|
|
188
|
-
? await handler(request.args, frame.body).catch(() => ({
|
|
189
|
-
response: actionError("exec_failed", "The action failed."),
|
|
190
|
-
body: empty,
|
|
191
|
-
}))
|
|
192
|
-
: {
|
|
193
|
-
response: actionError("unsupported_action", "The computer does not support this action."),
|
|
194
|
-
body: empty,
|
|
195
|
-
};
|
|
196
|
-
if (current !== generation)
|
|
197
|
-
return;
|
|
198
|
-
let meta = encodeMeta(result.response);
|
|
199
|
-
let body = result.body;
|
|
200
|
-
if (!frameFits(meta.byteLength, body.byteLength)) {
|
|
201
|
-
meta = encodeMeta(actionError("output_too_large", `The output does not fit in one ${MAX_FRAME_BYTES / 1024 / 1024} MiB frame and was discarded. The program ran. Use rome-node cp to copy large files.`));
|
|
202
|
-
body = empty;
|
|
203
|
-
}
|
|
204
|
-
sendFrame({ type: FRAME_TYPE.response, id: frame.id, peer: frame.peer, meta, body });
|
|
205
|
-
},
|
|
206
|
-
disconnect() {
|
|
207
|
-
generation++;
|
|
208
|
-
transfers.abortAll();
|
|
209
|
-
for (const child of children) {
|
|
210
|
-
child.kill("SIGTERM");
|
|
211
|
-
const timer = setTimeout(() => {
|
|
212
|
-
if (child.exitCode === null && child.signalCode === null)
|
|
213
|
-
child.kill("SIGKILL");
|
|
214
|
-
}, 1000);
|
|
215
|
-
timer.unref();
|
|
216
|
-
}
|
|
217
|
-
},
|
|
253
|
+
receive: (message) => track(() => receive(message)),
|
|
254
|
+
receiveFrame: (frame) => track(() => receiveFrame(frame)),
|
|
255
|
+
idle: () => running ? new Promise((resolve) => drained.push(resolve)) : Promise.resolve(),
|
|
256
|
+
disconnect,
|
|
218
257
|
};
|
|
258
|
+
async function receive(message) {
|
|
259
|
+
const request = message.payload;
|
|
260
|
+
if (!isRecord(request) || request.type !== "request" || typeof request.action !== "string")
|
|
261
|
+
return;
|
|
262
|
+
const current = generation;
|
|
263
|
+
const action = request.action;
|
|
264
|
+
const handler = Object.hasOwn(handlers, action) ? handlers[action] : undefined;
|
|
265
|
+
const notes = { fields: {}, content: {} };
|
|
266
|
+
const started = Date.now();
|
|
267
|
+
const seq = action === "system.info"
|
|
268
|
+
? 0
|
|
269
|
+
: await audit.start(action, startRecord(action, request.args, {
|
|
270
|
+
requestId: message.id,
|
|
271
|
+
from: message.from,
|
|
272
|
+
transport: "json",
|
|
273
|
+
generation: current,
|
|
274
|
+
}), {}, secrets);
|
|
275
|
+
const payload = seq === null
|
|
276
|
+
? refused()
|
|
277
|
+
: current !== generation
|
|
278
|
+
? replaced()
|
|
279
|
+
: handler
|
|
280
|
+
? await handler(request.args, notes).catch(() => actionError("exec_failed", "The action failed."))
|
|
281
|
+
: actionError("unsupported_action", "The computer does not support this action.");
|
|
282
|
+
const reply = { id: message.id, to: message.from, payload };
|
|
283
|
+
const interrupted = current !== generation;
|
|
284
|
+
if (seq)
|
|
285
|
+
endRecord(action, seq, started, payload, interrupted, notes, interrupted ? 0 : Buffer.byteLength(JSON.stringify(reply)));
|
|
286
|
+
if (!interrupted)
|
|
287
|
+
send(reply);
|
|
288
|
+
}
|
|
289
|
+
async function receiveFrame(frame) {
|
|
290
|
+
if (frame.type !== FRAME_TYPE.request)
|
|
291
|
+
return;
|
|
292
|
+
const request = decodeMeta(frame.meta);
|
|
293
|
+
const transfer = parseTransferMessage(request);
|
|
294
|
+
if (transfer) {
|
|
295
|
+
transfers.receive(frame.id, frame.peer, transfer, frame.body);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (!isRecord(request) || request.type !== "request" || typeof request.action !== "string")
|
|
299
|
+
return;
|
|
300
|
+
const current = generation;
|
|
301
|
+
const action = request.action;
|
|
302
|
+
const respond = (response) => current === generation &&
|
|
303
|
+
sendFrame({
|
|
304
|
+
type: FRAME_TYPE.response,
|
|
305
|
+
id: frame.id,
|
|
306
|
+
peer: frame.peer,
|
|
307
|
+
meta: encodeMeta(response),
|
|
308
|
+
body: empty,
|
|
309
|
+
});
|
|
310
|
+
const started = Date.now();
|
|
311
|
+
const seq = action === "system.info"
|
|
312
|
+
? 0
|
|
313
|
+
: await audit.start(action, startRecord(action, request.args, {
|
|
314
|
+
requestId: frame.id,
|
|
315
|
+
from: frame.peer,
|
|
316
|
+
transport: "binary",
|
|
317
|
+
generation: current,
|
|
318
|
+
}), { stdin: frame.body }, secrets);
|
|
319
|
+
if (seq === null) {
|
|
320
|
+
respond(refused());
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (current !== generation) {
|
|
324
|
+
endRecord(action, seq, started, replaced(), true, { fields: {}, content: {} }, 0);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (action === "transfer.open") {
|
|
328
|
+
const transfer = transfers.open(frame.id, frame.peer, request.args, (meta, body = empty) => {
|
|
329
|
+
if (current !== generation)
|
|
330
|
+
return false;
|
|
331
|
+
return sendFrame({
|
|
332
|
+
type: FRAME_TYPE.response,
|
|
333
|
+
id: frame.id,
|
|
334
|
+
peer: frame.peer,
|
|
335
|
+
meta: encodeMeta(meta),
|
|
336
|
+
body,
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
void track(() => transfer.then(({ code, file, ...fields }) => {
|
|
340
|
+
const response = code
|
|
341
|
+
? actionError(code, "")
|
|
342
|
+
: { type: "response", ok: true, result: null };
|
|
343
|
+
endRecord(action, seq, started, response, current !== generation, { fields, content: file ? { file } : {} }, 0);
|
|
344
|
+
}));
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const handler = Object.hasOwn(frameHandlers, action) ? frameHandlers[action] : undefined;
|
|
348
|
+
const notes = { fields: {}, content: {} };
|
|
349
|
+
const result = handler
|
|
350
|
+
? await handler(request.args, frame.body, notes).catch(() => ({
|
|
351
|
+
response: actionError("exec_failed", "The action failed."),
|
|
352
|
+
body: empty,
|
|
353
|
+
}))
|
|
354
|
+
: {
|
|
355
|
+
response: actionError("unsupported_action", "The computer does not support this action."),
|
|
356
|
+
body: empty,
|
|
357
|
+
};
|
|
358
|
+
const interrupted = current !== generation;
|
|
359
|
+
let response = result.response;
|
|
360
|
+
let meta = encodeMeta(response);
|
|
361
|
+
let body = result.body;
|
|
362
|
+
if (!frameFits(meta.byteLength, body.byteLength)) {
|
|
363
|
+
response = actionError("output_too_large", `The output does not fit in one ${MAX_FRAME_BYTES / 1024 / 1024} MiB frame and was discarded. The program ran. Use rome-node cp to copy large files.`);
|
|
364
|
+
meta = encodeMeta(response);
|
|
365
|
+
body = empty;
|
|
366
|
+
}
|
|
367
|
+
if (seq)
|
|
368
|
+
endRecord(action, seq, started, response, interrupted, notes, meta.byteLength + body.byteLength);
|
|
369
|
+
if (interrupted)
|
|
370
|
+
return;
|
|
371
|
+
sendFrame({ type: FRAME_TYPE.response, id: frame.id, peer: frame.peer, meta, body });
|
|
372
|
+
}
|
|
373
|
+
function disconnect() {
|
|
374
|
+
generation++;
|
|
375
|
+
transfers.abortAll();
|
|
376
|
+
for (const child of children) {
|
|
377
|
+
child.kill("SIGTERM");
|
|
378
|
+
const timer = setTimeout(() => {
|
|
379
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
380
|
+
child.kill("SIGKILL");
|
|
381
|
+
child.stdout?.destroy();
|
|
382
|
+
child.stderr?.destroy();
|
|
383
|
+
}, 1000);
|
|
384
|
+
timer.unref();
|
|
385
|
+
}
|
|
386
|
+
}
|
|
219
387
|
}
|
package/dist/host.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ export { connectHost, defaultDeviceName, type HostCredential, type HostEvent, ty
|
|
|
2
2
|
export { loginDevice, type DeviceSession } from "./login.js";
|
|
3
3
|
export { loginDeviceCode, type DeviceAuthorizationPrompt } from "./device-authorization.js";
|
|
4
4
|
export { createFileCredentialStore, type CredentialStore } from "./storage.js";
|
|
5
|
+
export { AuditStore, type AuditItem, type AuditQuery, type AuditResult, type AuditStats, type AuditStatus, type AuditSummary, type ContentName, } from "./audit.js";
|
package/dist/host.js
CHANGED
package/dist/storage.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export declare function readPrivateJson(path: string): Promise<unknown>;
|
|
2
|
+
export declare function ensurePrivateDirectory(dir: string): Promise<void>;
|
|
2
3
|
export declare function writePrivateJson(path: string, value: unknown): Promise<void>;
|
|
3
4
|
export interface CredentialStore<T> {
|
|
4
5
|
load(): Promise<unknown>;
|
package/dist/storage.js
CHANGED
|
@@ -13,8 +13,7 @@ export async function readPrivateJson(path) {
|
|
|
13
13
|
throw new Error("Could not read the local credential file.");
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
|
-
export async function
|
|
17
|
-
const dir = dirname(path);
|
|
16
|
+
export async function ensurePrivateDirectory(dir) {
|
|
18
17
|
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
19
18
|
if (process.platform === "win32") {
|
|
20
19
|
const run = promisify(execFile);
|
|
@@ -37,6 +36,9 @@ export async function writePrivateJson(path, value) {
|
|
|
37
36
|
}
|
|
38
37
|
else
|
|
39
38
|
await chmod(dir, 0o700);
|
|
39
|
+
}
|
|
40
|
+
export async function writePrivateJson(path, value) {
|
|
41
|
+
await ensurePrivateDirectory(dirname(path));
|
|
40
42
|
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
41
43
|
try {
|
|
42
44
|
await writeFile(temporary, JSON.stringify(value), { mode: 0o600, flag: "wx" });
|
package/dist/transfer.d.ts
CHANGED
|
@@ -73,13 +73,27 @@ export declare function createPartFile(path: string, id?: string): Promise<{
|
|
|
73
73
|
handle: FileHandle;
|
|
74
74
|
}>;
|
|
75
75
|
export declare function openDestination(path: string): Promise<Destination>;
|
|
76
|
+
export interface TransferResult {
|
|
77
|
+
code?: string;
|
|
78
|
+
direction?: "push" | "pull";
|
|
79
|
+
path?: string;
|
|
80
|
+
bytes?: number;
|
|
81
|
+
sha256?: string;
|
|
82
|
+
ackedBytes?: number;
|
|
83
|
+
replaced?: {
|
|
84
|
+
size: number;
|
|
85
|
+
mtimeMs: number;
|
|
86
|
+
} | null;
|
|
87
|
+
file?: Buffer;
|
|
88
|
+
}
|
|
76
89
|
export declare class TransferHost {
|
|
77
90
|
private options;
|
|
78
91
|
private transfers;
|
|
79
92
|
constructor(options?: TransferOptions & {
|
|
80
93
|
limit?: number;
|
|
94
|
+
keepBytes?: number;
|
|
81
95
|
});
|
|
82
|
-
open(id: string, peer: string, args: unknown, send: TransferSend): Promise<
|
|
96
|
+
open(id: string, peer: string, args: unknown, send: TransferSend): Promise<TransferResult>;
|
|
83
97
|
receive(id: string, peer: string, message: TransferMessage, body: Uint8Array): void;
|
|
84
98
|
abortAll(): void;
|
|
85
99
|
}
|
package/dist/transfer.js
CHANGED
|
@@ -349,30 +349,41 @@ export class TransferHost {
|
|
|
349
349
|
}
|
|
350
350
|
async open(id, peer, args, send) {
|
|
351
351
|
if (this.transfers.has(id))
|
|
352
|
-
return;
|
|
352
|
+
return { code: "duplicate_transfer" };
|
|
353
353
|
const request = parseOpen(args);
|
|
354
354
|
if (!request) {
|
|
355
355
|
send(actionError("invalid_args", "transfer.open requires direction push or pull, a path, and a size for push."));
|
|
356
|
-
return;
|
|
356
|
+
return { code: "invalid_args" };
|
|
357
357
|
}
|
|
358
358
|
if (this.transfers.size >= (this.options.limit ?? MAX_HOST_TRANSFERS)) {
|
|
359
359
|
send(actionError("busy", "The computer is already running its maximum number of transfers."));
|
|
360
|
-
return;
|
|
360
|
+
return { code: "busy" };
|
|
361
361
|
}
|
|
362
|
-
const
|
|
362
|
+
const { direction, path } = request;
|
|
363
|
+
const keep = direction === "push" && (request.size ?? 0) <= (this.options.keepBytes ?? 0);
|
|
364
|
+
const entry = { peer, canceled: false, acked: 0, chunks: keep ? [] : undefined };
|
|
365
|
+
const result = { direction, path };
|
|
363
366
|
this.transfers.set(id, entry);
|
|
364
367
|
try {
|
|
365
|
-
if (
|
|
366
|
-
const
|
|
367
|
-
|
|
368
|
-
|
|
368
|
+
if (direction === "push") {
|
|
369
|
+
const existing = await stat(path).catch(() => null);
|
|
370
|
+
result.replaced = existing?.isFile()
|
|
371
|
+
? { size: existing.size, mtimeMs: existing.mtimeMs }
|
|
372
|
+
: null;
|
|
373
|
+
const destination = await openDestination(path);
|
|
374
|
+
if (entry.canceled) {
|
|
375
|
+
await destination.cancel();
|
|
376
|
+
return { ...result, code: "canceled" };
|
|
377
|
+
}
|
|
369
378
|
entry.endpoint = destination.start(send, { ...this.options, size: request.size });
|
|
370
379
|
send({ type: "response", ok: true, result: {} });
|
|
371
380
|
}
|
|
372
381
|
else {
|
|
373
|
-
const source = await openSource(
|
|
374
|
-
if (entry.canceled)
|
|
375
|
-
|
|
382
|
+
const source = await openSource(path);
|
|
383
|
+
if (entry.canceled) {
|
|
384
|
+
await source.close();
|
|
385
|
+
return { ...result, code: "canceled" };
|
|
386
|
+
}
|
|
376
387
|
send({
|
|
377
388
|
type: "response",
|
|
378
389
|
ok: true,
|
|
@@ -380,13 +391,26 @@ export class TransferHost {
|
|
|
380
391
|
});
|
|
381
392
|
entry.endpoint = source.start(send, this.options);
|
|
382
393
|
}
|
|
383
|
-
await entry.endpoint.result
|
|
394
|
+
const summary = await entry.endpoint.result;
|
|
395
|
+
return {
|
|
396
|
+
...result,
|
|
397
|
+
bytes: summary.size,
|
|
398
|
+
sha256: summary.sha256,
|
|
399
|
+
...(direction === "pull" ? { ackedBytes: entry.acked } : {}),
|
|
400
|
+
...(entry.chunks ? { file: Buffer.concat(entry.chunks) } : {}),
|
|
401
|
+
};
|
|
384
402
|
}
|
|
385
403
|
catch (error) {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
404
|
+
const failure = error instanceof TransferError
|
|
405
|
+
? error
|
|
406
|
+
: new TransferError("io_error", "The transfer failed.");
|
|
407
|
+
if (!entry.canceled && !entry.endpoint)
|
|
408
|
+
send(actionError(failure.code, failure.message));
|
|
409
|
+
return {
|
|
410
|
+
...result,
|
|
411
|
+
code: failure.code,
|
|
412
|
+
...(direction === "pull" ? { ackedBytes: entry.acked } : {}),
|
|
413
|
+
};
|
|
390
414
|
}
|
|
391
415
|
finally {
|
|
392
416
|
this.transfers.delete(id);
|
|
@@ -396,6 +420,10 @@ export class TransferHost {
|
|
|
396
420
|
const entry = this.transfers.get(id);
|
|
397
421
|
if (!entry || entry.peer !== peer)
|
|
398
422
|
return;
|
|
423
|
+
if (message.kind === "data")
|
|
424
|
+
entry.chunks?.push(Buffer.from(body));
|
|
425
|
+
else if (message.kind === "ack")
|
|
426
|
+
entry.acked = Math.max(entry.acked, message.offset);
|
|
399
427
|
if (entry.endpoint)
|
|
400
428
|
entry.endpoint.receive(message, body);
|
|
401
429
|
else if (message.kind === "abort")
|