rocketride 1.0.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 (166) hide show
  1. package/README.md +476 -0
  2. package/dist/cjs/client.js +1745 -0
  3. package/dist/cjs/client.js.map +1 -0
  4. package/dist/cjs/constants.js +48 -0
  5. package/dist/cjs/constants.js.map +1 -0
  6. package/dist/cjs/core/DAPBase.js +328 -0
  7. package/dist/cjs/core/DAPBase.js.map +1 -0
  8. package/dist/cjs/core/DAPClient.js +226 -0
  9. package/dist/cjs/core/DAPClient.js.map +1 -0
  10. package/dist/cjs/core/TransportBase.js +152 -0
  11. package/dist/cjs/core/TransportBase.js.map +1 -0
  12. package/dist/cjs/core/TransportWebSocket.js +646 -0
  13. package/dist/cjs/core/TransportWebSocket.js.map +1 -0
  14. package/dist/cjs/exceptions/index.js +119 -0
  15. package/dist/cjs/exceptions/index.js.map +1 -0
  16. package/dist/cjs/index.js +56 -0
  17. package/dist/cjs/index.js.map +1 -0
  18. package/dist/cjs/package.json +3 -0
  19. package/dist/cjs/schema/Doc.js +79 -0
  20. package/dist/cjs/schema/Doc.js.map +1 -0
  21. package/dist/cjs/schema/DocFilter.js +133 -0
  22. package/dist/cjs/schema/DocFilter.js.map +1 -0
  23. package/dist/cjs/schema/DocGroup.js +230 -0
  24. package/dist/cjs/schema/DocGroup.js.map +1 -0
  25. package/dist/cjs/schema/DocMetadata.js +58 -0
  26. package/dist/cjs/schema/DocMetadata.js.map +1 -0
  27. package/dist/cjs/schema/Question.js +414 -0
  28. package/dist/cjs/schema/Question.js.map +1 -0
  29. package/dist/cjs/schema/index.js +50 -0
  30. package/dist/cjs/schema/index.js.map +1 -0
  31. package/dist/cjs/types/client.js +26 -0
  32. package/dist/cjs/types/client.js.map +1 -0
  33. package/dist/cjs/types/data.js +26 -0
  34. package/dist/cjs/types/data.js.map +1 -0
  35. package/dist/cjs/types/events.js +119 -0
  36. package/dist/cjs/types/events.js.map +1 -0
  37. package/dist/cjs/types/index.js +50 -0
  38. package/dist/cjs/types/index.js.map +1 -0
  39. package/dist/cjs/types/pipeline.js +26 -0
  40. package/dist/cjs/types/pipeline.js.map +1 -0
  41. package/dist/cjs/types/task.js +115 -0
  42. package/dist/cjs/types/task.js.map +1 -0
  43. package/dist/cli/cli/rocketride.js +1399 -0
  44. package/dist/cli/cli/rocketride.js.map +1 -0
  45. package/dist/cli/client/client.js +1745 -0
  46. package/dist/cli/client/client.js.map +1 -0
  47. package/dist/cli/client/constants.js +48 -0
  48. package/dist/cli/client/constants.js.map +1 -0
  49. package/dist/cli/client/core/DAPBase.js +328 -0
  50. package/dist/cli/client/core/DAPBase.js.map +1 -0
  51. package/dist/cli/client/core/DAPClient.js +226 -0
  52. package/dist/cli/client/core/DAPClient.js.map +1 -0
  53. package/dist/cli/client/core/TransportBase.js +152 -0
  54. package/dist/cli/client/core/TransportBase.js.map +1 -0
  55. package/dist/cli/client/core/TransportWebSocket.js +646 -0
  56. package/dist/cli/client/core/TransportWebSocket.js.map +1 -0
  57. package/dist/cli/client/exceptions/index.js +119 -0
  58. package/dist/cli/client/exceptions/index.js.map +1 -0
  59. package/dist/cli/client/index.js +56 -0
  60. package/dist/cli/client/index.js.map +1 -0
  61. package/dist/cli/client/schema/Doc.js +79 -0
  62. package/dist/cli/client/schema/Doc.js.map +1 -0
  63. package/dist/cli/client/schema/DocFilter.js +133 -0
  64. package/dist/cli/client/schema/DocFilter.js.map +1 -0
  65. package/dist/cli/client/schema/DocGroup.js +230 -0
  66. package/dist/cli/client/schema/DocGroup.js.map +1 -0
  67. package/dist/cli/client/schema/DocMetadata.js +58 -0
  68. package/dist/cli/client/schema/DocMetadata.js.map +1 -0
  69. package/dist/cli/client/schema/Question.js +414 -0
  70. package/dist/cli/client/schema/Question.js.map +1 -0
  71. package/dist/cli/client/schema/index.js +50 -0
  72. package/dist/cli/client/schema/index.js.map +1 -0
  73. package/dist/cli/client/types/client.js +26 -0
  74. package/dist/cli/client/types/client.js.map +1 -0
  75. package/dist/cli/client/types/data.js +26 -0
  76. package/dist/cli/client/types/data.js.map +1 -0
  77. package/dist/cli/client/types/events.js +119 -0
  78. package/dist/cli/client/types/events.js.map +1 -0
  79. package/dist/cli/client/types/index.js +50 -0
  80. package/dist/cli/client/types/index.js.map +1 -0
  81. package/dist/cli/client/types/pipeline.js +26 -0
  82. package/dist/cli/client/types/pipeline.js.map +1 -0
  83. package/dist/cli/client/types/task.js +115 -0
  84. package/dist/cli/client/types/task.js.map +1 -0
  85. package/dist/esm/client.js +1740 -0
  86. package/dist/esm/client.js.map +1 -0
  87. package/dist/esm/constants.js +45 -0
  88. package/dist/esm/constants.js.map +1 -0
  89. package/dist/esm/core/DAPBase.js +324 -0
  90. package/dist/esm/core/DAPBase.js.map +1 -0
  91. package/dist/esm/core/DAPClient.js +222 -0
  92. package/dist/esm/core/DAPClient.js.map +1 -0
  93. package/dist/esm/core/TransportBase.js +148 -0
  94. package/dist/esm/core/TransportBase.js.map +1 -0
  95. package/dist/esm/core/TransportWebSocket.js +609 -0
  96. package/dist/esm/core/TransportWebSocket.js.map +1 -0
  97. package/dist/esm/exceptions/index.js +109 -0
  98. package/dist/esm/exceptions/index.js.map +1 -0
  99. package/dist/esm/index.js +40 -0
  100. package/dist/esm/index.js.map +1 -0
  101. package/dist/esm/package.json +3 -0
  102. package/dist/esm/schema/Doc.js +75 -0
  103. package/dist/esm/schema/Doc.js.map +1 -0
  104. package/dist/esm/schema/DocFilter.js +129 -0
  105. package/dist/esm/schema/DocFilter.js.map +1 -0
  106. package/dist/esm/schema/DocGroup.js +226 -0
  107. package/dist/esm/schema/DocGroup.js.map +1 -0
  108. package/dist/esm/schema/DocMetadata.js +54 -0
  109. package/dist/esm/schema/DocMetadata.js.map +1 -0
  110. package/dist/esm/schema/Question.js +409 -0
  111. package/dist/esm/schema/Question.js.map +1 -0
  112. package/dist/esm/schema/index.js +34 -0
  113. package/dist/esm/schema/index.js.map +1 -0
  114. package/dist/esm/types/client.js +25 -0
  115. package/dist/esm/types/client.js.map +1 -0
  116. package/dist/esm/types/data.js +25 -0
  117. package/dist/esm/types/data.js.map +1 -0
  118. package/dist/esm/types/events.js +116 -0
  119. package/dist/esm/types/events.js.map +1 -0
  120. package/dist/esm/types/index.js +34 -0
  121. package/dist/esm/types/index.js.map +1 -0
  122. package/dist/esm/types/pipeline.js +25 -0
  123. package/dist/esm/types/pipeline.js.map +1 -0
  124. package/dist/esm/types/task.js +112 -0
  125. package/dist/esm/types/task.js.map +1 -0
  126. package/dist/types/client.d.ts +798 -0
  127. package/dist/types/client.d.ts.map +1 -0
  128. package/dist/types/constants.d.ts +45 -0
  129. package/dist/types/constants.d.ts.map +1 -0
  130. package/dist/types/core/DAPBase.d.ts +152 -0
  131. package/dist/types/core/DAPBase.d.ts.map +1 -0
  132. package/dist/types/core/DAPClient.d.ts +93 -0
  133. package/dist/types/core/DAPClient.d.ts.map +1 -0
  134. package/dist/types/core/TransportBase.d.ts +113 -0
  135. package/dist/types/core/TransportBase.d.ts.map +1 -0
  136. package/dist/types/core/TransportWebSocket.d.ts +100 -0
  137. package/dist/types/core/TransportWebSocket.d.ts.map +1 -0
  138. package/dist/types/exceptions/index.d.ts +87 -0
  139. package/dist/types/exceptions/index.d.ts.map +1 -0
  140. package/dist/types/index.d.ts +36 -0
  141. package/dist/types/index.d.ts.map +1 -0
  142. package/dist/types/schema/Doc.d.ts +69 -0
  143. package/dist/types/schema/Doc.d.ts.map +1 -0
  144. package/dist/types/schema/DocFilter.d.ts +101 -0
  145. package/dist/types/schema/DocFilter.d.ts.map +1 -0
  146. package/dist/types/schema/DocGroup.d.ts +113 -0
  147. package/dist/types/schema/DocGroup.d.ts.map +1 -0
  148. package/dist/types/schema/DocMetadata.d.ts +77 -0
  149. package/dist/types/schema/DocMetadata.d.ts.map +1 -0
  150. package/dist/types/schema/Question.d.ts +163 -0
  151. package/dist/types/schema/Question.d.ts.map +1 -0
  152. package/dist/types/schema/index.d.ts +34 -0
  153. package/dist/types/schema/index.d.ts.map +1 -0
  154. package/dist/types/types/client.d.ts +149 -0
  155. package/dist/types/types/client.d.ts.map +1 -0
  156. package/dist/types/types/data.d.ts +95 -0
  157. package/dist/types/types/data.d.ts.map +1 -0
  158. package/dist/types/types/events.d.ts +246 -0
  159. package/dist/types/types/events.d.ts.map +1 -0
  160. package/dist/types/types/index.d.ts +34 -0
  161. package/dist/types/types/index.d.ts.map +1 -0
  162. package/dist/types/types/pipeline.d.ts +83 -0
  163. package/dist/types/types/pipeline.d.ts.map +1 -0
  164. package/dist/types/types/task.d.ts +314 -0
  165. package/dist/types/types/task.d.ts.map +1 -0
  166. package/package.json +61 -0
@@ -0,0 +1,646 @@
1
+ "use strict";
2
+ /**
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2026 RocketRide, Inc.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
26
+ if (k2 === undefined) k2 = k;
27
+ var desc = Object.getOwnPropertyDescriptor(m, k);
28
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
29
+ desc = { enumerable: true, get: function() { return m[k]; } };
30
+ }
31
+ Object.defineProperty(o, k2, desc);
32
+ }) : (function(o, m, k, k2) {
33
+ if (k2 === undefined) k2 = k;
34
+ o[k2] = m[k];
35
+ }));
36
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
37
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
38
+ }) : function(o, v) {
39
+ o["default"] = v;
40
+ });
41
+ var __importStar = (this && this.__importStar) || (function () {
42
+ var ownKeys = function(o) {
43
+ ownKeys = Object.getOwnPropertyNames || function (o) {
44
+ var ar = [];
45
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
46
+ return ar;
47
+ };
48
+ return ownKeys(o);
49
+ };
50
+ return function (mod) {
51
+ if (mod && mod.__esModule) return mod;
52
+ var result = {};
53
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
54
+ __setModuleDefault(result, mod);
55
+ return result;
56
+ };
57
+ })();
58
+ Object.defineProperty(exports, "__esModule", { value: true });
59
+ exports.TransportWebSocket = void 0;
60
+ const TransportBase_1 = require("./TransportBase");
61
+ const constants_1 = require("../constants");
62
+ /**
63
+ * Node.js WebSocket (ws package) loading - ESM/CJS compatibility
64
+ *
65
+ * We use dynamic import() instead of require() because:
66
+ * - In ESM (type: "module"), require is not defined; require('ws') throws ReferenceError at load time
67
+ * - The package is dual-mode (exports both CJS and ESM builds); ESM consumers would hit this
68
+ *
69
+ * Loading is deferred until connect() (lazy) so:
70
+ * - Browser builds never load ws (no window check needed at module load)
71
+ * - Single load is cached; concurrent connect() calls share the same promise
72
+ *
73
+ * Install ws in Node.js projects: npm install ws
74
+ */
75
+ let NodeWebSocket;
76
+ let NodeWebSocketPromise = null;
77
+ async function ensureNodeWebSocket() {
78
+ if (NodeWebSocket !== undefined)
79
+ return NodeWebSocket;
80
+ if (typeof window !== 'undefined')
81
+ return undefined;
82
+ if (NodeWebSocketPromise)
83
+ return NodeWebSocketPromise;
84
+ NodeWebSocketPromise = (async () => {
85
+ try {
86
+ const wsModule = await Promise.resolve().then(() => __importStar(require('ws')));
87
+ // ESM interop: default export is the constructor; CJS-style modules may expose it directly
88
+ const WsConstructor = wsModule.default ?? wsModule;
89
+ NodeWebSocket = WsConstructor;
90
+ return NodeWebSocket;
91
+ }
92
+ catch {
93
+ return undefined;
94
+ }
95
+ })();
96
+ return NodeWebSocketPromise;
97
+ }
98
+ /**
99
+ * WebSocket transport implementation for DAP protocol communication.
100
+ *
101
+ * Provides WebSocket-based communication between RocketRide clients and servers
102
+ * with support for both text (JSON) and binary (CBOR) message formats. Handles
103
+ * connection lifecycle management, message serialization/deserialization, automatic
104
+ * heartbeat/ping messages, and connection timeout detection.
105
+ *
106
+ * Features:
107
+ * - Cross-platform: Works in both browser (native WebSocket) and Node.js (ws library)
108
+ * - Automatic message encoding/decoding (JSON and CBOR)
109
+ * - Connection timeout handling
110
+ * - Authentication via headers
111
+ * - Message queuing during connection
112
+ *
113
+ * @extends TransportBase
114
+ */
115
+ class TransportWebSocket extends TransportBase_1.TransportBase {
116
+ constructor(uri = constants_1.CONST_DEFAULT_SERVICE, auth) {
117
+ super();
118
+ this._messageTasks = new Set();
119
+ this._lastPong = Date.now();
120
+ this._uri = uri;
121
+ this._auth = auth;
122
+ }
123
+ /** Auth credential for use by connect flow (e.g. first DAP auth command). */
124
+ getAuth() {
125
+ return this._auth;
126
+ }
127
+ /** Connection info for the "connected" callback (URI). */
128
+ getConnectionInfo() {
129
+ return this._uri;
130
+ }
131
+ /** Update auth credential. Takes effect on the next connect(). */
132
+ setAuth(auth) { this._auth = auth; }
133
+ /** Update connection URI. Takes effect on the next connect(). */
134
+ setUri(uri) { this._uri = uri; }
135
+ /**
136
+ * Start ping interval for Node.js WebSocket connections.
137
+ * Sends ping frames at regular intervals and monitors for pong responses.
138
+ * If no pong is received within the timeout period, the connection is terminated.
139
+ */
140
+ _startPingInterval() {
141
+ // Only for Node.js WebSocket (browser handles ping/pong automatically)
142
+ if (typeof window !== 'undefined' || !this._websocket) {
143
+ return;
144
+ }
145
+ this._lastPong = Date.now();
146
+ this._pingInterval = setInterval(() => {
147
+ if (!this._connected || !this._websocket) {
148
+ this._stopPingInterval();
149
+ return;
150
+ }
151
+ // Cast to Node.js WebSocket type for ping/terminate methods
152
+ const nodeWs = this._websocket;
153
+ // Check if we've received a pong within the timeout period
154
+ const timeSinceLastPong = Date.now() - this._lastPong;
155
+ if (timeSinceLastPong > constants_1.CONST_WS_PING_TIMEOUT * 1000) {
156
+ this._debugMessage(`No pong received for ${timeSinceLastPong}ms - connection dead`);
157
+ this._stopPingInterval();
158
+ nodeWs.terminate();
159
+ return;
160
+ }
161
+ // Send ping
162
+ try {
163
+ if (nodeWs.readyState === 1) { // OPEN state
164
+ nodeWs.ping();
165
+ this._debugMessage('Sent ping to server');
166
+ }
167
+ }
168
+ catch (error) {
169
+ this._debugMessage(`Error sending ping: ${error}`);
170
+ }
171
+ }, constants_1.CONST_WS_PING_INTERVAL * 1000);
172
+ }
173
+ /**
174
+ * Stop the ping interval timer.
175
+ */
176
+ _stopPingInterval() {
177
+ if (this._pingInterval) {
178
+ clearInterval(this._pingInterval);
179
+ this._pingInterval = undefined;
180
+ }
181
+ }
182
+ /**
183
+ * Process raw WebSocket data into structured messages.
184
+ *
185
+ * Handles both JSON text messages and DAP binary format messages.
186
+ * Binary messages use format: JSON header + newline + binary payload.
187
+ */
188
+ async _receiveData(data) {
189
+ try {
190
+ if (!this._connected) {
191
+ return;
192
+ }
193
+ if (typeof data === 'string') {
194
+ // JSON text message
195
+ const jsonMessage = JSON.parse(data);
196
+ await this._transportReceive(jsonMessage);
197
+ }
198
+ else {
199
+ // Binary message - look for JSON header separator
200
+ const dataArray = new Uint8Array(data);
201
+ const newlinePos = dataArray.indexOf(10); // ASCII newline
202
+ if (newlinePos === -1) {
203
+ // No separator - treat as JSON text
204
+ const textData = new TextDecoder().decode(dataArray);
205
+ const jsonMessage = JSON.parse(textData);
206
+ await this._transportReceive(jsonMessage);
207
+ }
208
+ else {
209
+ // DAP binary format: JSON header + newline + binary data
210
+ const jsonHeader = dataArray.slice(0, newlinePos);
211
+ const binaryData = dataArray.slice(newlinePos + 1);
212
+ // Parse JSON header
213
+ const headerText = new TextDecoder().decode(jsonHeader);
214
+ const jsonMessage = JSON.parse(headerText);
215
+ // Add binary data to message arguments
216
+ if (!jsonMessage.arguments) {
217
+ jsonMessage.arguments = {};
218
+ }
219
+ jsonMessage.arguments.data = binaryData;
220
+ await this._transportReceive(jsonMessage);
221
+ }
222
+ }
223
+ }
224
+ catch (error) {
225
+ // Only log errors if still connected
226
+ if (this._connected) {
227
+ this._debugMessage(`Error processing WebSocket message: ${error}`);
228
+ }
229
+ }
230
+ }
231
+ /**
232
+ * Connect to WebSocket server and start receiving messages.
233
+ * Works in both browser and Node.js environments.
234
+ */
235
+ async connect(timeout) {
236
+ const isBrowser = typeof window !== 'undefined';
237
+ let nodeWs;
238
+ if (!isBrowser) {
239
+ // Must load ws before creating the connection; ensureNodeWebSocket() uses dynamic import()
240
+ // so it works when this package is consumed as ESM (e.g. from a project with "type": "module")
241
+ nodeWs = await ensureNodeWebSocket();
242
+ if (!nodeWs) {
243
+ throw new Error('WebSocket library (ws) not available in Node.js environment. Install it with: npm install ws');
244
+ }
245
+ }
246
+ return new Promise((resolve, reject) => {
247
+ let promiseResolved = false;
248
+ const resolveOnce = () => {
249
+ if (!promiseResolved) {
250
+ promiseResolved = true;
251
+ resolve();
252
+ }
253
+ };
254
+ const rejectOnce = (error) => {
255
+ if (!promiseResolved) {
256
+ promiseResolved = true;
257
+ reject(error);
258
+ }
259
+ };
260
+ try {
261
+ this._debugMessage(`Connecting to WebSocket server at ${this._uri}`);
262
+ if (this._connectionTimeout) {
263
+ clearTimeout(this._connectionTimeout);
264
+ this._connectionTimeout = undefined;
265
+ }
266
+ if (isBrowser) {
267
+ // ============================================================
268
+ // BROWSER WebSocket (native API)
269
+ // ============================================================
270
+ // Connect without auth on URL; first DAP message must be auth (sent by connect flow via request())
271
+ const wsUrl = this._uri;
272
+ this._websocket = new WebSocket(wsUrl);
273
+ this._websocket.binaryType = 'arraybuffer';
274
+ this._websocket.onopen = async () => {
275
+ if (this._connectionTimeout) {
276
+ clearTimeout(this._connectionTimeout);
277
+ this._connectionTimeout = undefined;
278
+ }
279
+ this._connected = true;
280
+ this._debugMessage(`Successfully connected to ${this._uri}`);
281
+ // Resolve so DAPClient can send auth; do not fire _transportConnected until after auth
282
+ resolveOnce();
283
+ };
284
+ this._websocket.onmessage = async (event) => {
285
+ const data = typeof event.data === 'string' ? event.data : event.data;
286
+ const task = this._receiveData(data);
287
+ this._messageTasks.add(task);
288
+ task.finally(() => this._messageTasks.delete(task));
289
+ };
290
+ this._websocket.onclose = async (event) => {
291
+ if (this._connectionTimeout) {
292
+ clearTimeout(this._connectionTimeout);
293
+ this._connectionTimeout = undefined;
294
+ }
295
+ this._connected = false;
296
+ const wasClean = event.code === 1000;
297
+ const reasonText = event.reason || (wasClean ? 'Connection closed normally' : 'Connection closed unexpectedly');
298
+ // Always notify disconnection to enable reconnection
299
+ await this._transportDisconnected(reasonText, !wasClean);
300
+ if (!promiseResolved) {
301
+ rejectOnce(new Error(`Connection failed: ${reasonText} (code: ${event.code})`));
302
+ }
303
+ };
304
+ this._websocket.onerror = async () => {
305
+ if (this._connectionTimeout) {
306
+ clearTimeout(this._connectionTimeout);
307
+ this._connectionTimeout = undefined;
308
+ }
309
+ this._connected = false;
310
+ this._debugMessage(`WebSocket error occurred`);
311
+ // Close the websocket and release for garbage collection
312
+ if (this._websocket) {
313
+ try {
314
+ this._websocket.close();
315
+ }
316
+ catch {
317
+ // Ignore close errors
318
+ }
319
+ this._websocket = null;
320
+ }
321
+ if (!promiseResolved) {
322
+ rejectOnce(new Error(`Failed to connect to ${this._uri}`));
323
+ }
324
+ else {
325
+ await this._transportDisconnected('WebSocket error', true);
326
+ }
327
+ };
328
+ }
329
+ else {
330
+ // ============================================================
331
+ // NODE.JS WebSocket (ws library)
332
+ // ============================================================
333
+ // nodeWs is the WebSocket constructor from 'ws' (loaded above via ensureNodeWebSocket)
334
+ // ws exports the constructor directly: new WebSocket(url)
335
+ this._websocket = new nodeWs(this._uri);
336
+ this._websocket.on('open', async () => {
337
+ if (this._connectionTimeout) {
338
+ clearTimeout(this._connectionTimeout);
339
+ this._connectionTimeout = undefined;
340
+ }
341
+ this._connected = true;
342
+ this._debugMessage(`Successfully connected to ${this._uri}`);
343
+ // Start ping/pong heartbeat for Node.js WebSocket
344
+ this._startPingInterval();
345
+ // Resolve so DAPClient can send auth; do not fire _transportConnected until after auth
346
+ resolveOnce();
347
+ });
348
+ // Handle pong responses to track connection health
349
+ this._websocket.on('pong', () => {
350
+ this._lastPong = Date.now();
351
+ this._debugMessage('Received pong from server');
352
+ });
353
+ this._websocket.on('message', async (data, isBinary) => {
354
+ // Convert to string or ArrayBuffer for compatibility
355
+ let messageData;
356
+ if (!isBinary && typeof data === 'string') {
357
+ messageData = data;
358
+ }
359
+ else if (Buffer.isBuffer(data)) {
360
+ // Convert Buffer to ArrayBuffer
361
+ const arrayBuffer = new ArrayBuffer(data.length);
362
+ const view = new Uint8Array(arrayBuffer);
363
+ view.set(data);
364
+ messageData = arrayBuffer;
365
+ }
366
+ else if (data instanceof ArrayBuffer) {
367
+ messageData = data;
368
+ }
369
+ else if (Array.isArray(data)) {
370
+ // Buffer[] - concatenate into single ArrayBuffer
371
+ const totalLength = data.reduce((sum, buf) => sum + buf.length, 0);
372
+ const arrayBuffer = new ArrayBuffer(totalLength);
373
+ const view = new Uint8Array(arrayBuffer);
374
+ let offset = 0;
375
+ for (const buf of data) {
376
+ view.set(buf, offset);
377
+ offset += buf.length;
378
+ }
379
+ messageData = arrayBuffer;
380
+ }
381
+ else {
382
+ // Fallback: treat as string
383
+ messageData = String(data);
384
+ }
385
+ const task = this._receiveData(messageData);
386
+ this._messageTasks.add(task);
387
+ task.finally(() => this._messageTasks.delete(task));
388
+ });
389
+ this._websocket.on('close', async (code, reason) => {
390
+ if (this._connectionTimeout) {
391
+ clearTimeout(this._connectionTimeout);
392
+ this._connectionTimeout = undefined;
393
+ }
394
+ this._connected = false;
395
+ const wasClean = code === 1000;
396
+ const reasonText = (reason ? reason.toString() : '') || (wasClean ? 'Connection closed normally' : 'Connection closed unexpectedly');
397
+ // Always notify disconnection to enable reconnection
398
+ await this._transportDisconnected(reasonText, !wasClean);
399
+ if (!promiseResolved) {
400
+ rejectOnce(new Error(`Connection failed: ${reasonText} (code: ${code})`));
401
+ }
402
+ });
403
+ this._websocket.on('error', async (error) => {
404
+ if (this._connectionTimeout) {
405
+ clearTimeout(this._connectionTimeout);
406
+ this._connectionTimeout = undefined;
407
+ }
408
+ this._connected = false;
409
+ this._debugMessage(`WebSocket error: ${error.message}`);
410
+ // Close the websocket and release for garbage collection
411
+ if (this._websocket) {
412
+ try {
413
+ this._websocket.close();
414
+ }
415
+ catch {
416
+ // Ignore close errors
417
+ }
418
+ this._websocket = null;
419
+ }
420
+ if (!promiseResolved) {
421
+ rejectOnce(new Error(`Failed to connect to ${this._uri}: ${error.message}`));
422
+ }
423
+ else {
424
+ await this._transportDisconnected(`WebSocket error: ${error.message}`, true);
425
+ }
426
+ });
427
+ }
428
+ // Set connection timeout (works same in both environments)
429
+ const effectiveTimeout = timeout ?? constants_1.CONST_SOCKET_TIMEOUT * 1000;
430
+ this._connectionTimeout = setTimeout(() => {
431
+ if (this._connectionTimeout) {
432
+ clearTimeout(this._connectionTimeout);
433
+ this._connectionTimeout = undefined;
434
+ }
435
+ // Force close WebSocket if still connecting and release for GC
436
+ if (this._websocket) {
437
+ if (isBrowser) {
438
+ this._websocket.close();
439
+ }
440
+ else {
441
+ this._websocket.close();
442
+ }
443
+ this._websocket = null;
444
+ }
445
+ rejectOnce(new Error(`Connection timeout after ${effectiveTimeout}ms`));
446
+ }, effectiveTimeout);
447
+ }
448
+ catch (error) {
449
+ if (this._connectionTimeout) {
450
+ clearTimeout(this._connectionTimeout);
451
+ this._connectionTimeout = undefined;
452
+ }
453
+ this._debugMessage(`Failed to connect to ${this._uri}: ${error}`);
454
+ rejectOnce(new Error(`Connection setup failed: ${error}`));
455
+ }
456
+ });
457
+ }
458
+ /**
459
+ * Disconnect and close the WebSocket connection gracefully.
460
+ * Works in both browser and Node.js environments.
461
+ * @param reason - Optional reason for disconnection (reported to onDisconnected).
462
+ * @param hasError - Optional; when true, report as error to onDisconnected.
463
+ */
464
+ async disconnect(reason, hasError) {
465
+ // Clear any pending connection timeout
466
+ if (this._connectionTimeout) {
467
+ clearTimeout(this._connectionTimeout);
468
+ this._connectionTimeout = undefined;
469
+ }
470
+ // Stop ping interval
471
+ this._stopPingInterval();
472
+ if (!this._connected || !this._websocket) {
473
+ return;
474
+ }
475
+ let callbackCalled = false;
476
+ const isBrowser = typeof window !== 'undefined';
477
+ try {
478
+ this._debugMessage('Gracefully disconnecting WebSocket');
479
+ // Cancel pending message processing tasks
480
+ if (this._messageTasks.size > 0) {
481
+ this._debugMessage(`Waiting for ${this._messageTasks.size} pending message tasks`);
482
+ await Promise.allSettled(Array.from(this._messageTasks));
483
+ this._debugMessage('Message tasks completed');
484
+ }
485
+ // Close WebSocket and wait for complete closure
486
+ if (this._websocket) {
487
+ const ws = this._websocket;
488
+ if (isBrowser) {
489
+ // Browser WebSocket
490
+ const browserWs = ws;
491
+ const closePromise = new Promise((resolve) => {
492
+ if (browserWs.readyState === WebSocket.CLOSED) {
493
+ resolve();
494
+ return;
495
+ }
496
+ let resolved = false;
497
+ const resolveOnce = (timerId) => {
498
+ if (!resolved) {
499
+ resolved = true;
500
+ if (timerId)
501
+ clearTimeout(timerId);
502
+ browserWs.onclose = null;
503
+ resolve();
504
+ }
505
+ };
506
+ browserWs.close(1000, 'Disconnected by request');
507
+ const timeoutId = setTimeout(() => {
508
+ this._debugMessage('WebSocket close timeout - forcing resolution');
509
+ resolveOnce();
510
+ }, 500);
511
+ browserWs.onclose = () => {
512
+ this._debugMessage('WebSocket close event received');
513
+ resolveOnce(timeoutId);
514
+ };
515
+ });
516
+ await closePromise;
517
+ }
518
+ else {
519
+ // Node.js WebSocket
520
+ ws.removeAllListeners();
521
+ const closePromise = new Promise((resolve) => {
522
+ if (ws.readyState === 3) { // CLOSED state
523
+ resolve();
524
+ return;
525
+ }
526
+ let resolved = false;
527
+ const nodeWs = ws;
528
+ const resolveOnce = (timerId) => {
529
+ if (!resolved) {
530
+ resolved = true;
531
+ if (timerId)
532
+ clearTimeout(timerId);
533
+ resolve();
534
+ }
535
+ };
536
+ const tempCloseHandler = () => {
537
+ this._debugMessage('WebSocket close event received');
538
+ resolveOnce(timeoutId);
539
+ };
540
+ nodeWs.on('close', tempCloseHandler);
541
+ ws.close(1000, 'Disconnected by request');
542
+ const timeoutId = setTimeout(() => {
543
+ nodeWs.removeListener('close', tempCloseHandler);
544
+ this._debugMessage('WebSocket close timeout - forcing resolution');
545
+ resolveOnce();
546
+ }, 500);
547
+ });
548
+ await closePromise;
549
+ }
550
+ }
551
+ this._debugMessage('WebSocket disconnected successfully');
552
+ // Notify about disconnection (use caller-provided reason/hasError when given)
553
+ await this._transportDisconnected(reason ?? 'Disconnected by request', hasError ?? false);
554
+ callbackCalled = true;
555
+ }
556
+ catch (error) {
557
+ this._debugMessage(`Error during disconnect: ${error}`);
558
+ if (!callbackCalled) {
559
+ await this._transportDisconnected(`Disconnect error: ${error}`, true);
560
+ callbackCalled = true;
561
+ }
562
+ }
563
+ finally {
564
+ // Always clean up resources
565
+ this._connected = false;
566
+ this._websocket = undefined;
567
+ this._messageTasks.clear();
568
+ this._debugMessage('Disconnect cleanup completed');
569
+ }
570
+ }
571
+ /**
572
+ * Send a DAP message with automatic format selection.
573
+ *
574
+ * Handles both standard JSON messages and DAP binary messages with
575
+ * data payloads. Automatically chooses appropriate WebSocket message
576
+ * format based on message content.
577
+ *
578
+ * Works in both browser and Node.js environments.
579
+ */
580
+ async send(message) {
581
+ if (!this.isConnected()) {
582
+ throw new Error('WebSocket not connected. Call connect() first.');
583
+ }
584
+ if (!this._websocket) {
585
+ throw new Error('WebSocket connection lost before send');
586
+ }
587
+ let binaryData;
588
+ const args = message.arguments || {};
589
+ try {
590
+ if ('data' in args) {
591
+ // Binary message - use DAP binary format
592
+ if (typeof args.data === 'string') {
593
+ binaryData = new TextEncoder().encode(args.data);
594
+ }
595
+ else if (args.data instanceof Uint8Array) {
596
+ binaryData = args.data;
597
+ }
598
+ else {
599
+ binaryData = new TextEncoder().encode(JSON.stringify(args.data));
600
+ }
601
+ // Create debug version for logging
602
+ const debugMessage = { ...message };
603
+ if (debugMessage.arguments) {
604
+ debugMessage.arguments = { ...debugMessage.arguments };
605
+ debugMessage.arguments.data = `<${binaryData.length} bytes>`;
606
+ }
607
+ this._debugProtocol(`SEND: ${JSON.stringify(debugMessage)}`);
608
+ // Remove data from message for header
609
+ const headerMessage = { ...message };
610
+ if (headerMessage.arguments) {
611
+ headerMessage.arguments = { ...headerMessage.arguments };
612
+ delete headerMessage.arguments.data;
613
+ }
614
+ // Create DAP binary message: JSON header + newline + binary data
615
+ const jsonHeader = new TextEncoder().encode(JSON.stringify(headerMessage));
616
+ const newline = new Uint8Array([10]); // ASCII newline
617
+ const combinedMessage = new Uint8Array(jsonHeader.length + 1 + binaryData.length);
618
+ combinedMessage.set(jsonHeader);
619
+ combinedMessage.set(newline, jsonHeader.length);
620
+ combinedMessage.set(binaryData, jsonHeader.length + 1);
621
+ // Send binary message
622
+ this._websocket.send(combinedMessage);
623
+ }
624
+ else {
625
+ // Standard JSON message
626
+ this._debugProtocol(`SEND: ${JSON.stringify(message)}`);
627
+ this._websocket.send(JSON.stringify(message));
628
+ }
629
+ }
630
+ catch (error) {
631
+ if (error instanceof Error && error.name === 'NetworkError') {
632
+ // Connection errors should update state
633
+ this._connected = false;
634
+ this._debugMessage(`Connection lost during send: ${error.message}`);
635
+ throw new Error(`Connection lost during send: ${error.message}`);
636
+ }
637
+ else {
638
+ // Log send failures for debugging
639
+ this._debugMessage(`Failed to send message: ${error}`);
640
+ throw error;
641
+ }
642
+ }
643
+ }
644
+ }
645
+ exports.TransportWebSocket = TransportWebSocket;
646
+ //# sourceMappingURL=TransportWebSocket.js.map