asyx 0.5.9 → 0.6.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/README.md +71 -9
- package/SECURITY.md +5 -3
- package/package.json +13 -23
- package/src/commands/name.js +23 -0
- package/src/commands/reject-messages.js +54 -0
- package/src/commands/setup.js +98 -102
- package/src/commands/tunnel.js +279 -2425
- package/src/lib/naming.js +81 -0
- package/src/main.js +9 -1
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +260 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +1088 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.d.ts +79 -0
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.js +138 -0
- package/vendor/tunnel-core/dist/control-plane/session.d.ts +50 -0
- package/vendor/tunnel-core/dist/control-plane/session.js +115 -0
- package/vendor/tunnel-core/dist/errors.d.ts +49 -0
- package/vendor/tunnel-core/dist/errors.js +53 -0
- package/vendor/tunnel-core/dist/flows/http.d.ts +50 -0
- package/vendor/tunnel-core/dist/flows/http.js +585 -0
- package/vendor/tunnel-core/dist/flows/tcp.d.ts +60 -0
- package/vendor/tunnel-core/dist/flows/tcp.js +546 -0
- package/vendor/tunnel-core/dist/flows/udp.d.ts +41 -0
- package/vendor/tunnel-core/dist/flows/udp.js +376 -0
- package/vendor/tunnel-core/dist/lib/adapt-logger.d.ts +2 -0
- package/vendor/tunnel-core/dist/lib/adapt-logger.js +6 -0
- package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +13 -0
- package/vendor/tunnel-core/dist/lib/fingerprint.js +95 -0
- package/vendor/tunnel-core/dist/lib/jwt-verifier.d.ts +27 -0
- package/vendor/tunnel-core/dist/lib/jwt-verifier.js +230 -0
- package/vendor/tunnel-core/dist/lib/logger.d.ts +9 -0
- package/vendor/tunnel-core/dist/lib/logger.js +58 -0
- package/vendor/tunnel-core/dist/lib/refresh-helpers.d.ts +16 -0
- package/vendor/tunnel-core/dist/lib/refresh-helpers.js +66 -0
- package/vendor/tunnel-core/dist/lifecycle.d.ts +24 -0
- package/vendor/tunnel-core/dist/lifecycle.js +29 -0
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.d.ts +56 -0
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.js +227 -0
- package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +58 -0
- package/vendor/tunnel-core/dist/mqtt/events/handlers.js +250 -0
- package/vendor/tunnel-core/dist/mqtt/events/index.d.ts +1 -0
- package/vendor/tunnel-core/dist/mqtt/events/index.js +191 -0
- package/vendor/tunnel-core/dist/mqtt/events/schemas.d.ts +498 -0
- package/vendor/tunnel-core/dist/mqtt/events/schemas.js +51 -0
- package/vendor/tunnel-core/dist/mqtt/ingress.d.ts +63 -0
- package/vendor/tunnel-core/dist/mqtt/ingress.js +170 -0
- package/vendor/tunnel-core/dist/state/assignment.d.ts +122 -0
- package/vendor/tunnel-core/dist/state/assignment.js +322 -0
- package/vendor/tunnel-core/dist/state/instance-registry.d.ts +88 -0
- package/{src → vendor/tunnel-core/dist}/state/instance-registry.js +38 -53
- package/vendor/tunnel-core/dist/tcp-v1/framing.d.ts +43 -0
- package/vendor/tunnel-core/dist/tcp-v1/framing.js +157 -0
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +137 -0
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +1591 -0
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.d.ts +13 -0
- package/{src → vendor/tunnel-core/dist}/tunnel/httpws-constants.js +5 -7
- package/vendor/tunnel-core/dist/tunnel/reconnect.d.ts +76 -0
- package/vendor/tunnel-core/dist/tunnel/reconnect.js +183 -0
- package/vendor/tunnel-core/dist/tunnel/request-sink.d.ts +40 -0
- package/vendor/tunnel-core/dist/tunnel/request-sink.js +39 -0
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +89 -0
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +838 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +90 -0
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +737 -0
- package/vendor/tunnel-core/dist/tunnel/ws-control.d.ts +22 -0
- package/vendor/tunnel-core/dist/tunnel/ws-control.js +124 -0
- package/vendor/tunnel-core/dist/types.d.ts +24 -0
- package/vendor/tunnel-core/dist/types.js +2 -0
- package/vendor/tunnel-core/dist/udp-v1/framing.d.ts +41 -0
- package/vendor/tunnel-core/dist/udp-v1/framing.js +158 -0
- package/vendor/tunnel-core/package.json +34 -0
- package/src/control-plane/MqttControlPlaneClient.js +0 -1196
- package/src/control-plane/MqttControlPlaneTransport.js +0 -150
- package/src/lib/fingerprint.js +0 -115
- package/src/lib/jwt-verifier.js +0 -260
- package/src/lib/logger.js +0 -62
- package/src/mqtt/awaiter-map.js +0 -247
- package/src/mqtt/events/handlers.js +0 -300
- package/src/mqtt/events/index.js +0 -222
- package/src/mqtt/events/schemas.js +0 -58
- package/src/mqtt/ingress.js +0 -182
- package/src/state/assignment.js +0 -333
- package/src/tcp-v1/framing.js +0 -203
- package/src/tunnel/http-tunnel.js +0 -1738
- package/src/tunnel/reconnect.js +0 -182
- package/src/tunnel/tcp-tunnel.js +0 -944
- package/src/tunnel/udp-tunnel.js +0 -813
- package/src/tunnel/ws-control.js +0 -135
- package/src/udp-v1/framing.js +0 -203
|
@@ -0,0 +1,322 @@
|
|
|
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
|
+
const assignmentByInstance = new Map();
|
|
6
|
+
// Reverse map: leaseId -> instanceId (for routing events that only have leaseId)
|
|
7
|
+
const leaseToInstance = new Map();
|
|
8
|
+
// Legacy: requestId -> waiters (for backward compatibility during transition)
|
|
9
|
+
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
|
+
*/
|
|
32
|
+
export function setAssignment(instanceId, assignment) {
|
|
33
|
+
// Backward compatibility: if instanceId is not provided, treat as legacy single-instance
|
|
34
|
+
if (typeof instanceId !== 'string' && typeof instanceId === 'object' && instanceId !== null) {
|
|
35
|
+
// Legacy call: setAssignment(assignment) - treat as single instance
|
|
36
|
+
const legacyAssignment = instanceId;
|
|
37
|
+
instanceId = 'legacy';
|
|
38
|
+
assignment = legacyAssignment;
|
|
39
|
+
}
|
|
40
|
+
const assignmentData = {
|
|
41
|
+
requestId: assignment.requestId,
|
|
42
|
+
relayId: assignment.relayId,
|
|
43
|
+
relayUrl: assignment.relayUrl,
|
|
44
|
+
jwt: assignment.jwt, // Stored in memory only
|
|
45
|
+
leaseMs: assignment.leaseMs,
|
|
46
|
+
leaseId: assignment.leaseId, // CLI-IMG-WSReconnect: Store leaseId for JWT refresh
|
|
47
|
+
quickpassSlug: assignment.quickpassSlug || null, // QPA-CLI-11: Store QuickPass slug for vault UX messaging
|
|
48
|
+
// TCP-S3: Store TCP-specific fields
|
|
49
|
+
tcpAliasFqdn: assignment.tcpAliasFqdn || null,
|
|
50
|
+
publicPort: assignment.publicPort || null,
|
|
51
|
+
fqdn: assignment.fqdn || null,
|
|
52
|
+
// E3-S8: Store SRV provisioning status
|
|
53
|
+
srvProvisioned: assignment.srvProvisioned === true,
|
|
54
|
+
srvFqdn: assignment.srvFqdn || null,
|
|
55
|
+
warnings: assignment.warnings || [],
|
|
56
|
+
// UDP-S1: Store UDP-specific fields
|
|
57
|
+
tunnelMode: assignment.tunnelMode || null,
|
|
58
|
+
protocol: assignment.protocol || null,
|
|
59
|
+
assignmentJwt: assignment.assignmentJwt || null, // UDP-S1: Store assignmentJwt separately
|
|
60
|
+
timestamp: Date.now(),
|
|
61
|
+
lastRefreshMs: undefined,
|
|
62
|
+
lastRefreshIso: undefined,
|
|
63
|
+
lastRefreshDeltaMs: undefined
|
|
64
|
+
};
|
|
65
|
+
// Store per instance
|
|
66
|
+
assignmentByInstance.set(instanceId, assignmentData);
|
|
67
|
+
// Update lease -> instance mapping
|
|
68
|
+
if (assignment.leaseId) {
|
|
69
|
+
leaseToInstance.set(assignment.leaseId, instanceId);
|
|
70
|
+
}
|
|
71
|
+
// Notify any waiters that are tracking this requestId (backward compatibility)
|
|
72
|
+
if (assignment.requestId && assignmentWaiters.size > 0) {
|
|
73
|
+
const waiters = assignmentWaiters.get(assignment.requestId);
|
|
74
|
+
if (waiters && waiters.length) {
|
|
75
|
+
assignmentWaiters.delete(assignment.requestId);
|
|
76
|
+
for (const waiter of waiters) {
|
|
77
|
+
clearTimeout(waiter.timer);
|
|
78
|
+
try {
|
|
79
|
+
waiter.resolve({ ...assignmentData });
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// Ignore waiter resolution errors; they should not impact CLI flow
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Get assignment for a specific tunnel instance
|
|
90
|
+
* TPB-12: Per-instance assignment retrieval
|
|
91
|
+
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
92
|
+
* @returns {Object|null} Assignment for the instance or null if none set
|
|
93
|
+
*/
|
|
94
|
+
export function getAssignment(instanceId) {
|
|
95
|
+
// Backward compatibility: if no instanceId provided, try to get from single instance or return null
|
|
96
|
+
if (!instanceId) {
|
|
97
|
+
// Legacy mode: if there's only one instance, return it
|
|
98
|
+
if (assignmentByInstance.size === 1) {
|
|
99
|
+
const [firstInstanceId] = assignmentByInstance.keys();
|
|
100
|
+
const assignment = assignmentByInstance.get(firstInstanceId);
|
|
101
|
+
return assignment ? { ...assignment } : null;
|
|
102
|
+
}
|
|
103
|
+
// Multiple instances or none - return null (caller must provide instanceId)
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const assignment = assignmentByInstance.get(instanceId);
|
|
107
|
+
return assignment ? { ...assignment } : null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Resolve instanceId from leaseId
|
|
111
|
+
* TPB-12: Helper to route events that only have leaseId
|
|
112
|
+
* @param {string} leaseId - Lease ID
|
|
113
|
+
* @returns {string|null} Instance ID or null if not found
|
|
114
|
+
*/
|
|
115
|
+
export function resolveInstanceByLease(leaseId) {
|
|
116
|
+
return leaseToInstance.get(leaseId) ?? null;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Clear assignment for a specific tunnel instance
|
|
120
|
+
* TPB-12: Per-instance assignment clearing
|
|
121
|
+
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
122
|
+
*/
|
|
123
|
+
export function clearAssignment(instanceId) {
|
|
124
|
+
if (!instanceId) {
|
|
125
|
+
// Legacy: clear all instances
|
|
126
|
+
assignmentByInstance.clear();
|
|
127
|
+
leaseToInstance.clear();
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const assignment = assignmentByInstance.get(instanceId);
|
|
131
|
+
if (assignment?.leaseId) {
|
|
132
|
+
leaseToInstance.delete(assignment.leaseId);
|
|
133
|
+
}
|
|
134
|
+
assignmentByInstance.delete(instanceId);
|
|
135
|
+
}
|
|
136
|
+
// Reject all outstanding waiters to avoid dangling promises
|
|
137
|
+
if (assignmentWaiters.size > 0) {
|
|
138
|
+
for (const [, waiters] of assignmentWaiters) {
|
|
139
|
+
for (const waiter of waiters) {
|
|
140
|
+
clearTimeout(waiter.timer);
|
|
141
|
+
try {
|
|
142
|
+
waiter.reject(new Error("Assignment cleared before completion"));
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// Ignore waiter rejections; consumers can handle silently
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
assignmentWaiters.clear();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Drop one instance key without touching a lease mapping that already points elsewhere.
|
|
154
|
+
* Does not reject assignment waiters.
|
|
155
|
+
* @param {string} instanceId
|
|
156
|
+
*/
|
|
157
|
+
export function forgetInstanceAssignment(instanceId) {
|
|
158
|
+
if (!instanceId)
|
|
159
|
+
return;
|
|
160
|
+
const assignment = assignmentByInstance.get(instanceId);
|
|
161
|
+
if (!assignment)
|
|
162
|
+
return;
|
|
163
|
+
if (assignment.leaseId && leaseToInstance.get(assignment.leaseId) === instanceId) {
|
|
164
|
+
leaseToInstance.delete(assignment.leaseId);
|
|
165
|
+
}
|
|
166
|
+
assignmentByInstance.delete(instanceId);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Copy an assignment onto a new instance id and drop the old key.
|
|
170
|
+
* When a tunnel object is passed, its instanceId moves with the assignment.
|
|
171
|
+
* @param {{ oldId: string, newId: string, tunnel?: { instanceId?: string } | null }} opts
|
|
172
|
+
* @returns {{ moved: boolean, leaseId: string | null, size: number }}
|
|
173
|
+
*/
|
|
174
|
+
export function migrateInstanceAssignment({ oldId, newId, tunnel } = /** @type {any} */ ({})) {
|
|
175
|
+
const oldAssignment = oldId ? getAssignment(oldId) : null;
|
|
176
|
+
const moved = !!(oldAssignment && newId && oldId !== newId);
|
|
177
|
+
if (moved) {
|
|
178
|
+
setAssignment(newId, oldAssignment);
|
|
179
|
+
forgetInstanceAssignment(oldId);
|
|
180
|
+
}
|
|
181
|
+
if (tunnel) {
|
|
182
|
+
tunnel.instanceId = newId;
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
moved,
|
|
186
|
+
leaseId: oldAssignment?.leaseId || null,
|
|
187
|
+
size: assignmentByInstance.size,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Assignment stored for this instance after a refresh ack.
|
|
192
|
+
* Throws when the keyed entry is missing or the JWT has no exp.
|
|
193
|
+
* @param {string} instanceId
|
|
194
|
+
* @returns {{ assignment: Object, expMs: number }}
|
|
195
|
+
*/
|
|
196
|
+
export function readRefreshAssignment(instanceId) {
|
|
197
|
+
const newAssignment = getAssignment(instanceId);
|
|
198
|
+
if (!newAssignment) {
|
|
199
|
+
throw new Error("No new assignment after refresh");
|
|
200
|
+
}
|
|
201
|
+
const parts = typeof newAssignment.jwt === 'string' ? newAssignment.jwt.split('.') : [];
|
|
202
|
+
if (parts.length !== 3) {
|
|
203
|
+
throw new Error("Invalid JWT format in new assignment");
|
|
204
|
+
}
|
|
205
|
+
const payload = JSON.parse(Buffer.from(parts[1].replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'));
|
|
206
|
+
const expMs = payload.exp ? payload.exp * 1000 : null;
|
|
207
|
+
if (!expMs) {
|
|
208
|
+
throw new Error("No expiration in new JWT");
|
|
209
|
+
}
|
|
210
|
+
return { assignment: newAssignment, expMs };
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Check if an assignment exists for a specific tunnel instance
|
|
214
|
+
* TPB-12: Per-instance assignment check
|
|
215
|
+
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
216
|
+
* @returns {boolean} True if assignment exists for the instance
|
|
217
|
+
*/
|
|
218
|
+
export function hasAssignment(instanceId) {
|
|
219
|
+
if (!instanceId) {
|
|
220
|
+
// Legacy: check if any assignment exists
|
|
221
|
+
return assignmentByInstance.size > 0;
|
|
222
|
+
}
|
|
223
|
+
return assignmentByInstance.has(instanceId);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Wait until an assignment with the specified requestId is available.
|
|
227
|
+
* @param {string} requestId - Request ID to wait for
|
|
228
|
+
* @param {number} [timeoutMs=10000] - Timeout before rejecting
|
|
229
|
+
* @returns {Promise<Object>} resolves with the assignment snapshot
|
|
230
|
+
*/
|
|
231
|
+
export function waitForAssignment(requestId, timeoutMs = 10000) {
|
|
232
|
+
if (!requestId) {
|
|
233
|
+
return Promise.reject(new Error("waitForAssignment requires a requestId"));
|
|
234
|
+
}
|
|
235
|
+
// Check all instances for matching requestId (backward compatibility)
|
|
236
|
+
for (const [instanceId, assignment] of assignmentByInstance) {
|
|
237
|
+
if (assignment.requestId === requestId) {
|
|
238
|
+
return Promise.resolve({ ...assignment });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return new Promise((resolve, reject) => {
|
|
242
|
+
const entry = {
|
|
243
|
+
resolve,
|
|
244
|
+
reject,
|
|
245
|
+
timer: setTimeout(() => {
|
|
246
|
+
// Timeout - remove from waiter list and reject
|
|
247
|
+
const queue = assignmentWaiters.get(requestId);
|
|
248
|
+
if (queue) {
|
|
249
|
+
const idx = queue.indexOf(entry);
|
|
250
|
+
if (idx >= 0)
|
|
251
|
+
queue.splice(idx, 1);
|
|
252
|
+
if (queue.length === 0)
|
|
253
|
+
assignmentWaiters.delete(requestId);
|
|
254
|
+
}
|
|
255
|
+
reject(new Error(`Assignment ${requestId} not received within ${timeoutMs}ms`));
|
|
256
|
+
}, timeoutMs),
|
|
257
|
+
};
|
|
258
|
+
const queue = assignmentWaiters.get(requestId);
|
|
259
|
+
if (queue) {
|
|
260
|
+
queue.push(entry);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
assignmentWaiters.set(requestId, [entry]);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Get assignment metadata (without JWT for logging)
|
|
269
|
+
* TPB-12: Per-instance metadata retrieval
|
|
270
|
+
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
271
|
+
* @returns {Object|null} Assignment metadata or null
|
|
272
|
+
*/
|
|
273
|
+
export function getAssignmentMetadata(instanceId) {
|
|
274
|
+
const assignment = getAssignment(instanceId);
|
|
275
|
+
if (!assignment)
|
|
276
|
+
return null;
|
|
277
|
+
return {
|
|
278
|
+
requestId: assignment.requestId,
|
|
279
|
+
relayId: assignment.relayId,
|
|
280
|
+
relayUrl: assignment.relayUrl,
|
|
281
|
+
leaseMs: assignment.leaseMs,
|
|
282
|
+
leaseId: assignment.leaseId,
|
|
283
|
+
timestamp: assignment.timestamp,
|
|
284
|
+
lastRefreshIso: assignment.lastRefreshIso,
|
|
285
|
+
lastRefreshDeltaMs: assignment.lastRefreshDeltaMs
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Record a successful refresh in the assignment cache and return delta ms since last refresh (or since initial assignment if none).
|
|
290
|
+
* TPB-12: Per-instance refresh recording
|
|
291
|
+
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
292
|
+
* @param {number} [nowMs]
|
|
293
|
+
* @returns {number} delta milliseconds since last refresh or initial assignment
|
|
294
|
+
*/
|
|
295
|
+
export function recordRefresh(instanceId, nowMs = Date.now()) {
|
|
296
|
+
// Backward compatibility: if instanceId is a number, treat as legacy
|
|
297
|
+
if (typeof instanceId === 'number') {
|
|
298
|
+
nowMs = instanceId;
|
|
299
|
+
instanceId = null;
|
|
300
|
+
}
|
|
301
|
+
const assignment = getAssignment(instanceId);
|
|
302
|
+
if (!assignment)
|
|
303
|
+
return 0;
|
|
304
|
+
const prev = typeof assignment.lastRefreshMs === 'number' ? assignment.lastRefreshMs : assignment.timestamp;
|
|
305
|
+
const delta = nowMs - prev;
|
|
306
|
+
// Update the assignment in the map
|
|
307
|
+
assignment.lastRefreshMs = nowMs;
|
|
308
|
+
assignment.lastRefreshIso = new Date(nowMs).toISOString();
|
|
309
|
+
assignment.lastRefreshDeltaMs = delta;
|
|
310
|
+
// Update the map entry
|
|
311
|
+
if (instanceId) {
|
|
312
|
+
assignmentByInstance.set(instanceId, assignment);
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
// Legacy: update first instance found
|
|
316
|
+
const [firstInstanceId] = assignmentByInstance.keys();
|
|
317
|
+
if (firstInstanceId) {
|
|
318
|
+
assignmentByInstance.set(firstInstanceId, assignment);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return delta;
|
|
322
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Register a tunnel instance
|
|
3
|
+
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
4
|
+
* @param {Object} context - Instance context
|
|
5
|
+
* @param {string} context.tunnelType - Tunnel type ('http' | 'tcp' | 'udp')
|
|
6
|
+
* @param {Function} context.setAssignment - Per-instance setAssignment callback
|
|
7
|
+
* @param {Function} context.getAssignment - Per-instance getAssignment callback
|
|
8
|
+
* @param {Object} [context.metadata] - Additional metadata
|
|
9
|
+
*/
|
|
10
|
+
export function registerInstance(instanceId: string, context: {
|
|
11
|
+
tunnelType: string;
|
|
12
|
+
setAssignment: Function;
|
|
13
|
+
getAssignment: Function;
|
|
14
|
+
metadata?: any;
|
|
15
|
+
}): void;
|
|
16
|
+
/**
|
|
17
|
+
* Unregister a tunnel instance
|
|
18
|
+
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
19
|
+
*/
|
|
20
|
+
export function unregisterInstance(instanceId: string): void;
|
|
21
|
+
/**
|
|
22
|
+
* Get instance context by instanceId
|
|
23
|
+
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
24
|
+
* @returns {InstanceContext|null} Instance context or null if not found
|
|
25
|
+
*/
|
|
26
|
+
export function getInstance(instanceId: string): InstanceContext | null;
|
|
27
|
+
/**
|
|
28
|
+
* Get all registered instances (for debugging)
|
|
29
|
+
* @returns {Array<InstanceContext>} Array of all instance contexts
|
|
30
|
+
*/
|
|
31
|
+
export function getAllInstances(): Array<InstanceContext>;
|
|
32
|
+
/**
|
|
33
|
+
* Set expected requestId for an instance (for pre-assignment routing)
|
|
34
|
+
* TPB-12: Track requestId before assignment exists so open_ack can be routed
|
|
35
|
+
*
|
|
36
|
+
* Note: This automatically clears any previous requestId for this instance,
|
|
37
|
+
* so reconnect/retry flows that use a new requestId are handled correctly.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
40
|
+
* @param {string} requestId - Expected requestId from cmd/open
|
|
41
|
+
*/
|
|
42
|
+
export function setExpectedRequestId(instanceId: string, requestId: string): void;
|
|
43
|
+
/**
|
|
44
|
+
* Clear expected requestId for an instance (called after assignment is created)
|
|
45
|
+
* TPB-12: Clean up requestId mapping once assignment exists (routing uses leaseId after that)
|
|
46
|
+
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
47
|
+
*/
|
|
48
|
+
export function clearExpectedRequestId(instanceId: string): void;
|
|
49
|
+
/**
|
|
50
|
+
* Resolve instanceId from requestId (for pre-assignment routing)
|
|
51
|
+
* TPB-12: Route open_ack events by requestId before assignment exists
|
|
52
|
+
* @param {string} requestId - Request ID from cmd/open
|
|
53
|
+
* @returns {string|null} Instance ID or null if not found
|
|
54
|
+
*/
|
|
55
|
+
export function resolveInstanceByRequestId(requestId: string): string | null;
|
|
56
|
+
/**
|
|
57
|
+
* Clear all instances (for cleanup/testing)
|
|
58
|
+
*/
|
|
59
|
+
export function clearAll(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Instance context for a tunnel instance
|
|
62
|
+
*/
|
|
63
|
+
export type InstanceContext = {
|
|
64
|
+
/**
|
|
65
|
+
* - Full MQTT clientId (instanceId)
|
|
66
|
+
*/
|
|
67
|
+
instanceId: string;
|
|
68
|
+
/**
|
|
69
|
+
* - Tunnel type ('http' | 'tcp' | 'udp')
|
|
70
|
+
*/
|
|
71
|
+
tunnelType: string;
|
|
72
|
+
/**
|
|
73
|
+
* - Per-instance setAssignment callback
|
|
74
|
+
*/
|
|
75
|
+
setAssignment: Function;
|
|
76
|
+
/**
|
|
77
|
+
* - Per-instance getAssignment callback
|
|
78
|
+
*/
|
|
79
|
+
getAssignment: Function;
|
|
80
|
+
/**
|
|
81
|
+
* - Additional metadata (baseClientId, etc.)
|
|
82
|
+
*/
|
|
83
|
+
metadata?: any;
|
|
84
|
+
/**
|
|
85
|
+
* - Expected requestId for initial open_ack routing
|
|
86
|
+
*/
|
|
87
|
+
expectedRequestId?: string;
|
|
88
|
+
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// TPB-12: Instance Registry for per-tunnel routing
|
|
2
2
|
// Tracks tunnel instances and their context (setAssignment callback, tunnel type, etc.)
|
|
3
|
-
|
|
4
3
|
/**
|
|
5
4
|
* Instance context for a tunnel instance
|
|
6
5
|
* @typedef {Object} InstanceContext
|
|
@@ -11,14 +10,11 @@
|
|
|
11
10
|
* @property {Object} [metadata] - Additional metadata (baseClientId, etc.)
|
|
12
11
|
* @property {string} [expectedRequestId] - Expected requestId for initial open_ack routing
|
|
13
12
|
*/
|
|
14
|
-
|
|
15
13
|
// Global instance registry: instanceId -> InstanceContext
|
|
16
14
|
const instanceRegistry = new Map();
|
|
17
|
-
|
|
18
15
|
// TPB-12: Track expected requestIds per instance for pre-assignment routing
|
|
19
16
|
// requestId -> instanceId (for routing open_ack before assignment exists)
|
|
20
17
|
const requestIdToInstance = new Map();
|
|
21
|
-
|
|
22
18
|
/**
|
|
23
19
|
* Register a tunnel instance
|
|
24
20
|
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
@@ -29,87 +25,78 @@ const requestIdToInstance = new Map();
|
|
|
29
25
|
* @param {Object} [context.metadata] - Additional metadata
|
|
30
26
|
*/
|
|
31
27
|
export function registerInstance(instanceId, context) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
});
|
|
28
|
+
if (!instanceId || typeof instanceId !== 'string') {
|
|
29
|
+
throw new Error('instanceId must be a non-empty string');
|
|
30
|
+
}
|
|
31
|
+
instanceRegistry.set(instanceId, {
|
|
32
|
+
instanceId,
|
|
33
|
+
tunnelType: context.tunnelType || 'unknown',
|
|
34
|
+
setAssignment: context.setAssignment,
|
|
35
|
+
getAssignment: context.getAssignment,
|
|
36
|
+
metadata: context.metadata || {}
|
|
37
|
+
});
|
|
43
38
|
}
|
|
44
|
-
|
|
45
39
|
/**
|
|
46
40
|
* Unregister a tunnel instance
|
|
47
41
|
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
48
42
|
*/
|
|
49
43
|
export function unregisterInstance(instanceId) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
const context = instanceRegistry.get(instanceId);
|
|
45
|
+
if (context?.expectedRequestId) {
|
|
46
|
+
requestIdToInstance.delete(context.expectedRequestId);
|
|
47
|
+
}
|
|
48
|
+
instanceRegistry.delete(instanceId);
|
|
55
49
|
}
|
|
56
|
-
|
|
57
50
|
/**
|
|
58
51
|
* Get instance context by instanceId
|
|
59
52
|
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
60
53
|
* @returns {InstanceContext|null} Instance context or null if not found
|
|
61
54
|
*/
|
|
62
55
|
export function getInstance(instanceId) {
|
|
63
|
-
|
|
56
|
+
return instanceRegistry.get(instanceId) ?? null;
|
|
64
57
|
}
|
|
65
|
-
|
|
66
58
|
/**
|
|
67
59
|
* Get all registered instances (for debugging)
|
|
68
60
|
* @returns {Array<InstanceContext>} Array of all instance contexts
|
|
69
61
|
*/
|
|
70
62
|
export function getAllInstances() {
|
|
71
|
-
|
|
63
|
+
return Array.from(instanceRegistry.values());
|
|
72
64
|
}
|
|
73
|
-
|
|
74
65
|
/**
|
|
75
66
|
* Set expected requestId for an instance (for pre-assignment routing)
|
|
76
67
|
* TPB-12: Track requestId before assignment exists so open_ack can be routed
|
|
77
|
-
*
|
|
68
|
+
*
|
|
78
69
|
* Note: This automatically clears any previous requestId for this instance,
|
|
79
70
|
* so reconnect/retry flows that use a new requestId are handled correctly.
|
|
80
|
-
*
|
|
71
|
+
*
|
|
81
72
|
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
82
73
|
* @param {string} requestId - Expected requestId from cmd/open
|
|
83
74
|
*/
|
|
84
75
|
export function setExpectedRequestId(instanceId, requestId) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
context.expectedRequestId = requestId;
|
|
97
|
-
requestIdToInstance.set(requestId, instanceId);
|
|
76
|
+
const context = instanceRegistry.get(instanceId);
|
|
77
|
+
if (!context) {
|
|
78
|
+
throw new Error(`Instance ${instanceId} not found in registry`);
|
|
79
|
+
}
|
|
80
|
+
// Remove old requestId mapping if exists (handles reconnect/retry with new requestId)
|
|
81
|
+
if (context.expectedRequestId) {
|
|
82
|
+
requestIdToInstance.delete(context.expectedRequestId);
|
|
83
|
+
}
|
|
84
|
+
// Set new requestId
|
|
85
|
+
context.expectedRequestId = requestId;
|
|
86
|
+
requestIdToInstance.set(requestId, instanceId);
|
|
98
87
|
}
|
|
99
|
-
|
|
100
88
|
/**
|
|
101
89
|
* Clear expected requestId for an instance (called after assignment is created)
|
|
102
90
|
* TPB-12: Clean up requestId mapping once assignment exists (routing uses leaseId after that)
|
|
103
91
|
* @param {string} instanceId - Full MQTT clientId (instanceId)
|
|
104
92
|
*/
|
|
105
93
|
export function clearExpectedRequestId(instanceId) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
94
|
+
const context = instanceRegistry.get(instanceId);
|
|
95
|
+
if (context?.expectedRequestId) {
|
|
96
|
+
requestIdToInstance.delete(context.expectedRequestId);
|
|
97
|
+
context.expectedRequestId = undefined;
|
|
98
|
+
}
|
|
111
99
|
}
|
|
112
|
-
|
|
113
100
|
/**
|
|
114
101
|
* Resolve instanceId from requestId (for pre-assignment routing)
|
|
115
102
|
* TPB-12: Route open_ack events by requestId before assignment exists
|
|
@@ -117,14 +104,12 @@ export function clearExpectedRequestId(instanceId) {
|
|
|
117
104
|
* @returns {string|null} Instance ID or null if not found
|
|
118
105
|
*/
|
|
119
106
|
export function resolveInstanceByRequestId(requestId) {
|
|
120
|
-
|
|
107
|
+
return requestIdToInstance.get(requestId) ?? null;
|
|
121
108
|
}
|
|
122
|
-
|
|
123
109
|
/**
|
|
124
110
|
* Clear all instances (for cleanup/testing)
|
|
125
111
|
*/
|
|
126
112
|
export function clearAll() {
|
|
127
|
-
|
|
128
|
-
|
|
113
|
+
instanceRegistry.clear();
|
|
114
|
+
requestIdToInstance.clear();
|
|
129
115
|
}
|
|
130
|
-
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encode a frame to binary format
|
|
3
|
+
* @param {Object} frame - Frame object with type, streamId, and payload
|
|
4
|
+
* @param {number} frame.type - Frame type (FrameType constant)
|
|
5
|
+
* @param {number} frame.streamId - Stream ID (24-bit, 0 for PING/PONG)
|
|
6
|
+
* @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
|
|
7
|
+
* @returns {Buffer} Encoded frame
|
|
8
|
+
* @throws {TcpV1FramingError} If frame is invalid
|
|
9
|
+
*/
|
|
10
|
+
export function encodeFrame(frame: {
|
|
11
|
+
type: number;
|
|
12
|
+
streamId: number;
|
|
13
|
+
payload: Buffer | Uint8Array;
|
|
14
|
+
}): Buffer;
|
|
15
|
+
/**
|
|
16
|
+
* Decode a frame from binary format
|
|
17
|
+
* @param {Buffer|Uint8Array} data - Binary frame data
|
|
18
|
+
* @param {number} [offset=0] - Offset to start reading from
|
|
19
|
+
* @returns {Object} Decoded frame object with type, streamId, payload, and consumed bytes
|
|
20
|
+
* @throws {TcpV1FramingError} If frame is invalid
|
|
21
|
+
*/
|
|
22
|
+
export function decodeFrame(data: Buffer | Uint8Array, offset?: number): any;
|
|
23
|
+
/**
|
|
24
|
+
* Validate a frame for protocol violations
|
|
25
|
+
* @param {Object} frame - Frame object to validate
|
|
26
|
+
* @returns {void}
|
|
27
|
+
* @throws {TcpV1FramingError} If frame is invalid
|
|
28
|
+
*/
|
|
29
|
+
export function validateFrame(frame: any): void;
|
|
30
|
+
export namespace FrameType {
|
|
31
|
+
let STREAM_OPEN: number;
|
|
32
|
+
let STREAM_DATA: number;
|
|
33
|
+
let STREAM_CLOSE: number;
|
|
34
|
+
let STREAM_ERROR: number;
|
|
35
|
+
let PING: number;
|
|
36
|
+
let PONG: number;
|
|
37
|
+
}
|
|
38
|
+
export const MAX_FRAME_PAYLOAD: number;
|
|
39
|
+
export const FRAME_HEADER_SIZE: 9;
|
|
40
|
+
export class TcpV1FramingError extends Error {
|
|
41
|
+
constructor(message: any, code: any);
|
|
42
|
+
code: any;
|
|
43
|
+
}
|