@rome-os/node-core 0.1.6 → 0.1.7
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/dist/auth.d.ts +1 -0
- package/dist/auth.js +13 -0
- package/dist/client.d.ts +2 -1
- package/dist/client.js +32 -3
- package/dist/cloud.d.ts +3 -2
- package/dist/cloud.js +1 -1
- package/dist/connect.js +1 -1
- package/dist/connector.d.ts +1 -1
- package/dist/connector.js +10 -6
- package/dist/daemon.js +1 -1
- package/dist/local.d.ts +3 -1
- package/dist/local.js +13 -5
- package/dist/protocol.d.ts +1 -0
- package/dist/protocol.js +1 -0
- package/dist/signer.d.ts +10 -0
- package/dist/signer.js +52 -0
- package/package.json +5 -1
package/dist/auth.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { type NodeConfig } from "./local.js";
|
|
2
2
|
export declare function configureCaller(origin: string, token: string, config: NodeConfig): Promise<void>;
|
|
3
|
+
export declare function configureSignerCaller(origin: string, signerSocket: string, config: NodeConfig): Promise<void>;
|
|
3
4
|
export declare function authorizeServer(origin: string, instanceToken: string | undefined, config: NodeConfig, onRetry?: () => void): Promise<void>;
|
package/dist/auth.js
CHANGED
|
@@ -20,6 +20,19 @@ export async function configureCaller(origin, token, config) {
|
|
|
20
20
|
await gatewayConfig(origin, token);
|
|
21
21
|
await writePrivateJson(callerCredentialPath(config), { cloudUrl: origin, token });
|
|
22
22
|
}
|
|
23
|
+
export async function configureSignerCaller(origin, signerSocket, config) {
|
|
24
|
+
const cloudUrl = cloudOrigin(origin);
|
|
25
|
+
const existing = await readOptionalCallerCredential(config);
|
|
26
|
+
if (existing?.cloudUrl === cloudUrl && existing.signerSocket === signerSocket)
|
|
27
|
+
return;
|
|
28
|
+
if (await daemonStatus(config))
|
|
29
|
+
throw new CallerConfigurationError("daemon_running", "Stop the caller daemon before changing credentials.");
|
|
30
|
+
await writePrivateJson(callerCredentialPath(config), {
|
|
31
|
+
version: 2,
|
|
32
|
+
cloudUrl,
|
|
33
|
+
signerSocket,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
23
36
|
async function exchangeInstanceToken(origin, token) {
|
|
24
37
|
let response;
|
|
25
38
|
try {
|
package/dist/client.d.ts
CHANGED
|
@@ -16,7 +16,8 @@ export interface ClientSocket {
|
|
|
16
16
|
export type ConnectionStatus = "connecting" | "online" | "retrying" | "revoked" | "superseded" | "stopped";
|
|
17
17
|
export interface GatewayClientOptions {
|
|
18
18
|
gatewayUrl: string;
|
|
19
|
-
|
|
19
|
+
getToken(): string | Promise<string>;
|
|
20
|
+
fetch?: typeof fetch;
|
|
20
21
|
createSocket(url: string, authorization: string): ClientSocket;
|
|
21
22
|
onMessage(message: GatewayMessage): void;
|
|
22
23
|
onFrame?(frame: Frame): void;
|
package/dist/client.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { encodeFrame, FRAME_TYPE, frameFits, isUuid, parseFrame } from "./frame.js";
|
|
2
2
|
import { parseGatewayUrl } from "./gateway-url.js";
|
|
3
|
+
import { isJwt } from "./signer.js";
|
|
3
4
|
import { CLOSE, gatewayMessage, outboundEnvelope, } from "./protocol.js";
|
|
5
|
+
const REAUTH_INTERVAL_MS = 60_000;
|
|
6
|
+
const DEVICE_TOKEN = /^(?:romedev_|romemob_)[A-Za-z0-9_-]{43}$/;
|
|
4
7
|
export function connectGateway(options) {
|
|
5
8
|
let url = parseGatewayUrl(options.gatewayUrl);
|
|
6
|
-
if (!/^(?:romedev_|romemob_)[A-Za-z0-9_-]{43}$/.test(options.deviceToken))
|
|
7
|
-
throw new Error("Invalid device token format");
|
|
8
9
|
let stopped = false;
|
|
9
10
|
let socket = null;
|
|
10
11
|
let attempt = 0;
|
|
11
12
|
let timer;
|
|
12
13
|
let connectingTimer;
|
|
13
14
|
let stableTimer;
|
|
15
|
+
let reauthTimer;
|
|
14
16
|
const status = (value) => options.onStatus?.(value);
|
|
15
17
|
const schedule = () => {
|
|
16
18
|
if (stopped || timer)
|
|
@@ -22,12 +24,31 @@ export function connectGateway(options) {
|
|
|
22
24
|
void open();
|
|
23
25
|
}, delay * (0.75 + (options.random ?? Math.random)() * 0.5));
|
|
24
26
|
};
|
|
27
|
+
const refresh = async (current, reauth) => {
|
|
28
|
+
try {
|
|
29
|
+
const response = await (options.fetch ?? fetch)(reauth, {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: { Authorization: `Bearer ${await options.getToken()}` },
|
|
32
|
+
redirect: "error",
|
|
33
|
+
signal: AbortSignal.timeout(10_000),
|
|
34
|
+
cache: "no-store",
|
|
35
|
+
});
|
|
36
|
+
if (response.status === 404 && socket === current)
|
|
37
|
+
current.close();
|
|
38
|
+
}
|
|
39
|
+
catch { }
|
|
40
|
+
};
|
|
25
41
|
const open = async () => {
|
|
26
42
|
if (stopped)
|
|
27
43
|
return;
|
|
28
44
|
status("connecting");
|
|
29
45
|
let current;
|
|
46
|
+
let token;
|
|
30
47
|
try {
|
|
48
|
+
const pending = options.getToken();
|
|
49
|
+
token = typeof pending === "string" ? pending : await pending;
|
|
50
|
+
if (!DEVICE_TOKEN.test(token) && !isJwt(token))
|
|
51
|
+
throw new Error("Invalid token format");
|
|
31
52
|
if (options.beforeConnect) {
|
|
32
53
|
const next = await options.beforeConnect();
|
|
33
54
|
if (stopped)
|
|
@@ -39,7 +60,7 @@ export function connectGateway(options) {
|
|
|
39
60
|
}
|
|
40
61
|
url = parseGatewayUrl(next);
|
|
41
62
|
}
|
|
42
|
-
current = options.createSocket(url.toString(), `Bearer ${
|
|
63
|
+
current = options.createSocket(url.toString(), `Bearer ${token}`);
|
|
43
64
|
}
|
|
44
65
|
catch {
|
|
45
66
|
schedule();
|
|
@@ -53,6 +74,7 @@ export function connectGateway(options) {
|
|
|
53
74
|
finished = true;
|
|
54
75
|
clearTimeout(connectingTimer);
|
|
55
76
|
clearTimeout(stableTimer);
|
|
77
|
+
clearInterval(reauthTimer);
|
|
56
78
|
socket = null;
|
|
57
79
|
if (code === CLOSE.revoked || code === CLOSE.superseded) {
|
|
58
80
|
stopped = true;
|
|
@@ -75,6 +97,12 @@ export function connectGateway(options) {
|
|
|
75
97
|
attempt = 0;
|
|
76
98
|
}, 30_000);
|
|
77
99
|
status("online");
|
|
100
|
+
if (isJwt(token)) {
|
|
101
|
+
const reauth = new URL(url);
|
|
102
|
+
reauth.protocol = url.protocol === "wss:" ? "https:" : "http:";
|
|
103
|
+
reauth.pathname = "/reauth";
|
|
104
|
+
reauthTimer = setInterval(() => void refresh(current, reauth), REAUTH_INTERVAL_MS);
|
|
105
|
+
}
|
|
78
106
|
});
|
|
79
107
|
current.addEventListener("message", ({ data }) => {
|
|
80
108
|
if (stopped || socket !== current)
|
|
@@ -150,6 +178,7 @@ export function connectGateway(options) {
|
|
|
150
178
|
clearTimeout(timer);
|
|
151
179
|
clearTimeout(connectingTimer);
|
|
152
180
|
clearTimeout(stableTimer);
|
|
181
|
+
clearInterval(reauthTimer);
|
|
153
182
|
const previous = socket;
|
|
154
183
|
socket = null;
|
|
155
184
|
previous?.close(1000, "client stopped");
|
package/dist/cloud.d.ts
CHANGED
|
@@ -3,5 +3,6 @@ export declare class CloudError extends Error {
|
|
|
3
3
|
constructor(code: string);
|
|
4
4
|
}
|
|
5
5
|
export declare function cloudOrigin(value: string): string;
|
|
6
|
-
export
|
|
7
|
-
export declare function
|
|
6
|
+
export type TokenSource = string | (() => string | Promise<string>);
|
|
7
|
+
export declare function cloudRequest(origin: string, path: string, token: TokenSource, method?: string, json?: unknown): Promise<unknown>;
|
|
8
|
+
export declare function gatewayConfig(origin: string, token: TokenSource): Promise<string>;
|
package/dist/cloud.js
CHANGED
|
@@ -32,7 +32,7 @@ export async function cloudRequest(origin, path, token, method = "GET", json) {
|
|
|
32
32
|
response = await fetch(new URL(path, cloudOrigin(origin)), {
|
|
33
33
|
method,
|
|
34
34
|
headers: {
|
|
35
|
-
Authorization: `Bearer ${token}`,
|
|
35
|
+
Authorization: `Bearer ${typeof token === "string" ? token : await token()}`,
|
|
36
36
|
...(json === undefined ? {} : { "content-type": "application/json" }),
|
|
37
37
|
},
|
|
38
38
|
body: json === undefined ? undefined : JSON.stringify(json),
|
package/dist/connect.js
CHANGED
|
@@ -73,7 +73,7 @@ export async function connectHost(options) {
|
|
|
73
73
|
const abort = () => finish();
|
|
74
74
|
const connection = connectGateway({
|
|
75
75
|
gatewayUrl,
|
|
76
|
-
|
|
76
|
+
getToken: () => active.token,
|
|
77
77
|
createSocket: createNodeSocket,
|
|
78
78
|
beforeConnect: async () => {
|
|
79
79
|
try {
|
package/dist/connector.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type ActionResponse, type BinaryActionResult } from "./actions.js";
|
|
2
2
|
import { connectGateway, type ConnectionStatus } from "./client.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type CallerCredential } from "./local.js";
|
|
4
4
|
import type { ChannelEvents, TransferChannel } from "./transfer.js";
|
|
5
5
|
export interface DeviceConnectorOptions {
|
|
6
6
|
credential: CallerCredential;
|
package/dist/connector.js
CHANGED
|
@@ -5,6 +5,7 @@ import { cloudRequest, CloudError, gatewayConfig } from "./cloud.js";
|
|
|
5
5
|
import { decodeMeta, encodeMeta, FRAME_TYPE, frameFits, isUuid, MAX_FRAME_BYTES, } from "./frame.js";
|
|
6
6
|
import { validId } from "./protocol.js";
|
|
7
7
|
import { createNodeSocket } from "./socket.js";
|
|
8
|
+
import { callerToken } from "./local.js";
|
|
8
9
|
const MAX_PENDING = 128;
|
|
9
10
|
export class DeviceConnector {
|
|
10
11
|
options;
|
|
@@ -37,13 +38,14 @@ export class DeviceConnector {
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
async initialize() {
|
|
40
|
-
const { cloudUrl
|
|
41
|
+
const { cloudUrl } = this.options.credential;
|
|
42
|
+
const token = callerToken(this.options.credential);
|
|
41
43
|
const gatewayUrl = await gatewayConfig(cloudUrl, token);
|
|
42
44
|
if (this.stopped)
|
|
43
45
|
throw new Error("connector_stopped");
|
|
44
46
|
this.connection = (this.options.connect ?? connectGateway)({
|
|
45
47
|
gatewayUrl,
|
|
46
|
-
|
|
48
|
+
getToken: token,
|
|
47
49
|
createSocket: createNodeSocket,
|
|
48
50
|
beforeConnect: async () => {
|
|
49
51
|
try {
|
|
@@ -74,8 +76,8 @@ export class DeviceConnector {
|
|
|
74
76
|
async list() {
|
|
75
77
|
if (this.stopped)
|
|
76
78
|
throw new Error("connector_stopped");
|
|
77
|
-
const { cloudUrl
|
|
78
|
-
const body = await cloudRequest(cloudUrl, "/api/account/devices",
|
|
79
|
+
const { cloudUrl } = this.options.credential;
|
|
80
|
+
const body = await cloudRequest(cloudUrl, "/api/account/devices", callerToken(this.options.credential));
|
|
79
81
|
if (!isRecord(body) || !Array.isArray(body.items))
|
|
80
82
|
throw new CloudError("invalid_response");
|
|
81
83
|
return { items: body.items };
|
|
@@ -85,7 +87,8 @@ export class DeviceConnector {
|
|
|
85
87
|
throw new Error("connector_stopped");
|
|
86
88
|
if (!isUuid(deviceId))
|
|
87
89
|
throw new CloudError("device_not_found");
|
|
88
|
-
const { cloudUrl
|
|
90
|
+
const { cloudUrl } = this.options.credential;
|
|
91
|
+
const token = callerToken(this.options.credential);
|
|
89
92
|
await cloudRequest(cloudUrl, `/api/account/devices/${deviceId}`, token, "PATCH", { name });
|
|
90
93
|
}
|
|
91
94
|
async remove(deviceId) {
|
|
@@ -93,7 +96,8 @@ export class DeviceConnector {
|
|
|
93
96
|
throw new Error("connector_stopped");
|
|
94
97
|
if (!isUuid(deviceId))
|
|
95
98
|
throw new CloudError("device_not_found");
|
|
96
|
-
const { cloudUrl
|
|
99
|
+
const { cloudUrl } = this.options.credential;
|
|
100
|
+
const token = callerToken(this.options.credential);
|
|
97
101
|
await cloudRequest(cloudUrl, `/api/account/devices/${deviceId}`, token, "DELETE");
|
|
98
102
|
}
|
|
99
103
|
receive(message) {
|
package/dist/daemon.js
CHANGED
|
@@ -89,7 +89,7 @@ export async function serveDaemon(config, signal) {
|
|
|
89
89
|
let closing = false;
|
|
90
90
|
const store = new ObserveStore({
|
|
91
91
|
path: join(config.directory, "observe.db"),
|
|
92
|
-
secrets: [credential.token, state.token],
|
|
92
|
+
secrets: credential.token ? [credential.token, state.token] : [state.token],
|
|
93
93
|
});
|
|
94
94
|
const caller = {
|
|
95
95
|
connection: "stopped",
|
package/dist/local.d.ts
CHANGED
|
@@ -11,8 +11,10 @@ export declare const callerCredentialPath: (config: NodeConfig) => string;
|
|
|
11
11
|
export declare const daemonStatePath: (config: NodeConfig) => string;
|
|
12
12
|
export interface CallerCredential {
|
|
13
13
|
cloudUrl: string;
|
|
14
|
-
token
|
|
14
|
+
token?: string;
|
|
15
|
+
signerSocket?: string;
|
|
15
16
|
}
|
|
17
|
+
export declare function callerToken(credential: CallerCredential): () => string | Promise<string>;
|
|
16
18
|
export declare class CallerConfigurationError extends Error {
|
|
17
19
|
readonly code: "not_configured" | "daemon_running" | "instance_token_required";
|
|
18
20
|
constructor(code: "not_configured" | "daemon_running" | "instance_token_required", message: string);
|
package/dist/local.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { join, resolve } from "node:path";
|
|
3
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
4
4
|
import { isRecord } from "./actions.js";
|
|
5
5
|
import { cloudOrigin } from "./cloud.js";
|
|
6
|
+
import { requestSignerToken } from "./signer.js";
|
|
6
7
|
import { readPrivateJson } from "./storage.js";
|
|
7
8
|
export function createNodeConfig(options) {
|
|
8
9
|
const directory = resolve(options.directory);
|
|
@@ -23,6 +24,10 @@ export function nodeConfigFromEnvironment(env) {
|
|
|
23
24
|
}
|
|
24
25
|
export const callerCredentialPath = (config) => join(config.directory, "caller.json");
|
|
25
26
|
export const daemonStatePath = (config) => join(config.directory, "daemon.json");
|
|
27
|
+
export function callerToken(credential) {
|
|
28
|
+
const { signerSocket, token } = credential;
|
|
29
|
+
return signerSocket ? () => requestSignerToken(signerSocket) : () => token;
|
|
30
|
+
}
|
|
26
31
|
export class CallerConfigurationError extends Error {
|
|
27
32
|
code;
|
|
28
33
|
constructor(code, message) {
|
|
@@ -32,10 +37,13 @@ export class CallerConfigurationError extends Error {
|
|
|
32
37
|
}
|
|
33
38
|
export async function readOptionalCallerCredential(config) {
|
|
34
39
|
const value = await readPrivateJson(callerCredentialPath(config));
|
|
35
|
-
if (!isRecord(value) ||
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
if (!isRecord(value) || typeof value.cloudUrl !== "string")
|
|
41
|
+
return null;
|
|
42
|
+
if (value.version === 2)
|
|
43
|
+
return typeof value.signerSocket === "string" && isAbsolute(value.signerSocket)
|
|
44
|
+
? { cloudUrl: cloudOrigin(value.cloudUrl), signerSocket: value.signerSocket }
|
|
45
|
+
: null;
|
|
46
|
+
if (typeof value.token !== "string" || !/^romedev_[A-Za-z0-9_-]{43}$/.test(value.token))
|
|
39
47
|
return null;
|
|
40
48
|
return { cloudUrl: cloudOrigin(value.cloudUrl), token: value.token };
|
|
41
49
|
}
|
package/dist/protocol.d.ts
CHANGED
package/dist/protocol.js
CHANGED
package/dist/signer.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function isJwt(value: string): boolean;
|
|
2
|
+
export declare class SignerError extends Error {
|
|
3
|
+
readonly code: "signer_unavailable" | "rate_limited";
|
|
4
|
+
constructor(code: "signer_unavailable" | "rate_limited");
|
|
5
|
+
}
|
|
6
|
+
export declare function requestSignerToken(socketPath: string): Promise<string>;
|
|
7
|
+
export declare function signerHealth(socketPath: string): Promise<{
|
|
8
|
+
jkt: string;
|
|
9
|
+
aud: string;
|
|
10
|
+
}>;
|
package/dist/signer.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { request } from "node:http";
|
|
2
|
+
import { isRecord } from "./actions.js";
|
|
3
|
+
const JWT_SHAPE = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
|
|
4
|
+
export function isJwt(value) {
|
|
5
|
+
return value.length <= 2048 && JWT_SHAPE.test(value);
|
|
6
|
+
}
|
|
7
|
+
export class SignerError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
constructor(code) {
|
|
10
|
+
super(code === "rate_limited"
|
|
11
|
+
? "The signer is rate limited."
|
|
12
|
+
: "The signing service is not running.");
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function signerCall(socketPath, method, path) {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const req = request({ socketPath, method, path, timeout: 2000 }, (res) => {
|
|
19
|
+
const chunks = [];
|
|
20
|
+
res.on("data", (chunk) => chunks.push(chunk));
|
|
21
|
+
res.on("error", () => reject(new SignerError("signer_unavailable")));
|
|
22
|
+
res.on("end", () => {
|
|
23
|
+
if (res.statusCode === 200) {
|
|
24
|
+
try {
|
|
25
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
catch { }
|
|
29
|
+
}
|
|
30
|
+
reject(new SignerError(res.statusCode === 429 ? "rate_limited" : "signer_unavailable"));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
req.on("timeout", () => req.destroy());
|
|
34
|
+
req.on("error", () => reject(new SignerError("signer_unavailable")));
|
|
35
|
+
req.end();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export async function requestSignerToken(socketPath) {
|
|
39
|
+
const body = await signerCall(socketPath, "POST", "/v1/token");
|
|
40
|
+
if (!isRecord(body) || typeof body.token !== "string" || !isJwt(body.token))
|
|
41
|
+
throw new SignerError("signer_unavailable");
|
|
42
|
+
return body.token;
|
|
43
|
+
}
|
|
44
|
+
export async function signerHealth(socketPath) {
|
|
45
|
+
const body = await signerCall(socketPath, "GET", "/v1/health");
|
|
46
|
+
if (!isRecord(body) ||
|
|
47
|
+
body.ok !== true ||
|
|
48
|
+
typeof body.jkt !== "string" ||
|
|
49
|
+
typeof body.aud !== "string")
|
|
50
|
+
throw new SignerError("signer_unavailable");
|
|
51
|
+
return { jkt: body.jkt, aud: body.aud };
|
|
52
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rome-os/node-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "JavaScript APIs for Rome device hosts and shared caller daemons",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -37,6 +37,10 @@
|
|
|
37
37
|
"types": "./dist/auth.d.ts",
|
|
38
38
|
"default": "./dist/auth.js"
|
|
39
39
|
},
|
|
40
|
+
"./signer": {
|
|
41
|
+
"types": "./dist/signer.d.ts",
|
|
42
|
+
"default": "./dist/signer.js"
|
|
43
|
+
},
|
|
40
44
|
"./socket": {
|
|
41
45
|
"types": "./dist/socket.d.ts",
|
|
42
46
|
"default": "./dist/socket.js"
|