@linkshell/gateway 0.5.0 → 0.5.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linkshell/gateway",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "type": "module",
5
5
  "main": "dist/gateway/src/index.js",
6
6
  "exports": {
@@ -28,8 +28,8 @@
28
28
  "dependencies": {
29
29
  "zod": "^3.24.3",
30
30
  "ws": "^8.18.1",
31
- "@linkshell/wire": "0.1.0",
32
- "@linkshell/gateway-v2": "0.1.0",
31
+ "@linkshell/gateway-v2": "0.1.2",
32
+ "@linkshell/wire": "0.1.2",
33
33
  "@linkshell/protocol": "0.4.10"
34
34
  },
35
35
  "devDependencies": {
package/src/embedded.ts CHANGED
@@ -28,6 +28,11 @@ export interface EmbeddedGatewayOptions {
28
28
  port?: number;
29
29
  logLevel?: "debug" | "info" | "warn" | "error";
30
30
  silent?: boolean;
31
+ /**
32
+ * Also serve the v2 relay (end-to-end encrypted, at /v2/connect), keeping its
33
+ * pairings in SQLite here. Needs Node.js 22.13+ (node:sqlite).
34
+ */
35
+ relayDataPath?: string;
31
36
  }
32
37
 
33
38
  export interface EmbeddedGateway {
@@ -91,7 +96,7 @@ function extractBearerToken(req: IncomingMessage): string | null {
91
96
  * Start an embedded gateway. Returns a handle to get URLs and close it.
92
97
  * Used by CLI when no external --gateway is provided.
93
98
  */
94
- export function startEmbeddedGateway(
99
+ export async function startEmbeddedGateway(
95
100
  options: EmbeddedGatewayOptions = {},
96
101
  ): Promise<EmbeddedGateway> {
97
102
  const targetPort = options.port ?? 0; // 0 = random available port
@@ -272,9 +277,17 @@ export function startEmbeddedGateway(
272
277
  maxPayload: MAX_WS_MESSAGE_SIZE,
273
278
  });
274
279
 
280
+ // Loaded on demand: v1 users on older Node never touch node:sqlite.
281
+ const relay = options.relayDataPath ? await startRelay(options.relayDataPath, log) : undefined;
282
+
275
283
  server.on("upgrade", (request, socket, head) => {
276
284
  const url = new URL(request.url ?? "/", `http://${request.headers.host}`);
277
285
 
286
+ if (relay && url.pathname === relay.path) {
287
+ relay.gateway.handleUpgrade(request, socket, head);
288
+ return;
289
+ }
290
+
278
291
  // Tunnel WebSocket upgrade (for HMR etc.)
279
292
  const tunnelParsed = parseTunnelPath(url.pathname);
280
293
  if (tunnelParsed) {
@@ -488,9 +501,26 @@ export function startEmbeddedGateway(
488
501
  sessionManager.destroy();
489
502
  pairingManager.destroy();
490
503
  tokenManager.destroy();
504
+ void relay?.gateway.stop();
491
505
  server.close(() => res());
492
506
  }),
493
507
  });
494
508
  });
495
509
  });
496
510
  }
511
+
512
+ async function startRelay(databasePath: string, log: (level: "debug" | "info" | "warn" | "error", message: string) => void) {
513
+ const [major = 0, minor = 0] = process.versions.node.split(".").map(Number);
514
+ if (major < 22 || (major === 22 && minor < 13)) {
515
+ log("warn", `v2 relay needs Node.js 22.13+ (this is ${process.version}); serving v1 only`);
516
+ return undefined;
517
+ }
518
+ const { mkdirSync } = await import("node:fs");
519
+ const { dirname } = await import("node:path");
520
+ const [{ Gateway }, { RELAY_PATH }] = await Promise.all([import("@linkshell/gateway-v2"), import("@linkshell/wire")]);
521
+ mkdirSync(dirname(databasePath), { recursive: true });
522
+ const gateway = new Gateway({ port: 0, databasePath, log: (message) => log("info", `[relay] ${message}`) });
523
+ gateway.attach();
524
+ log("info", `v2 relay on ${RELAY_PATH}`);
525
+ return { gateway, path: RELAY_PATH };
526
+ }
package/src/index.ts CHANGED
@@ -286,7 +286,8 @@ async function handleRequest(
286
286
 
287
287
  // Health check
288
288
  if (method === "GET" && url.pathname === "/healthz") {
289
- json(res, 200, { ok: true, version: GATEWAY_VERSION, relay: relay.connected });
289
+ // memoryMb: what the process holds, to watch against the container's limit.
290
+ json(res, 200, { ok: true, version: GATEWAY_VERSION, relay: relay.connected, memoryMb: Math.round(process.memoryUsage.rss() / 1048576) });
290
291
  return;
291
292
  }
292
293