@kortix/agent-tunnel 0.14.19 → 0.15.1
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 +1 -1
- package/src/server/relay.ts +11 -4
- package/src/server/server.ts +5 -5
- package/src/server/ws-handler.ts +8 -8
package/package.json
CHANGED
package/src/server/relay.ts
CHANGED
|
@@ -11,8 +11,15 @@ import {
|
|
|
11
11
|
TunnelErrorCode,
|
|
12
12
|
} from '../shared/types';
|
|
13
13
|
|
|
14
|
+
/** What the relay uses of an agent's socket. A DOM `WebSocket` and Bun's `ServerWebSocket` both fit it. */
|
|
15
|
+
export interface AgentSocket {
|
|
16
|
+
readonly readyState: number;
|
|
17
|
+
send(data: string): void;
|
|
18
|
+
close(code?: number, reason?: string): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
14
21
|
interface AgentConnection {
|
|
15
|
-
ws:
|
|
22
|
+
ws: AgentSocket;
|
|
16
23
|
signingKey: string;
|
|
17
24
|
nonce: number;
|
|
18
25
|
lastResponseNonce: number;
|
|
@@ -43,7 +50,7 @@ export class TunnelRelay extends EventEmitter {
|
|
|
43
50
|
|
|
44
51
|
registerAgent(
|
|
45
52
|
tunnelId: string,
|
|
46
|
-
ws:
|
|
53
|
+
ws: AgentSocket,
|
|
47
54
|
signingKey: string,
|
|
48
55
|
metadata?: Record<string, unknown>,
|
|
49
56
|
): void {
|
|
@@ -70,7 +77,7 @@ export class TunnelRelay extends EventEmitter {
|
|
|
70
77
|
console.log(`[tunnel-relay] Agent registered: ${tunnelId} (total: ${this.agents.size})`);
|
|
71
78
|
}
|
|
72
79
|
|
|
73
|
-
unregisterAgent(tunnelId: string, ws?:
|
|
80
|
+
unregisterAgent(tunnelId: string, ws?: AgentSocket): boolean {
|
|
74
81
|
const existing = this.agents.get(tunnelId);
|
|
75
82
|
if (!existing) {
|
|
76
83
|
return false;
|
|
@@ -139,7 +146,7 @@ export class TunnelRelay extends EventEmitter {
|
|
|
139
146
|
return true;
|
|
140
147
|
}
|
|
141
148
|
|
|
142
|
-
handleAgentMessage(tunnelId: string, ws:
|
|
149
|
+
handleAgentMessage(tunnelId: string, ws: AgentSocket, raw: string | Buffer): void {
|
|
143
150
|
let msg: any;
|
|
144
151
|
try {
|
|
145
152
|
msg = JSON.parse(typeof raw === 'string' ? raw : raw.toString('utf-8'));
|
package/src/server/server.ts
CHANGED
|
@@ -63,7 +63,7 @@ export function startTunnelServer(config?: TunnelServerConfig): TunnelServer {
|
|
|
63
63
|
|
|
64
64
|
heartbeat.start();
|
|
65
65
|
|
|
66
|
-
const bunServer = Bun.serve({
|
|
66
|
+
const bunServer = Bun.serve<{ tunnelId: string }>({
|
|
67
67
|
port,
|
|
68
68
|
fetch(req, server) {
|
|
69
69
|
const url = new URL(req.url);
|
|
@@ -91,7 +91,7 @@ export function startTunnelServer(config?: TunnelServerConfig): TunnelServer {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
const success = server.upgrade(req, {
|
|
94
|
-
data: { tunnelId }
|
|
94
|
+
data: { tunnelId },
|
|
95
95
|
});
|
|
96
96
|
if (success) return undefined;
|
|
97
97
|
|
|
@@ -103,13 +103,13 @@ export function startTunnelServer(config?: TunnelServerConfig): TunnelServer {
|
|
|
103
103
|
|
|
104
104
|
websocket: {
|
|
105
105
|
idleTimeout: 0,
|
|
106
|
-
open(ws
|
|
106
|
+
open(ws) {
|
|
107
107
|
wsHandlers.onOpen(ws.data.tunnelId, ws);
|
|
108
108
|
},
|
|
109
|
-
message(ws
|
|
109
|
+
message(ws, message: string | Buffer) {
|
|
110
110
|
wsHandlers.onMessage(ws.data.tunnelId, ws, message);
|
|
111
111
|
},
|
|
112
|
-
close(ws
|
|
112
|
+
close(ws) {
|
|
113
113
|
wsHandlers.onClose(ws.data.tunnelId, ws);
|
|
114
114
|
},
|
|
115
115
|
},
|
package/src/server/ws-handler.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { TunnelRelay } from './relay';
|
|
1
|
+
import type { AgentSocket, TunnelRelay } from './relay';
|
|
2
2
|
import type { HeartbeatManager } from './heartbeat';
|
|
3
3
|
import type { TunnelAuthMessage, AuthResult } from '../shared/types';
|
|
4
4
|
|
|
@@ -14,9 +14,9 @@ export interface WsHandlerOptions {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export interface WsHandlers {
|
|
17
|
-
onOpen(tunnelId: string, ws:
|
|
18
|
-
onMessage(tunnelId: string, ws:
|
|
19
|
-
onClose(tunnelId: string, ws?:
|
|
17
|
+
onOpen(tunnelId: string, ws: AgentSocket): void;
|
|
18
|
+
onMessage(tunnelId: string, ws: AgentSocket, message: string | Buffer): void;
|
|
19
|
+
onClose(tunnelId: string, ws?: AgentSocket): void;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export function createWsHandlers(relay: TunnelRelay, opts?: WsHandlerOptions): WsHandlers {
|
|
@@ -26,7 +26,7 @@ export function createWsHandlers(relay: TunnelRelay, opts?: WsHandlerOptions): W
|
|
|
26
26
|
const authTimeoutMs = opts?.authTimeoutMs ?? 10_000;
|
|
27
27
|
|
|
28
28
|
const pendingConnections = new Map<
|
|
29
|
-
|
|
29
|
+
AgentSocket,
|
|
30
30
|
{
|
|
31
31
|
tunnelId: string;
|
|
32
32
|
timer: ReturnType<typeof setTimeout>;
|
|
@@ -35,7 +35,7 @@ export function createWsHandlers(relay: TunnelRelay, opts?: WsHandlerOptions): W
|
|
|
35
35
|
>();
|
|
36
36
|
|
|
37
37
|
return {
|
|
38
|
-
onOpen(tunnelId: string, ws:
|
|
38
|
+
onOpen(tunnelId: string, ws: AgentSocket) {
|
|
39
39
|
const timer = setTimeout(() => {
|
|
40
40
|
pendingConnections.delete(ws);
|
|
41
41
|
try {
|
|
@@ -46,7 +46,7 @@ export function createWsHandlers(relay: TunnelRelay, opts?: WsHandlerOptions): W
|
|
|
46
46
|
pendingConnections.set(ws, { tunnelId, timer, authenticating: false });
|
|
47
47
|
},
|
|
48
48
|
|
|
49
|
-
async onMessage(tunnelId: string, ws:
|
|
49
|
+
async onMessage(tunnelId: string, ws: AgentSocket, message: string | Buffer) {
|
|
50
50
|
const msgStr = typeof message === 'string' ? message : message.toString('utf-8');
|
|
51
51
|
const msgSize =
|
|
52
52
|
typeof message === 'string'
|
|
@@ -159,7 +159,7 @@ export function createWsHandlers(relay: TunnelRelay, opts?: WsHandlerOptions): W
|
|
|
159
159
|
relay.handleAgentMessage(tunnelId, ws, message);
|
|
160
160
|
},
|
|
161
161
|
|
|
162
|
-
onClose(tunnelId: string, ws?:
|
|
162
|
+
onClose(tunnelId: string, ws?: AgentSocket) {
|
|
163
163
|
const pending = ws ? pendingConnections.get(ws) : undefined;
|
|
164
164
|
if (pending) {
|
|
165
165
|
clearTimeout(pending.timer);
|