feat(edge): support connection tokens when starting an edge and add token encode/decode utilities

This commit is contained in:
2026-02-17 19:36:40 +00:00
parent 73fc4ea28e
commit 93592bf909
6 changed files with 147 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
import * as plugins from './plugins.js';
import { EventEmitter } from 'events';
import { decodeConnectionToken } from './classes.token.js';
// Command map for the edge side of remoteingress-bin
type TEdgeCommands = {
@@ -13,8 +14,6 @@ type TEdgeCommands = {
hubPort: number;
edgeId: string;
secret: string;
listenPorts: number[];
stunIntervalSecs?: number;
};
result: { started: boolean };
};
@@ -39,8 +38,6 @@ export interface IEdgeConfig {
hubPort?: number;
edgeId: string;
secret: string;
listenPorts: number[];
stunIntervalSecs?: number;
}
export class RemoteIngressEdge extends EventEmitter {
@@ -86,20 +83,33 @@ export class RemoteIngressEdge extends EventEmitter {
/**
* Start the edge — spawns the Rust binary and connects to the hub.
* Accepts either a connection token or an explicit IEdgeConfig.
*/
public async start(config: IEdgeConfig): Promise<void> {
public async start(config: { token: string } | IEdgeConfig): Promise<void> {
let edgeConfig: IEdgeConfig;
if ('token' in config) {
const decoded = decodeConnectionToken(config.token);
edgeConfig = {
hubHost: decoded.hubHost,
hubPort: decoded.hubPort,
edgeId: decoded.edgeId,
secret: decoded.secret,
};
} else {
edgeConfig = config;
}
const spawned = await this.bridge.spawn();
if (!spawned) {
throw new Error('Failed to spawn remoteingress-bin');
}
await this.bridge.sendCommand('startEdge', {
hubHost: config.hubHost,
hubPort: config.hubPort ?? 8443,
edgeId: config.edgeId,
secret: config.secret,
listenPorts: config.listenPorts,
stunIntervalSecs: config.stunIntervalSecs,
hubHost: edgeConfig.hubHost,
hubPort: edgeConfig.hubPort ?? 8443,
edgeId: edgeConfig.edgeId,
secret: edgeConfig.secret,
});
this.started = true;