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.
Files changed (88) hide show
  1. package/README.md +71 -9
  2. package/SECURITY.md +5 -3
  3. package/package.json +13 -23
  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 +279 -2425
  8. package/src/lib/naming.js +81 -0
  9. package/src/main.js +9 -1
  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 -1738
  84. package/src/tunnel/reconnect.js +0 -182
  85. package/src/tunnel/tcp-tunnel.js +0 -944
  86. package/src/tunnel/udp-tunnel.js +0 -813
  87. package/src/tunnel/ws-control.js +0 -135
  88. package/src/udp-v1/framing.js +0 -203
@@ -0,0 +1,157 @@
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
+ export const FrameType = {
19
+ STREAM_OPEN: 0x01,
20
+ STREAM_DATA: 0x02,
21
+ STREAM_CLOSE: 0x03,
22
+ STREAM_ERROR: 0x04,
23
+ PING: 0x05,
24
+ PONG: 0x06,
25
+ };
26
+ export const MAX_FRAME_PAYLOAD = 64 * 1024; // 64 KiB
27
+ export const FRAME_HEADER_SIZE = 9; // 1 byte type + 4 bytes streamId + 4 bytes length
28
+ export class TcpV1FramingError extends Error {
29
+ constructor(message, code) {
30
+ super(message);
31
+ this.name = 'TcpV1FramingError';
32
+ this.code = code;
33
+ }
34
+ }
35
+ /**
36
+ * Encode a frame to binary format
37
+ * @param {Object} frame - Frame object with type, streamId, and payload
38
+ * @param {number} frame.type - Frame type (FrameType constant)
39
+ * @param {number} frame.streamId - Stream ID (24-bit, 0 for PING/PONG)
40
+ * @param {Buffer|Uint8Array} frame.payload - Frame payload (max 64 KiB)
41
+ * @returns {Buffer} Encoded frame
42
+ * @throws {TcpV1FramingError} If frame is invalid
43
+ */
44
+ export function encodeFrame(frame) {
45
+ const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
46
+ // Validate frame type
47
+ if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
48
+ throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
49
+ }
50
+ // Convert payload to Buffer if needed
51
+ const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
52
+ // Validate payload size
53
+ if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
54
+ throw new TcpV1FramingError(`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
55
+ }
56
+ // StreamID validation (PING/PONG can have 0)
57
+ if (type !== FrameType.PING && type !== FrameType.PONG) {
58
+ if (streamId === 0) {
59
+ throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
60
+ }
61
+ // Ensure streamId fits in 24 bits
62
+ if (streamId > 0x00FFFFFF) {
63
+ throw new TcpV1FramingError(`StreamID ${streamId} exceeds 24-bit limit`, 'INVALID_STREAM_ID');
64
+ }
65
+ }
66
+ // Create frame buffer
67
+ const frameBuf = Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadBuf.length);
68
+ // Write frame type (1 byte)
69
+ frameBuf[0] = type;
70
+ // Write streamId (4 bytes, big-endian, only lower 24 bits used)
71
+ frameBuf.writeUInt32BE(streamId & 0x00FFFFFF, 1);
72
+ // Write payload length (4 bytes, big-endian)
73
+ frameBuf.writeUInt32BE(payloadBuf.length, 5);
74
+ // Write payload
75
+ if (payloadBuf.length > 0) {
76
+ payloadBuf.copy(frameBuf, FRAME_HEADER_SIZE);
77
+ }
78
+ return frameBuf;
79
+ }
80
+ /**
81
+ * Decode a frame from binary format
82
+ * @param {Buffer|Uint8Array} data - Binary frame data
83
+ * @param {number} [offset=0] - Offset to start reading from
84
+ * @returns {Object} Decoded frame object with type, streamId, payload, and consumed bytes
85
+ * @throws {TcpV1FramingError} If frame is invalid
86
+ */
87
+ export function decodeFrame(data, offset = 0) {
88
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
89
+ // Check if we have enough data for header
90
+ if (buf.length - offset < FRAME_HEADER_SIZE) {
91
+ throw new TcpV1FramingError(`Insufficient data for frame header: need ${FRAME_HEADER_SIZE} bytes, got ${buf.length - offset}`, 'INCOMPLETE_FRAME');
92
+ }
93
+ // If offset is provided, create a subarray view
94
+ const view = offset > 0 ? buf.subarray(offset) : buf;
95
+ // Read frame type (1 byte)
96
+ const type = view[0];
97
+ // Validate frame type
98
+ if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
99
+ throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
100
+ }
101
+ // Read streamId (4 bytes, big-endian, only lower 24 bits used)
102
+ const streamId = view.readUInt32BE(1) & 0x00FFFFFF;
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 TcpV1FramingError(`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 TcpV1FramingError(`Insufficient data for frame payload: need ${totalFrameSize} bytes, got ${view.length}`, 'INCOMPLETE_FRAME');
113
+ }
114
+ // StreamID validation (PING/PONG can have 0)
115
+ if (type !== FrameType.PING && type !== FrameType.PONG) {
116
+ if (streamId === 0) {
117
+ throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_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
+ streamId,
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 {TcpV1FramingError} If frame is invalid
136
+ */
137
+ export function validateFrame(frame) {
138
+ const { type, streamId = 0, payload = Buffer.alloc(0) } = frame;
139
+ // Validate frame type
140
+ if (type < FrameType.STREAM_OPEN || type > FrameType.PONG) {
141
+ throw new TcpV1FramingError(`Invalid frame type: ${type}`, 'INVALID_FRAME_TYPE');
142
+ }
143
+ // Validate payload size
144
+ const payloadBuf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
145
+ if (payloadBuf.length > MAX_FRAME_PAYLOAD) {
146
+ throw new TcpV1FramingError(`Payload size ${payloadBuf.length} exceeds max ${MAX_FRAME_PAYLOAD}`, 'PAYLOAD_TOO_LARGE');
147
+ }
148
+ // StreamID validation (PING/PONG can have 0)
149
+ if (type !== FrameType.PING && type !== FrameType.PONG) {
150
+ if (streamId === 0) {
151
+ throw new TcpV1FramingError('StreamID required for non-PING/PONG frames', 'MISSING_STREAM_ID');
152
+ }
153
+ if (streamId > 0x00FFFFFF) {
154
+ throw new TcpV1FramingError(`StreamID ${streamId} exceeds 24-bit limit`, 'INVALID_STREAM_ID');
155
+ }
156
+ }
157
+ }
@@ -0,0 +1,137 @@
1
+ export const LOCAL_UNAVAILABLE_PAGE: "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Service not reachable</title></head><body style=\"font-family:system-ui,sans-serif;max-width:32rem;margin:4rem auto;padding:0 1rem;color:#334155\"><h1 style=\"font-size:1.4rem;color:#0f172a\">This service is not reachable right now</h1><p>The tunnel is up, but the app behind it is not answering. It may be restarting; try again in a moment.</p></body></html>";
2
+ export class HttpTunnel {
3
+ constructor({ relayUrl, jwt, deviceId, requestId, localPort, log, coordinatorCore, coordinatorIngress, isControlPlaneConnected, waitForControlPlane, getLastMqttError, getForceReconnect, controlPlaneTimeoutMs, secGroupSlug, instanceId, onFatal, onNotice, requestSink, }: {
4
+ relayUrl: any;
5
+ jwt: any;
6
+ deviceId: any;
7
+ requestId: any;
8
+ localPort: any;
9
+ log: any;
10
+ coordinatorCore: any;
11
+ coordinatorIngress: any;
12
+ isControlPlaneConnected?: () => boolean;
13
+ waitForControlPlane?: () => Promise<void>;
14
+ getLastMqttError?: () => any;
15
+ getForceReconnect?: () => any;
16
+ controlPlaneTimeoutMs?: number;
17
+ secGroupSlug?: any;
18
+ instanceId?: any;
19
+ onFatal?: any;
20
+ onNotice?: any;
21
+ requestSink?: any;
22
+ });
23
+ requestSink: any;
24
+ onFatal: any;
25
+ onNotice: any;
26
+ relayUrl: any;
27
+ jwt: any;
28
+ deviceId: any;
29
+ requestId: any;
30
+ localPort: any;
31
+ log: any;
32
+ ws: WebSocket;
33
+ secGroupSlug: any;
34
+ instanceId: any;
35
+ coordinatorCore: any;
36
+ coordinatorIngress: any;
37
+ isControlPlaneConnected: () => boolean;
38
+ waitForControlPlane: () => Promise<void>;
39
+ getLastMqttError: () => any;
40
+ getForceReconnect: () => any;
41
+ controlPlaneTimeoutMs: number;
42
+ heartbeatTimer: NodeJS.Timeout;
43
+ lastActivityMs: number;
44
+ shouldReconnect: boolean;
45
+ retryCount: number;
46
+ maxRetries: number;
47
+ reconnectTimer: any;
48
+ headers: {
49
+ Authorization: string;
50
+ 'X-Asyx-DeviceId': any;
51
+ 'X-Asyx-RequestId': any;
52
+ } | {
53
+ Authorization: string;
54
+ 'X-Asyx-DeviceId': any;
55
+ 'X-Asyx-RequestId': any;
56
+ } | {
57
+ Authorization: string;
58
+ 'X-Asyx-DeviceId': any;
59
+ 'X-Asyx-RequestId': any;
60
+ } | {
61
+ Authorization: string;
62
+ 'X-Asyx-DeviceId': any;
63
+ 'X-Asyx-RequestId': any;
64
+ } | {
65
+ Authorization: string;
66
+ 'X-Asyx-DeviceId': any;
67
+ 'X-Asyx-RequestId': any;
68
+ };
69
+ refreshState: string;
70
+ refreshAttempts: number;
71
+ refreshMaxAttempts: number;
72
+ refreshRetryTimer: any;
73
+ refreshDeadline: number;
74
+ refreshRetryCount: number;
75
+ refreshRetryMaxDelay: number;
76
+ refreshTimer: any;
77
+ jwtMeta: {
78
+ token: any;
79
+ expMs: number;
80
+ } | {
81
+ token: any;
82
+ expMs: number;
83
+ };
84
+ LEAD_MS: number;
85
+ reauthAckTimeoutMs: number;
86
+ reauthInFlight: Promise<boolean>;
87
+ httpWsVersion: any;
88
+ activeV2Responses: Map<any, any>;
89
+ _decodeJwtExp(token: any): {
90
+ expMs: number;
91
+ issuedAtMs: number;
92
+ skewMs: number;
93
+ };
94
+ _extractJti(token: any): any;
95
+ scheduleRefresh(token: any, context?: string): void;
96
+ reauthInBand(newJwt: any, prevJti: any, trigger?: string): Promise<boolean>;
97
+ _checkRefreshLeadWindow(): boolean;
98
+ _ensureControlPlane(reason: any, forceWait?: boolean): Promise<boolean>;
99
+ _extractMqttReasonFromError(mqttError: any): any;
100
+ /** Story 12.1: report a fatal condition (formerly process.exit(1)) to the owner of the tunnel. */
101
+ _fatal(code: any, details?: {}): any;
102
+ /** Story 12.1: a message for the user (formerly printed here); the caller decides the wording. */
103
+ _notice(code: any, details?: {}): void;
104
+ _localUnreachable(err: any): void;
105
+ localServiceDown: boolean;
106
+ _localReachable(): void;
107
+ /** Story 3.8: the visitor's answer when the local service cannot be reached. */
108
+ sendLocalUnavailable(requestId: any): void;
109
+ open(): Promise<any>;
110
+ _attemptConnect(): Promise<any>;
111
+ _helloAttempts: any;
112
+ scheduleReconnect(code: any, reason: any): any;
113
+ attemptReconnect(): Promise<void>;
114
+ _scheduleRefreshRetry(reason: any): any;
115
+ _cancelRefreshRetry(): void;
116
+ attemptJwtRefresh(): Promise<any>;
117
+ _fallbackToFreshTunnel(): Promise<any>;
118
+ sendTunnelError(requestId: any, status: any): void;
119
+ handleHttpRequest(msg: any): void;
120
+ handleHttpRequestBuffer(buffer: any): void;
121
+ _sendHello(): void;
122
+ _helloTimer: any;
123
+ _handleHelloAck(helloAck: any): void;
124
+ _canUseV2(): boolean;
125
+ _encodeRequestId(requestId: any): Buffer<ArrayBuffer>;
126
+ _selectResponseMode(contentLength: any, isChunked: any): "v2" | "v1";
127
+ _sendV2Chunk(requestId: any, payload: any): boolean;
128
+ _chunkPayload(payload: any): any[];
129
+ _handleV2Response(proxyRes: any, requestId: any): void;
130
+ _handleV1Response(proxyRes: any, requestId: any): void;
131
+ close(): void;
132
+ getHeartbeatInterval(): number;
133
+ startHeartbeat(): void;
134
+ sendHeartbeat(): void;
135
+ stopHeartbeat(): void;
136
+ }
137
+ import WebSocket from 'ws';