@cloudflare/sandbox 0.0.0-eb0ea62 → 0.0.0-eca93b9
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/CHANGELOG.md +167 -0
- package/Dockerfile +99 -11
- package/README.md +805 -24
- package/container_src/bun.lock +122 -0
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/control-process.ts +784 -0
- package/container_src/handler/exec.ts +185 -0
- package/container_src/handler/file.ts +406 -0
- package/container_src/handler/git.ts +130 -0
- package/container_src/handler/ports.ts +314 -0
- package/container_src/handler/process.ts +568 -0
- package/container_src/handler/session.ts +92 -0
- package/container_src/index.ts +441 -2740
- package/container_src/isolation.ts +1039 -0
- package/container_src/jupyter-server.ts +579 -0
- package/container_src/jupyter-service.ts +461 -0
- package/container_src/jupyter_config.py +48 -0
- package/container_src/mime-processor.ts +255 -0
- package/container_src/package.json +9 -0
- package/container_src/shell-escape.ts +42 -0
- package/container_src/startup.sh +84 -0
- package/container_src/types.ts +131 -0
- package/package.json +6 -8
- package/src/client.ts +442 -1362
- package/src/errors.ts +218 -0
- package/src/index.ts +63 -128
- package/src/interpreter-types.ts +383 -0
- package/src/interpreter.ts +150 -0
- package/src/jupyter-client.ts +349 -0
- package/src/request-handler.ts +144 -0
- package/src/sandbox.ts +747 -0
- package/src/security.ts +113 -0
- package/src/sse-parser.ts +147 -0
- package/src/types.ts +502 -0
- package/tsconfig.json +1 -1
- package/tests/client.example.ts +0 -308
- package/tests/connection-test.ts +0 -81
- package/tests/simple-test.ts +0 -81
- package/tests/test1.ts +0 -281
- package/tests/test2.ts +0 -929
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { HttpClient } from "./client.js";
|
|
2
|
+
import { isRetryableError, parseErrorResponse } from "./errors.js";
|
|
3
|
+
import type {
|
|
4
|
+
CodeContext,
|
|
5
|
+
CreateContextOptions,
|
|
6
|
+
ExecutionError,
|
|
7
|
+
OutputMessage,
|
|
8
|
+
Result,
|
|
9
|
+
} from "./interpreter-types.js";
|
|
10
|
+
|
|
11
|
+
// API Response types
|
|
12
|
+
interface ContextResponse {
|
|
13
|
+
id: string;
|
|
14
|
+
language: string;
|
|
15
|
+
cwd: string;
|
|
16
|
+
createdAt: string; // ISO date string from JSON
|
|
17
|
+
lastUsed: string; // ISO date string from JSON
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ContextListResponse {
|
|
21
|
+
contexts: ContextResponse[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Streaming execution data from the server
|
|
25
|
+
interface StreamingExecutionData {
|
|
26
|
+
type: "result" | "stdout" | "stderr" | "error" | "execution_complete";
|
|
27
|
+
text?: string;
|
|
28
|
+
html?: string;
|
|
29
|
+
png?: string; // base64
|
|
30
|
+
jpeg?: string; // base64
|
|
31
|
+
svg?: string;
|
|
32
|
+
latex?: string;
|
|
33
|
+
markdown?: string;
|
|
34
|
+
javascript?: string;
|
|
35
|
+
json?: unknown;
|
|
36
|
+
chart?: {
|
|
37
|
+
type:
|
|
38
|
+
| "line"
|
|
39
|
+
| "bar"
|
|
40
|
+
| "scatter"
|
|
41
|
+
| "pie"
|
|
42
|
+
| "histogram"
|
|
43
|
+
| "heatmap"
|
|
44
|
+
| "unknown";
|
|
45
|
+
data: unknown;
|
|
46
|
+
options?: unknown;
|
|
47
|
+
};
|
|
48
|
+
data?: unknown;
|
|
49
|
+
metadata?: Record<string, unknown>;
|
|
50
|
+
execution_count?: number;
|
|
51
|
+
ename?: string;
|
|
52
|
+
evalue?: string;
|
|
53
|
+
traceback?: string[];
|
|
54
|
+
lineNumber?: number;
|
|
55
|
+
timestamp?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface ExecutionCallbacks {
|
|
59
|
+
onStdout?: (output: OutputMessage) => void | Promise<void>;
|
|
60
|
+
onStderr?: (output: OutputMessage) => void | Promise<void>;
|
|
61
|
+
onResult?: (result: Result) => void | Promise<void>;
|
|
62
|
+
onError?: (error: ExecutionError) => void | Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class JupyterClient extends HttpClient {
|
|
66
|
+
private readonly maxRetries = 3;
|
|
67
|
+
private readonly retryDelayMs = 1000;
|
|
68
|
+
|
|
69
|
+
async createCodeContext(
|
|
70
|
+
options: CreateContextOptions = {}
|
|
71
|
+
): Promise<CodeContext> {
|
|
72
|
+
return this.executeWithRetry(async () => {
|
|
73
|
+
const response = await this.doFetch("/api/contexts", {
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: { "Content-Type": "application/json" },
|
|
76
|
+
body: JSON.stringify({
|
|
77
|
+
language: options.language || "python",
|
|
78
|
+
cwd: options.cwd || "/workspace",
|
|
79
|
+
env_vars: options.envVars,
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (!response.ok) {
|
|
84
|
+
throw await parseErrorResponse(response);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const data = (await response.json()) as ContextResponse;
|
|
88
|
+
return {
|
|
89
|
+
id: data.id,
|
|
90
|
+
language: data.language,
|
|
91
|
+
cwd: data.cwd,
|
|
92
|
+
createdAt: new Date(data.createdAt),
|
|
93
|
+
lastUsed: new Date(data.lastUsed),
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async runCodeStream(
|
|
99
|
+
contextId: string | undefined,
|
|
100
|
+
code: string,
|
|
101
|
+
language: string | undefined,
|
|
102
|
+
callbacks: ExecutionCallbacks
|
|
103
|
+
): Promise<void> {
|
|
104
|
+
return this.executeWithRetry(async () => {
|
|
105
|
+
const response = await this.doFetch("/api/execute/code", {
|
|
106
|
+
method: "POST",
|
|
107
|
+
headers: {
|
|
108
|
+
"Content-Type": "application/json",
|
|
109
|
+
Accept: "text/event-stream",
|
|
110
|
+
},
|
|
111
|
+
body: JSON.stringify({
|
|
112
|
+
context_id: contextId,
|
|
113
|
+
code,
|
|
114
|
+
language,
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (!response.ok) {
|
|
119
|
+
throw await parseErrorResponse(response);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!response.body) {
|
|
123
|
+
throw new Error("No response body for streaming execution");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Process streaming response
|
|
127
|
+
for await (const chunk of this.readLines(response.body)) {
|
|
128
|
+
await this.parseExecutionResult(chunk, callbacks);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private async *readLines(
|
|
134
|
+
stream: ReadableStream<Uint8Array>
|
|
135
|
+
): AsyncGenerator<string> {
|
|
136
|
+
const reader = stream.getReader();
|
|
137
|
+
let buffer = "";
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
while (true) {
|
|
141
|
+
const { done, value } = await reader.read();
|
|
142
|
+
if (value) {
|
|
143
|
+
buffer += new TextDecoder().decode(value);
|
|
144
|
+
}
|
|
145
|
+
if (done) break;
|
|
146
|
+
|
|
147
|
+
let newlineIdx = buffer.indexOf("\n");
|
|
148
|
+
while (newlineIdx !== -1) {
|
|
149
|
+
yield buffer.slice(0, newlineIdx);
|
|
150
|
+
buffer = buffer.slice(newlineIdx + 1);
|
|
151
|
+
newlineIdx = buffer.indexOf("\n");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Yield any remaining data
|
|
156
|
+
if (buffer.length > 0) {
|
|
157
|
+
yield buffer;
|
|
158
|
+
}
|
|
159
|
+
} finally {
|
|
160
|
+
reader.releaseLock();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private async parseExecutionResult(
|
|
165
|
+
line: string,
|
|
166
|
+
callbacks: ExecutionCallbacks
|
|
167
|
+
) {
|
|
168
|
+
if (!line.trim()) return;
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
const data = JSON.parse(line) as StreamingExecutionData;
|
|
172
|
+
|
|
173
|
+
switch (data.type) {
|
|
174
|
+
case "stdout":
|
|
175
|
+
if (callbacks.onStdout && data.text) {
|
|
176
|
+
await callbacks.onStdout({
|
|
177
|
+
text: data.text,
|
|
178
|
+
timestamp: data.timestamp || Date.now(),
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
|
|
183
|
+
case "stderr":
|
|
184
|
+
if (callbacks.onStderr && data.text) {
|
|
185
|
+
await callbacks.onStderr({
|
|
186
|
+
text: data.text,
|
|
187
|
+
timestamp: data.timestamp || Date.now(),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
|
|
192
|
+
case "result":
|
|
193
|
+
if (callbacks.onResult) {
|
|
194
|
+
// Convert raw result to Result interface
|
|
195
|
+
const result: Result = {
|
|
196
|
+
text: data.text,
|
|
197
|
+
html: data.html,
|
|
198
|
+
png: data.png,
|
|
199
|
+
jpeg: data.jpeg,
|
|
200
|
+
svg: data.svg,
|
|
201
|
+
latex: data.latex,
|
|
202
|
+
markdown: data.markdown,
|
|
203
|
+
javascript: data.javascript,
|
|
204
|
+
json: data.json,
|
|
205
|
+
chart: data.chart,
|
|
206
|
+
data: data.data,
|
|
207
|
+
formats: () => {
|
|
208
|
+
const formats: string[] = [];
|
|
209
|
+
if (data.text) formats.push("text");
|
|
210
|
+
if (data.html) formats.push("html");
|
|
211
|
+
if (data.png) formats.push("png");
|
|
212
|
+
if (data.jpeg) formats.push("jpeg");
|
|
213
|
+
if (data.svg) formats.push("svg");
|
|
214
|
+
if (data.latex) formats.push("latex");
|
|
215
|
+
if (data.markdown) formats.push("markdown");
|
|
216
|
+
if (data.javascript) formats.push("javascript");
|
|
217
|
+
if (data.json) formats.push("json");
|
|
218
|
+
if (data.chart) formats.push("chart");
|
|
219
|
+
return formats;
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
await callbacks.onResult(result);
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
|
|
226
|
+
case "error":
|
|
227
|
+
if (callbacks.onError) {
|
|
228
|
+
await callbacks.onError({
|
|
229
|
+
name: data.ename || "Error",
|
|
230
|
+
value: data.evalue || data.text || "Unknown error",
|
|
231
|
+
traceback: data.traceback || [],
|
|
232
|
+
lineNumber: data.lineNumber,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
|
|
237
|
+
case "execution_complete":
|
|
238
|
+
// Execution completed successfully
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error("[JupyterClient] Error parsing execution result:", error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async listCodeContexts(): Promise<CodeContext[]> {
|
|
247
|
+
return this.executeWithRetry(async () => {
|
|
248
|
+
const response = await this.doFetch("/api/contexts", {
|
|
249
|
+
method: "GET",
|
|
250
|
+
headers: { "Content-Type": "application/json" },
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
if (!response.ok) {
|
|
254
|
+
throw await parseErrorResponse(response);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const data = (await response.json()) as ContextListResponse;
|
|
258
|
+
return data.contexts.map((ctx) => ({
|
|
259
|
+
id: ctx.id,
|
|
260
|
+
language: ctx.language,
|
|
261
|
+
cwd: ctx.cwd,
|
|
262
|
+
createdAt: new Date(ctx.createdAt),
|
|
263
|
+
lastUsed: new Date(ctx.lastUsed),
|
|
264
|
+
}));
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async deleteCodeContext(contextId: string): Promise<void> {
|
|
269
|
+
return this.executeWithRetry(async () => {
|
|
270
|
+
const response = await this.doFetch(`/api/contexts/${contextId}`, {
|
|
271
|
+
method: "DELETE",
|
|
272
|
+
headers: { "Content-Type": "application/json" },
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
if (!response.ok) {
|
|
276
|
+
throw await parseErrorResponse(response);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Override parent doFetch to be public for this class
|
|
282
|
+
public async doFetch(path: string, options?: RequestInit): Promise<Response> {
|
|
283
|
+
return super.doFetch(path, options);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Execute an operation with automatic retry for transient errors
|
|
288
|
+
*/
|
|
289
|
+
private async executeWithRetry<T>(operation: () => Promise<T>): Promise<T> {
|
|
290
|
+
let lastError: Error | undefined;
|
|
291
|
+
|
|
292
|
+
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
|
|
293
|
+
try {
|
|
294
|
+
return await operation();
|
|
295
|
+
} catch (error) {
|
|
296
|
+
lastError = error as Error;
|
|
297
|
+
|
|
298
|
+
// Check if it's a retryable error (circuit breaker or Jupyter not ready)
|
|
299
|
+
if (this.isRetryableError(error)) {
|
|
300
|
+
// Don't retry on the last attempt
|
|
301
|
+
if (attempt < this.maxRetries - 1) {
|
|
302
|
+
// Exponential backoff with jitter
|
|
303
|
+
const delay =
|
|
304
|
+
this.retryDelayMs * 2 ** attempt + Math.random() * 1000;
|
|
305
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Non-retryable error or last attempt - throw immediately
|
|
311
|
+
throw error;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// All retries exhausted - throw a clean error without implementation details
|
|
316
|
+
if (lastError?.message.includes("Code execution")) {
|
|
317
|
+
// If the error already has a clean message about code execution, use it
|
|
318
|
+
throw lastError;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Otherwise, throw a generic but user-friendly error
|
|
322
|
+
throw new Error("Unable to execute code at this time");
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Check if an error is retryable
|
|
327
|
+
*/
|
|
328
|
+
private isRetryableError(error: unknown): boolean {
|
|
329
|
+
// Use the SDK's built-in retryable check
|
|
330
|
+
if (isRetryableError(error)) {
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Also check for circuit breaker specific errors
|
|
335
|
+
if (error instanceof Error) {
|
|
336
|
+
// Circuit breaker errors (from the container's response)
|
|
337
|
+
if (error.message.includes("Circuit breaker is open")) {
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Check if error has a status property
|
|
342
|
+
if ("status" in error && error.status === "circuit_open") {
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { getSandbox, type Sandbox } from "./sandbox";
|
|
2
|
+
import {
|
|
3
|
+
logSecurityEvent,
|
|
4
|
+
sanitizeSandboxId,
|
|
5
|
+
validatePort
|
|
6
|
+
} from "./security";
|
|
7
|
+
|
|
8
|
+
export interface SandboxEnv {
|
|
9
|
+
Sandbox: DurableObjectNamespace<Sandbox>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RouteInfo {
|
|
13
|
+
port: number;
|
|
14
|
+
sandboxId: string;
|
|
15
|
+
path: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function proxyToSandbox<E extends SandboxEnv>(
|
|
19
|
+
request: Request,
|
|
20
|
+
env: E
|
|
21
|
+
): Promise<Response | null> {
|
|
22
|
+
try {
|
|
23
|
+
const url = new URL(request.url);
|
|
24
|
+
const routeInfo = extractSandboxRoute(url);
|
|
25
|
+
|
|
26
|
+
if (!routeInfo) {
|
|
27
|
+
return null; // Not a request to an exposed container port
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const { sandboxId, port, path } = routeInfo;
|
|
31
|
+
const sandbox = getSandbox(env.Sandbox, sandboxId);
|
|
32
|
+
|
|
33
|
+
// Build proxy request with proper headers
|
|
34
|
+
let proxyUrl: string;
|
|
35
|
+
|
|
36
|
+
// Route based on the target port
|
|
37
|
+
if (port !== 3000) {
|
|
38
|
+
// Route directly to user's service on the specified port
|
|
39
|
+
proxyUrl = `http://localhost:${port}${path}${url.search}`;
|
|
40
|
+
} else {
|
|
41
|
+
// Port 3000 is our control plane - route normally
|
|
42
|
+
proxyUrl = `http://localhost:3000${path}${url.search}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const proxyRequest = new Request(proxyUrl, {
|
|
46
|
+
method: request.method,
|
|
47
|
+
headers: {
|
|
48
|
+
...Object.fromEntries(request.headers),
|
|
49
|
+
'X-Original-URL': request.url,
|
|
50
|
+
'X-Forwarded-Host': url.hostname,
|
|
51
|
+
'X-Forwarded-Proto': url.protocol.replace(':', ''),
|
|
52
|
+
'X-Sandbox-Name': sandboxId, // Pass the friendly name
|
|
53
|
+
},
|
|
54
|
+
body: request.body,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return sandbox.containerFetch(proxyRequest, port);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.error('[Sandbox] Proxy routing error:', error);
|
|
60
|
+
return new Response('Proxy routing error', { status: 500 });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function extractSandboxRoute(url: URL): RouteInfo | null {
|
|
65
|
+
// Parse subdomain pattern: port-sandboxId.domain
|
|
66
|
+
const subdomainMatch = url.hostname.match(/^(\d{4,5})-([^.-][^.]*[^.-]|[^.-])\.(.+)$/);
|
|
67
|
+
|
|
68
|
+
if (!subdomainMatch) {
|
|
69
|
+
// Log malformed subdomain attempts
|
|
70
|
+
if (url.hostname.includes('-') && url.hostname.includes('.')) {
|
|
71
|
+
logSecurityEvent('MALFORMED_SUBDOMAIN_ATTEMPT', {
|
|
72
|
+
hostname: url.hostname,
|
|
73
|
+
url: url.toString()
|
|
74
|
+
}, 'medium');
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const portStr = subdomainMatch[1];
|
|
80
|
+
const sandboxId = subdomainMatch[2];
|
|
81
|
+
const domain = subdomainMatch[3];
|
|
82
|
+
|
|
83
|
+
const port = parseInt(portStr, 10);
|
|
84
|
+
if (!validatePort(port)) {
|
|
85
|
+
logSecurityEvent('INVALID_PORT_IN_SUBDOMAIN', {
|
|
86
|
+
port,
|
|
87
|
+
portStr,
|
|
88
|
+
sandboxId,
|
|
89
|
+
hostname: url.hostname,
|
|
90
|
+
url: url.toString()
|
|
91
|
+
}, 'high');
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let sanitizedSandboxId: string;
|
|
96
|
+
try {
|
|
97
|
+
sanitizedSandboxId = sanitizeSandboxId(sandboxId);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
logSecurityEvent('INVALID_SANDBOX_ID_IN_SUBDOMAIN', {
|
|
100
|
+
sandboxId,
|
|
101
|
+
port,
|
|
102
|
+
hostname: url.hostname,
|
|
103
|
+
url: url.toString(),
|
|
104
|
+
error: error instanceof Error ? error.message : 'Unknown error'
|
|
105
|
+
}, 'high');
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// DNS subdomain length limit is 63 characters
|
|
110
|
+
if (sandboxId.length > 63) {
|
|
111
|
+
logSecurityEvent('SANDBOX_ID_LENGTH_VIOLATION', {
|
|
112
|
+
sandboxId,
|
|
113
|
+
length: sandboxId.length,
|
|
114
|
+
port,
|
|
115
|
+
hostname: url.hostname
|
|
116
|
+
}, 'medium');
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
logSecurityEvent('SANDBOX_ROUTE_EXTRACTED', {
|
|
121
|
+
port,
|
|
122
|
+
sandboxId: sanitizedSandboxId,
|
|
123
|
+
domain,
|
|
124
|
+
path: url.pathname || "/",
|
|
125
|
+
hostname: url.hostname
|
|
126
|
+
}, 'low');
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
port,
|
|
130
|
+
sandboxId: sanitizedSandboxId,
|
|
131
|
+
path: url.pathname || "/",
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function isLocalhostPattern(hostname: string): boolean {
|
|
136
|
+
const hostPart = hostname.split(":")[0];
|
|
137
|
+
return (
|
|
138
|
+
hostPart === "localhost" ||
|
|
139
|
+
hostPart === "127.0.0.1" ||
|
|
140
|
+
hostPart === "::1" ||
|
|
141
|
+
hostPart === "[::1]" ||
|
|
142
|
+
hostPart === "0.0.0.0"
|
|
143
|
+
);
|
|
144
|
+
}
|