asyx 0.10.1 → 0.11.0
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/AGENTS.md +42 -0
- package/bin/asyx.js +1 -1
- package/package.json +3 -2
- package/src/commands/account.js +80 -71
- package/src/commands/app.js +317 -151
- package/src/commands/in-use-messages.js +47 -48
- package/src/commands/mqtt-broker.js +36 -49
- package/src/commands/mqtt.js +111 -149
- package/src/commands/name.js +24 -22
- package/src/commands/reject-messages.js +123 -123
- package/src/commands/setup.js +107 -131
- package/src/commands/sts.js +45 -61
- package/src/commands/tunnel.js +853 -932
- package/src/commands/validate-target-host.js +36 -59
- package/src/lib/aws-iot-config.js +26 -24
- package/src/lib/certs.js +15 -18
- package/src/lib/csr.js +107 -182
- package/src/lib/device.js +26 -37
- package/src/lib/ids.js +1 -2
- package/src/lib/naming.js +74 -68
- package/src/lib/pairing.js +10 -11
- package/src/lib/shared-transport.js +5 -9
- package/src/main.js +63 -53
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +25 -287
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.js +0 -72
- package/vendor/tunnel-core/dist/control-plane/session.js +0 -20
- package/vendor/tunnel-core/dist/errors.js +0 -19
- package/vendor/tunnel-core/dist/flows/account.js +1 -6
- package/vendor/tunnel-core/dist/flows/goodbye.js +0 -4
- package/vendor/tunnel-core/dist/flows/http.js +24 -112
- package/vendor/tunnel-core/dist/flows/identity.js +11 -10
- package/vendor/tunnel-core/dist/flows/in-use.js +0 -6
- package/vendor/tunnel-core/dist/flows/reopen.js +0 -4
- package/vendor/tunnel-core/dist/flows/tcp.js +18 -84
- package/vendor/tunnel-core/dist/flows/udp.js +15 -52
- package/vendor/tunnel-core/dist/lib/fingerprint.js +38 -42
- package/vendor/tunnel-core/dist/lib/jwt-verifier.js +14 -67
- package/vendor/tunnel-core/dist/lib/logger.js +1 -9
- package/vendor/tunnel-core/dist/lib/refresh-helpers.js +1 -22
- package/vendor/tunnel-core/dist/lifecycle.js +1 -8
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.js +1 -53
- package/vendor/tunnel-core/dist/mqtt/events/handlers.js +10 -64
- package/vendor/tunnel-core/dist/mqtt/events/index.js +3 -36
- package/vendor/tunnel-core/dist/mqtt/events/schemas.js +0 -1
- package/vendor/tunnel-core/dist/mqtt/ingress.js +0 -60
- package/vendor/tunnel-core/dist/state/assignment.js +6 -122
- package/vendor/tunnel-core/dist/state/instance-registry.js +0 -63
- package/vendor/tunnel-core/dist/tcp-v1/framing.js +2 -64
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +64 -331
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.js +5 -14
- package/vendor/tunnel-core/dist/tunnel/reconnect.js +10 -50
- package/vendor/tunnel-core/dist/tunnel/request-sink.js +0 -16
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +32 -195
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +28 -153
- package/vendor/tunnel-core/dist/tunnel/ws-control.js +4 -39
- package/vendor/tunnel-core/dist/types.js +0 -2
- package/vendor/tunnel-core/dist/udp-v1/framing.js +2 -64
- package/vendor/tunnel-core/package.json +0 -25
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +0 -273
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.d.ts +0 -79
- package/vendor/tunnel-core/dist/control-plane/session.d.ts +0 -55
- package/vendor/tunnel-core/dist/errors.d.ts +0 -49
- package/vendor/tunnel-core/dist/flows/account.d.ts +0 -24
- package/vendor/tunnel-core/dist/flows/goodbye.d.ts +0 -10
- package/vendor/tunnel-core/dist/flows/http.d.ts +0 -61
- package/vendor/tunnel-core/dist/flows/identity.d.ts +0 -23
- package/vendor/tunnel-core/dist/flows/in-use.d.ts +0 -27
- package/vendor/tunnel-core/dist/flows/reopen.d.ts +0 -9
- package/vendor/tunnel-core/dist/flows/tcp.d.ts +0 -85
- package/vendor/tunnel-core/dist/flows/udp.d.ts +0 -52
- package/vendor/tunnel-core/dist/lib/adapt-logger.d.ts +0 -2
- package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +0 -145
- package/vendor/tunnel-core/dist/lib/jwt-verifier.d.ts +0 -27
- package/vendor/tunnel-core/dist/lib/logger.d.ts +0 -9
- package/vendor/tunnel-core/dist/lib/refresh-helpers.d.ts +0 -16
- package/vendor/tunnel-core/dist/lifecycle.d.ts +0 -24
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.d.ts +0 -56
- package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +0 -60
- package/vendor/tunnel-core/dist/mqtt/events/index.d.ts +0 -1
- package/vendor/tunnel-core/dist/mqtt/events/schemas.d.ts +0 -498
- package/vendor/tunnel-core/dist/mqtt/ingress.d.ts +0 -63
- package/vendor/tunnel-core/dist/state/assignment.d.ts +0 -136
- package/vendor/tunnel-core/dist/state/instance-registry.d.ts +0 -88
- package/vendor/tunnel-core/dist/tcp-v1/framing.d.ts +0 -43
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +0 -140
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.d.ts +0 -13
- package/vendor/tunnel-core/dist/tunnel/reconnect.d.ts +0 -76
- package/vendor/tunnel-core/dist/tunnel/request-sink.d.ts +0 -40
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +0 -92
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +0 -93
- package/vendor/tunnel-core/dist/tunnel/ws-control.d.ts +0 -22
- package/vendor/tunnel-core/dist/types.d.ts +0 -24
- package/vendor/tunnel-core/dist/udp-v1/framing.d.ts +0 -41
|
@@ -1,39 +1,8 @@
|
|
|
1
|
-
// In-memory assignment store for JWT and relay metadata
|
|
2
|
-
// Never writes to disk - memory only
|
|
3
|
-
// TPB-12: Per-instance assignment stores to prevent cross-wiring between tunnel instances
|
|
4
|
-
// Per-instance assignment storage (instanceId -> assignment)
|
|
5
1
|
const assignmentByInstance = new Map();
|
|
6
|
-
// Reverse map: leaseId -> instanceId (for routing events that only have leaseId)
|
|
7
2
|
const leaseToInstance = new Map();
|
|
8
|
-
// Legacy: requestId -> waiters (for backward compatibility during transition)
|
|
9
3
|
const assignmentWaiters = new Map();
|
|
10
|
-
/**
|
|
11
|
-
* Set assignment for a specific tunnel instance
|
|
12
|
-
* TPB-12: Per-instance assignment storage to prevent cross-wiring
|
|
13
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
14
|
-
* @param {Object} assignment - Assignment data
|
|
15
|
-
* @param {string} assignment.requestId - Request ID
|
|
16
|
-
* @param {string} assignment.relayId - Relay ID
|
|
17
|
-
* @param {string} assignment.relayUrl - Relay URL
|
|
18
|
-
* @param {string} assignment.jwt - JWT token (stored in memory only)
|
|
19
|
-
* @param {number} assignment.leaseMs - Lease duration in milliseconds
|
|
20
|
-
* @param {string} assignment.leaseId - Lease ID for JWT refresh (optional)
|
|
21
|
-
* @param {string} [assignment.quickpassSlug]
|
|
22
|
-
* @param {string} [assignment.tcpAliasFqdn]
|
|
23
|
-
* @param {number} [assignment.publicPort]
|
|
24
|
-
* @param {string} [assignment.fqdn]
|
|
25
|
-
* @param {boolean} [assignment.srvProvisioned]
|
|
26
|
-
* @param {string} [assignment.srvFqdn]
|
|
27
|
-
* @param {unknown[]} [assignment.warnings]
|
|
28
|
-
* @param {string} [assignment.tunnelMode]
|
|
29
|
-
* @param {string} [assignment.protocol]
|
|
30
|
-
* @param {string} [assignment.assignmentJwt]
|
|
31
|
-
* @param {{ port: number, kind: string, from: string, to: string }} [assignment.portMoved] Story 2.11 c3
|
|
32
|
-
*/
|
|
33
4
|
export function setAssignment(instanceId, assignment) {
|
|
34
|
-
// Backward compatibility: if instanceId is not provided, treat as legacy single-instance
|
|
35
5
|
if (typeof instanceId !== 'string' && typeof instanceId === 'object' && instanceId !== null) {
|
|
36
|
-
// Legacy call: setAssignment(assignment) - treat as single instance
|
|
37
6
|
const legacyAssignment = instanceId;
|
|
38
7
|
instanceId = 'legacy';
|
|
39
8
|
assignment = legacyAssignment;
|
|
@@ -42,35 +11,29 @@ export function setAssignment(instanceId, assignment) {
|
|
|
42
11
|
requestId: assignment.requestId,
|
|
43
12
|
relayId: assignment.relayId,
|
|
44
13
|
relayUrl: assignment.relayUrl,
|
|
45
|
-
jwt: assignment.jwt,
|
|
14
|
+
jwt: assignment.jwt,
|
|
46
15
|
leaseMs: assignment.leaseMs,
|
|
47
|
-
leaseId: assignment.leaseId,
|
|
48
|
-
quickpassSlug: assignment.quickpassSlug || null,
|
|
49
|
-
// TCP-S3: Store TCP-specific fields
|
|
16
|
+
leaseId: assignment.leaseId,
|
|
17
|
+
quickpassSlug: assignment.quickpassSlug || null,
|
|
50
18
|
tcpAliasFqdn: assignment.tcpAliasFqdn || null,
|
|
51
19
|
publicPort: assignment.publicPort || null,
|
|
52
20
|
fqdn: assignment.fqdn || null,
|
|
53
|
-
// E3-S8: Store SRV provisioning status
|
|
54
21
|
srvProvisioned: assignment.srvProvisioned === true,
|
|
55
22
|
srvFqdn: assignment.srvFqdn || null,
|
|
56
23
|
warnings: assignment.warnings || [],
|
|
57
|
-
// UDP-S1: Store UDP-specific fields
|
|
58
24
|
tunnelMode: assignment.tunnelMode || null,
|
|
59
25
|
protocol: assignment.protocol || null,
|
|
60
|
-
assignmentJwt: assignment.assignmentJwt || null,
|
|
61
|
-
portMoved: assignment.portMoved || null,
|
|
26
|
+
assignmentJwt: assignment.assignmentJwt || null,
|
|
27
|
+
portMoved: assignment.portMoved || null,
|
|
62
28
|
timestamp: Date.now(),
|
|
63
29
|
lastRefreshMs: undefined,
|
|
64
30
|
lastRefreshIso: undefined,
|
|
65
31
|
lastRefreshDeltaMs: undefined
|
|
66
32
|
};
|
|
67
|
-
// Store per instance
|
|
68
33
|
assignmentByInstance.set(instanceId, assignmentData);
|
|
69
|
-
// Update lease -> instance mapping
|
|
70
34
|
if (assignment.leaseId) {
|
|
71
35
|
leaseToInstance.set(assignment.leaseId, instanceId);
|
|
72
36
|
}
|
|
73
|
-
// Notify any waiters that are tracking this requestId (backward compatibility)
|
|
74
37
|
if (assignment.requestId && assignmentWaiters.size > 0) {
|
|
75
38
|
const waiters = assignmentWaiters.get(assignment.requestId);
|
|
76
39
|
if (waiters && waiters.length) {
|
|
@@ -81,50 +44,28 @@ export function setAssignment(instanceId, assignment) {
|
|
|
81
44
|
waiter.resolve({ ...assignmentData });
|
|
82
45
|
}
|
|
83
46
|
catch {
|
|
84
|
-
// Ignore waiter resolution errors; they should not impact CLI flow
|
|
85
47
|
}
|
|
86
48
|
}
|
|
87
49
|
}
|
|
88
50
|
}
|
|
89
51
|
}
|
|
90
|
-
/**
|
|
91
|
-
* Get assignment for a specific tunnel instance
|
|
92
|
-
* TPB-12: Per-instance assignment retrieval
|
|
93
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
94
|
-
* @returns {Object|null} Assignment for the instance or null if none set
|
|
95
|
-
*/
|
|
96
52
|
export function getAssignment(instanceId) {
|
|
97
|
-
// Backward compatibility: if no instanceId provided, try to get from single instance or return null
|
|
98
53
|
if (!instanceId) {
|
|
99
|
-
// Legacy mode: if there's only one instance, return it
|
|
100
54
|
if (assignmentByInstance.size === 1) {
|
|
101
55
|
const [firstInstanceId] = assignmentByInstance.keys();
|
|
102
56
|
const assignment = assignmentByInstance.get(firstInstanceId);
|
|
103
57
|
return assignment ? { ...assignment } : null;
|
|
104
58
|
}
|
|
105
|
-
// Multiple instances or none - return null (caller must provide instanceId)
|
|
106
59
|
return null;
|
|
107
60
|
}
|
|
108
61
|
const assignment = assignmentByInstance.get(instanceId);
|
|
109
62
|
return assignment ? { ...assignment } : null;
|
|
110
63
|
}
|
|
111
|
-
/**
|
|
112
|
-
* Resolve instanceId from leaseId
|
|
113
|
-
* TPB-12: Helper to route events that only have leaseId
|
|
114
|
-
* @param {string} leaseId - Lease ID
|
|
115
|
-
* @returns {string|null} Instance ID or null if not found
|
|
116
|
-
*/
|
|
117
64
|
export function resolveInstanceByLease(leaseId) {
|
|
118
65
|
return leaseToInstance.get(leaseId) ?? null;
|
|
119
66
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Clear assignment for a specific tunnel instance
|
|
122
|
-
* TPB-12: Per-instance assignment clearing
|
|
123
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
124
|
-
*/
|
|
125
67
|
export function clearAssignment(instanceId) {
|
|
126
68
|
if (!instanceId) {
|
|
127
|
-
// Legacy: clear all instances
|
|
128
69
|
assignmentByInstance.clear();
|
|
129
70
|
leaseToInstance.clear();
|
|
130
71
|
}
|
|
@@ -135,7 +76,6 @@ export function clearAssignment(instanceId) {
|
|
|
135
76
|
}
|
|
136
77
|
assignmentByInstance.delete(instanceId);
|
|
137
78
|
}
|
|
138
|
-
// Reject all outstanding waiters to avoid dangling promises
|
|
139
79
|
if (assignmentWaiters.size > 0) {
|
|
140
80
|
for (const [, waiters] of assignmentWaiters) {
|
|
141
81
|
for (const waiter of waiters) {
|
|
@@ -144,18 +84,12 @@ export function clearAssignment(instanceId) {
|
|
|
144
84
|
waiter.reject(new Error("Assignment cleared before completion"));
|
|
145
85
|
}
|
|
146
86
|
catch {
|
|
147
|
-
// Ignore waiter rejections; consumers can handle silently
|
|
148
87
|
}
|
|
149
88
|
}
|
|
150
89
|
}
|
|
151
90
|
assignmentWaiters.clear();
|
|
152
91
|
}
|
|
153
92
|
}
|
|
154
|
-
/**
|
|
155
|
-
* Drop one instance key without touching a lease mapping that already points elsewhere.
|
|
156
|
-
* Does not reject assignment waiters.
|
|
157
|
-
* @param {string} instanceId
|
|
158
|
-
*/
|
|
159
93
|
export function forgetInstanceAssignment(instanceId) {
|
|
160
94
|
if (!instanceId)
|
|
161
95
|
return;
|
|
@@ -167,13 +101,7 @@ export function forgetInstanceAssignment(instanceId) {
|
|
|
167
101
|
}
|
|
168
102
|
assignmentByInstance.delete(instanceId);
|
|
169
103
|
}
|
|
170
|
-
|
|
171
|
-
* Copy an assignment onto a new instance id and drop the old key.
|
|
172
|
-
* When a tunnel object is passed, its instanceId moves with the assignment.
|
|
173
|
-
* @param {{ oldId: string, newId: string, tunnel?: { instanceId?: string } | null }} opts
|
|
174
|
-
* @returns {{ moved: boolean, leaseId: string | null, size: number }}
|
|
175
|
-
*/
|
|
176
|
-
export function migrateInstanceAssignment({ oldId, newId, tunnel } = /** @type {any} */ ({})) {
|
|
104
|
+
export function migrateInstanceAssignment({ oldId, newId, tunnel } = ({})) {
|
|
177
105
|
const oldAssignment = oldId ? getAssignment(oldId) : null;
|
|
178
106
|
const moved = !!(oldAssignment && newId && oldId !== newId);
|
|
179
107
|
if (moved) {
|
|
@@ -189,12 +117,6 @@ export function migrateInstanceAssignment({ oldId, newId, tunnel } = /** @type {
|
|
|
189
117
|
size: assignmentByInstance.size,
|
|
190
118
|
};
|
|
191
119
|
}
|
|
192
|
-
/**
|
|
193
|
-
* Assignment stored for this instance after a refresh ack.
|
|
194
|
-
* Throws when the keyed entry is missing or the JWT has no exp.
|
|
195
|
-
* @param {string} instanceId
|
|
196
|
-
* @returns {{ assignment: Object, expMs: number }}
|
|
197
|
-
*/
|
|
198
120
|
export function readRefreshAssignment(instanceId) {
|
|
199
121
|
const newAssignment = getAssignment(instanceId);
|
|
200
122
|
if (!newAssignment) {
|
|
@@ -211,30 +133,16 @@ export function readRefreshAssignment(instanceId) {
|
|
|
211
133
|
}
|
|
212
134
|
return { assignment: newAssignment, expMs };
|
|
213
135
|
}
|
|
214
|
-
/**
|
|
215
|
-
* Check if an assignment exists for a specific tunnel instance
|
|
216
|
-
* TPB-12: Per-instance assignment check
|
|
217
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
218
|
-
* @returns {boolean} True if assignment exists for the instance
|
|
219
|
-
*/
|
|
220
136
|
export function hasAssignment(instanceId) {
|
|
221
137
|
if (!instanceId) {
|
|
222
|
-
// Legacy: check if any assignment exists
|
|
223
138
|
return assignmentByInstance.size > 0;
|
|
224
139
|
}
|
|
225
140
|
return assignmentByInstance.has(instanceId);
|
|
226
141
|
}
|
|
227
|
-
/**
|
|
228
|
-
* Wait until an assignment with the specified requestId is available.
|
|
229
|
-
* @param {string} requestId - Request ID to wait for
|
|
230
|
-
* @param {number} [timeoutMs=10000] - Timeout before rejecting
|
|
231
|
-
* @returns {Promise<Object>} resolves with the assignment snapshot
|
|
232
|
-
*/
|
|
233
142
|
export function waitForAssignment(requestId, timeoutMs = 10000) {
|
|
234
143
|
if (!requestId) {
|
|
235
144
|
return Promise.reject(new Error("waitForAssignment requires a requestId"));
|
|
236
145
|
}
|
|
237
|
-
// Check all instances for matching requestId (backward compatibility)
|
|
238
146
|
for (const [instanceId, assignment] of assignmentByInstance) {
|
|
239
147
|
if (assignment.requestId === requestId) {
|
|
240
148
|
return Promise.resolve({ ...assignment });
|
|
@@ -245,7 +153,6 @@ export function waitForAssignment(requestId, timeoutMs = 10000) {
|
|
|
245
153
|
resolve,
|
|
246
154
|
reject,
|
|
247
155
|
timer: setTimeout(() => {
|
|
248
|
-
// Timeout - remove from waiter list and reject
|
|
249
156
|
const queue = assignmentWaiters.get(requestId);
|
|
250
157
|
if (queue) {
|
|
251
158
|
const idx = queue.indexOf(entry);
|
|
@@ -266,12 +173,6 @@ export function waitForAssignment(requestId, timeoutMs = 10000) {
|
|
|
266
173
|
}
|
|
267
174
|
});
|
|
268
175
|
}
|
|
269
|
-
/**
|
|
270
|
-
* Story 1.9: fail the open waiting for requestId with `error` (e.g. address_in_use), so the flow can
|
|
271
|
-
* decide to wait and ask again instead of timing out. Returns true when someone was waiting.
|
|
272
|
-
* @param {string} requestId
|
|
273
|
-
* @param {Error} error
|
|
274
|
-
*/
|
|
275
176
|
export function rejectAssignment(requestId, error) {
|
|
276
177
|
const queue = assignmentWaiters.get(requestId);
|
|
277
178
|
if (!queue || queue.length === 0)
|
|
@@ -283,12 +184,6 @@ export function rejectAssignment(requestId, error) {
|
|
|
283
184
|
}
|
|
284
185
|
return true;
|
|
285
186
|
}
|
|
286
|
-
/**
|
|
287
|
-
* Get assignment metadata (without JWT for logging)
|
|
288
|
-
* TPB-12: Per-instance metadata retrieval
|
|
289
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
290
|
-
* @returns {Object|null} Assignment metadata or null
|
|
291
|
-
*/
|
|
292
187
|
export function getAssignmentMetadata(instanceId) {
|
|
293
188
|
const assignment = getAssignment(instanceId);
|
|
294
189
|
if (!assignment)
|
|
@@ -304,15 +199,7 @@ export function getAssignmentMetadata(instanceId) {
|
|
|
304
199
|
lastRefreshDeltaMs: assignment.lastRefreshDeltaMs
|
|
305
200
|
};
|
|
306
201
|
}
|
|
307
|
-
/**
|
|
308
|
-
* Record a successful refresh in the assignment cache and return delta ms since last refresh (or since initial assignment if none).
|
|
309
|
-
* TPB-12: Per-instance refresh recording
|
|
310
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
311
|
-
* @param {number} [nowMs]
|
|
312
|
-
* @returns {number} delta milliseconds since last refresh or initial assignment
|
|
313
|
-
*/
|
|
314
202
|
export function recordRefresh(instanceId, nowMs = Date.now()) {
|
|
315
|
-
// Backward compatibility: if instanceId is a number, treat as legacy
|
|
316
203
|
if (typeof instanceId === 'number') {
|
|
317
204
|
nowMs = instanceId;
|
|
318
205
|
instanceId = null;
|
|
@@ -322,16 +209,13 @@ export function recordRefresh(instanceId, nowMs = Date.now()) {
|
|
|
322
209
|
return 0;
|
|
323
210
|
const prev = typeof assignment.lastRefreshMs === 'number' ? assignment.lastRefreshMs : assignment.timestamp;
|
|
324
211
|
const delta = nowMs - prev;
|
|
325
|
-
// Update the assignment in the map
|
|
326
212
|
assignment.lastRefreshMs = nowMs;
|
|
327
213
|
assignment.lastRefreshIso = new Date(nowMs).toISOString();
|
|
328
214
|
assignment.lastRefreshDeltaMs = delta;
|
|
329
|
-
// Update the map entry
|
|
330
215
|
if (instanceId) {
|
|
331
216
|
assignmentByInstance.set(instanceId, assignment);
|
|
332
217
|
}
|
|
333
218
|
else {
|
|
334
|
-
// Legacy: update first instance found
|
|
335
219
|
const [firstInstanceId] = assignmentByInstance.keys();
|
|
336
220
|
if (firstInstanceId) {
|
|
337
221
|
assignmentByInstance.set(firstInstanceId, assignment);
|
|
@@ -1,29 +1,5 @@
|
|
|
1
|
-
// TPB-12: Instance Registry for per-tunnel routing
|
|
2
|
-
// Tracks tunnel instances and their context (setAssignment callback, tunnel type, etc.)
|
|
3
|
-
/**
|
|
4
|
-
* Instance context for a tunnel instance
|
|
5
|
-
* @typedef {Object} InstanceContext
|
|
6
|
-
* @property {string} instanceId - Full MQTT clientId (instanceId)
|
|
7
|
-
* @property {string} tunnelType - Tunnel type ('http' | 'tcp' | 'udp')
|
|
8
|
-
* @property {Function} setAssignment - Per-instance setAssignment callback
|
|
9
|
-
* @property {Function} getAssignment - Per-instance getAssignment callback
|
|
10
|
-
* @property {Object} [metadata] - Additional metadata (baseClientId, etc.)
|
|
11
|
-
* @property {string} [expectedRequestId] - Expected requestId for initial open_ack routing
|
|
12
|
-
*/
|
|
13
|
-
// Global instance registry: instanceId -> InstanceContext
|
|
14
1
|
const instanceRegistry = new Map();
|
|
15
|
-
// TPB-12: Track expected requestIds per instance for pre-assignment routing
|
|
16
|
-
// requestId -> instanceId (for routing open_ack before assignment exists)
|
|
17
2
|
const requestIdToInstance = new Map();
|
|
18
|
-
/**
|
|
19
|
-
* Register a tunnel instance
|
|
20
|
-
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
21
|
-
* @param {Object} context - Instance context
|
|
22
|
-
* @param {string} context.tunnelType - Tunnel type ('http' | 'tcp' | 'udp')
|
|
23
|
-
* @param {Function} context.setAssignment - Per-instance setAssignment callback
|
|
24
|
-
* @param {Function} context.getAssignment - Per-instance getAssignment callback
|
|
25
|
-
* @param {Object} [context.metadata] - Additional metadata
|
|
26
|
-
*/
|
|
27
3
|
export function registerInstance(instanceId, context) {
|
|
28
4
|
if (!instanceId || typeof instanceId !== 'string') {
|
|
29
5
|
throw new Error('instanceId must be a non-empty string');
|
|
@@ -36,10 +12,6 @@ export function registerInstance(instanceId, context) {
|
|
|
36
12
|
metadata: context.metadata || {}
|
|
37
13
|
});
|
|
38
14
|
}
|
|
39
|
-
/**
|
|
40
|
-
* Unregister a tunnel instance
|
|
41
|
-
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
42
|
-
*/
|
|
43
15
|
export function unregisterInstance(instanceId) {
|
|
44
16
|
const context = instanceRegistry.get(instanceId);
|
|
45
17
|
if (context?.expectedRequestId) {
|
|
@@ -47,49 +19,23 @@ export function unregisterInstance(instanceId) {
|
|
|
47
19
|
}
|
|
48
20
|
instanceRegistry.delete(instanceId);
|
|
49
21
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Get instance context by instanceId
|
|
52
|
-
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
53
|
-
* @returns {InstanceContext|null} Instance context or null if not found
|
|
54
|
-
*/
|
|
55
22
|
export function getInstance(instanceId) {
|
|
56
23
|
return instanceRegistry.get(instanceId) ?? null;
|
|
57
24
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Get all registered instances (for debugging)
|
|
60
|
-
* @returns {Array<InstanceContext>} Array of all instance contexts
|
|
61
|
-
*/
|
|
62
25
|
export function getAllInstances() {
|
|
63
26
|
return Array.from(instanceRegistry.values());
|
|
64
27
|
}
|
|
65
|
-
/**
|
|
66
|
-
* Set expected requestId for an instance (for pre-assignment routing)
|
|
67
|
-
* TPB-12: Track requestId before assignment exists so open_ack can be routed
|
|
68
|
-
*
|
|
69
|
-
* Note: This automatically clears any previous requestId for this instance,
|
|
70
|
-
* so reconnect/retry flows that use a new requestId are handled correctly.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
73
|
-
* @param {string} requestId - Expected requestId from cmd/open
|
|
74
|
-
*/
|
|
75
28
|
export function setExpectedRequestId(instanceId, requestId) {
|
|
76
29
|
const context = instanceRegistry.get(instanceId);
|
|
77
30
|
if (!context) {
|
|
78
31
|
throw new Error(`Instance ${instanceId} not found in registry`);
|
|
79
32
|
}
|
|
80
|
-
// Remove old requestId mapping if exists (handles reconnect/retry with new requestId)
|
|
81
33
|
if (context.expectedRequestId) {
|
|
82
34
|
requestIdToInstance.delete(context.expectedRequestId);
|
|
83
35
|
}
|
|
84
|
-
// Set new requestId
|
|
85
36
|
context.expectedRequestId = requestId;
|
|
86
37
|
requestIdToInstance.set(requestId, instanceId);
|
|
87
38
|
}
|
|
88
|
-
/**
|
|
89
|
-
* Clear expected requestId for an instance (called after assignment is created)
|
|
90
|
-
* TPB-12: Clean up requestId mapping once assignment exists (routing uses leaseId after that)
|
|
91
|
-
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
92
|
-
*/
|
|
93
39
|
export function clearExpectedRequestId(instanceId) {
|
|
94
40
|
const context = instanceRegistry.get(instanceId);
|
|
95
41
|
if (context?.expectedRequestId) {
|
|
@@ -97,18 +43,9 @@ export function clearExpectedRequestId(instanceId) {
|
|
|
97
43
|
context.expectedRequestId = undefined;
|
|
98
44
|
}
|
|
99
45
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Resolve instanceId from requestId (for pre-assignment routing)
|
|
102
|
-
* TPB-12: Route open_ack events by requestId before assignment exists
|
|
103
|
-
* @param {string} requestId - Request ID from cmd/open
|
|
104
|
-
* @returns {string|null} Instance ID or null if not found
|
|
105
|
-
*/
|
|
106
46
|
export function resolveInstanceByRequestId(requestId) {
|
|
107
47
|
return requestIdToInstance.get(requestId) ?? null;
|
|
108
48
|
}
|
|
109
|
-
/**
|
|
110
|
-
* Clear all instances (for cleanup/testing)
|
|
111
|
-
*/
|
|
112
49
|
export function clearAll() {
|
|
113
50
|
instanceRegistry.clear();
|
|
114
51
|
requestIdToInstance.clear();
|
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TCP-v1 Framing Protocol
|
|
3
|
-
*
|
|
4
|
-
* Binary frame format:
|
|
5
|
-
* [frameType:1 byte][streamId:4 bytes (big-endian)][length:4 bytes (big-endian)][payload:length bytes]
|
|
6
|
-
*
|
|
7
|
-
* Frame types:
|
|
8
|
-
* - 0x01: STREAM_OPEN
|
|
9
|
-
* - 0x02: STREAM_DATA
|
|
10
|
-
* - 0x03: STREAM_CLOSE
|
|
11
|
-
* - 0x04: STREAM_ERROR
|
|
12
|
-
* - 0x05: PING
|
|
13
|
-
* - 0x06: PONG
|
|
14
|
-
*
|
|
15
|
-
* Max payload size: 64 KiB (65535 bytes)
|
|
16
|
-
* StreamId: 24-bit value (stored as 32-bit, but only lower 24 bits used)
|
|
17
|
-
*/
|
|
18
1
|
export const FrameType = {
|
|
19
2
|
STREAM_OPEN: 0x01,
|
|
20
3
|
STREAM_DATA: 0x02,
|
|
@@ -23,8 +6,8 @@ export const FrameType = {
|
|
|
23
6
|
PING: 0x05,
|
|
24
7
|
PONG: 0x06,
|
|
25
8
|
};
|
|
26
|
-
export const MAX_FRAME_PAYLOAD = 64 * 1024;
|
|
27
|
-
export const FRAME_HEADER_SIZE = 9;
|
|
9
|
+
export const MAX_FRAME_PAYLOAD = 64 * 1024;
|
|
10
|
+
export const FRAME_HEADER_SIZE = 9;
|
|
28
11
|
export class TcpV1FramingError extends Error {
|
|
29
12
|
constructor(message, code) {
|
|
30
13
|
super(message);
|
|
@@ -32,92 +15,56 @@ export class TcpV1FramingError extends Error {
|
|
|
32
15
|
this.code = code;
|
|
33
16
|
}
|
|
34
17
|
}
|
|
35
|
-
/**
|
|
36
|
-
* Encode a frame to binary format
|
|
37
|
-
* @param {Object} frame - Frame object with type, streamId, and payload
|
|
38
|
-
* @param {number} frame.type - Frame type (FrameType constant)
|
|
39
|
-
* @param {number} frame.streamId - Stream ID (24-bit, 0 for PING/PONG)
|
|
40
|
-
* @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
|
|
41
|
-
* @returns {Buffer} Encoded frame
|
|
42
|
-
* @throws {TcpV1FramingError} If frame is invalid
|
|
43
|
-
*/
|
|
44
18
|
export function encodeFrame(frame) {
|
|
45
19
|
const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
|
|
46
|
-
// Validate frame type
|
|
47
20
|
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
48
21
|
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
49
22
|
}
|
|
50
|
-
// Convert payload to Buffer if needed
|
|
51
23
|
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
52
|
-
// Validate payload size
|
|
53
24
|
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
54
25
|
throw new TcpV1FramingError(`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
|
|
55
26
|
}
|
|
56
|
-
// StreamID validation (PING/PONG can have 0)
|
|
57
27
|
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
58
28
|
if (streamId === 0) {
|
|
59
29
|
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
60
30
|
}
|
|
61
|
-
// Ensure streamId fits in 24 bits
|
|
62
31
|
if (streamId > 0x00FFFFFF) {
|
|
63
32
|
throw new TcpV1FramingError(`StreamID ${streamId} exceeds 24-bit limit`, 'INVALID_STREAM_ID');
|
|
64
33
|
}
|
|
65
34
|
}
|
|
66
|
-
// Create frame buffer
|
|
67
35
|
const frameBuf = Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadBuf.length);
|
|
68
|
-
// Write frame type (1 byte)
|
|
69
36
|
frameBuf[0] = type;
|
|
70
|
-
// Write streamId (4 bytes, big-endian, only lower 24 bits used)
|
|
71
37
|
frameBuf.writeUInt32BE(streamId & 0x00FFFFFF, 1);
|
|
72
|
-
// Write payload length (4 bytes, big-endian)
|
|
73
38
|
frameBuf.writeUInt32BE(payloadBuf.length, 5);
|
|
74
|
-
// Write payload
|
|
75
39
|
if (payloadBuf.length > 0) {
|
|
76
40
|
payloadBuf.copy(frameBuf, FRAME_HEADER_SIZE);
|
|
77
41
|
}
|
|
78
42
|
return frameBuf;
|
|
79
43
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Decode a frame from binary format
|
|
82
|
-
* @param {Buffer|Uint8Array} data - Binary frame data
|
|
83
|
-
* @param {number} [offset=0] - Offset to start reading from
|
|
84
|
-
* @returns {Object} Decoded frame object with type, streamId, payload, and consumed bytes
|
|
85
|
-
* @throws {TcpV1FramingError} If frame is invalid
|
|
86
|
-
*/
|
|
87
44
|
export function decodeFrame(data, offset = 0) {
|
|
88
45
|
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
89
|
-
// Check if we have enough data for header
|
|
90
46
|
if (buf.length - offset < FRAME_HEADER_SIZE) {
|
|
91
47
|
throw new TcpV1FramingError(`Insufficient data for frame header: need ${FRAME_HEADER_SIZE} bytes, got ${buf.length - offset}`, 'INCOMPLETE_FRAME');
|
|
92
48
|
}
|
|
93
|
-
// If offset is provided, create a subarray view
|
|
94
49
|
const view = offset > 0 ? buf.subarray(offset) : buf;
|
|
95
|
-
// Read frame type (1 byte)
|
|
96
50
|
const type = view[0];
|
|
97
|
-
// Validate frame type
|
|
98
51
|
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
99
52
|
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
100
53
|
}
|
|
101
|
-
// Read streamId (4 bytes, big-endian, only lower 24 bits used)
|
|
102
54
|
const streamId = view.readUInt32BE(1) & 0x00FFFFFF;
|
|
103
|
-
// Read payload length (4 bytes, big-endian)
|
|
104
55
|
const payloadLength = view.readUInt32BE(5);
|
|
105
|
-
// Validate payload length
|
|
106
56
|
if (payloadLength > MAX_FRAME_PAYLOAD) {
|
|
107
57
|
throw new TcpV1FramingError(`Payload length ${payloadLength} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
|
|
108
58
|
}
|
|
109
|
-
// Check if we have enough data for payload
|
|
110
59
|
const totalFrameSize = FRAME_HEADER_SIZE + payloadLength;
|
|
111
60
|
if (view.length < totalFrameSize) {
|
|
112
61
|
throw new TcpV1FramingError(`Insufficient data for frame payload: need ${totalFrameSize} bytes, got ${view.length}`, 'INCOMPLETE_FRAME');
|
|
113
62
|
}
|
|
114
|
-
// StreamID validation (PING/PONG can have 0)
|
|
115
63
|
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
116
64
|
if (streamId === 0) {
|
|
117
65
|
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
118
66
|
}
|
|
119
67
|
}
|
|
120
|
-
// Read payload
|
|
121
68
|
const payload = payloadLength > 0
|
|
122
69
|
? view.subarray(FRAME_HEADER_SIZE, FRAME_HEADER_SIZE + payloadLength)
|
|
123
70
|
: Buffer.alloc(0);
|
|
@@ -128,24 +75,15 @@ export function decodeFrame(data, offset = 0) {
|
|
|
128
75
|
consumed: totalFrameSize,
|
|
129
76
|
};
|
|
130
77
|
}
|
|
131
|
-
/**
|
|
132
|
-
* Validate a frame for protocol violations
|
|
133
|
-
* @param {Object} frame - Frame object to validate
|
|
134
|
-
* @returns {void}
|
|
135
|
-
* @throws {TcpV1FramingError} If frame is invalid
|
|
136
|
-
*/
|
|
137
78
|
export function validateFrame(frame) {
|
|
138
79
|
const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
|
|
139
|
-
// Validate frame type
|
|
140
80
|
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
141
81
|
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
142
82
|
}
|
|
143
|
-
// Validate payload size
|
|
144
83
|
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
145
84
|
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
146
85
|
throw new TcpV1FramingError(`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
|
|
147
86
|
}
|
|
148
|
-
// StreamID validation (PING/PONG can have 0)
|
|
149
87
|
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
150
88
|
if (streamId === 0) {
|
|
151
89
|
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|