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
package/src/state/assignment.js
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
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
|
-
|
|
5
|
-
// Per-instance assignment storage (instanceId -> assignment)
|
|
6
|
-
const assignmentByInstance = new Map();
|
|
7
|
-
|
|
8
|
-
// Reverse map: leaseId -> instanceId (for routing events that only have leaseId)
|
|
9
|
-
const leaseToInstance = new Map();
|
|
10
|
-
|
|
11
|
-
// Legacy: requestId -> waiters (for backward compatibility during transition)
|
|
12
|
-
const assignmentWaiters = new Map();
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Set assignment for a specific tunnel instance
|
|
16
|
-
* TPB-12: Per-instance assignment storage to prevent cross-wiring
|
|
17
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
18
|
-
* @param {Object} assignment - Assignment data
|
|
19
|
-
* @param {string} assignment.requestId - Request ID
|
|
20
|
-
* @param {string} assignment.relayId - Relay ID
|
|
21
|
-
* @param {string} assignment.relayUrl - Relay URL
|
|
22
|
-
* @param {string} assignment.jwt - JWT token (stored in memory only)
|
|
23
|
-
* @param {number} assignment.leaseMs - Lease duration in milliseconds
|
|
24
|
-
* @param {string} assignment.leaseId - Lease ID for JWT refresh (optional)
|
|
25
|
-
*/
|
|
26
|
-
export function setAssignment(instanceId, assignment) {
|
|
27
|
-
// Backward compatibility: if instanceId is not provided, treat as legacy single-instance
|
|
28
|
-
if (typeof instanceId !== 'string' && typeof instanceId === 'object' && instanceId !== null) {
|
|
29
|
-
// Legacy call: setAssignment(assignment) - treat as single instance
|
|
30
|
-
const legacyAssignment = instanceId;
|
|
31
|
-
instanceId = 'legacy';
|
|
32
|
-
assignment = legacyAssignment;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const assignmentData = {
|
|
36
|
-
requestId: assignment.requestId,
|
|
37
|
-
relayId: assignment.relayId,
|
|
38
|
-
relayUrl: assignment.relayUrl,
|
|
39
|
-
jwt: assignment.jwt, // Stored in memory only
|
|
40
|
-
leaseMs: assignment.leaseMs,
|
|
41
|
-
leaseId: assignment.leaseId, // CLI-IMG-WSReconnect: Store leaseId for JWT refresh
|
|
42
|
-
quickpassSlug: assignment.quickpassSlug || null, // QPA-CLI-11: Store QuickPass slug for vault UX messaging
|
|
43
|
-
// TCP-S3: Store TCP-specific fields
|
|
44
|
-
tcpAliasFqdn: assignment.tcpAliasFqdn || null,
|
|
45
|
-
publicPort: assignment.publicPort || null,
|
|
46
|
-
fqdn: assignment.fqdn || null,
|
|
47
|
-
// E3-S8: Store SRV provisioning status
|
|
48
|
-
srvProvisioned: assignment.srvProvisioned === true,
|
|
49
|
-
srvFqdn: assignment.srvFqdn || null,
|
|
50
|
-
warnings: assignment.warnings || [],
|
|
51
|
-
// UDP-S1: Store UDP-specific fields
|
|
52
|
-
tunnelMode: assignment.tunnelMode || null,
|
|
53
|
-
protocol: assignment.protocol || null,
|
|
54
|
-
assignmentJwt: assignment.assignmentJwt || null, // UDP-S1: Store assignmentJwt separately
|
|
55
|
-
timestamp: Date.now(),
|
|
56
|
-
lastRefreshMs: undefined,
|
|
57
|
-
lastRefreshIso: undefined,
|
|
58
|
-
lastRefreshDeltaMs: undefined
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// Store per instance
|
|
62
|
-
assignmentByInstance.set(instanceId, assignmentData);
|
|
63
|
-
|
|
64
|
-
// Update lease -> instance mapping
|
|
65
|
-
if (assignment.leaseId) {
|
|
66
|
-
leaseToInstance.set(assignment.leaseId, instanceId);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Notify any waiters that are tracking this requestId (backward compatibility)
|
|
70
|
-
if (assignment.requestId && assignmentWaiters.size > 0) {
|
|
71
|
-
const waiters = assignmentWaiters.get(assignment.requestId);
|
|
72
|
-
if (waiters && waiters.length) {
|
|
73
|
-
assignmentWaiters.delete(assignment.requestId);
|
|
74
|
-
for (const waiter of waiters) {
|
|
75
|
-
clearTimeout(waiter.timer);
|
|
76
|
-
try {
|
|
77
|
-
waiter.resolve({ ...assignmentData });
|
|
78
|
-
} catch {
|
|
79
|
-
// Ignore waiter resolution errors; they should not impact CLI flow
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Get assignment for a specific tunnel instance
|
|
88
|
-
* TPB-12: Per-instance assignment retrieval
|
|
89
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
90
|
-
* @returns {Object|null} Assignment for the instance or null if none set
|
|
91
|
-
*/
|
|
92
|
-
export function getAssignment(instanceId) {
|
|
93
|
-
// Backward compatibility: if no instanceId provided, try to get from single instance or return null
|
|
94
|
-
if (!instanceId) {
|
|
95
|
-
// Legacy mode: if there's only one instance, return it
|
|
96
|
-
if (assignmentByInstance.size === 1) {
|
|
97
|
-
const [firstInstanceId] = assignmentByInstance.keys();
|
|
98
|
-
const assignment = assignmentByInstance.get(firstInstanceId);
|
|
99
|
-
return assignment ? { ...assignment } : null;
|
|
100
|
-
}
|
|
101
|
-
// Multiple instances or none - return null (caller must provide instanceId)
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const assignment = assignmentByInstance.get(instanceId);
|
|
106
|
-
return assignment ? { ...assignment } : null;
|
|
107
|
-
}
|
|
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
|
-
/**
|
|
120
|
-
* Clear assignment for a specific tunnel instance
|
|
121
|
-
* TPB-12: Per-instance assignment clearing
|
|
122
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
123
|
-
*/
|
|
124
|
-
export function clearAssignment(instanceId) {
|
|
125
|
-
if (!instanceId) {
|
|
126
|
-
// Legacy: clear all instances
|
|
127
|
-
assignmentByInstance.clear();
|
|
128
|
-
leaseToInstance.clear();
|
|
129
|
-
} else {
|
|
130
|
-
const assignment = assignmentByInstance.get(instanceId);
|
|
131
|
-
if (assignment?.leaseId) {
|
|
132
|
-
leaseToInstance.delete(assignment.leaseId);
|
|
133
|
-
}
|
|
134
|
-
assignmentByInstance.delete(instanceId);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Reject all outstanding waiters to avoid dangling promises
|
|
138
|
-
if (assignmentWaiters.size > 0) {
|
|
139
|
-
for (const [, waiters] of assignmentWaiters) {
|
|
140
|
-
for (const waiter of waiters) {
|
|
141
|
-
clearTimeout(waiter.timer);
|
|
142
|
-
try {
|
|
143
|
-
waiter.reject(new Error("Assignment cleared before completion"));
|
|
144
|
-
} catch {
|
|
145
|
-
// Ignore waiter rejections; consumers can handle silently
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
assignmentWaiters.clear();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Drop one instance key without touching a lease mapping that already points elsewhere.
|
|
155
|
-
* Does not reject assignment waiters.
|
|
156
|
-
* @param {string} instanceId
|
|
157
|
-
*/
|
|
158
|
-
export function forgetInstanceAssignment(instanceId) {
|
|
159
|
-
if (!instanceId) return;
|
|
160
|
-
const assignment = assignmentByInstance.get(instanceId);
|
|
161
|
-
if (!assignment) return;
|
|
162
|
-
if (assignment.leaseId && leaseToInstance.get(assignment.leaseId) === instanceId) {
|
|
163
|
-
leaseToInstance.delete(assignment.leaseId);
|
|
164
|
-
}
|
|
165
|
-
assignmentByInstance.delete(instanceId);
|
|
166
|
-
}
|
|
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 } = {}) {
|
|
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
|
-
/**
|
|
192
|
-
* Assignment stored for this instance after a refresh ack.
|
|
193
|
-
* Throws when the keyed entry is missing or the JWT has no exp.
|
|
194
|
-
* @param {string} instanceId
|
|
195
|
-
* @returns {{ assignment: Object, expMs: number }}
|
|
196
|
-
*/
|
|
197
|
-
export function readRefreshAssignment(instanceId) {
|
|
198
|
-
const newAssignment = getAssignment(instanceId);
|
|
199
|
-
if (!newAssignment) {
|
|
200
|
-
throw new Error("No new assignment after refresh");
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const parts = typeof newAssignment.jwt === 'string' ? newAssignment.jwt.split('.') : [];
|
|
204
|
-
if (parts.length !== 3) {
|
|
205
|
-
throw new Error("Invalid JWT format in new assignment");
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const payload = JSON.parse(Buffer.from(parts[1].replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'));
|
|
209
|
-
const expMs = payload.exp ? payload.exp * 1000 : null;
|
|
210
|
-
if (!expMs) {
|
|
211
|
-
throw new Error("No expiration in new JWT");
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return { assignment: newAssignment, expMs };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Check if an assignment exists for a specific tunnel instance
|
|
219
|
-
* TPB-12: Per-instance assignment check
|
|
220
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
221
|
-
* @returns {boolean} True if assignment exists for the instance
|
|
222
|
-
*/
|
|
223
|
-
export function hasAssignment(instanceId) {
|
|
224
|
-
if (!instanceId) {
|
|
225
|
-
// Legacy: check if any assignment exists
|
|
226
|
-
return assignmentByInstance.size > 0;
|
|
227
|
-
}
|
|
228
|
-
return assignmentByInstance.has(instanceId);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Wait until an assignment with the specified requestId is available.
|
|
233
|
-
* @param {string} requestId - Request ID to wait for
|
|
234
|
-
* @param {number} [timeoutMs=10000] - Timeout before rejecting
|
|
235
|
-
* @returns {Promise<Object>} resolves with the assignment snapshot
|
|
236
|
-
*/
|
|
237
|
-
export function waitForAssignment(requestId, timeoutMs = 10000) {
|
|
238
|
-
if (!requestId) {
|
|
239
|
-
return Promise.reject(new Error("waitForAssignment requires a requestId"));
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Check all instances for matching requestId (backward compatibility)
|
|
243
|
-
for (const [instanceId, assignment] of assignmentByInstance) {
|
|
244
|
-
if (assignment.requestId === requestId) {
|
|
245
|
-
return Promise.resolve({ ...assignment });
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return new Promise((resolve, reject) => {
|
|
250
|
-
const entry = {
|
|
251
|
-
resolve,
|
|
252
|
-
reject,
|
|
253
|
-
timer: setTimeout(() => {
|
|
254
|
-
// Timeout - remove from waiter list and reject
|
|
255
|
-
const queue = assignmentWaiters.get(requestId);
|
|
256
|
-
if (queue) {
|
|
257
|
-
const idx = queue.indexOf(entry);
|
|
258
|
-
if (idx >= 0) queue.splice(idx, 1);
|
|
259
|
-
if (queue.length === 0) assignmentWaiters.delete(requestId);
|
|
260
|
-
}
|
|
261
|
-
reject(new Error(`Assignment ${requestId} not received within ${timeoutMs}ms`));
|
|
262
|
-
}, timeoutMs),
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
const queue = assignmentWaiters.get(requestId);
|
|
266
|
-
if (queue) {
|
|
267
|
-
queue.push(entry);
|
|
268
|
-
} else {
|
|
269
|
-
assignmentWaiters.set(requestId, [entry]);
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Get assignment metadata (without JWT for logging)
|
|
276
|
-
* TPB-12: Per-instance metadata retrieval
|
|
277
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
278
|
-
* @returns {Object|null} Assignment metadata or null
|
|
279
|
-
*/
|
|
280
|
-
export function getAssignmentMetadata(instanceId) {
|
|
281
|
-
const assignment = getAssignment(instanceId);
|
|
282
|
-
if (!assignment) return null;
|
|
283
|
-
|
|
284
|
-
return {
|
|
285
|
-
requestId: assignment.requestId,
|
|
286
|
-
relayId: assignment.relayId,
|
|
287
|
-
relayUrl: assignment.relayUrl,
|
|
288
|
-
leaseMs: assignment.leaseMs,
|
|
289
|
-
leaseId: assignment.leaseId,
|
|
290
|
-
timestamp: assignment.timestamp,
|
|
291
|
-
lastRefreshIso: assignment.lastRefreshIso,
|
|
292
|
-
lastRefreshDeltaMs: assignment.lastRefreshDeltaMs
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Record a successful refresh in the assignment cache and return delta ms since last refresh (or since initial assignment if none).
|
|
298
|
-
* TPB-12: Per-instance refresh recording
|
|
299
|
-
* @param {string} instanceId - Tunnel instance ID (full MQTT clientId)
|
|
300
|
-
* @param {number} [nowMs]
|
|
301
|
-
* @returns {number} delta milliseconds since last refresh or initial assignment
|
|
302
|
-
*/
|
|
303
|
-
export function recordRefresh(instanceId, nowMs = Date.now()) {
|
|
304
|
-
// Backward compatibility: if instanceId is a number, treat as legacy
|
|
305
|
-
if (typeof instanceId === 'number') {
|
|
306
|
-
nowMs = instanceId;
|
|
307
|
-
instanceId = null;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const assignment = getAssignment(instanceId);
|
|
311
|
-
if (!assignment) return 0;
|
|
312
|
-
|
|
313
|
-
const prev = typeof assignment.lastRefreshMs === 'number' ? assignment.lastRefreshMs : assignment.timestamp;
|
|
314
|
-
const delta = nowMs - prev;
|
|
315
|
-
|
|
316
|
-
// Update the assignment in the map
|
|
317
|
-
assignment.lastRefreshMs = nowMs;
|
|
318
|
-
assignment.lastRefreshIso = new Date(nowMs).toISOString();
|
|
319
|
-
assignment.lastRefreshDeltaMs = delta;
|
|
320
|
-
|
|
321
|
-
// Update the map entry
|
|
322
|
-
if (instanceId) {
|
|
323
|
-
assignmentByInstance.set(instanceId, assignment);
|
|
324
|
-
} else {
|
|
325
|
-
// Legacy: update first instance found
|
|
326
|
-
const [firstInstanceId] = assignmentByInstance.keys();
|
|
327
|
-
if (firstInstanceId) {
|
|
328
|
-
assignmentByInstance.set(firstInstanceId, assignment);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return delta;
|
|
333
|
-
}
|
package/src/tcp-v1/framing.js
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
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
|
-
|
|
19
|
-
export const FrameType = {
|
|
20
|
-
STREAM_OPEN: 0x01,
|
|
21
|
-
STREAM_DATA: 0x02,
|
|
22
|
-
STREAM_CLOSE: 0x03,
|
|
23
|
-
STREAM_ERROR: 0x04,
|
|
24
|
-
PING: 0x05,
|
|
25
|
-
PONG: 0x06,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export const MAX_FRAME_PAYLOAD = 64 * 1024; // 64 KiB
|
|
29
|
-
export const FRAME_HEADER_SIZE = 9; // 1 byte type + 4 bytes streamId + 4 bytes length
|
|
30
|
-
|
|
31
|
-
export class TcpV1FramingError extends Error {
|
|
32
|
-
constructor(message, code) {
|
|
33
|
-
super(message);
|
|
34
|
-
this.name = 'TcpV1FramingError';
|
|
35
|
-
this.code = code;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Encode a frame to binary format
|
|
41
|
-
* @param {Object} frame - Frame object with type, streamId, and payload
|
|
42
|
-
* @param {number} frame.type - Frame type (FrameType constant)
|
|
43
|
-
* @param {number} frame.streamId - Stream ID (24-bit, 0 for PING/PONG)
|
|
44
|
-
* @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
|
|
45
|
-
* @returns {Buffer} Encoded frame
|
|
46
|
-
* @throws {TcpV1FramingError} If frame is invalid
|
|
47
|
-
*/
|
|
48
|
-
export function encodeFrame(frame) {
|
|
49
|
-
const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
|
|
50
|
-
|
|
51
|
-
// Validate frame type
|
|
52
|
-
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
53
|
-
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Convert payload to Buffer if needed
|
|
57
|
-
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
58
|
-
|
|
59
|
-
// Validate payload size
|
|
60
|
-
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
61
|
-
throw new TcpV1FramingError(
|
|
62
|
-
`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
63
|
-
'PAYLOAD_TOO_LARGE'
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// StreamID validation (PING/PONG can have 0)
|
|
68
|
-
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
69
|
-
if (streamId === 0) {
|
|
70
|
-
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
71
|
-
}
|
|
72
|
-
// Ensure streamId fits in 24 bits
|
|
73
|
-
if (streamId > 0x00FFFFFF) {
|
|
74
|
-
throw new TcpV1FramingError(`StreamID ${streamId} exceeds 24-bit limit`, 'INVALID_STREAM_ID');
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Create frame buffer
|
|
79
|
-
const frameBuf = Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadBuf.length);
|
|
80
|
-
|
|
81
|
-
// Write frame type (1 byte)
|
|
82
|
-
frameBuf[0] = type;
|
|
83
|
-
|
|
84
|
-
// Write streamId (4 bytes, big-endian, only lower 24 bits used)
|
|
85
|
-
frameBuf.writeUInt32BE(streamId & 0x00FFFFFF, 1);
|
|
86
|
-
|
|
87
|
-
// Write payload length (4 bytes, big-endian)
|
|
88
|
-
frameBuf.writeUInt32BE(payloadBuf.length, 5);
|
|
89
|
-
|
|
90
|
-
// Write payload
|
|
91
|
-
if (payloadBuf.length > 0) {
|
|
92
|
-
payloadBuf.copy(frameBuf, FRAME_HEADER_SIZE);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return frameBuf;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Decode a frame from binary format
|
|
100
|
-
* @param {Buffer|Uint8Array} data - Binary frame data
|
|
101
|
-
* @param {number} [offset=0] - Offset to start reading from
|
|
102
|
-
* @returns {Object} Decoded frame object with type, streamId, payload, and consumed bytes
|
|
103
|
-
* @throws {TcpV1FramingError} If frame is invalid
|
|
104
|
-
*/
|
|
105
|
-
export function decodeFrame(data, offset = 0) {
|
|
106
|
-
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
107
|
-
|
|
108
|
-
// Check if we have enough data for header
|
|
109
|
-
if (buf.length - offset < FRAME_HEADER_SIZE) {
|
|
110
|
-
throw new TcpV1FramingError(
|
|
111
|
-
`Insufficient data for frame header: need ${FRAME_HEADER_SIZE} bytes, got ${buf.length - offset}`,
|
|
112
|
-
'INCOMPLETE_FRAME'
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// If offset is provided, create a subarray view
|
|
117
|
-
const view = offset > 0 ? buf.subarray(offset) : buf;
|
|
118
|
-
|
|
119
|
-
// Read frame type (1 byte)
|
|
120
|
-
const type = view[0];
|
|
121
|
-
|
|
122
|
-
// Validate frame type
|
|
123
|
-
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
124
|
-
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Read streamId (4 bytes, big-endian, only lower 24 bits used)
|
|
128
|
-
const streamId = view.readUInt32BE(1) & 0x00FFFFFF;
|
|
129
|
-
|
|
130
|
-
// Read payload length (4 bytes, big-endian)
|
|
131
|
-
const payloadLength = view.readUInt32BE(5);
|
|
132
|
-
|
|
133
|
-
// Validate payload length
|
|
134
|
-
if (payloadLength > MAX_FRAME_PAYLOAD) {
|
|
135
|
-
throw new TcpV1FramingError(
|
|
136
|
-
`Payload length ${payloadLength} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
137
|
-
'PAYLOAD_TOO_LARGE'
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Check if we have enough data for payload
|
|
142
|
-
const totalFrameSize = FRAME_HEADER_SIZE + payloadLength;
|
|
143
|
-
if (view.length < totalFrameSize) {
|
|
144
|
-
throw new TcpV1FramingError(
|
|
145
|
-
`Insufficient data for frame payload: need ${totalFrameSize} bytes, got ${view.length}`,
|
|
146
|
-
'INCOMPLETE_FRAME'
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// StreamID validation (PING/PONG can have 0)
|
|
151
|
-
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
152
|
-
if (streamId === 0) {
|
|
153
|
-
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Read payload
|
|
158
|
-
const payload = payloadLength > 0
|
|
159
|
-
? view.subarray(FRAME_HEADER_SIZE, FRAME_HEADER_SIZE + payloadLength)
|
|
160
|
-
: Buffer.alloc(0);
|
|
161
|
-
|
|
162
|
-
return {
|
|
163
|
-
type,
|
|
164
|
-
streamId,
|
|
165
|
-
payload,
|
|
166
|
-
consumed: totalFrameSize,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Validate a frame for protocol violations
|
|
172
|
-
* @param {Object} frame - Frame object to validate
|
|
173
|
-
* @returns {void}
|
|
174
|
-
* @throws {TcpV1FramingError} If frame is invalid
|
|
175
|
-
*/
|
|
176
|
-
export function validateFrame(frame) {
|
|
177
|
-
const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
|
|
178
|
-
|
|
179
|
-
// Validate frame type
|
|
180
|
-
if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
|
|
181
|
-
throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Validate payload size
|
|
185
|
-
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
186
|
-
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
187
|
-
throw new TcpV1FramingError(
|
|
188
|
-
`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
|
|
189
|
-
'PAYLOAD_TOO_LARGE'
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// StreamID validation (PING/PONG can have 0)
|
|
194
|
-
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
195
|
-
if (streamId === 0) {
|
|
196
|
-
throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
|
|
197
|
-
}
|
|
198
|
-
if (streamId > 0x00FFFFFF) {
|
|
199
|
-
throw new TcpV1FramingError(`StreamID ${streamId} exceeds 24-bit limit`, 'INVALID_STREAM_ID');
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|