@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/cloud.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { isRecord } from "./actions.js";
|
|
2
2
|
import { parseGatewayUrl } from "./gateway-url.js";
|
|
3
|
+
const messages = {
|
|
4
|
+
invalid_device_session: "Device authorization is invalid.",
|
|
5
|
+
device_not_found: "No device with this ID exists in the account.",
|
|
6
|
+
invalid_device_name: "Use a name of 1 to 80 characters without control characters.",
|
|
7
|
+
revocation_pending: "The device is revoked. Rome Cloud deletes it after the Gateway confirms the revocation. Remove it again later.",
|
|
8
|
+
};
|
|
9
|
+
const deviceErrors = new Set(["device_not_found", "invalid_device_name", "revocation_pending"]);
|
|
3
10
|
export class CloudError extends Error {
|
|
4
11
|
code;
|
|
5
12
|
constructor(code) {
|
|
6
|
-
super(code
|
|
7
|
-
? "Device authorization is invalid."
|
|
8
|
-
: "Rome Cloud is unavailable. Retry later.");
|
|
13
|
+
super(messages[code] ?? "Rome Cloud is unavailable. Retry later.");
|
|
9
14
|
this.code = code;
|
|
10
15
|
}
|
|
11
16
|
}
|
|
@@ -21,12 +26,16 @@ export function cloudOrigin(value) {
|
|
|
21
26
|
throw new Error("Use an HTTPS Cloud origin. HTTP is allowed only on loopback.");
|
|
22
27
|
return url.origin;
|
|
23
28
|
}
|
|
24
|
-
export async function cloudRequest(origin, path, token, method = "GET") {
|
|
29
|
+
export async function cloudRequest(origin, path, token, method = "GET", json) {
|
|
25
30
|
let response;
|
|
26
31
|
try {
|
|
27
32
|
response = await fetch(new URL(path, cloudOrigin(origin)), {
|
|
28
33
|
method,
|
|
29
|
-
headers: {
|
|
34
|
+
headers: {
|
|
35
|
+
Authorization: `Bearer ${token}`,
|
|
36
|
+
...(json === undefined ? {} : { "content-type": "application/json" }),
|
|
37
|
+
},
|
|
38
|
+
body: json === undefined ? undefined : JSON.stringify(json),
|
|
30
39
|
redirect: "error",
|
|
31
40
|
signal: AbortSignal.timeout(10_000),
|
|
32
41
|
cache: "no-store",
|
|
@@ -39,6 +48,8 @@ export async function cloudRequest(origin, path, token, method = "GET") {
|
|
|
39
48
|
if (!response.ok) {
|
|
40
49
|
if (response.status === 401 && isRecord(body) && body.error === "invalid_device_session")
|
|
41
50
|
throw new CloudError("invalid_device_session");
|
|
51
|
+
if (isRecord(body) && typeof body.error === "string" && deviceErrors.has(body.error))
|
|
52
|
+
throw new CloudError(body.error);
|
|
42
53
|
throw new CloudError("cloud_unavailable");
|
|
43
54
|
}
|
|
44
55
|
return body;
|
package/dist/connect.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { DeviceSession } from "./login.js";
|
|
2
2
|
import type { CredentialStore } from "./storage.js";
|
|
3
|
+
import { type HostAudit } from "./executor.js";
|
|
3
4
|
export type HostCredential = DeviceSession & {
|
|
4
5
|
name: string;
|
|
5
6
|
};
|
|
@@ -22,6 +23,7 @@ export interface HostOptions {
|
|
|
22
23
|
credentials: CredentialStore<HostCredential>;
|
|
23
24
|
authorize(cloudUrl: string, name: string, signal: AbortSignal): Promise<DeviceSession>;
|
|
24
25
|
onEvent?(event: HostEvent): void;
|
|
26
|
+
audit: HostAudit;
|
|
25
27
|
}
|
|
26
28
|
export declare function connectHost(options: HostOptions): Promise<void>;
|
|
27
29
|
export declare const defaultDeviceName: () => string;
|
package/dist/connect.js
CHANGED
|
@@ -59,7 +59,7 @@ export async function connectHost(options) {
|
|
|
59
59
|
const active = session;
|
|
60
60
|
options.onEvent?.({ type: "device", name: active.name, deviceId: active.deviceId });
|
|
61
61
|
await new Promise((resolve, reject) => {
|
|
62
|
-
const executor = createExecutor(active.name, (message) => connection.send(message), [active.token], (frame) => connection.sendFrame(frame));
|
|
62
|
+
const executor = createExecutor(active.name, options.audit, (message) => connection.send(message), [active.token], (frame) => connection.sendFrame(frame));
|
|
63
63
|
let finished = false;
|
|
64
64
|
const finish = (error) => {
|
|
65
65
|
if (finished)
|
|
@@ -68,10 +68,7 @@ export async function connectHost(options) {
|
|
|
68
68
|
signal.removeEventListener("abort", abort);
|
|
69
69
|
executor.disconnect();
|
|
70
70
|
connection.stop();
|
|
71
|
-
|
|
72
|
-
reject(error);
|
|
73
|
-
else
|
|
74
|
-
resolve();
|
|
71
|
+
void Promise.race([executor.idle(), delay(3000, undefined, { ref: false })]).then(() => error ? reject(error) : resolve());
|
|
75
72
|
};
|
|
76
73
|
const abort = () => finish();
|
|
77
74
|
const connection = connectGateway({
|
|
@@ -94,6 +91,13 @@ export async function connectHost(options) {
|
|
|
94
91
|
},
|
|
95
92
|
onFrame: (frame) => void executor.receiveFrame(frame),
|
|
96
93
|
onStatus: (status) => {
|
|
94
|
+
options.audit.host({
|
|
95
|
+
event: "connection",
|
|
96
|
+
status,
|
|
97
|
+
deviceId: active.deviceId,
|
|
98
|
+
name: active.name,
|
|
99
|
+
cloudOrigin: cloudUrl,
|
|
100
|
+
}, [active.token]);
|
|
97
101
|
options.onEvent?.({ type: "connection", status });
|
|
98
102
|
if (status !== "online" && status !== "connecting")
|
|
99
103
|
executor.disconnect();
|
package/dist/connector.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export declare class DeviceConnector {
|
|
|
22
22
|
private ensure;
|
|
23
23
|
private initialize;
|
|
24
24
|
list(): Promise<unknown>;
|
|
25
|
+
rename(deviceId: string, name: string): Promise<void>;
|
|
26
|
+
remove(deviceId: string): Promise<void>;
|
|
25
27
|
private receive;
|
|
26
28
|
private receiveFrame;
|
|
27
29
|
private admit;
|
package/dist/connector.js
CHANGED
|
@@ -80,6 +80,22 @@ export class DeviceConnector {
|
|
|
80
80
|
throw new CloudError("invalid_response");
|
|
81
81
|
return { items: body.items };
|
|
82
82
|
}
|
|
83
|
+
async rename(deviceId, name) {
|
|
84
|
+
if (this.stopped)
|
|
85
|
+
throw new Error("connector_stopped");
|
|
86
|
+
if (!isUuid(deviceId))
|
|
87
|
+
throw new CloudError("device_not_found");
|
|
88
|
+
const { cloudUrl, token } = this.options.credential;
|
|
89
|
+
await cloudRequest(cloudUrl, `/api/account/devices/${deviceId}`, token, "PATCH", { name });
|
|
90
|
+
}
|
|
91
|
+
async remove(deviceId) {
|
|
92
|
+
if (this.stopped)
|
|
93
|
+
throw new Error("connector_stopped");
|
|
94
|
+
if (!isUuid(deviceId))
|
|
95
|
+
throw new CloudError("device_not_found");
|
|
96
|
+
const { cloudUrl, token } = this.options.credential;
|
|
97
|
+
await cloudRequest(cloudUrl, `/api/account/devices/${deviceId}`, token, "DELETE");
|
|
98
|
+
}
|
|
83
99
|
receive(message) {
|
|
84
100
|
const pending = this.pending.get(message.id);
|
|
85
101
|
if (!pending || pending.binary)
|
package/dist/daemon-client.d.ts
CHANGED
|
@@ -32,6 +32,8 @@ export declare function createNodeClient(config: NodeConfig, options?: NodeClien
|
|
|
32
32
|
getConnectionStatus(): Promise<DaemonStatus | null>;
|
|
33
33
|
getDevicesStatus(): Promise<DevicesStatus | null>;
|
|
34
34
|
listDevices(): Promise<DeviceList>;
|
|
35
|
+
renameDevice: (deviceId: string, name: string) => Promise<void>;
|
|
36
|
+
removeDevice: (deviceId: string) => Promise<void>;
|
|
35
37
|
run: (deviceId: string, action: string, args?: unknown) => Promise<ActionResponse>;
|
|
36
38
|
runBinary: (deviceId: string, action: string, args?: unknown, body?: Uint8Array) => Promise<BinaryActionResult>;
|
|
37
39
|
copy: (request: CopyRequest, options?: {
|
package/dist/daemon-client.js
CHANGED
|
@@ -146,6 +146,38 @@ export function createNodeClient(config, options = {}) {
|
|
|
146
146
|
throw error;
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
|
+
async function manage(method, params) {
|
|
150
|
+
assertOpen();
|
|
151
|
+
let value;
|
|
152
|
+
try {
|
|
153
|
+
value = await call(method, params);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (error instanceof RpcError && error.code === -32601)
|
|
157
|
+
throw new DaemonVersionError();
|
|
158
|
+
if (error instanceof DaemonRequestError)
|
|
159
|
+
throw new DeviceActionError({
|
|
160
|
+
type: "response",
|
|
161
|
+
ok: false,
|
|
162
|
+
error: { code: error.code, message: error.message },
|
|
163
|
+
});
|
|
164
|
+
if (error instanceof RpcError)
|
|
165
|
+
throw new DeviceActionError({
|
|
166
|
+
type: "response",
|
|
167
|
+
ok: false,
|
|
168
|
+
error: {
|
|
169
|
+
code: error.code === -32602 ? "invalid_request" : "cloud_unavailable",
|
|
170
|
+
message: error.message,
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
const response = parseResponse(value);
|
|
176
|
+
if (!response)
|
|
177
|
+
throw new Error("Invalid device management response.");
|
|
178
|
+
if (!response.ok)
|
|
179
|
+
throw new DeviceActionError(response);
|
|
180
|
+
}
|
|
149
181
|
async function runBinary(deviceId, action, args = {}, body = new Uint8Array()) {
|
|
150
182
|
assertOpen();
|
|
151
183
|
const empty = new Uint8Array();
|
|
@@ -281,6 +313,8 @@ export function createNodeClient(config, options = {}) {
|
|
|
281
313
|
throw new Error("Invalid device list response.");
|
|
282
314
|
return body;
|
|
283
315
|
},
|
|
316
|
+
renameDevice: (deviceId, name) => manage("devices.rename", { deviceId, name }),
|
|
317
|
+
removeDevice: (deviceId) => manage("devices.remove", { deviceId }),
|
|
284
318
|
run,
|
|
285
319
|
runBinary,
|
|
286
320
|
copy,
|
package/dist/daemon.js
CHANGED
|
@@ -5,6 +5,7 @@ import { join } from "node:path";
|
|
|
5
5
|
import WebSocket, { WebSocketServer } from "ws";
|
|
6
6
|
import { actionError, isRecord } from "./actions.js";
|
|
7
7
|
import { ObserveStore, QueryError } from "./observe.js";
|
|
8
|
+
import { CloudError } from "./cloud.js";
|
|
8
9
|
import { DeviceConnector } from "./connector.js";
|
|
9
10
|
import { daemonStatePath, readCallerCredential } from "./local.js";
|
|
10
11
|
import { writePrivateJson } from "./storage.js";
|
|
@@ -364,6 +365,28 @@ export async function serveDaemon(config, signal) {
|
|
|
364
365
|
case "devices.list":
|
|
365
366
|
result = await connector.list();
|
|
366
367
|
break;
|
|
368
|
+
case "devices.rename":
|
|
369
|
+
case "devices.remove": {
|
|
370
|
+
if (typeof params.deviceId !== "string" ||
|
|
371
|
+
(value.method === "devices.rename" && typeof params.name !== "string"))
|
|
372
|
+
throw new RpcError(-32602, "A device ID and, for rename, a name are required.");
|
|
373
|
+
try {
|
|
374
|
+
if (value.method === "devices.rename")
|
|
375
|
+
await connector.rename(params.deviceId, params.name);
|
|
376
|
+
else
|
|
377
|
+
await connector.remove(params.deviceId);
|
|
378
|
+
result = { type: "response", ok: true, result: {} };
|
|
379
|
+
}
|
|
380
|
+
catch (cause) {
|
|
381
|
+
if (!(cause instanceof CloudError))
|
|
382
|
+
throw cause;
|
|
383
|
+
result = actionError(cause.code, cause.message);
|
|
384
|
+
}
|
|
385
|
+
finally {
|
|
386
|
+
deviceStatus.invalidate();
|
|
387
|
+
}
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
367
390
|
case "devices.run":
|
|
368
391
|
if (typeof params.deviceId !== "string" || typeof params.action !== "string")
|
|
369
392
|
throw new RpcError(-32602, "A device ID and action are required.");
|
package/dist/devices-status.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ export interface DeviceStatus {
|
|
|
5
5
|
name: string;
|
|
6
6
|
platform: string | null;
|
|
7
7
|
status: "connected" | "not_connected" | "unknown" | "revoked";
|
|
8
|
+
audit?: {
|
|
9
|
+
state: "ok" | "unavailable";
|
|
10
|
+
code?: string;
|
|
11
|
+
message?: string;
|
|
12
|
+
};
|
|
8
13
|
}
|
|
9
14
|
export interface DevicesStatus {
|
|
10
15
|
connection: ConnectionStatus;
|
package/dist/devices-status.js
CHANGED
|
@@ -55,6 +55,13 @@ export class DeviceStatusReader {
|
|
|
55
55
|
: response.error.code === "target_unavailable"
|
|
56
56
|
? "not_connected"
|
|
57
57
|
: "unknown";
|
|
58
|
+
const audit = response.ok && isRecord(response.result) ? response.result.audit : undefined;
|
|
59
|
+
if (isRecord(audit) && (audit.state === "ok" || audit.state === "unavailable"))
|
|
60
|
+
device.audit = {
|
|
61
|
+
state: audit.state,
|
|
62
|
+
...(typeof audit.code === "string" ? { code: audit.code } : {}),
|
|
63
|
+
...(typeof audit.message === "string" ? { message: audit.message } : {}),
|
|
64
|
+
};
|
|
58
65
|
}
|
|
59
66
|
}));
|
|
60
67
|
const connection = this.connector.getStatus();
|
package/dist/executor.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import { type AuditStore } from "./audit.js";
|
|
1
2
|
import { type Frame } from "./frame.js";
|
|
2
3
|
import type { InboundEnvelope, OutboundEnvelope } from "./protocol.js";
|
|
4
|
+
export declare const FS_LIST_LIMIT = 1000;
|
|
3
5
|
export declare function redactBytes(data: Buffer, secrets: string[]): Buffer;
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
export type HostAudit = Pick<AuditStore, "start" | "end" | "host" | "status" | "hint">;
|
|
7
|
+
export declare function createExecutor(name: string, audit: HostAudit, send: (message: OutboundEnvelope) => boolean, secrets?: string[], sendFrame?: (frame: Frame) => boolean): {
|
|
8
|
+
receive: (message: InboundEnvelope) => Promise<void>;
|
|
9
|
+
receiveFrame: (frame: Frame) => Promise<void>;
|
|
10
|
+
idle: () => Promise<void>;
|
|
11
|
+
disconnect: () => void;
|
|
8
12
|
};
|