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,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI-REAUTH: Dispatch incoming control frames to waiting promises
|
|
3
|
+
* Called from WebSocket message handler when a text frame matches control pattern
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} frame - Parsed JSON control frame (must have 'op' field)
|
|
6
|
+
*/
|
|
7
|
+
export function dispatchControlFrame(frame: any): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* CLI-REAUTH: Cleanup all waiters (call on socket close to prevent leaks)
|
|
10
|
+
* Rejects all pending waiters to ensure promises settle and don't linger in microtask queue
|
|
11
|
+
*/
|
|
12
|
+
export function clearAllWaiters(): void;
|
|
13
|
+
/**
|
|
14
|
+
* CLI-REAUTH: Send control frame and wait for acknowledgment
|
|
15
|
+
*
|
|
16
|
+
* @param {import('ws').WebSocket} ws - WebSocket connection
|
|
17
|
+
* @param {string} payloadJson - Serialized JSON payload to send
|
|
18
|
+
* @param {string} expectedOp - Expected 'op' value in response (e.g., 'reauth_ack')
|
|
19
|
+
* @param {number} timeoutMs - Timeout in milliseconds (default 3000)
|
|
20
|
+
* @returns {Promise<Object>} Resolves with parsed control frame, or {status:'error', error:'timeout'} on timeout
|
|
21
|
+
*/
|
|
22
|
+
export function sendControlAndWaitAck(ws: import("ws").WebSocket, payloadJson: string, expectedOp: string, timeoutMs?: number): Promise<any>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI-REAUTH: WebSocket Control Frame Helper
|
|
3
|
+
* Manages control frame communication with relay for in-band reauth
|
|
4
|
+
*/
|
|
5
|
+
// Global waiter registry: op -> Set<{ resolve, reject, timer }>
|
|
6
|
+
const controlWaiters = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* CLI-REAUTH: Dispatch incoming control frames to waiting promises
|
|
9
|
+
* Called from WebSocket message handler when a text frame matches control pattern
|
|
10
|
+
*
|
|
11
|
+
* @param {Object} frame - Parsed JSON control frame (must have 'op' field)
|
|
12
|
+
*/
|
|
13
|
+
export function dispatchControlFrame(frame) {
|
|
14
|
+
if (!frame || typeof frame !== 'object' || !frame.op) {
|
|
15
|
+
return false; // Not a control frame
|
|
16
|
+
}
|
|
17
|
+
const waiters = controlWaiters.get(frame.op);
|
|
18
|
+
if (!waiters || waiters.size === 0) {
|
|
19
|
+
return false; // No waiters for this op
|
|
20
|
+
}
|
|
21
|
+
// Resolve all waiters for this op and remove them
|
|
22
|
+
const resolvedWaiters = Array.from(waiters);
|
|
23
|
+
controlWaiters.delete(frame.op);
|
|
24
|
+
for (const waiter of resolvedWaiters) {
|
|
25
|
+
clearTimeout(waiter.timer);
|
|
26
|
+
try {
|
|
27
|
+
waiter.resolve(frame);
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
// Ignore resolution errors
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return true; // Frame was dispatched
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* CLI-REAUTH: Cleanup all waiters (call on socket close to prevent leaks)
|
|
37
|
+
* Rejects all pending waiters to ensure promises settle and don't linger in microtask queue
|
|
38
|
+
*/
|
|
39
|
+
export function clearAllWaiters() {
|
|
40
|
+
for (const [, waiters] of controlWaiters) {
|
|
41
|
+
for (const waiter of waiters) {
|
|
42
|
+
clearTimeout(waiter.timer);
|
|
43
|
+
try {
|
|
44
|
+
// Reject the promise - tests should await these to prevent unhandled rejections
|
|
45
|
+
waiter.reject(new Error('WebSocket closed before control frame received'));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Ignore rejection errors (promise already settled)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
controlWaiters.clear();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* CLI-REAUTH: Send control frame and wait for acknowledgment
|
|
56
|
+
*
|
|
57
|
+
* @param {import('ws').WebSocket} ws - WebSocket connection
|
|
58
|
+
* @param {string} payloadJson - Serialized JSON payload to send
|
|
59
|
+
* @param {string} expectedOp - Expected 'op' value in response (e.g., 'reauth_ack')
|
|
60
|
+
* @param {number} timeoutMs - Timeout in milliseconds (default 3000)
|
|
61
|
+
* @returns {Promise<Object>} Resolves with parsed control frame, or {status:'error', error:'timeout'} on timeout
|
|
62
|
+
*/
|
|
63
|
+
export function sendControlAndWaitAck(ws, payloadJson, expectedOp, timeoutMs = 3000) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
// Validate inputs
|
|
66
|
+
if (!ws || ws.readyState !== 1) { // WebSocket.OPEN = 1
|
|
67
|
+
resolve({ status: 'error', error: 'socket_not_open' });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!payloadJson || typeof payloadJson !== 'string') {
|
|
71
|
+
resolve({ status: 'error', error: 'invalid_payload' });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (!expectedOp || typeof expectedOp !== 'string') {
|
|
75
|
+
resolve({ status: 'error', error: 'invalid_expected_op' });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
// Set up timeout
|
|
79
|
+
const timer = setTimeout(() => {
|
|
80
|
+
// Remove from registry
|
|
81
|
+
const waiters = controlWaiters.get(expectedOp);
|
|
82
|
+
if (waiters) {
|
|
83
|
+
waiters.delete(waiter);
|
|
84
|
+
if (waiters.size === 0) {
|
|
85
|
+
controlWaiters.delete(expectedOp);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
resolve({ status: 'error', error: 'timeout' });
|
|
89
|
+
}, timeoutMs);
|
|
90
|
+
// Create waiter
|
|
91
|
+
const waiter = {
|
|
92
|
+
resolve: (frame) => {
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
resolve(frame);
|
|
95
|
+
},
|
|
96
|
+
reject: (error) => {
|
|
97
|
+
clearTimeout(timer);
|
|
98
|
+
reject(error);
|
|
99
|
+
},
|
|
100
|
+
timer
|
|
101
|
+
};
|
|
102
|
+
// Register waiter
|
|
103
|
+
if (!controlWaiters.has(expectedOp)) {
|
|
104
|
+
controlWaiters.set(expectedOp, new Set());
|
|
105
|
+
}
|
|
106
|
+
controlWaiters.get(expectedOp).add(waiter);
|
|
107
|
+
// Send control frame
|
|
108
|
+
try {
|
|
109
|
+
ws.send(payloadJson);
|
|
110
|
+
}
|
|
111
|
+
catch (sendError) {
|
|
112
|
+
// Clean up waiter on send failure
|
|
113
|
+
const waiters = controlWaiters.get(expectedOp);
|
|
114
|
+
if (waiters) {
|
|
115
|
+
waiters.delete(waiter);
|
|
116
|
+
if (waiters.size === 0) {
|
|
117
|
+
controlWaiters.delete(expectedOp);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
clearTimeout(timer);
|
|
121
|
+
resolve({ status: 'error', error: 'send_failed', message: sendError.message });
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** What the core logs through: message first, then structured fields. The caller supplies it. */
|
|
2
|
+
export interface Logger {
|
|
3
|
+
info(msg: string, fields?: Record<string, unknown>): void;
|
|
4
|
+
warn(msg: string, fields?: Record<string, unknown>): void;
|
|
5
|
+
error(msg: string, fields?: Record<string, unknown>): void;
|
|
6
|
+
debug(msg: string, fields?: Record<string, unknown>): void;
|
|
7
|
+
}
|
|
8
|
+
/** The tunnel classes log as (fields, msg) or (msg, fields); see lib/adapt-logger. */
|
|
9
|
+
export type LogFn = (msgOrFields: string | Record<string, unknown>, maybeFields?: string | Record<string, unknown>) => void;
|
|
10
|
+
export interface ClassLogger {
|
|
11
|
+
info: LogFn;
|
|
12
|
+
warn: LogFn;
|
|
13
|
+
error: LogFn;
|
|
14
|
+
debug: LogFn;
|
|
15
|
+
}
|
|
16
|
+
/** The part of the MQTT core client the flows publish through. */
|
|
17
|
+
export interface Publisher {
|
|
18
|
+
publish(message: {
|
|
19
|
+
topic: string;
|
|
20
|
+
payload: string | Uint8Array;
|
|
21
|
+
qos?: 0 | 1;
|
|
22
|
+
retain?: boolean;
|
|
23
|
+
}): Promise<unknown>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encode a frame to binary format
|
|
3
|
+
* @param {Object} frame - Frame object with type, flowId, and payload
|
|
4
|
+
* @param {number} frame.type - Frame type (FrameType constant)
|
|
5
|
+
* @param {number} frame.flowId - Flow ID (32-bit, 0 for PING/PONG)
|
|
6
|
+
* @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
|
|
7
|
+
* @returns {Buffer} Encoded frame
|
|
8
|
+
* @throws {UdpV1FramingError} If frame is invalid
|
|
9
|
+
*/
|
|
10
|
+
export function encodeFrame(frame: {
|
|
11
|
+
type: number;
|
|
12
|
+
flowId: 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, flowId, payload, and consumed bytes
|
|
20
|
+
* @throws {UdpV1FramingError} 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 {UdpV1FramingError} If frame is invalid
|
|
28
|
+
*/
|
|
29
|
+
export function validateFrame(frame: any): void;
|
|
30
|
+
export namespace FrameType {
|
|
31
|
+
let UDP_DGRAM: number;
|
|
32
|
+
let UDP_CLOSE: number;
|
|
33
|
+
let PING: number;
|
|
34
|
+
let PONG: number;
|
|
35
|
+
}
|
|
36
|
+
export const MAX_FRAME_PAYLOAD: number;
|
|
37
|
+
export const FRAME_HEADER_SIZE: 9;
|
|
38
|
+
export class UdpV1FramingError extends Error {
|
|
39
|
+
constructor(message: any, code: any);
|
|
40
|
+
code: any;
|
|
41
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
export const FrameType = {
|
|
19
|
+
UDP_DGRAM: 0x10,
|
|
20
|
+
UDP_CLOSE: 0x11,
|
|
21
|
+
PING: 0x05,
|
|
22
|
+
PONG: 0x06,
|
|
23
|
+
};
|
|
24
|
+
export const MAX_FRAME_PAYLOAD = 64 * 1024; // 64 KiB
|
|
25
|
+
export const FRAME_HEADER_SIZE = 9; // 1 byte type + 4 bytes flowId + 4 bytes length
|
|
26
|
+
export class UdpV1FramingError extends Error {
|
|
27
|
+
constructor(message, code) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = 'UdpV1FramingError';
|
|
30
|
+
this.code = code;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Encode a frame to binary format
|
|
35
|
+
* @param {Object} frame - Frame object with type, flowId, and payload
|
|
36
|
+
* @param {number} frame.type - Frame type (FrameType constant)
|
|
37
|
+
* @param {number} frame.flowId - Flow ID (32-bit, 0 for PING/PONG)
|
|
38
|
+
* @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
|
|
39
|
+
* @returns {Buffer} Encoded frame
|
|
40
|
+
* @throws {UdpV1FramingError} If frame is invalid
|
|
41
|
+
*/
|
|
42
|
+
export function encodeFrame(frame) {
|
|
43
|
+
const { type, flowId = 0, payload = Buffer.alloc(0) } = frame;
|
|
44
|
+
// Validate frame type
|
|
45
|
+
const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
|
|
46
|
+
if (!validTypes.includes(type)) {
|
|
47
|
+
throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
48
|
+
}
|
|
49
|
+
// Convert payload to Buffer if needed
|
|
50
|
+
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
51
|
+
// Validate payload size
|
|
52
|
+
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
53
|
+
throw new UdpV1FramingError(`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
|
|
54
|
+
}
|
|
55
|
+
// FlowID validation (PING/PONG can have 0)
|
|
56
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
57
|
+
if (flowId === 0) {
|
|
58
|
+
throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_ID');
|
|
59
|
+
}
|
|
60
|
+
// Ensure flowId fits in 32 bits
|
|
61
|
+
if (flowId > 0xFFFFFFFF) {
|
|
62
|
+
throw new UdpV1FramingError(`FlowID ${flowId} exceeds 32-bit limit`, 'INVALID_FLOW_ID');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// Create frame buffer
|
|
66
|
+
const frameBuf = Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadBuf.length);
|
|
67
|
+
// Write frame type (1 byte)
|
|
68
|
+
frameBuf[0] = type;
|
|
69
|
+
// Write flowId (4 bytes, big-endian)
|
|
70
|
+
frameBuf.writeUInt32BE(flowId, 1);
|
|
71
|
+
// Write payload length (4 bytes, big-endian)
|
|
72
|
+
frameBuf.writeUInt32BE(payloadBuf.length, 5);
|
|
73
|
+
// Write payload
|
|
74
|
+
if (payloadBuf.length > 0) {
|
|
75
|
+
payloadBuf.copy(frameBuf, FRAME_HEADER_SIZE);
|
|
76
|
+
}
|
|
77
|
+
return frameBuf;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Decode a frame from binary format
|
|
81
|
+
* @param {Buffer|Uint8Array} data - Binary frame data
|
|
82
|
+
* @param {number} [offset=0] - Offset to start reading from
|
|
83
|
+
* @returns {Object} Decoded frame object with type, flowId, payload, and consumed bytes
|
|
84
|
+
* @throws {UdpV1FramingError} If frame is invalid
|
|
85
|
+
*/
|
|
86
|
+
export function decodeFrame(data, offset = 0) {
|
|
87
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
88
|
+
// Check if we have enough data for header
|
|
89
|
+
if (buf.length - offset < FRAME_HEADER_SIZE) {
|
|
90
|
+
throw new UdpV1FramingError(`Insufficient data for frame header: need ${FRAME_HEADER_SIZE} bytes, got ${buf.length - offset}`, 'INCOMPLETE_FRAME');
|
|
91
|
+
}
|
|
92
|
+
// If offset is provided, create a subarray view
|
|
93
|
+
const view = offset > 0 ? buf.subarray(offset) : buf;
|
|
94
|
+
// Read frame type (1 byte)
|
|
95
|
+
const type = view[0];
|
|
96
|
+
// Validate frame type
|
|
97
|
+
const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
|
|
98
|
+
if (!validTypes.includes(type)) {
|
|
99
|
+
throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
100
|
+
}
|
|
101
|
+
// Read flowId (4 bytes, big-endian)
|
|
102
|
+
const flowId = view.readUInt32BE(1);
|
|
103
|
+
// Read payload length (4 bytes, big-endian)
|
|
104
|
+
const payloadLength = view.readUInt32BE(5);
|
|
105
|
+
// Validate payload length
|
|
106
|
+
if (payloadLength > MAX_FRAME_PAYLOAD) {
|
|
107
|
+
throw new UdpV1FramingError(`Payload length ${payloadLength} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
|
|
108
|
+
}
|
|
109
|
+
// Check if we have enough data for payload
|
|
110
|
+
const totalFrameSize = FRAME_HEADER_SIZE + payloadLength;
|
|
111
|
+
if (view.length < totalFrameSize) {
|
|
112
|
+
throw new UdpV1FramingError(`Insufficient data for frame payload: need ${totalFrameSize} bytes, got ${view.length}`, 'INCOMPLETE_FRAME');
|
|
113
|
+
}
|
|
114
|
+
// FlowID validation (PING/PONG can have 0)
|
|
115
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
116
|
+
if (flowId === 0) {
|
|
117
|
+
throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_ID');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Read payload
|
|
121
|
+
const payload = payloadLength > 0
|
|
122
|
+
? view.subarray(FRAME_HEADER_SIZE, FRAME_HEADER_SIZE + payloadLength)
|
|
123
|
+
: Buffer.alloc(0);
|
|
124
|
+
return {
|
|
125
|
+
type,
|
|
126
|
+
flowId,
|
|
127
|
+
payload,
|
|
128
|
+
consumed: totalFrameSize,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Validate a frame for protocol violations
|
|
133
|
+
* @param {Object} frame - Frame object to validate
|
|
134
|
+
* @returns {void}
|
|
135
|
+
* @throws {UdpV1FramingError} If frame is invalid
|
|
136
|
+
*/
|
|
137
|
+
export function validateFrame(frame) {
|
|
138
|
+
const { type, flowId = 0, payload = Buffer.alloc(0) } = frame;
|
|
139
|
+
// Validate frame type
|
|
140
|
+
const validTypes = [FrameType.UDP_DGRAM, FrameType.UDP_CLOSE, FrameType.PING, FrameType.PONG];
|
|
141
|
+
if (!validTypes.includes(type)) {
|
|
142
|
+
throw new UdpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
|
|
143
|
+
}
|
|
144
|
+
// Validate payload size
|
|
145
|
+
const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
146
|
+
if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
|
|
147
|
+
throw new UdpV1FramingError(`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
|
|
148
|
+
}
|
|
149
|
+
// FlowID validation (PING/PONG can have 0)
|
|
150
|
+
if (type !== FrameType.PING && type !== FrameType.PONG) {
|
|
151
|
+
if (flowId === 0) {
|
|
152
|
+
throw new UdpV1FramingError('FlowID required for non-PING/PONG frames', 'MISSING_FLOW_ID');
|
|
153
|
+
}
|
|
154
|
+
if (flowId > 0xFFFFFFFF) {
|
|
155
|
+
throw new UdpV1FramingError(`FlowID ${flowId} exceeds 32-bit limit`, 'INVALID_FLOW_ID');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@asyx/tunnel-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "Shared tunnel protocol for the Asyx CLI and SDK (Story 12.1). Public surface in TypeScript; internals JavaScript with compiler-generated declarations. Not published yet: the CLI carries the built output inside its package.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=18.17"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
"./*": "./dist/*"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/**"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "npm --prefix ../mqtt-core run build >/dev/null && rm -rf dist && tsc -p tsconfig.json",
|
|
18
|
+
"typecheck": "npm --prefix ../mqtt-core run build >/dev/null && tsc -p tsconfig.json --noEmit",
|
|
19
|
+
"test": "NODE_OPTIONS=\"--experimental-vm-modules --max-old-space-size=8192\" jest --runInBand"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@asyx/mqtt-core": "^0.1.0",
|
|
23
|
+
"hw-fingerprint": "^3.2.0",
|
|
24
|
+
"jose": "^5.10.0",
|
|
25
|
+
"mqtt": "^5.3.4",
|
|
26
|
+
"ulid": "^2.4.0",
|
|
27
|
+
"ws": "^8.14.2",
|
|
28
|
+
"zod": "3.23.8"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@jest/globals": "^30.2.0",
|
|
32
|
+
"jest": "^30.2.0"
|
|
33
|
+
}
|
|
34
|
+
}
|