asyx 0.5.8 → 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.
Files changed (87) hide show
  1. package/README.md +103 -17
  2. package/SECURITY.md +5 -3
  3. package/package.json +15 -22
  4. package/src/commands/name.js +23 -0
  5. package/src/commands/reject-messages.js +54 -0
  6. package/src/commands/setup.js +98 -102
  7. package/src/commands/tunnel.js +304 -2316
  8. package/src/lib/naming.js +81 -0
  9. package/src/main.js +24 -6
  10. package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +260 -0
  11. package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +1088 -0
  12. package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.d.ts +79 -0
  13. package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.js +138 -0
  14. package/vendor/tunnel-core/dist/control-plane/session.d.ts +50 -0
  15. package/vendor/tunnel-core/dist/control-plane/session.js +115 -0
  16. package/vendor/tunnel-core/dist/errors.d.ts +49 -0
  17. package/vendor/tunnel-core/dist/errors.js +53 -0
  18. package/vendor/tunnel-core/dist/flows/http.d.ts +50 -0
  19. package/vendor/tunnel-core/dist/flows/http.js +585 -0
  20. package/vendor/tunnel-core/dist/flows/tcp.d.ts +60 -0
  21. package/vendor/tunnel-core/dist/flows/tcp.js +546 -0
  22. package/vendor/tunnel-core/dist/flows/udp.d.ts +41 -0
  23. package/vendor/tunnel-core/dist/flows/udp.js +376 -0
  24. package/vendor/tunnel-core/dist/lib/adapt-logger.d.ts +2 -0
  25. package/vendor/tunnel-core/dist/lib/adapt-logger.js +6 -0
  26. package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +13 -0
  27. package/vendor/tunnel-core/dist/lib/fingerprint.js +95 -0
  28. package/vendor/tunnel-core/dist/lib/jwt-verifier.d.ts +27 -0
  29. package/vendor/tunnel-core/dist/lib/jwt-verifier.js +230 -0
  30. package/vendor/tunnel-core/dist/lib/logger.d.ts +9 -0
  31. package/vendor/tunnel-core/dist/lib/logger.js +58 -0
  32. package/vendor/tunnel-core/dist/lib/refresh-helpers.d.ts +16 -0
  33. package/vendor/tunnel-core/dist/lib/refresh-helpers.js +66 -0
  34. package/vendor/tunnel-core/dist/lifecycle.d.ts +24 -0
  35. package/vendor/tunnel-core/dist/lifecycle.js +29 -0
  36. package/vendor/tunnel-core/dist/mqtt/awaiter-map.d.ts +56 -0
  37. package/vendor/tunnel-core/dist/mqtt/awaiter-map.js +227 -0
  38. package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +58 -0
  39. package/vendor/tunnel-core/dist/mqtt/events/handlers.js +250 -0
  40. package/vendor/tunnel-core/dist/mqtt/events/index.d.ts +1 -0
  41. package/vendor/tunnel-core/dist/mqtt/events/index.js +191 -0
  42. package/vendor/tunnel-core/dist/mqtt/events/schemas.d.ts +498 -0
  43. package/vendor/tunnel-core/dist/mqtt/events/schemas.js +51 -0
  44. package/vendor/tunnel-core/dist/mqtt/ingress.d.ts +63 -0
  45. package/vendor/tunnel-core/dist/mqtt/ingress.js +170 -0
  46. package/vendor/tunnel-core/dist/state/assignment.d.ts +122 -0
  47. package/vendor/tunnel-core/dist/state/assignment.js +322 -0
  48. package/vendor/tunnel-core/dist/state/instance-registry.d.ts +88 -0
  49. package/{src → vendor/tunnel-core/dist}/state/instance-registry.js +38 -53
  50. package/vendor/tunnel-core/dist/tcp-v1/framing.d.ts +43 -0
  51. package/vendor/tunnel-core/dist/tcp-v1/framing.js +157 -0
  52. package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +137 -0
  53. package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +1591 -0
  54. package/vendor/tunnel-core/dist/tunnel/httpws-constants.d.ts +13 -0
  55. package/{src → vendor/tunnel-core/dist}/tunnel/httpws-constants.js +5 -7
  56. package/vendor/tunnel-core/dist/tunnel/reconnect.d.ts +76 -0
  57. package/vendor/tunnel-core/dist/tunnel/reconnect.js +183 -0
  58. package/vendor/tunnel-core/dist/tunnel/request-sink.d.ts +40 -0
  59. package/vendor/tunnel-core/dist/tunnel/request-sink.js +39 -0
  60. package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +89 -0
  61. package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +838 -0
  62. package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +90 -0
  63. package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +737 -0
  64. package/vendor/tunnel-core/dist/tunnel/ws-control.d.ts +22 -0
  65. package/vendor/tunnel-core/dist/tunnel/ws-control.js +124 -0
  66. package/vendor/tunnel-core/dist/types.d.ts +24 -0
  67. package/vendor/tunnel-core/dist/types.js +2 -0
  68. package/vendor/tunnel-core/dist/udp-v1/framing.d.ts +41 -0
  69. package/vendor/tunnel-core/dist/udp-v1/framing.js +158 -0
  70. package/vendor/tunnel-core/package.json +34 -0
  71. package/src/control-plane/MqttControlPlaneClient.js +0 -1196
  72. package/src/control-plane/MqttControlPlaneTransport.js +0 -150
  73. package/src/lib/fingerprint.js +0 -115
  74. package/src/lib/jwt-verifier.js +0 -260
  75. package/src/lib/logger.js +0 -62
  76. package/src/mqtt/awaiter-map.js +0 -247
  77. package/src/mqtt/events/handlers.js +0 -300
  78. package/src/mqtt/events/index.js +0 -222
  79. package/src/mqtt/events/schemas.js +0 -58
  80. package/src/mqtt/ingress.js +0 -182
  81. package/src/state/assignment.js +0 -333
  82. package/src/tcp-v1/framing.js +0 -203
  83. package/src/tunnel/http-tunnel.js +0 -1694
  84. package/src/tunnel/tcp-tunnel.js +0 -862
  85. package/src/tunnel/udp-tunnel.js +0 -708
  86. package/src/tunnel/ws-control.js +0 -135
  87. package/src/udp-v1/framing.js +0 -203
@@ -1,135 +0,0 @@
1
- /**
2
- * CLI-REAUTH: WebSocket Control Frame Helper
3
- * Manages control frame communication with relay for in-band reauth
4
- */
5
-
6
- // Global waiter registry: op -> Set<{ resolve, reject, timer }>
7
- const controlWaiters = new Map();
8
-
9
- /**
10
- * CLI-REAUTH: Dispatch incoming control frames to waiting promises
11
- * Called from WebSocket message handler when a text frame matches control pattern
12
- *
13
- * @param {Object} frame - Parsed JSON control frame (must have 'op' field)
14
- */
15
- export function dispatchControlFrame(frame) {
16
- if (!frame || typeof frame !== 'object' || !frame.op) {
17
- return false; // Not a control frame
18
- }
19
-
20
- const waiters = controlWaiters.get(frame.op);
21
- if (!waiters || waiters.size === 0) {
22
- return false; // No waiters for this op
23
- }
24
-
25
- // Resolve all waiters for this op and remove them
26
- const resolvedWaiters = Array.from(waiters);
27
- controlWaiters.delete(frame.op);
28
-
29
- for (const waiter of resolvedWaiters) {
30
- clearTimeout(waiter.timer);
31
- try {
32
- waiter.resolve(frame);
33
- } catch (err) {
34
- // Ignore resolution errors
35
- }
36
- }
37
-
38
- return true; // Frame was dispatched
39
- }
40
-
41
- /**
42
- * CLI-REAUTH: Cleanup all waiters (call on socket close to prevent leaks)
43
- * Rejects all pending waiters to ensure promises settle and don't linger in microtask queue
44
- */
45
- export function clearAllWaiters() {
46
- for (const [, waiters] of controlWaiters) {
47
- for (const waiter of waiters) {
48
- clearTimeout(waiter.timer);
49
- try {
50
- // Reject the promise - tests should await these to prevent unhandled rejections
51
- waiter.reject(new Error('WebSocket closed before control frame received'));
52
- } catch {
53
- // Ignore rejection errors (promise already settled)
54
- }
55
- }
56
- }
57
- controlWaiters.clear();
58
- }
59
-
60
- /**
61
- * CLI-REAUTH: Send control frame and wait for acknowledgment
62
- *
63
- * @param {WebSocket} ws - WebSocket connection
64
- * @param {string} payloadJson - Serialized JSON payload to send
65
- * @param {string} expectedOp - Expected 'op' value in response (e.g., 'reauth_ack')
66
- * @param {number} timeoutMs - Timeout in milliseconds (default 3000)
67
- * @returns {Promise<Object>} Resolves with parsed control frame, or {status:'error', error:'timeout'} on timeout
68
- */
69
- export function sendControlAndWaitAck(ws, payloadJson, expectedOp, timeoutMs = 3000) {
70
- return new Promise((resolve, reject) => {
71
- // Validate inputs
72
- if (!ws || ws.readyState !== 1) { // WebSocket.OPEN = 1
73
- resolve({ status: 'error', error: 'socket_not_open' });
74
- return;
75
- }
76
-
77
- if (!payloadJson || typeof payloadJson !== 'string') {
78
- resolve({ status: 'error', error: 'invalid_payload' });
79
- return;
80
- }
81
-
82
- if (!expectedOp || typeof expectedOp !== 'string') {
83
- resolve({ status: 'error', error: 'invalid_expected_op' });
84
- return;
85
- }
86
-
87
- // Set up timeout
88
- const timer = setTimeout(() => {
89
- // Remove from registry
90
- const waiters = controlWaiters.get(expectedOp);
91
- if (waiters) {
92
- waiters.delete(waiter);
93
- if (waiters.size === 0) {
94
- controlWaiters.delete(expectedOp);
95
- }
96
- }
97
- resolve({ status: 'error', error: 'timeout' });
98
- }, timeoutMs);
99
-
100
- // Create waiter
101
- const waiter = {
102
- resolve: (frame) => {
103
- clearTimeout(timer);
104
- resolve(frame);
105
- },
106
- reject: (error) => {
107
- clearTimeout(timer);
108
- reject(error);
109
- },
110
- timer
111
- };
112
-
113
- // Register waiter
114
- if (!controlWaiters.has(expectedOp)) {
115
- controlWaiters.set(expectedOp, new Set());
116
- }
117
- controlWaiters.get(expectedOp).add(waiter);
118
-
119
- // Send control frame
120
- try {
121
- ws.send(payloadJson);
122
- } catch (sendError) {
123
- // Clean up waiter on send failure
124
- const waiters = controlWaiters.get(expectedOp);
125
- if (waiters) {
126
- waiters.delete(waiter);
127
- if (waiters.size === 0) {
128
- controlWaiters.delete(expectedOp);
129
- }
130
- }
131
- clearTimeout(timer);
132
- resolve({ status: 'error', error: 'send_failed', message: sendError.message });
133
- }
134
- });
135
- }
@@ -1,203 +0,0 @@
1
- /**
2
- * UDP-v1 (udpWs-v1) Framing Protocol
3
- *
4
- * Binary frame format (same as tcpWs-v1):
5
- * [frameType:1 byte][flowId:4 bytes (big-endian)][length:4 bytes (big-endian)][payload:length bytes]
6
- *
7
- * Frame types:
8
- * - 0x10: UDP_DGRAM
9
- * - 0x11: UDP_CLOSE
10
- * - 0x05: PING
11
- * - 0x06: PONG
12
- *
13
- * Max payload size: 64 KiB (65536 bytes)
14
- * FlowId: 32-bit value (big-endian)
15
- *
16
- * Reference: docs/v4.1/v4.1_tcpWs_udpWs_protocol_v1.md
17
- */
18
-
19
- export const FrameType = {
20
- UDP_DGRAM: 0x10,
21
- UDP_CLOSE: 0x11,
22
- PING: 0x05,
23
- PONG: 0x06,
24
- };
25
-
26
- export const MAX_FRAME_PAYLOAD = 64 * 1024; // 64 KiB
27
- export const FRAME_HEADER_SIZE = 9; // 1 byte type + 4 bytes flowId + 4 bytes length
28
-
29
- export class UdpV1FramingError extends Error {
30
- constructor(message, code) {
31
- super(message);
32
- this.name = 'UdpV1FramingError';
33
- this.code = code;
34
- }
35
- }
36
-
37
- /**
38
- * Encode a frame to binary format
39
- * @param {Object} frame - Frame object with type, flowId, and payload
40
- * @param {number} frame.type - Frame type (FrameType constant)
41
- * @param {number} frame.flowId - Flow ID (32-bit, 0 for PING/PONG)
42
- * @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
43
- * @returns {Buffer} Encoded frame
44
- * @throws {UdpV1FramingError} If frame is invalid
45
- */
46
- export function encodeFrame(frame) {
47
- const { type, flowId = 0, payload = Buffer.alloc(0) } = frame;
48
-
49
- // Validate frame type
50
- const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
51
- if (!validTypes.includes(type)) {
52
- throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
53
- }
54
-
55
- // Convert payload to Buffer if needed
56
- const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
57
-
58
- // Validate payload size
59
- if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
60
- throw new UdpV1FramingError(
61
- `Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
62
- 'PAYLOAD_TOO_LARGE'
63
- );
64
- }
65
-
66
- // FlowID validation (PING/PONG can have 0)
67
- if (type !== FrameType.PING && type !== FrameType.PONG) {
68
- if (flowId === 0) {
69
- throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_ID');
70
- }
71
- // Ensure flowId fits in 32 bits
72
- if (flowId > 0xFFFFFFFF) {
73
- throw new UdpV1FramingError(`FlowID ${flowId} exceeds 32-bit limit`, 'INVALID_FLOW_ID');
74
- }
75
- }
76
-
77
- // Create frame buffer
78
- const frameBuf = Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadBuf.length);
79
-
80
- // Write frame type (1 byte)
81
- frameBuf[0] = type;
82
-
83
- // Write flowId (4 bytes, big-endian)
84
- frameBuf.writeUInt32BE(flowId, 1);
85
-
86
- // Write payload length (4 bytes, big-endian)
87
- frameBuf.writeUInt32BE(payloadBuf.length, 5);
88
-
89
- // Write payload
90
- if (payloadBuf.length > 0) {
91
- payloadBuf.copy(frameBuf, FRAME_HEADER_SIZE);
92
- }
93
-
94
- return frameBuf;
95
- }
96
-
97
- /**
98
- * Decode a frame from binary format
99
- * @param {Buffer|Uint8Array} data - Binary frame data
100
- * @param {number} [offset=0] - Offset to start reading from
101
- * @returns {Object} Decoded frame object with type, flowId, payload, and consumed bytes
102
- * @throws {UdpV1FramingError} If frame is invalid
103
- */
104
- export function decodeFrame(data, offset = 0) {
105
- const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
106
-
107
- // Check if we have enough data for header
108
- if (buf.length - offset < FRAME_HEADER_SIZE) {
109
- throw new UdpV1FramingError(
110
- `Insufficient data for frame header: need ${FRAME_HEADER_SIZE} bytes, got ${buf.length - offset}`,
111
- 'INCOMPLETE_FRAME'
112
- );
113
- }
114
-
115
- // If offset is provided, create a subarray view
116
- const view = offset > 0 ? buf.subarray(offset) : buf;
117
-
118
- // Read frame type (1 byte)
119
- const type = view[0];
120
-
121
- // Validate frame type
122
- const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
123
- if (!validTypes.includes(type)) {
124
- throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
125
- }
126
-
127
- // Read flowId (4 bytes, big-endian)
128
- const flowId = view.readUInt32BE(1);
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 UdpV1FramingError(
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 UdpV1FramingError(
145
- `Insufficient data for frame payload: need ${totalFrameSize} bytes, got ${view.length}`,
146
- 'INCOMPLETE_FRAME'
147
- );
148
- }
149
-
150
- // FlowID validation (PING/PONG can have 0)
151
- if (type !== FrameType.PING && type !== FrameType.PONG) {
152
- if (flowId === 0) {
153
- throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_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
- flowId,
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 {UdpV1FramingError} If frame is invalid
175
- */
176
- export function validateFrame(frame) {
177
- const { type, flowId = 0, payload = Buffer.alloc(0) } = frame;
178
-
179
- // Validate frame type
180
- const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
181
- if (!validTypes.includes(type)) {
182
- throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
183
- }
184
-
185
- // Validate payload size
186
- const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
187
- if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
188
- throw new UdpV1FramingError(
189
- `Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`,
190
- 'PAYLOAD_TOO_LARGE'
191
- );
192
- }
193
-
194
- // FlowID validation (PING/PONG can have 0)
195
- if (type !== FrameType.PING && type !== FrameType.PONG) {
196
- if (flowId === 0) {
197
- throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_ID');
198
- }
199
- if (flowId > 0xFFFFFFFF) {
200
- throw new UdpV1FramingError(`FlowID ${flowId} exceeds 32-bit limit`, 'INVALID_FLOW_ID');
201
- }
202
- }
203
- }