asyx 0.0.1 → 0.1.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/LICENSE +67 -0
- package/README.md +134 -5
- package/SECURITY.md +40 -0
- package/bin/asyx.js +2 -0
- package/package.json +47 -11
- package/src/commands/.gitkeep +0 -0
- package/src/commands/mqtt-broker.js +56 -0
- package/src/commands/mqtt.js +164 -0
- package/src/commands/setup.js +137 -0
- package/src/commands/sts.js +73 -0
- package/src/commands/tunnel.js +540 -0
- package/src/lib/.gitkeep +0 -0
- package/src/lib/certs.js +25 -0
- package/src/lib/csr.js +45 -0
- package/src/lib/device.js +18 -0
- package/src/lib/fingerprint.js +115 -0
- package/src/lib/ids.js +5 -0
- package/src/lib/logger.js +58 -0
- package/src/lib/logger.level.spec.js +81 -0
- package/src/lib/logger.test.js +42 -0
- package/src/lib/pairing.js +15 -0
- package/src/lib/shared-transport.js +15 -0
- package/src/main.js +57 -0
- package/src/mqtt/awaiter-map.js +157 -0
- package/src/mqtt/events/handlers.js +90 -0
- package/src/mqtt/events/index.js +14 -0
- package/src/mqtt/events/schemas.js +58 -0
- package/src/mqtt/ingress.js +172 -0
- package/src/state/assignment.js +156 -0
- package/src/tunnel/__test_utils__/testUtils.js +76 -0
- package/src/tunnel/__tests__/jwt-refresh.spec.js +948 -0
- package/src/tunnel/__tests__/ws-control.spec.js +164 -0
- package/src/tunnel/http-tunnel.js +1131 -0
- package/src/tunnel/ws-control.js +144 -0
- package/index.js +0 -13
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const Envelope = z.object({
|
|
4
|
+
type: z.enum(["setup_required","open_ack","open_reject","error"]),
|
|
5
|
+
v: z.literal(1),
|
|
6
|
+
requestId: z.string().min(1),
|
|
7
|
+
ts: z.number().int().positive(),
|
|
8
|
+
data: z.unknown(),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export const SetupRequired = Envelope.extend({
|
|
12
|
+
type: z.literal("setup_required"),
|
|
13
|
+
data: z.object({ reason: z.string().optional() }),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const OpenAck = Envelope.extend({
|
|
17
|
+
type: z.literal("open_ack"),
|
|
18
|
+
data: z.object({
|
|
19
|
+
relayId: z.string().min(1),
|
|
20
|
+
leaseMs: z.number().int().positive(),
|
|
21
|
+
assignment: z.object({
|
|
22
|
+
jwt: z.string().min(10),
|
|
23
|
+
fqdn: z.string().min(1),
|
|
24
|
+
proto: z.string().min(1),
|
|
25
|
+
path: z.string().min(1),
|
|
26
|
+
kid: z.string().optional(),
|
|
27
|
+
alg: z.string().optional(),
|
|
28
|
+
exp: z.number().optional(),
|
|
29
|
+
}),
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Legacy OpenAck schema for backward compatibility
|
|
34
|
+
export const OpenAckLegacy = Envelope.extend({
|
|
35
|
+
type: z.literal("open_ack"),
|
|
36
|
+
data: z.object({
|
|
37
|
+
relayId: z.string().min(1),
|
|
38
|
+
jwt: z.string().min(10),
|
|
39
|
+
leaseMs: z.number().int().positive(),
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export const OpenReject = Envelope.extend({
|
|
44
|
+
type: z.literal("open_reject"),
|
|
45
|
+
data: z.object({
|
|
46
|
+
reason: z.string().min(1),
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
export const ErrorEvt = Envelope.extend({
|
|
51
|
+
type: z.enum(["error"]),
|
|
52
|
+
data: z.object({
|
|
53
|
+
code: z.string().min(1),
|
|
54
|
+
msg: z.string().optional(),
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export const AnyEvt = z.union([SetupRequired, OpenAck, OpenAckLegacy, OpenReject, ErrorEvt]);
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single Ingress Message Handler
|
|
3
|
+
*
|
|
4
|
+
* Provides a single point of entry for all MQTT messages that:
|
|
5
|
+
* 1. Parses messages once
|
|
6
|
+
* 2. Resolves awaiters for request/response flows
|
|
7
|
+
* 3. Always forwards messages to the CP-3F dispatcher
|
|
8
|
+
*
|
|
9
|
+
* This eliminates the handler-order race that prevents CP-3F envelopes
|
|
10
|
+
* from being processed and establishes the pattern for future stories.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { AwaiterMap } from './awaiter-map.js';
|
|
14
|
+
|
|
15
|
+
export class MessageIngress {
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.awaiterMap = new AwaiterMap(options.maxAwaiters || 100);
|
|
18
|
+
this.dispatcher = null;
|
|
19
|
+
this.observability = options.observability || false;
|
|
20
|
+
this.messageCounter = 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Set the CP-3F event dispatcher
|
|
25
|
+
* @param {Function} dispatcher - The dispatchEvt function
|
|
26
|
+
*/
|
|
27
|
+
setDispatcher(dispatcher) {
|
|
28
|
+
this.dispatcher = dispatcher;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Register an awaiter for request/response correlation
|
|
33
|
+
* @param {Function} criteria - Function that returns true if message matches
|
|
34
|
+
* @param {number} timeoutMs - Timeout in milliseconds
|
|
35
|
+
* @returns {Promise} Promise that resolves with the matching message
|
|
36
|
+
*/
|
|
37
|
+
registerAwaiter(criteria, timeoutMs = 5000) {
|
|
38
|
+
return this.awaiterMap.register(criteria, timeoutMs);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Main ingress handler - processes all incoming MQTT messages
|
|
43
|
+
* @param {Object} message - Raw MQTT message from transport
|
|
44
|
+
*/
|
|
45
|
+
async handleMessage(message) {
|
|
46
|
+
const msgId = ++this.messageCounter;
|
|
47
|
+
const ts = Date.now();
|
|
48
|
+
const sizeBytes = message.payload ? message.payload.length : 0;
|
|
49
|
+
|
|
50
|
+
// Parse message once
|
|
51
|
+
let parsedMessage;
|
|
52
|
+
let parseStatus = 'success';
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const raw = message.payload;
|
|
56
|
+
const buffer =
|
|
57
|
+
typeof raw === 'string' ? Buffer.from(raw, 'utf8')
|
|
58
|
+
: Buffer.isBuffer(raw) ? raw
|
|
59
|
+
: ArrayBuffer.isView(raw) ? Buffer.from(raw.buffer, raw.byteOffset, raw.byteLength)
|
|
60
|
+
: raw instanceof ArrayBuffer ? Buffer.from(raw)
|
|
61
|
+
: null;
|
|
62
|
+
|
|
63
|
+
let parsedPayload;
|
|
64
|
+
if (buffer) {
|
|
65
|
+
try {
|
|
66
|
+
parsedPayload = JSON.parse(buffer.toString('utf8'));
|
|
67
|
+
} catch {
|
|
68
|
+
parseStatus = 'json_parse_error';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
parsedMessage = {
|
|
73
|
+
topic: message.topic,
|
|
74
|
+
payload: parsedPayload,
|
|
75
|
+
raw: buffer,
|
|
76
|
+
sizeBytes,
|
|
77
|
+
msgId,
|
|
78
|
+
ts
|
|
79
|
+
};
|
|
80
|
+
} catch (error) {
|
|
81
|
+
parseStatus = 'parse_error';
|
|
82
|
+
parsedMessage = {
|
|
83
|
+
topic: message.topic,
|
|
84
|
+
payload: undefined,
|
|
85
|
+
raw: message.payload,
|
|
86
|
+
sizeBytes,
|
|
87
|
+
msgId,
|
|
88
|
+
ts,
|
|
89
|
+
error: error.message
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Log ingress (CP-5 ready)
|
|
94
|
+
if (this.observability) {
|
|
95
|
+
console.log(`[ingress] ts=${ts} msgId=${msgId} topic=${parsedMessage.topic} parseStatus=${parseStatus} sizeBytes=${sizeBytes}`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Check awaiters and resolve any matching promises
|
|
99
|
+
const awaiterResolved = this.awaiterMap.processMessage(parsedMessage);
|
|
100
|
+
|
|
101
|
+
// Always dispatch to CP-3F dispatcher (non-exclusive broadcast)
|
|
102
|
+
if (this.dispatcher) {
|
|
103
|
+
try {
|
|
104
|
+
await this.dispatcher(parsedMessage);
|
|
105
|
+
|
|
106
|
+
// Log dispatch completion (CP-5 ready)
|
|
107
|
+
if (this.observability) {
|
|
108
|
+
console.log(`[dispatch] ts=${Date.now()} msgId=${msgId} dispatched=true`);
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
// Log dispatch error but don't crash
|
|
112
|
+
if (this.observability) {
|
|
113
|
+
console.log(`[dispatch] ts=${Date.now()} msgId=${msgId} dispatched=false error=${error.message}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Re-throw dispatcher errors (they contain business logic like exit codes)
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
msgId,
|
|
123
|
+
awaiterResolved,
|
|
124
|
+
parseStatus,
|
|
125
|
+
sizeBytes
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Get statistics about the ingress system
|
|
131
|
+
* @returns {Object} Statistics object
|
|
132
|
+
*/
|
|
133
|
+
getStats() {
|
|
134
|
+
return {
|
|
135
|
+
messageCounter: this.messageCounter,
|
|
136
|
+
awaiterStats: this.awaiterMap.getStats(),
|
|
137
|
+
hasDispatcher: !!this.dispatcher,
|
|
138
|
+
observabilityEnabled: this.observability
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Clear all awaiters (for cleanup)
|
|
144
|
+
*/
|
|
145
|
+
clear() {
|
|
146
|
+
this.awaiterMap.clear();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Create a session ID awaiter (backward compatibility)
|
|
151
|
+
* @param {string} sessionId - Session ID to match
|
|
152
|
+
* @param {string} expectedTopic - Expected topic pattern
|
|
153
|
+
* @param {number} timeoutMs - Timeout in milliseconds
|
|
154
|
+
* @returns {Promise} Promise that resolves with the matching message
|
|
155
|
+
*/
|
|
156
|
+
awaitSessionId(sessionId, expectedTopic, timeoutMs = 5000) {
|
|
157
|
+
const criteria = AwaiterMap.createSessionIdCriteria(sessionId, expectedTopic);
|
|
158
|
+
return this.registerAwaiter(criteria, timeoutMs);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Create a correlation ID awaiter (future use)
|
|
163
|
+
* @param {string} correlationId - Correlation ID to match
|
|
164
|
+
* @param {string} expectedTopic - Expected topic pattern
|
|
165
|
+
* @param {number} timeoutMs - Timeout in milliseconds
|
|
166
|
+
* @returns {Promise} Promise that resolves with the matching message
|
|
167
|
+
*/
|
|
168
|
+
awaitCorrelationId(correlationId, expectedTopic, timeoutMs = 5000) {
|
|
169
|
+
const criteria = AwaiterMap.createCorrelationIdCriteria(correlationId, expectedTopic);
|
|
170
|
+
return this.registerAwaiter(criteria, timeoutMs);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// In-memory assignment store for JWT and relay metadata
|
|
2
|
+
// Never writes to disk - memory only
|
|
3
|
+
|
|
4
|
+
let currentAssignment = null;
|
|
5
|
+
const assignmentWaiters = new Map();
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Set the current assignment with JWT and relay metadata
|
|
9
|
+
* @param {Object} assignment - Assignment data
|
|
10
|
+
* @param {string} assignment.requestId - Request ID
|
|
11
|
+
* @param {string} assignment.relayId - Relay ID
|
|
12
|
+
* @param {string} assignment.relayUrl - Relay URL
|
|
13
|
+
* @param {string} assignment.jwt - JWT token (stored in memory only)
|
|
14
|
+
* @param {number} assignment.leaseMs - Lease duration in milliseconds
|
|
15
|
+
* @param {string} assignment.leaseId - Lease ID for JWT refresh (optional)
|
|
16
|
+
*/
|
|
17
|
+
export function setAssignment(assignment) {
|
|
18
|
+
currentAssignment = {
|
|
19
|
+
requestId: assignment.requestId,
|
|
20
|
+
relayId: assignment.relayId,
|
|
21
|
+
relayUrl: assignment.relayUrl,
|
|
22
|
+
jwt: assignment.jwt, // Stored in memory only
|
|
23
|
+
leaseMs: assignment.leaseMs,
|
|
24
|
+
leaseId: assignment.leaseId, // CLI-IMG-WSReconnect: Store leaseId for JWT refresh
|
|
25
|
+
timestamp: Date.now(),
|
|
26
|
+
lastRefreshMs: undefined,
|
|
27
|
+
lastRefreshIso: undefined,
|
|
28
|
+
lastRefreshDeltaMs: undefined
|
|
29
|
+
};
|
|
30
|
+
// Notify any waiters that are tracking this requestId
|
|
31
|
+
if (assignment.requestId && assignmentWaiters.size > 0) {
|
|
32
|
+
const waiters = assignmentWaiters.get(assignment.requestId);
|
|
33
|
+
if (waiters && waiters.length) {
|
|
34
|
+
assignmentWaiters.delete(assignment.requestId);
|
|
35
|
+
for (const waiter of waiters) {
|
|
36
|
+
clearTimeout(waiter.timer);
|
|
37
|
+
try {
|
|
38
|
+
waiter.resolve({ ...currentAssignment });
|
|
39
|
+
} catch {
|
|
40
|
+
// Ignore waiter resolution errors; they should not impact CLI flow
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get the current assignment
|
|
49
|
+
* @returns {Object|null} Current assignment or null if none set
|
|
50
|
+
*/
|
|
51
|
+
export function getAssignment() {
|
|
52
|
+
return currentAssignment ? { ...currentAssignment } : null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Clear the current assignment
|
|
57
|
+
*/
|
|
58
|
+
export function clearAssignment() {
|
|
59
|
+
currentAssignment = null;
|
|
60
|
+
// Reject all outstanding waiters to avoid dangling promises
|
|
61
|
+
if (assignmentWaiters.size > 0) {
|
|
62
|
+
for (const [, waiters] of assignmentWaiters) {
|
|
63
|
+
for (const waiter of waiters) {
|
|
64
|
+
clearTimeout(waiter.timer);
|
|
65
|
+
try {
|
|
66
|
+
waiter.reject(new Error("Assignment cleared before completion"));
|
|
67
|
+
} catch {
|
|
68
|
+
// Ignore waiter rejections; consumers can handle silently
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
assignmentWaiters.clear();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check if an assignment exists
|
|
78
|
+
* @returns {boolean} True if assignment exists
|
|
79
|
+
*/
|
|
80
|
+
export function hasAssignment() {
|
|
81
|
+
return currentAssignment !== null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Wait until an assignment with the specified requestId is available.
|
|
86
|
+
* @param {string} requestId - Request ID to wait for
|
|
87
|
+
* @param {number} [timeoutMs=10000] - Timeout before rejecting
|
|
88
|
+
* @returns {Promise<Object>} resolves with the assignment snapshot
|
|
89
|
+
*/
|
|
90
|
+
export function waitForAssignment(requestId, timeoutMs = 10000) {
|
|
91
|
+
if (!requestId) {
|
|
92
|
+
return Promise.reject(new Error("waitForAssignment requires a requestId"));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (currentAssignment && currentAssignment.requestId === requestId) {
|
|
96
|
+
return Promise.resolve({ ...currentAssignment });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
const entry = {
|
|
101
|
+
resolve,
|
|
102
|
+
reject,
|
|
103
|
+
timer: setTimeout(() => {
|
|
104
|
+
// Timeout - remove from waiter list and reject
|
|
105
|
+
const queue = assignmentWaiters.get(requestId);
|
|
106
|
+
if (queue) {
|
|
107
|
+
const idx = queue.indexOf(entry);
|
|
108
|
+
if (idx >= 0) queue.splice(idx, 1);
|
|
109
|
+
if (queue.length === 0) assignmentWaiters.delete(requestId);
|
|
110
|
+
}
|
|
111
|
+
reject(new Error(`Assignment ${requestId} not received within ${timeoutMs}ms`));
|
|
112
|
+
}, timeoutMs),
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const queue = assignmentWaiters.get(requestId);
|
|
116
|
+
if (queue) {
|
|
117
|
+
queue.push(entry);
|
|
118
|
+
} else {
|
|
119
|
+
assignmentWaiters.set(requestId, [entry]);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Get assignment metadata (without JWT for logging)
|
|
126
|
+
* @returns {Object|null} Assignment metadata or null
|
|
127
|
+
*/
|
|
128
|
+
export function getAssignmentMetadata() {
|
|
129
|
+
if (!currentAssignment) return null;
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
requestId: currentAssignment.requestId,
|
|
133
|
+
relayId: currentAssignment.relayId,
|
|
134
|
+
relayUrl: currentAssignment.relayUrl,
|
|
135
|
+
leaseMs: currentAssignment.leaseMs,
|
|
136
|
+
leaseId: currentAssignment.leaseId,
|
|
137
|
+
timestamp: currentAssignment.timestamp,
|
|
138
|
+
lastRefreshIso: currentAssignment.lastRefreshIso,
|
|
139
|
+
lastRefreshDeltaMs: currentAssignment.lastRefreshDeltaMs
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Record a successful refresh in the assignment cache and return delta ms since last refresh (or since initial assignment if none).
|
|
145
|
+
* @param {number} [nowMs]
|
|
146
|
+
* @returns {number} delta milliseconds since last refresh or initial assignment
|
|
147
|
+
*/
|
|
148
|
+
export function recordRefresh(nowMs = Date.now()) {
|
|
149
|
+
if (!currentAssignment) return 0;
|
|
150
|
+
const prev = typeof currentAssignment.lastRefreshMs === 'number' ? currentAssignment.lastRefreshMs : currentAssignment.timestamp;
|
|
151
|
+
const delta = nowMs - prev;
|
|
152
|
+
currentAssignment.lastRefreshMs = nowMs;
|
|
153
|
+
currentAssignment.lastRefreshIso = new Date(nowMs).toISOString();
|
|
154
|
+
currentAssignment.lastRefreshDeltaMs = delta;
|
|
155
|
+
return delta;
|
|
156
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
export function createMockLogger() {
|
|
4
|
+
return {
|
|
5
|
+
info: jest.fn(),
|
|
6
|
+
warn: jest.fn(),
|
|
7
|
+
error: jest.fn(),
|
|
8
|
+
debug: jest.fn(),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function createMockCoordinatorCore() {
|
|
13
|
+
return {
|
|
14
|
+
publish: jest.fn(async () => undefined),
|
|
15
|
+
end: jest.fn(),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function createMockIngress() {
|
|
20
|
+
const awaiters = new Set();
|
|
21
|
+
return {
|
|
22
|
+
registerAwaiter: jest.fn((predicate, _timeoutMs) => {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
awaiters.add({ predicate, resolve, reject });
|
|
25
|
+
});
|
|
26
|
+
}),
|
|
27
|
+
resolve(payload) {
|
|
28
|
+
for (const entry of awaiters) {
|
|
29
|
+
try {
|
|
30
|
+
if (entry.predicate({ payload })) {
|
|
31
|
+
entry.resolve({ payload });
|
|
32
|
+
awaiters.delete(entry);
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
} catch {}
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
},
|
|
39
|
+
rejectAll(error) {
|
|
40
|
+
for (const entry of awaiters) {
|
|
41
|
+
entry.reject(error);
|
|
42
|
+
awaiters.delete(entry);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function createHttpTunnel(overrides = {}) {
|
|
49
|
+
const logger = overrides.logger ?? createMockLogger();
|
|
50
|
+
const coordinatorCore = overrides.coordinatorCore ?? createMockCoordinatorCore();
|
|
51
|
+
const coordinatorIngress = overrides.coordinatorIngress ?? createMockIngress();
|
|
52
|
+
|
|
53
|
+
const module = await import('../http-tunnel.js');
|
|
54
|
+
const { HttpTunnel } = module;
|
|
55
|
+
|
|
56
|
+
const tunnel = new HttpTunnel({
|
|
57
|
+
relayUrl: overrides.relayUrl ?? 'wss://relay.example.com/agent',
|
|
58
|
+
jwt: overrides.jwt ?? 'initial.jwt',
|
|
59
|
+
deviceId: overrides.deviceId ?? 'cli_123456789ABC',
|
|
60
|
+
requestId: overrides.requestId ?? 'initial-request',
|
|
61
|
+
localPort: overrides.localPort ?? 3000,
|
|
62
|
+
coordinatorCore,
|
|
63
|
+
coordinatorIngress,
|
|
64
|
+
isControlPlaneConnected: overrides.isControlPlaneConnected ?? (() => true),
|
|
65
|
+
waitForControlPlane: overrides.waitForControlPlane ?? (() => Promise.resolve()),
|
|
66
|
+
controlPlaneTimeoutMs: overrides.controlPlaneTimeoutMs ?? 100,
|
|
67
|
+
log: logger,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Avoid real WS connects in unit tests
|
|
71
|
+
tunnel._attemptConnect = jest.fn(async () => {});
|
|
72
|
+
|
|
73
|
+
return { tunnel, logger, coordinatorCore, coordinatorIngress };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|