@daytona/sdk 0.198.0 → 0.199.0-alpha.1

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.
@@ -1,394 +0,0 @@
1
- /*
2
- * Copyright Daytona Platforms Inc.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- import { io } from 'socket.io-client';
6
- /**
7
- * Extracts the resource ID from an event payload.
8
- *
9
- * Handles two payload shapes:
10
- * - Wrapper: {sandbox: {id: ...}, ...} -> nested resource ID
11
- * - Direct: {id: ...} -> top-level ID
12
- */
13
- function extractIdFromEvent(data) {
14
- if (!data || typeof data !== 'object')
15
- return undefined;
16
- for (const key of ['sandbox', 'volume', 'snapshot', 'runner']) {
17
- const nested = data[key];
18
- if (nested && typeof nested === 'object' && typeof nested.id === 'string') {
19
- return nested.id;
20
- }
21
- }
22
- if (typeof data.id === 'string') {
23
- return data.id;
24
- }
25
- return undefined;
26
- }
27
- /**
28
- * Manages a Socket.IO connection to the Daytona notification gateway
29
- * and dispatches resource events to per-resource handlers.
30
- */
31
- export class EventDispatcher {
32
- apiUrl;
33
- token;
34
- organizationId;
35
- source;
36
- sdkVersion;
37
- socket;
38
- _connected = false;
39
- _closed = false;
40
- _failed = false;
41
- _failError = null;
42
- listeners = new Map();
43
- registeredEvents = new Set();
44
- connectPromise = null;
45
- ensureConnectPromise = null;
46
- reconnectAttempts = 0;
47
- maxReconnectAttempts = 10;
48
- // Cleared on connect/error/disconnect to prevent late state mutation.
49
- _connectTimer = null;
50
- disconnectTimer = null;
51
- disconnectGeneration = 0;
52
- static DISCONNECT_DELAY_MS = 30_000;
53
- // Set once on the first Manager 'open' by capability probe, not runtime name.
54
- // `undefined` (pre-connect) is treated as `false` → ephemeral disconnect.
55
- supportsBackgroundUnref = undefined;
56
- constructor(apiUrl, token, organizationId, source, sdkVersion) {
57
- this.apiUrl = apiUrl;
58
- this.token = token;
59
- this.organizationId = organizationId;
60
- this.source = source;
61
- this.sdkVersion = sdkVersion;
62
- }
63
- /**
64
- * Idempotent: ensure a connection attempt is in progress or already established.
65
- *
66
- * Non-blocking. Fires-and-forgets a connect() call via a stored promise if not
67
- * already connected and no attempt is currently running.
68
- */
69
- ensureConnected() {
70
- // No-op after disconnect — prevents socket resurrection.
71
- if (this._closed)
72
- return;
73
- if (this._connected)
74
- return;
75
- if (this.connectPromise)
76
- return;
77
- if (this.ensureConnectPromise)
78
- return;
79
- this.ensureConnectPromise = this.connect()
80
- .catch(() => {
81
- // Callers check isConnected when they need it
82
- })
83
- .finally(() => {
84
- this.ensureConnectPromise = null;
85
- });
86
- }
87
- /**
88
- * Establishes the Socket.IO connection. Resolves when connected.
89
- * Throws if the connection fails within the timeout.
90
- */
91
- async connect(timeoutMs = 5000) {
92
- if (this._closed) {
93
- return;
94
- }
95
- if (this._connected && this.socket) {
96
- return;
97
- }
98
- if (this.connectPromise) {
99
- return this.connectPromise;
100
- }
101
- this.connectPromise = this.doConnect(timeoutMs);
102
- try {
103
- await this.connectPromise;
104
- if (this.listeners.size === 0) {
105
- this.scheduleDelayedDisconnect();
106
- }
107
- }
108
- catch (error) {
109
- if (this.listeners.size === 0) {
110
- // Stop retries if nobody is listening.
111
- this.socket?.disconnect();
112
- }
113
- throw error;
114
- }
115
- finally {
116
- this.connectPromise = null;
117
- }
118
- }
119
- doConnect(timeoutMs) {
120
- return new Promise((resolve, reject) => {
121
- if (this.socket) {
122
- this.socket.removeAllListeners();
123
- this.socket.disconnect();
124
- this.socket = undefined;
125
- }
126
- // Derive path from the API URL so reverse-proxy prefixes are preserved (not hardcoded /api).
127
- const url = new URL(this.apiUrl);
128
- const origin = url.origin;
129
- const socketPath = `${url.pathname.replace(/\/$/, '')}/socket.io/`;
130
- const query = {};
131
- if (this.organizationId) {
132
- query.organizationId = this.organizationId;
133
- }
134
- if (this.source) {
135
- query.source = this.source;
136
- }
137
- if (this.sdkVersion) {
138
- query.sdkVersion = this.sdkVersion;
139
- }
140
- this.socket = io(origin, {
141
- path: socketPath,
142
- autoConnect: false,
143
- transports: ['websocket'],
144
- query,
145
- reconnection: true,
146
- reconnectionAttempts: this.maxReconnectAttempts,
147
- reconnectionDelay: 1000,
148
- reconnectionDelayMax: 30000,
149
- });
150
- this.socket.auth = { token: this.token };
151
- this.clearConnectTimer();
152
- this._connectTimer = setTimeout(() => {
153
- if (!this._connected) {
154
- this.socket?.disconnect();
155
- this.clearConnectTimer();
156
- this._failed = true;
157
- this._failError = 'WebSocket connection timed out';
158
- reject(new Error(this._failError));
159
- }
160
- }, timeoutMs);
161
- if (typeof this._connectTimer.unref === 'function') {
162
- this._connectTimer.unref();
163
- }
164
- this.socket.on('connect', () => {
165
- this.clearConnectTimer();
166
- this._connected = true;
167
- this._failed = false;
168
- this._failError = null;
169
- this.reconnectAttempts = 0;
170
- resolve();
171
- });
172
- this.socket.on('connect_error', (err) => {
173
- if (!this._connected) {
174
- this.clearConnectTimer();
175
- this._failed = true;
176
- this._failError = `WebSocket connection failed: ${err.message}`;
177
- reject(new Error(this._failError));
178
- }
179
- });
180
- this.socket.on('disconnect', (reason) => {
181
- this._connected = false;
182
- if (reason === 'io server disconnect') {
183
- // Server initiated disconnect - try to reconnect
184
- this.socket?.connect();
185
- }
186
- });
187
- this.socket.io.on('reconnect', () => {
188
- this._connected = true;
189
- this._failed = false;
190
- this._failError = null;
191
- this.reconnectAttempts = 0;
192
- });
193
- this.socket.io.on('reconnect_attempt', () => {
194
- this.reconnectAttempts++;
195
- });
196
- this.socket.io.on('reconnect_failed', () => {
197
- this._connected = false;
198
- this._failed = true;
199
- this._failError = `WebSocket reconnection failed after ${this.maxReconnectAttempts} attempts`;
200
- });
201
- // Fires before engine.io's first _resetPingTimeout(), so enabling autoUnref here
202
- // also covers the initial + recurring ping timers and reconnect timers.
203
- this.socket.io.on('open', () => {
204
- this.applyUnrefCapability();
205
- });
206
- // Re-register any events that were added before the socket was created
207
- const pendingEvents = [...this.registeredEvents];
208
- this.registeredEvents.clear();
209
- this.registerEvents(pendingEvents);
210
- this.socket.connect();
211
- });
212
- }
213
- /**
214
- * Registers Socket.IO event handlers (idempotent -- each event is registered once).
215
- */
216
- registerEvents(events) {
217
- for (const eventName of events) {
218
- if (this.registeredEvents.has(eventName)) {
219
- continue;
220
- }
221
- this.registeredEvents.add(eventName);
222
- // If socket isn't created yet, the event will be registered when connect() runs
223
- if (!this.socket)
224
- continue;
225
- const handler = (data) => {
226
- const resourceId = extractIdFromEvent(data);
227
- if (resourceId) {
228
- this.dispatch(resourceId, eventName, data);
229
- }
230
- };
231
- this.socket.on(eventName, handler);
232
- }
233
- }
234
- /**
235
- * Registers a handler for events targeting a specific resource.
236
- * Returns an unsubscribe function.
237
- *
238
- * @param resourceId - The ID of the resource (e.g. sandbox ID).
239
- * @param handler - Callback receiving (eventName, rawData).
240
- * @param events - List of Socket.IO event names to listen for.
241
- */
242
- subscribe(resourceId, handler, events) {
243
- // No-op after disconnect — prevents socket resurrection.
244
- if (this._closed) {
245
- return () => {
246
- return;
247
- };
248
- }
249
- this.cancelDelayedDisconnect();
250
- this.disconnectGeneration++;
251
- this.ensureConnected();
252
- if (!this.listeners.has(resourceId)) {
253
- this.listeners.set(resourceId, new Set());
254
- }
255
- this.listeners.get(resourceId).add(handler);
256
- // Register any new events with the Socket.IO client
257
- this.registerEvents(events);
258
- return () => {
259
- const handlers = this.listeners.get(resourceId);
260
- if (handlers) {
261
- handlers.delete(handler);
262
- if (handlers.size === 0) {
263
- this.listeners.delete(resourceId);
264
- }
265
- }
266
- // Schedule delayed disconnect when no resources are listening anymore
267
- if (this.listeners.size === 0) {
268
- this.scheduleDelayedDisconnect();
269
- }
270
- };
271
- }
272
- /** Whether the WebSocket is currently connected */
273
- get isConnected() {
274
- return this._connected;
275
- }
276
- /** Whether the WebSocket has permanently failed (exhausted reconnection attempts) */
277
- get isFailed() {
278
- return this._failed;
279
- }
280
- /** The error message if the connection has failed */
281
- get failError() {
282
- return this._failError;
283
- }
284
- /** Disconnects and cleans up all resources */
285
- disconnect() {
286
- this._closed = true;
287
- this.cancelDelayedDisconnect();
288
- this.clearConnectTimer();
289
- this.connectPromise = null;
290
- this.ensureConnectPromise = null;
291
- this.disconnectSocket();
292
- }
293
- disconnectSocket() {
294
- if (this.socket) {
295
- this.socket.removeAllListeners();
296
- this.socket.disconnect();
297
- this.socket = undefined;
298
- }
299
- this._connected = false;
300
- this.listeners.clear();
301
- this.registeredEvents.clear();
302
- }
303
- /**
304
- * Decides, by capability rather than runtime name, whether this runtime can keep a
305
- * background socket open without blocking process exit.
306
- *
307
- * A runtime qualifies only if it can unref BOTH the transport's raw socket AND timers:
308
- * Node/Deno (ws package exposes `_socket`, timers are objects) qualify; Bun (native
309
- * WebSocket has no `_socket`) and browsers (timers are numbers, no `_socket`) do not.
310
- *
311
- * When it qualifies, this unrefs the current connection's socket (engine.io skipped it —
312
- * autoUnref was off during the transport's onopen) and enables engine.io's own autoUnref
313
- * so it keeps every later ping-timeout and reconnect timer unref'd. When it does not,
314
- * autoUnref stays off (avoiding the `ws._socket.unref()` and number-timer crashes) and
315
- * the socket is dropped the instant it goes idle (see scheduleDelayedDisconnect).
316
- */
317
- applyUnrefCapability() {
318
- const manager = this.socket?.io;
319
- // Known-unsupported runtime: nothing to unref; ephemeral disconnect handles exit.
320
- if (this.supportsBackgroundUnref === false)
321
- return;
322
- // Re-apply per Manager: an idle disconnect replaces the socket with a fresh manager
323
- // whose autoUnref is off, so skip only when this manager already has it applied.
324
- if (this.supportsBackgroundUnref === true && manager?.opts?.autoUnref === true)
325
- return;
326
- try {
327
- const engine = manager?.engine;
328
- const rawSocket = engine?.transport?.ws?._socket;
329
- const socketUnrefable = !!rawSocket && typeof rawSocket.unref === 'function';
330
- const probe = setTimeout(() => undefined, 0);
331
- const timerUnrefable = typeof probe?.unref === 'function';
332
- clearTimeout(probe);
333
- if (socketUnrefable && timerUnrefable) {
334
- rawSocket.unref();
335
- engine.opts.autoUnref = true;
336
- manager.opts.autoUnref = true;
337
- this.supportsBackgroundUnref = true;
338
- }
339
- else if (this.supportsBackgroundUnref === undefined) {
340
- this.supportsBackgroundUnref = false;
341
- }
342
- }
343
- catch {
344
- if (this.supportsBackgroundUnref === undefined)
345
- this.supportsBackgroundUnref = false;
346
- }
347
- }
348
- dispatch(resourceId, eventName, data) {
349
- if (!resourceId)
350
- return;
351
- const handlers = this.listeners.get(resourceId);
352
- if (handlers) {
353
- for (const handler of handlers) {
354
- try {
355
- handler(eventName, data);
356
- }
357
- catch {
358
- // Don't let a handler error break other handlers
359
- }
360
- }
361
- }
362
- }
363
- cancelDelayedDisconnect() {
364
- if (this.disconnectTimer) {
365
- clearTimeout(this.disconnectTimer);
366
- this.disconnectTimer = null;
367
- }
368
- }
369
- clearConnectTimer() {
370
- if (this._connectTimer) {
371
- clearTimeout(this._connectTimer);
372
- this._connectTimer = null;
373
- }
374
- }
375
- scheduleDelayedDisconnect() {
376
- this.cancelDelayedDisconnect();
377
- const generation = this.disconnectGeneration;
378
- // Where the socket can't be unref'd (Bun/browser), an open idle socket keeps the
379
- // event loop alive, so drop it immediately; elsewhere keep it warm for reuse.
380
- const delay = this.supportsBackgroundUnref === true ? EventDispatcher.DISCONNECT_DELAY_MS : 0;
381
- this.disconnectTimer = setTimeout(() => {
382
- if (generation !== this.disconnectGeneration) {
383
- return;
384
- }
385
- if (this.listeners.size === 0) {
386
- this.disconnectSocket();
387
- }
388
- }, delay);
389
- if (typeof this.disconnectTimer.unref === 'function') {
390
- this.disconnectTimer.unref();
391
- }
392
- }
393
- }
394
- //# sourceMappingURL=EventDispatcher.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"EventDispatcher.js","sourceRoot":"","sources":["../../../../../sdk-typescript/src/utils/EventDispatcher.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,EAAE,EAAU,MAAM,kBAAkB,CAAA;AAK7C;;;;;;GAMG;AACH,SAAS,kBAAkB,CAAC,IAAS;IACnC,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK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AAC;IACH,CAAC;IAEO,iBAAiB;QACvB,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACvB,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;YAChC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAA;QAC3B,CAAC;IACH,CAAC;IAEO,yBAAyB;QAC/B,IAAI,CAAC,uBAAuB,EAAE,CAAA;QAC9B,MAAM,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAA;QAE5C,iFAAiF;QACjF,8EAA8E;QAC9E,MAAM,KAAK,GAAG,IAAI,CAAC,uBAAuB,KAAK,IAAI,CAAC,CAAC,CAAC,eAAe,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAA;QAE7F,IAAI,CAAC,eAAe,GAAG,UAAU,CAAC,GAAG,EAAE;YACrC,IAAI,UAAU,KAAK,IAAI,CAAC,oBAAoB,EAAE,CAAC;gBAC7C,OAAM;YACR,CAAC;YAED,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBAC9B,IAAI,CAAC,gBAAgB,EAAE,CAAA;YACzB,CAAC;QACH,CAAC,EAAE,KAAK,CAAC,CAAA;QAET,IAAI,OAAO,IAAI,CAAC,eAAe,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;YACrD,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAA;QAC9B,CAAC;IACH,CAAC"}
@@ -1,14 +0,0 @@
1
- import { EventDispatcher, type EventHandler } from './EventDispatcher.js';
2
- export declare class EventSubscriptionManager {
3
- private readonly dispatcher;
4
- private static readonly SUBSCRIPTION_TTL;
5
- private readonly subscriptions;
6
- private _closed;
7
- constructor(dispatcher: EventDispatcher | null);
8
- subscribe(resourceId: string, handler: EventHandler, events: string[]): string;
9
- refresh(subId: string): boolean;
10
- unsubscribe(subId: string): void;
11
- shutdown(): void;
12
- private createTimer;
13
- }
14
- //# sourceMappingURL=EventSubscriptionManager.d.ts.map
@@ -1,94 +0,0 @@
1
- /*
2
- * Copyright Daytona Platforms Inc.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- function createSubscriptionId() {
6
- // Browser-safe UUID generation.
7
- if (typeof globalThis.crypto?.randomUUID === 'function') {
8
- return globalThis.crypto.randomUUID();
9
- }
10
- const randomHex = () => Math.floor(Math.random() * 0xffffffff)
11
- .toString(16)
12
- .padStart(8, '0');
13
- return `${randomHex()}${randomHex()}${randomHex()}${randomHex()}`;
14
- }
15
- export class EventSubscriptionManager {
16
- dispatcher;
17
- static SUBSCRIPTION_TTL = 300;
18
- subscriptions = new Map();
19
- _closed = false;
20
- constructor(dispatcher) {
21
- this.dispatcher = dispatcher;
22
- }
23
- subscribe(resourceId, handler, events) {
24
- // No-op when shut down or when streaming is disabled (no dispatcher) — callers poll.
25
- if (this._closed || !this.dispatcher) {
26
- return '';
27
- }
28
- const subId = createSubscriptionId();
29
- const unsubscribe = this.dispatcher.subscribe(resourceId, handler, events);
30
- try {
31
- if (this._closed) {
32
- throw new Error('EventSubscriptionManager is closed');
33
- }
34
- this.subscriptions.set(subId, {
35
- unsubscribe,
36
- timer: this.createTimer(subId),
37
- });
38
- return subId;
39
- }
40
- catch (error) {
41
- // Rollback dispatcher subscription on failure.
42
- unsubscribe();
43
- if (this._closed) {
44
- return '';
45
- }
46
- throw error;
47
- }
48
- }
49
- refresh(subId) {
50
- // Reject operations after shutdown to prevent use-after-close.
51
- if (this._closed) {
52
- return false;
53
- }
54
- const subscription = this.subscriptions.get(subId);
55
- if (!subscription) {
56
- return false;
57
- }
58
- clearTimeout(subscription.timer);
59
- subscription.timer = this.createTimer(subId);
60
- return true;
61
- }
62
- unsubscribe(subId) {
63
- const subscription = this.subscriptions.get(subId);
64
- if (!subscription) {
65
- return;
66
- }
67
- clearTimeout(subscription.timer);
68
- this.subscriptions.delete(subId);
69
- subscription.unsubscribe();
70
- }
71
- shutdown() {
72
- this._closed = true;
73
- for (const [subId, subscription] of this.subscriptions) {
74
- clearTimeout(subscription.timer);
75
- this.subscriptions.delete(subId);
76
- subscription.unsubscribe();
77
- }
78
- }
79
- createTimer(subId) {
80
- const timer = setTimeout(() => {
81
- const subscription = this.subscriptions.get(subId);
82
- if (!subscription || subscription.timer !== timer) {
83
- return;
84
- }
85
- this.subscriptions.delete(subId);
86
- subscription.unsubscribe();
87
- }, EventSubscriptionManager.SUBSCRIPTION_TTL * 1000);
88
- if (typeof timer.unref === 'function') {
89
- timer.unref();
90
- }
91
- return timer;
92
- }
93
- }
94
- //# sourceMappingURL=EventSubscriptionManager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"EventSubscriptionManager.js","sourceRoot":"","sources":["../../../../../sdk-typescript/src/utils/EventSubscriptionManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AASH,SAAS,oBAAoB;IAC3B,gCAAgC;IAChC,IAAI,OAAO,UAAU,CAAC,MAAM,EAAE,UAAU,KAAK,UAAU,EAAE,CAAC;QACxD,OAAO,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAA;IACvC,CAAC;IAED,MAAM,SAAS,GAAG,GAAG,EAAE,CACrB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,UAAU,CAAC;SACnC,QAAQ,CAAC,EAAE,CAAC;SACZ,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;IACrB,OAAO,GAAG,SAAS,EAAE,GAAG,SAAS,EAAE,GAAG,SAAS,EAAE,GAAG,SAAS,EAAE,EAAE,CAAA;AACnE,CAAC;AAED,MAAM,OAAO,wBAAwB;IAKN;IAJrB,MAAM,CAAU,gBAAgB,GAAG,GAAG,CAAA;IAC7B,aAAa,GAAG,IAAI,GAAG,EAA+B,CAAA;IAC/D,OAAO,GAAG,KAAK,CAAA;IAEvB,YAA6B,UAAkC;QAAlC,eAAU,GAAV,UAAU,CAAwB;IAAG,CAAC;IAEnE,SAAS,CAAC,UAAkB,EAAE,OAAqB,EAAE,MAAgB;QACnE,qFAAqF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