@cloudflare/sandbox 0.8.10 → 0.8.11

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.
@@ -0,0 +1,187 @@
1
+ import { DurableObject } from "cloudflare:workers";
2
+
3
+ //#region src/bridge/types.d.ts
4
+
5
+ /**
6
+ * Wire types and configuration types for the Cloudflare Sandbox Bridge.
7
+ *
8
+ * These types define the JSON payloads exchanged between HTTP clients
9
+ * (e.g. the Python `CloudflareSandboxClient`) and the bridge worker.
10
+ */
11
+ /**
12
+ * Configuration options for the bridge() factory.
13
+ */
14
+ interface BridgeConfig {
15
+ /**
16
+ * Override the default binding names used to look up Durable Objects.
17
+ *
18
+ * @default { sandbox: "Sandbox", warmPool: "WarmPool" }
19
+ */
20
+ bindings?: {
21
+ /** Name of the Sandbox Durable Object binding. @default "Sandbox" */
22
+ sandbox?: string;
23
+ /** Name of the WarmPool Durable Object binding. @default "WarmPool" */
24
+ warmPool?: string;
25
+ };
26
+ /**
27
+ * URL prefix for all bridge API routes.
28
+ *
29
+ * @default "/v1"
30
+ */
31
+ apiRoutePrefix?: string;
32
+ /**
33
+ * Path for the health-check endpoint.
34
+ *
35
+ * @default "/health"
36
+ */
37
+ healthRoute?: string;
38
+ }
39
+ /**
40
+ * The user-provided worker handlers that bridge() wraps.
41
+ *
42
+ * The bridge wraps `fetch` and `scheduled` with its own logic;
43
+ * all other properties are passed through unchanged.
44
+ */
45
+ interface WorkerHandlers {
46
+ fetch?(request: Request, env: any, ctx: ExecutionContext): Response | Promise<Response>;
47
+ scheduled?(controller: ScheduledController, env: any, ctx: ExecutionContext): void | Promise<void>;
48
+ [key: string]: unknown;
49
+ }
50
+ /**
51
+ * Minimum environment shape required by the bridge.
52
+ * The actual bindings are looked up dynamically by name.
53
+ */
54
+ interface BridgeEnv {
55
+ SANDBOX_API_KEY?: string;
56
+ WARM_POOL_TARGET?: string;
57
+ WARM_POOL_REFRESH_INTERVAL?: string;
58
+ [key: string]: unknown;
59
+ }
60
+ //#endregion
61
+ //#region src/bridge/helpers.d.ts
62
+ /**
63
+ * Shell-quote a single argv token so it is safe to embed in a sh command
64
+ * string. Tokens that contain only safe characters are returned unchanged
65
+ * for readability. All others are wrapped in ANSI-C $'...' quoting which
66
+ * can represent newlines, tabs, and other control characters as escape
67
+ * sequences — unlike plain single quotes which pass content literally and
68
+ * break when the value contains a real newline.
69
+ */
70
+ declare function shellQuote(arg: string): string;
71
+ /**
72
+ * POSIX-normalise a path (resolve `.` / `..` segments) and verify it lives
73
+ * under /workspace. Returns the resolved absolute path on success, or null
74
+ * if the path escapes the workspace.
75
+ */
76
+ declare function resolveWorkspacePath(userPath: string): string | null;
77
+ //#endregion
78
+ //#region src/bridge/warm-pool.d.ts
79
+ interface WarmPoolConfig {
80
+ /** Target number of warm (unassigned) containers to maintain. @default 0 */
81
+ warmTarget?: number;
82
+ /** How often to check and replenish warm containers (ms). @default 10000 */
83
+ refreshInterval?: number;
84
+ }
85
+ interface PoolStats {
86
+ /** Number of warm (unassigned) containers ready for use */
87
+ warm: number;
88
+ /** Number of containers assigned to sandbox IDs */
89
+ assigned: number;
90
+ /** Total containers tracked by the pool */
91
+ total: number;
92
+ /** Current pool configuration */
93
+ config: Required<WarmPoolConfig>;
94
+ /** Inferred max_instances limit, or null if not yet known */
95
+ maxInstances: number | null;
96
+ }
97
+ /**
98
+ * The WarmPool expects an environment with a `Sandbox` Durable Object binding.
99
+ * This interface describes the minimum shape; the actual binding name is
100
+ * configurable via the bridge, but defaults to "Sandbox".
101
+ */
102
+ interface WarmPoolEnv {
103
+ Sandbox: DurableObjectNamespace;
104
+ [key: string]: unknown;
105
+ }
106
+ declare class WarmPool extends DurableObject<WarmPoolEnv> {
107
+ private config;
108
+ /** Container UUIDs that are warm and available for assignment */
109
+ private warmContainers;
110
+ /** Maps caller-provided sandbox IDs to container UUIDs (1:1, no sharing) */
111
+ private assignments;
112
+ /** Containers currently starting — excluded from health checks */
113
+ private startingContainers;
114
+ /** Inferred max_instances limit learned from Cloudflare errors, or null */
115
+ private knownMaxInstances;
116
+ private capacityExhausted;
117
+ private initialized;
118
+ /**
119
+ * Get a container UUID for the given sandbox ID.
120
+ * - If this ID already has an assigned container that's still running, return it.
121
+ * - Otherwise assign a warm container (or start a new one).
122
+ */
123
+ getContainer(sandboxId: string): Promise<string>;
124
+ /**
125
+ * Look up an existing container assignment without allocating.
126
+ * Returns the container UUID if the sandbox ID has an active assignment, null otherwise.
127
+ * Used by DELETE to avoid starting a container just to destroy it.
128
+ */
129
+ lookupContainer(sandboxId: string): Promise<string | null>;
130
+ /**
131
+ * Report that a container has stopped — removes it from tracking.
132
+ */
133
+ reportStopped(containerUUID: string): Promise<void>;
134
+ /**
135
+ * Get current pool statistics.
136
+ */
137
+ getStats(): Promise<PoolStats>;
138
+ /**
139
+ * Update pool configuration. Idempotent — called on every request to keep
140
+ * config in sync with wrangler vars across deploys.
141
+ */
142
+ configure(config: WarmPoolConfig): Promise<void>;
143
+ /**
144
+ * Shutdown all pre-warmed (unassigned) containers.
145
+ * Does not affect containers that are assigned to sandbox IDs.
146
+ */
147
+ shutdownPrewarmed(): Promise<void>;
148
+ alarm(): Promise<void>;
149
+ private init;
150
+ private persist;
151
+ private scheduleRefresh;
152
+ private startContainer;
153
+ private isContainerRunning;
154
+ private checkContainerHealth;
155
+ /**
156
+ * Renew activity timeout on all warm containers to prevent them from sleeping.
157
+ */
158
+ private keepWarmContainersAlive;
159
+ /**
160
+ * Scale the warm pool towards warmTarget, respecting inferred max_instances.
161
+ */
162
+ private adjustPool;
163
+ private removeContainer;
164
+ private remainingCapacity;
165
+ private isMaxInstancesError;
166
+ private recordCapacityLimit;
167
+ private throwCapacityError;
168
+ private getSandboxStub;
169
+ }
170
+ //#endregion
171
+ //#region src/bridge/index.d.ts
172
+ /**
173
+ * Create a Worker export that wraps user handlers with bridge functionality.
174
+ *
175
+ * The bridge:
176
+ * 1. Checks that required Durable Object bindings exist (logs errors if missing).
177
+ * 2. Wraps `fetch()` to serve bridge API routes first, then falls through to the user handler.
178
+ * 3. Wraps `scheduled()` to prime the warm pool, then calls the user handler.
179
+ * 4. Passes through all other properties unchanged.
180
+ *
181
+ * @param worker - The user's worker handlers (fetch, scheduled, and any others).
182
+ * @param config - Optional configuration for binding names and route paths.
183
+ */
184
+ declare function bridge(worker: WorkerHandlers, config?: BridgeConfig): ExportedHandler<BridgeEnv>;
185
+ //#endregion
186
+ export { type BridgeConfig, type BridgeEnv, type PoolStats, WarmPool, type WarmPoolConfig, type WorkerHandlers, bridge, resolveWorkspacePath, shellQuote };
187
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/bridge/types.ts","../../src/bridge/helpers.ts","../../src/bridge/warm-pool.ts","../../src/bridge/index.ts"],"sourcesContent":[],"mappings":";;;;;;;AAgBA;AAgCA;;;;;AAKgB,UArCC,YAAA,CAqCD;EAEA;;;;AAehB;;;;ICEgB;IAyBA,QAAA,CAAA,EAAA,MAAA;;;;AC7EhB;AAOA;AAWC;EA6CY,cAAS,CAAA,EAAA,MAAA;EAAsB;;;;;EA6FxB,WAAA,CAAA,EAAA,MAAA;;;;;;;;UFhIH,cAAA;kBAEJ,wBAEJ,mBACJ,WAAW,QAAQ;EGiCR,SAAM,EAAA,UAAA,EH/BN,mBG+BM,EAAA,GAAA,EAAA,GAAA,EAAA,GAAA,EH7Bb,gBG6Ba,CAAA,EAAA,IAAA,GH5BV,OG4BU,CAAA,IAAA,CAAA;EACZ,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,OAAA;;;;;;UHjBO,SAAA;;;;;;;;;;;AAAjB;;;;ACEA;AAyBgB,iBAzBA,UAAA,CAyBoB,GAAA,EAAA,MAAA,CAAA,EAAA,MAAA;;;;AC7EpC;AAOA;AAmDU,iBDmBM,oBAAA,CClBL,QAAsB,EAAA,MAAA,CAAA,EAAA,MAAA,GAAA,IAAA;;;AFThB,UElDA,cAAA,C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