@cloudflare/sandbox 0.8.1 → 0.8.3

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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { $ as DirectoryBackup, A as DesktopStopResponse, At as StreamOptions, B as ExecuteResponse, Bt as PtyOptions, C as ClickOptions, Ct as ProcessOptions, D as DesktopStartOptions, Dt as RestoreBackupResult, E as DesktopClient, Et as RemoteMountBucketOptions, F as ScreenshotRegion, Ft as isProcess, G as HttpClientOptions, Gt as RunCodeOptions, H as BaseApiResponse, Ht as CreateContextOptions, I as ScreenshotResponse, It as isProcessStatus, J as SessionRequest, K as RequestConfig, L as ScrollDirection, Lt as ExecuteRequest, M as ScreenSizeResponse, Mt as WaitForPortOptions, N as ScreenshotBytesResponse, Nt as WatchOptions, O as DesktopStartResponse, Ot as SandboxOptions, P as ScreenshotOptions, Pt as isExecResult, Q as BucketProvider, R as TypeOptions, Rt as ExposePortRequest, S as WriteFileRequest, St as ProcessLogsResult, T as Desktop, Tt as ProcessStatus, U as ContainerStub, Ut as Execution, V as BackupClient, Vt as CodeContext, W as ErrorResponse, Wt as ExecutionResult, X as BaseExecOptions, Y as BackupOptions, Z as BucketCredentials, _ as GitClient, _t as Process, a as CreateSessionRequest, at as FileMetadata, b as MkdirRequest, bt as ProcessKillResult, c as DeleteSessionResponse, ct as GitCheckoutResult, d as ProcessClient, dt as LocalMountBucketOptions, et as ExecEvent, f as PortClient, ft as LogEvent, g as GitCheckoutRequest, gt as PortListResult, h as InterpreterClient, ht as PortExposeResult, i as CommandsResponse, it as FileChunk, j as KeyInput, jt as WaitForLogResult, k as DesktopStatusResponse, kt as SessionOptions, l as PingResponse, lt as ISandbox, m as ExecutionCallbacks, mt as PortCloseResult, n as getSandbox, nt as ExecResult, o as CreateSessionResponse, ot as FileStreamEvent, p as UnexposePortRequest, pt as MountBucketOptions, q as ResponseHandler, r as SandboxClient, rt as ExecutionSession, s as DeleteSessionRequest, st as FileWatchSSEEvent, t as Sandbox, tt as ExecOptions, u as UtilityClient, ut as ListFilesOptions, v as FileClient, vt as ProcessCleanupResult, w as CursorPositionResponse, wt as ProcessStartResult, x as ReadFileRequest, xt as ProcessListResult, y as FileOperationRequest, yt as ProcessInfoResult, z as CommandClient, zt as StartProcessRequest } from "./sandbox-DDDSuWA6.js";
1
+ import { $ as CheckChangesOptions, A as DesktopStopResponse, At as SandboxOptions, B as ExecuteResponse, Bt as ExposePortRequest, C as ClickOptions, Ct as ProcessListResult, D as DesktopStartOptions, Dt as ProcessStatus, E as DesktopClient, Et as ProcessStartResult, F as ScreenshotRegion, Ft as WatchOptions, G as HttpClientOptions, Gt as Execution, H as BaseApiResponse, Ht as PtyOptions, I as ScreenshotResponse, It as isExecResult, J as SessionRequest, K as RequestConfig, Kt as ExecutionResult, L as ScrollDirection, Lt as isProcess, M as ScreenSizeResponse, Mt as StreamOptions, N as ScreenshotBytesResponse, Nt as WaitForLogResult, O as DesktopStartResponse, Ot as RemoteMountBucketOptions, P as ScreenshotOptions, Pt as WaitForPortOptions, Q as BucketProvider, R as TypeOptions, Rt as isProcessStatus, S as WriteFileRequest, St as ProcessKillResult, T as Desktop, Tt as ProcessOptions, U as ContainerStub, Ut as CodeContext, V as BackupClient, Vt as StartProcessRequest, W as ErrorResponse, Wt as CreateContextOptions, X as BaseExecOptions, Y as BackupOptions, Z as BucketCredentials, _ as GitClient, _t as PortExposeResult, a as CreateSessionRequest, at as ExecutionSession, b as MkdirRequest, bt as ProcessCleanupResult, c as DeleteSessionResponse, ct as FileStreamEvent, d as ProcessClient, dt as ISandbox, et as CheckChangesResult, f as PortClient, ft as ListFilesOptions, g as GitCheckoutRequest, gt as PortCloseResult, h as InterpreterClient, ht as MountBucketOptions, i as CommandsResponse, it as ExecResult, j as KeyInput, jt as SessionOptions, k as DesktopStatusResponse, kt as RestoreBackupResult, l as PingResponse, lt as FileWatchSSEEvent, m as ExecutionCallbacks, mt as LogEvent, n as getSandbox, nt as ExecEvent, o as CreateSessionResponse, ot as FileChunk, p as UnexposePortRequest, pt as LocalMountBucketOptions, q as ResponseHandler, qt as RunCodeOptions, r as SandboxClient, rt as ExecOptions, s as DeleteSessionRequest, st as FileMetadata, t as Sandbox, tt as DirectoryBackup, u as UtilityClient, ut as GitCheckoutResult, v as FileClient, vt as PortListResult, w as CursorPositionResponse, wt as ProcessLogsResult, x as ReadFileRequest, xt as ProcessInfoResult, y as FileOperationRequest, yt as Process, z as CommandClient, zt as ExecuteRequest } from "./sandbox-BoLbdjOe.js";
2
2
  import { a as DesktopCoordinateErrorContext, d as ErrorResponse$1, f as OperationType, i as BackupRestoreContext, l as ProcessExitedBeforeReadyContext, n as BackupExpiredContext, o as DesktopErrorContext, p as ErrorCode, r as BackupNotFoundContext, s as InvalidBackupConfigContext, t as BackupCreateContext, u as ProcessReadyTimeoutContext } from "./contexts-CeQR115r.js";
3
3
  import { ContainerProxy } from "@cloudflare/containers";
4
4
 
@@ -249,5 +249,5 @@ declare class InvalidMountConfigError extends BucketMountError {
249
249
  constructor(message: string);
250
250
  }
251
251
  //#endregion
252
- export { BackupClient, BackupCreateError, BackupExpiredError, BackupNotFoundError, type BackupOptions, BackupRestoreError, type BaseApiResponse, type BaseExecOptions, type BucketCredentials, BucketMountError, type BucketProvider, type ClickOptions, type CodeContext, CodeInterpreter, CommandClient, type ExecuteResponse as CommandExecuteResponse, type CommandsResponse, ContainerProxy, type ContainerStub, type CreateContextOptions, type CreateSessionRequest, type CreateSessionResponse, type CursorPositionResponse, type DeleteSessionRequest, type DeleteSessionResponse, type Desktop, DesktopClient, DesktopInvalidCoordinatesError, DesktopInvalidOptionsError, DesktopNotStartedError, DesktopProcessCrashedError, DesktopStartFailedError, type DesktopStartOptions, type DesktopStartResponse, type DesktopStatusResponse, type DesktopStopResponse, DesktopUnavailableError, type DirectoryBackup, type ErrorResponse, type ExecEvent, type ExecOptions, type ExecResult, type ExecuteRequest, type ExecutionCallbacks, type ExecutionResult, type ExecutionSession, type ExposePortRequest, type FileChunk, FileClient, type FileMetadata, type FileOperationRequest, type FileStreamEvent, type FileWatchSSEEvent, type GitCheckoutRequest, type GitCheckoutResult, GitClient, type ISandbox, type InterpreterClient, InvalidBackupConfigError, InvalidMountConfigError, type KeyInput, type ListFilesOptions, type LocalMountBucketOptions, type LogEvent, MissingCredentialsError, type MkdirRequest, type MountBucketOptions, type PingResponse, PortClient, type PortCloseResult, type PortExposeResult, type PortListResult, type Process, type ProcessCleanupResult, ProcessClient, ProcessExitedBeforeReadyError, type ProcessInfoResult, type ProcessKillResult, type ProcessListResult, type ProcessLogsResult, type ProcessOptions, ProcessReadyTimeoutError, type ProcessStartResult, type ProcessStatus, type PtyOptions, type ReadFileRequest, type RemoteMountBucketOptions, type RequestConfig, type ResponseHandler, type RestoreBackupResult, type RouteInfo, type RunCodeOptions, S3FSMountError, Sandbox, SandboxClient, type HttpClientOptions as SandboxClientOptions, type SandboxEnv, type SandboxOptions, type ScreenSizeResponse, type ScreenshotBytesResponse, type ScreenshotOptions, type ScreenshotRegion, type ScreenshotResponse, type ScrollDirection, type SessionOptions, type SessionRequest, type StartProcessRequest, type StreamOptions, type TypeOptions, type UnexposePortRequest, UtilityClient, type WaitForLogResult, type WaitForPortOptions, type WatchOptions, type WriteFileRequest, asyncIterableToSSEStream, collectFile, getSandbox, isExecResult, isProcess, isProcessStatus, parseSSEStream, proxyTerminal, proxyToSandbox, responseToAsyncIterable, streamFile };
252
+ export { BackupClient, BackupCreateError, BackupExpiredError, BackupNotFoundError, type BackupOptions, BackupRestoreError, type BaseApiResponse, type BaseExecOptions, type BucketCredentials, BucketMountError, type BucketProvider, type CheckChangesOptions, type CheckChangesResult, type ClickOptions, type CodeContext, CodeInterpreter, CommandClient, type ExecuteResponse as CommandExecuteResponse, type CommandsResponse, ContainerProxy, type ContainerStub, type CreateContextOptions, type CreateSessionRequest, type CreateSessionResponse, type CursorPositionResponse, type DeleteSessionRequest, type DeleteSessionResponse, type Desktop, DesktopClient, DesktopInvalidCoordinatesError, DesktopInvalidOptionsError, DesktopNotStartedError, DesktopProcessCrashedError, DesktopStartFailedError, type DesktopStartOptions, type DesktopStartResponse, type DesktopStatusResponse, type DesktopStopResponse, DesktopUnavailableError, type DirectoryBackup, type ErrorResponse, type ExecEvent, type ExecOptions, type ExecResult, type ExecuteRequest, type ExecutionCallbacks, type ExecutionResult, type ExecutionSession, type ExposePortRequest, type FileChunk, FileClient, type FileMetadata, type FileOperationRequest, type FileStreamEvent, type FileWatchSSEEvent, type GitCheckoutRequest, type GitCheckoutResult, GitClient, type ISandbox, type InterpreterClient, InvalidBackupConfigError, InvalidMountConfigError, type KeyInput, type ListFilesOptions, type LocalMountBucketOptions, type LogEvent, MissingCredentialsError, type MkdirRequest, type MountBucketOptions, type PingResponse, PortClient, type PortCloseResult, type PortExposeResult, type PortListResult, type Process, type ProcessCleanupResult, ProcessClient, ProcessExitedBeforeReadyError, type ProcessInfoResult, type ProcessKillResult, type ProcessListResult, type ProcessLogsResult, type ProcessOptions, ProcessReadyTimeoutError, type ProcessStartResult, type ProcessStatus, type PtyOptions, type ReadFileRequest, type RemoteMountBucketOptions, type RequestConfig, type ResponseHandler, type RestoreBackupResult, type RouteInfo, type RunCodeOptions, S3FSMountError, Sandbox, SandboxClient, type HttpClientOptions as SandboxClientOptions, type SandboxEnv, type SandboxOptions, type ScreenSizeResponse, type ScreenshotBytesResponse, type ScreenshotOptions, type ScreenshotRegion, type ScreenshotResponse, type ScrollDirection, type SessionOptions, type SessionRequest, type StartProcessRequest, type StreamOptions, type TypeOptions, type UnexposePortRequest, UtilityClient, type WaitForLogResult, type WaitForPortOptions, type WatchOptions, type WriteFileRequest, asyncIterableToSSEStream, collectFile, getSandbox, isExecResult, isProcess, isProcessStatus, parseSSEStream, proxyTerminal, proxyToSandbox, responseToAsyncIterable, streamFile };
253
253
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -844,6 +844,7 @@ var HttpTransport = class extends BaseTransport {
844
844
  const DEFAULT_REQUEST_TIMEOUT_MS = 12e4;
845
845
  const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 3e5;
846
846
  const DEFAULT_CONNECT_TIMEOUT_MS = 3e4;
847
+ const DEFAULT_IDLE_DISCONNECT_MS = 1e3;
847
848
  const MIN_TIME_FOR_CONNECT_RETRY_MS = 15e3;
848
849
  /**
849
850
  * WebSocket transport implementation
@@ -856,6 +857,7 @@ var WebSocketTransport = class extends BaseTransport {
856
857
  state = "disconnected";
857
858
  pendingRequests = /* @__PURE__ */ new Map();
858
859
  connectPromise = null;
860
+ idleDisconnectTimer = null;
859
861
  boundHandleMessage;
860
862
  boundHandleClose;
861
863
  constructor(config) {
@@ -880,6 +882,7 @@ var WebSocketTransport = class extends BaseTransport {
880
882
  * callers share the same connection attempt.
881
883
  */
882
884
  async connect() {
885
+ this.clearIdleDisconnectTimer();
883
886
  if (this.isConnected()) return;
884
887
  if (this.connectPromise) return this.connectPromise;
885
888
  this.connectPromise = this.doConnect();
@@ -1055,6 +1058,7 @@ var WebSocketTransport = class extends BaseTransport {
1055
1058
  */
1056
1059
  async request(method, path$1, body, headers) {
1057
1060
  await this.connect();
1061
+ this.clearIdleDisconnectTimer();
1058
1062
  const id = generateRequestId();
1059
1063
  const request = {
1060
1064
  type: "request",
@@ -1068,12 +1072,14 @@ var WebSocketTransport = class extends BaseTransport {
1068
1072
  const timeoutMs = this.config.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
1069
1073
  const timeoutId = setTimeout(() => {
1070
1074
  this.pendingRequests.delete(id);
1075
+ this.scheduleIdleDisconnect();
1071
1076
  reject(/* @__PURE__ */ new Error(`Request timeout after ${timeoutMs}ms: ${method} ${path$1}`));
1072
1077
  }, timeoutMs);
1073
1078
  this.pendingRequests.set(id, {
1074
1079
  resolve: (response) => {
1075
1080
  clearTimeout(timeoutId);
1076
1081
  this.pendingRequests.delete(id);
1082
+ this.scheduleIdleDisconnect();
1077
1083
  resolve({
1078
1084
  status: response.status,
1079
1085
  body: response.body
@@ -1082,6 +1088,7 @@ var WebSocketTransport = class extends BaseTransport {
1082
1088
  reject: (error) => {
1083
1089
  clearTimeout(timeoutId);
1084
1090
  this.pendingRequests.delete(id);
1091
+ this.scheduleIdleDisconnect();
1085
1092
  reject(error);
1086
1093
  },
1087
1094
  isStreaming: false,
@@ -1092,6 +1099,7 @@ var WebSocketTransport = class extends BaseTransport {
1092
1099
  } catch (error) {
1093
1100
  clearTimeout(timeoutId);
1094
1101
  this.pendingRequests.delete(id);
1102
+ this.scheduleIdleDisconnect();
1095
1103
  reject(error instanceof Error ? error : new Error(String(error)));
1096
1104
  }
1097
1105
  });
@@ -1112,6 +1120,7 @@ var WebSocketTransport = class extends BaseTransport {
1112
1120
  */
1113
1121
  async requestStream(method, path$1, body, headers) {
1114
1122
  await this.connect();
1123
+ this.clearIdleDisconnectTimer();
1115
1124
  const id = generateRequestId();
1116
1125
  const request = {
1117
1126
  type: "request",
@@ -1128,6 +1137,7 @@ var WebSocketTransport = class extends BaseTransport {
1128
1137
  const createIdleTimeout = () => {
1129
1138
  return setTimeout(() => {
1130
1139
  this.pendingRequests.delete(id);
1140
+ this.scheduleIdleDisconnect();
1131
1141
  const error = /* @__PURE__ */ new Error(`Stream idle timeout after ${idleTimeoutMs}ms: ${method} ${path$1}`);
1132
1142
  if (firstMessageReceived) try {
1133
1143
  streamController?.error(error);
@@ -1155,6 +1165,7 @@ var WebSocketTransport = class extends BaseTransport {
1155
1165
  });
1156
1166
  }
1157
1167
  this.pendingRequests.delete(id);
1168
+ this.scheduleIdleDisconnect();
1158
1169
  }
1159
1170
  });
1160
1171
  this.pendingRequests.set(id, {
@@ -1162,6 +1173,7 @@ var WebSocketTransport = class extends BaseTransport {
1162
1173
  const pending = this.pendingRequests.get(id);
1163
1174
  if (pending?.timeoutId) clearTimeout(pending.timeoutId);
1164
1175
  this.pendingRequests.delete(id);
1176
+ this.scheduleIdleDisconnect();
1165
1177
  if (!firstMessageReceived) {
1166
1178
  firstMessageReceived = true;
1167
1179
  if (response.status >= 400) rejectStream(/* @__PURE__ */ new Error(`Stream error: ${response.status} - ${JSON.stringify(response.body)}`));
@@ -1178,6 +1190,7 @@ var WebSocketTransport = class extends BaseTransport {
1178
1190
  const pending = this.pendingRequests.get(id);
1179
1191
  if (pending?.timeoutId) clearTimeout(pending.timeoutId);
1180
1192
  this.pendingRequests.delete(id);
1193
+ this.scheduleIdleDisconnect();
1181
1194
  if (firstMessageReceived) try {
1182
1195
  streamController?.error(error);
1183
1196
  } catch {}
@@ -1202,6 +1215,7 @@ var WebSocketTransport = class extends BaseTransport {
1202
1215
  });
1203
1216
  if (pending.timeoutId) clearTimeout(pending.timeoutId);
1204
1217
  this.pendingRequests.delete(id);
1218
+ this.scheduleIdleDisconnect();
1205
1219
  }
1206
1220
  pending.bufferedChunks = void 0;
1207
1221
  }
@@ -1215,6 +1229,7 @@ var WebSocketTransport = class extends BaseTransport {
1215
1229
  } catch (error) {
1216
1230
  clearTimeout(timeoutId);
1217
1231
  this.pendingRequests.delete(id);
1232
+ this.scheduleIdleDisconnect();
1218
1233
  rejectStream(error instanceof Error ? error : new Error(String(error)));
1219
1234
  }
1220
1235
  });
@@ -1301,6 +1316,7 @@ var WebSocketTransport = class extends BaseTransport {
1301
1316
  });
1302
1317
  if (pending.timeoutId) clearTimeout(pending.timeoutId);
1303
1318
  this.pendingRequests.delete(chunk.id);
1319
+ this.scheduleIdleDisconnect();
1304
1320
  }
1305
1321
  }
1306
1322
  /**
@@ -1312,6 +1328,7 @@ var WebSocketTransport = class extends BaseTransport {
1312
1328
  const idleTimeoutMs = this.config.streamIdleTimeoutMs ?? DEFAULT_STREAM_IDLE_TIMEOUT_MS;
1313
1329
  pending.timeoutId = setTimeout(() => {
1314
1330
  this.pendingRequests.delete(id);
1331
+ this.scheduleIdleDisconnect();
1315
1332
  if (pending.streamController) try {
1316
1333
  pending.streamController.error(/* @__PURE__ */ new Error(`Stream idle timeout after ${idleTimeoutMs}ms`));
1317
1334
  } catch {}
@@ -1354,6 +1371,7 @@ var WebSocketTransport = class extends BaseTransport {
1354
1371
  * Cleanup resources
1355
1372
  */
1356
1373
  cleanup() {
1374
+ this.clearIdleDisconnectTimer();
1357
1375
  if (this.ws) {
1358
1376
  this.ws.removeEventListener("close", this.boundHandleClose);
1359
1377
  this.ws.removeEventListener("message", this.boundHandleMessage);
@@ -1365,6 +1383,23 @@ var WebSocketTransport = class extends BaseTransport {
1365
1383
  for (const pending of this.pendingRequests.values()) if (pending.timeoutId) clearTimeout(pending.timeoutId);
1366
1384
  this.pendingRequests.clear();
1367
1385
  }
1386
+ scheduleIdleDisconnect() {
1387
+ if (!this.isConnected() || this.pendingRequests.size > 0) return;
1388
+ this.clearIdleDisconnectTimer();
1389
+ this.idleDisconnectTimer = setTimeout(() => {
1390
+ this.idleDisconnectTimer = null;
1391
+ if (this.pendingRequests.size === 0 && this.isConnected()) {
1392
+ this.logger.debug("Disconnecting idle WebSocket transport");
1393
+ this.cleanup();
1394
+ }
1395
+ }, DEFAULT_IDLE_DISCONNECT_MS);
1396
+ }
1397
+ clearIdleDisconnectTimer() {
1398
+ if (this.idleDisconnectTimer) {
1399
+ clearTimeout(this.idleDisconnectTimer);
1400
+ this.idleDisconnectTimer = null;
1401
+ }
1402
+ }
1368
1403
  };
1369
1404
 
1370
1405
  //#endregion
@@ -2581,9 +2616,15 @@ var UtilityClient = class extends BaseHttpClient {
2581
2616
  * Uses inotify under the hood for native filesystem event notifications
2582
2617
  *
2583
2618
  * @internal This client is used internally by the SDK.
2584
- * Users should use `sandbox.watch()` instead.
2619
+ * Users should use `sandbox.watch()` or `sandbox.checkChanges()` instead.
2585
2620
  */
2586
2621
  var WatchClient = class extends BaseHttpClient {
2622
+ /**
2623
+ * Check whether a path changed since a previously returned version.
2624
+ */
2625
+ async checkChanges(request) {
2626
+ return this.post("/api/watch/check", request);
2627
+ }
2587
2628
  /**
2588
2629
  * Start watching a directory for changes.
2589
2630
  * The returned promise resolves only after the watcher is established
@@ -3591,7 +3632,7 @@ function buildS3fsSource(bucket, prefix) {
3591
3632
  * This file is auto-updated by .github/changeset-version.ts during releases
3592
3633
  * DO NOT EDIT MANUALLY - Changes will be overwritten on the next version bump
3593
3634
  */
3594
- const SDK_VERSION = "0.8.1";
3635
+ const SDK_VERSION = "0.8.3";
3595
3636
 
3596
3637
  //#endregion
3597
3638
  //#region src/sandbox.ts
@@ -5174,6 +5215,27 @@ var Sandbox = class Sandbox extends Container {
5174
5215
  });
5175
5216
  }
5176
5217
  /**
5218
+ * Check whether a path changed while this caller was disconnected.
5219
+ *
5220
+ * Pass the `version` returned from a prior call in `options.since` to learn
5221
+ * whether the path is unchanged, changed, or needs a full resync because the
5222
+ * retained change state was reset.
5223
+ *
5224
+ * @param path - Path to check (absolute or relative to /workspace)
5225
+ * @param options - Change-check options
5226
+ */
5227
+ async checkChanges(path$1, options = {}) {
5228
+ const sessionId = options.sessionId ?? await this.ensureDefaultSession();
5229
+ return this.client.watch.checkChanges({
5230
+ path: path$1,
5231
+ recursive: options.recursive,
5232
+ include: options.include,
5233
+ exclude: options.exclude,
5234
+ since: options.since,
5235
+ sessionId
5236
+ });
5237
+ }
5238
+ /**
5177
5239
  * Expose a port and get a preview URL for accessing services running in the sandbox
5178
5240
  *
5179
5241
  * @param port - Port number to expose (1024-65535)
@@ -5425,6 +5487,10 @@ var Sandbox = class Sandbox extends Container {
5425
5487
  ...options,
5426
5488
  sessionId
5427
5489
  }),
5490
+ checkChanges: (path$1, options) => this.checkChanges(path$1, {
5491
+ ...options,
5492
+ sessionId
5493
+ }),
5428
5494
  mkdir: (path$1, options) => this.mkdir(path$1, {
5429
5495
  ...options,
5430
5496
  sessionId