withruntime 0.49.0 → 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.
- package/dist/generated/apps.d.ts +309 -0
- package/dist/generated/apps.js +333 -0
- package/dist/generated/audit.d.ts +47 -0
- package/dist/generated/audit.js +14 -0
- package/dist/generated/browsers.d.ts +309 -0
- package/dist/generated/browsers.js +333 -0
- package/dist/generated/builds.d.ts +309 -0
- package/dist/generated/builds.js +333 -0
- package/dist/generated/ci.d.ts +309 -0
- package/dist/generated/ci.js +333 -0
- package/dist/generated/containers.d.ts +309 -0
- package/dist/generated/containers.js +333 -0
- package/dist/generated/desktops.d.ts +309 -0
- package/dist/generated/desktops.js +333 -0
- package/dist/generated/domains.d.ts +80 -0
- package/dist/generated/domains.js +58 -0
- package/dist/generated/egressIps.d.ts +36 -0
- package/dist/generated/egressIps.js +14 -0
- package/dist/generated/events.d.ts +37 -0
- package/dist/generated/events.js +14 -0
- package/dist/generated/feedback.d.ts +44 -0
- package/dist/generated/feedback.js +13 -0
- package/dist/generated/functions.d.ts +309 -0
- package/dist/generated/functions.js +333 -0
- package/dist/generated/identities.d.ts +36 -0
- package/dist/generated/identities.js +22 -0
- package/dist/generated/images.d.ts +200 -0
- package/dist/generated/images.js +65 -0
- package/dist/generated/index.d.ts +143 -0
- package/dist/generated/index.js +184 -0
- package/dist/generated/jobRuns.d.ts +68 -0
- package/dist/generated/jobRuns.js +22 -0
- package/dist/generated/jobs.d.ts +144 -0
- package/dist/generated/jobs.js +85 -0
- package/dist/generated/keys.d.ts +105 -0
- package/dist/generated/keys.js +26 -0
- package/dist/generated/me.d.ts +33 -0
- package/dist/generated/me.js +13 -0
- package/dist/generated/networks.d.ts +32 -0
- package/dist/generated/networks.js +14 -0
- package/dist/generated/notebooks.d.ts +309 -0
- package/dist/generated/notebooks.js +333 -0
- package/dist/generated/otel.d.ts +46 -0
- package/dist/generated/otel.js +22 -0
- package/dist/generated/ports.d.ts +55 -0
- package/dist/generated/ports.js +22 -0
- package/dist/generated/previews.d.ts +86 -0
- package/dist/generated/previews.js +67 -0
- package/dist/generated/projects.d.ts +47 -0
- package/dist/generated/projects.js +26 -0
- package/dist/generated/roles.d.ts +36 -0
- package/dist/generated/roles.js +22 -0
- package/dist/generated/routes.d.ts +3 -0
- package/dist/generated/routes.js +4 -0
- package/dist/generated/runtime.d.ts +176 -0
- package/dist/generated/runtime.js +416 -0
- package/dist/generated/sandboxes.d.ts +952 -0
- package/dist/generated/sandboxes.js +342 -0
- package/dist/generated/secrets.d.ts +116 -0
- package/dist/generated/secrets.js +54 -0
- package/dist/generated/sessions.d.ts +47 -0
- package/dist/generated/sessions.js +17 -0
- package/dist/generated/signing.d.ts +68 -0
- package/dist/generated/signing.js +187 -0
- package/dist/generated/sites.d.ts +309 -0
- package/dist/generated/sites.js +333 -0
- package/dist/generated/snapshots.d.ts +27 -0
- package/dist/generated/snapshots.js +22 -0
- package/dist/generated/tunnels.d.ts +68 -0
- package/dist/generated/tunnels.js +14 -0
- package/dist/generated/usage.d.ts +155 -0
- package/dist/generated/usage.js +17 -0
- package/dist/generated/volumes.d.ts +36 -0
- package/dist/generated/volumes.js +26 -0
- package/dist/generated/webhooks.d.ts +44 -0
- package/dist/generated/webhooks.js +22 -0
- package/dist/generated/workspaces.d.ts +309 -0
- package/dist/generated/workspaces.js +333 -0
- package/dist/v2/cli.d.ts +7 -0
- package/dist/v2/cli.js +283 -0
- package/dist/v2/index.d.ts +1 -0
- package/dist/v2/index.js +4 -0
- package/dist/v2/login.d.ts +35 -0
- package/dist/v2/login.js +225 -0
- package/dist/v2/mcp.d.ts +1 -0
- package/dist/v2/mcp.js +49 -0
- package/dist/v2/openapi.json +75750 -0
- package/dist/v2/shims/base.d.ts +33 -0
- package/dist/v2/shims/base.js +54 -0
- package/dist/v2/shims/blaxel.d.ts +60 -0
- package/dist/v2/shims/blaxel.js +71 -0
- package/dist/v2/shims/cloudflare.d.ts +52 -0
- package/dist/v2/shims/cloudflare.js +55 -0
- package/dist/v2/shims/daytona.d.ts +58 -0
- package/dist/v2/shims/daytona.js +74 -0
- package/dist/v2/shims/e2b.d.ts +142 -0
- package/dist/v2/shims/e2b.js +238 -0
- package/dist/v2/shims/fly.d.ts +6 -0
- package/dist/v2/shims/fly.js +91 -0
- package/dist/v2/shims/modal.d.ts +54 -0
- package/dist/v2/shims/modal.js +79 -0
- package/dist/v2/shims/morph.d.ts +60 -0
- package/dist/v2/shims/morph.js +87 -0
- package/dist/v2/shims/vercel.d.ts +53 -0
- package/dist/v2/shims/vercel.js +90 -0
- package/dist/v2/table.d.ts +53 -0
- package/dist/v2/table.js +122 -0
- package/package.json +20 -310
- package/BLAXEL.md +0 -290
- package/COMPATIBILITY.md +0 -447
- package/DAYTONA.md +0 -237
- package/E2B.md +0 -314
- package/README.md +0 -331
- package/README.src.md +0 -333
- package/VERCEL.md +0 -230
- package/agent-platforms-inventory.json +0 -387
- package/compatibility-lock.json +0 -305
- package/dist/agents/browser.d.ts +0 -12
- package/dist/agents/browser.js +0 -12
- package/dist/agents/index.d.ts +0 -11
- package/dist/agents/index.js +0 -11
- package/dist/agents-cli.d.ts +0 -16
- package/dist/agents-cli.js +0 -1237
- package/dist/agents-deploy.d.ts +0 -196
- package/dist/agents-deploy.js +0 -872
- package/dist/agents-file.d.ts +0 -19
- package/dist/agents-file.js +0 -135
- package/dist/ai/index.d.ts +0 -49
- package/dist/ai/index.js +0 -31
- package/dist/ai-harness/index.d.ts +0 -194
- package/dist/ai-harness/index.js +0 -522
- package/dist/api-defaults.d.ts +0 -17
- package/dist/api-defaults.js +0 -22
- package/dist/apps/index.d.ts +0 -10
- package/dist/apps/index.js +0 -11
- package/dist/apps-cli.d.ts +0 -16
- package/dist/apps-cli.js +0 -1021
- package/dist/billing-cli.d.ts +0 -23
- package/dist/billing-cli.js +0 -264
- package/dist/blaxel/client.d.ts +0 -59
- package/dist/blaxel/client.js +0 -86
- package/dist/blaxel/context.d.ts +0 -59
- package/dist/blaxel/context.js +0 -162
- package/dist/blaxel/errors.d.ts +0 -130
- package/dist/blaxel/errors.js +0 -315
- package/dist/blaxel/filesystem.d.ts +0 -147
- package/dist/blaxel/filesystem.js +0 -585
- package/dist/blaxel/index.d.ts +0 -287
- package/dist/blaxel/index.js +0 -351
- package/dist/blaxel/interpreter.d.ts +0 -71
- package/dist/blaxel/interpreter.js +0 -256
- package/dist/blaxel/lookup.d.ts +0 -39
- package/dist/blaxel/lookup.js +0 -84
- package/dist/blaxel/pagination.d.ts +0 -39
- package/dist/blaxel/pagination.js +0 -88
- package/dist/blaxel/preview.d.ts +0 -136
- package/dist/blaxel/preview.js +0 -304
- package/dist/blaxel/process.d.ts +0 -126
- package/dist/blaxel/process.js +0 -581
- package/dist/blaxel/sandbox.d.ts +0 -198
- package/dist/blaxel/sandbox.js +0 -856
- package/dist/blaxel/session.d.ts +0 -31
- package/dist/blaxel/session.js +0 -136
- package/dist/blaxel/snapshot.d.ts +0 -123
- package/dist/blaxel/snapshot.js +0 -199
- package/dist/blaxel/types.d.ts +0 -145
- package/dist/blaxel/types.js +0 -60
- package/dist/blaxel/unsupported.d.ts +0 -20
- package/dist/blaxel/unsupported.js +0 -32
- package/dist/browser-stub.d.ts +0 -1
- package/dist/browser-stub.js +0 -1
- package/dist/browsers/index.d.ts +0 -9
- package/dist/browsers/index.js +0 -10
- package/dist/browsers-cli.d.ts +0 -13
- package/dist/browsers-cli.js +0 -279
- package/dist/builds/index.d.ts +0 -10
- package/dist/builds/index.js +0 -11
- package/dist/ci/index.d.ts +0 -13
- package/dist/ci/index.js +0 -14
- package/dist/ci-cli.d.ts +0 -20
- package/dist/ci-cli.js +0 -537
- package/dist/ci-import.d.ts +0 -23
- package/dist/ci-import.js +0 -565
- package/dist/claude-agent-sdk/index.d.ts +0 -7
- package/dist/claude-agent-sdk/index.js +0 -65
- package/dist/cli/logs.d.ts +0 -21
- package/dist/cli/logs.js +0 -188
- package/dist/cli-extras.d.ts +0 -39
- package/dist/cli-extras.js +0 -267
- package/dist/cli-name.d.ts +0 -6
- package/dist/cli-name.js +0 -8
- package/dist/cli-update.d.ts +0 -11
- package/dist/cli-update.js +0 -46
- package/dist/cli-words.d.ts +0 -36
- package/dist/cli-words.js +0 -167
- package/dist/cli.d.ts +0 -92
- package/dist/cli.js +0 -5463
- package/dist/client.d.ts +0 -189
- package/dist/client.js +0 -205
- package/dist/cloudflare/errors.d.ts +0 -166
- package/dist/cloudflare/errors.js +0 -183
- package/dist/cloudflare/identity.d.ts +0 -7
- package/dist/cloudflare/identity.js +0 -13
- package/dist/cloudflare/index.d.ts +0 -545
- package/dist/cloudflare/index.js +0 -1106
- package/dist/cloudflare/interpreter.d.ts +0 -119
- package/dist/cloudflare/interpreter.js +0 -232
- package/dist/cloudflare/mime.d.ts +0 -1
- package/dist/cloudflare/mime.js +0 -66
- package/dist/cloudflare/streams.d.ts +0 -46
- package/dist/cloudflare/streams.js +0 -175
- package/dist/cloudflare/types.d.ts +0 -338
- package/dist/cloudflare/types.js +0 -80
- package/dist/cloudflare/watch.d.ts +0 -9
- package/dist/cloudflare/watch.js +0 -24
- package/dist/cloudflare/worker.d.ts +0 -706
- package/dist/cloudflare/worker.js +0 -633
- package/dist/codesandbox/guest-service.d.ts +0 -31
- package/dist/codesandbox/guest-service.js +0 -490
- package/dist/codesandbox/index.d.ts +0 -574
- package/dist/codesandbox/index.js +0 -1747
- package/dist/compat/core.d.ts +0 -58
- package/dist/compat/core.js +0 -267
- package/dist/compat/files.d.ts +0 -27
- package/dist/compat/files.js +0 -157
- package/dist/compat/lease.d.ts +0 -37
- package/dist/compat/lease.js +0 -93
- package/dist/compat/shell.d.ts +0 -4
- package/dist/compat/shell.js +0 -165
- package/dist/containers/index.d.ts +0 -10
- package/dist/containers/index.js +0 -11
- package/dist/credentials.d.ts +0 -71
- package/dist/credentials.js +0 -340
- package/dist/daytona/client.d.ts +0 -48
- package/dist/daytona/client.js +0 -45
- package/dist/daytona/computer-use.d.ts +0 -171
- package/dist/daytona/computer-use.js +0 -382
- package/dist/daytona/context.d.ts +0 -41
- package/dist/daytona/context.js +0 -50
- package/dist/daytona/daytona.d.ts +0 -213
- package/dist/daytona/daytona.js +0 -578
- package/dist/daytona/errors.d.ts +0 -117
- package/dist/daytona/errors.js +0 -201
- package/dist/daytona/filesystem.d.ts +0 -99
- package/dist/daytona/filesystem.js +0 -300
- package/dist/daytona/git.d.ts +0 -33
- package/dist/daytona/git.js +0 -128
- package/dist/daytona/image.d.ts +0 -49
- package/dist/daytona/image.js +0 -146
- package/dist/daytona/index.d.ts +0 -87
- package/dist/daytona/index.js +0 -102
- package/dist/daytona/process.d.ts +0 -143
- package/dist/daytona/process.js +0 -565
- package/dist/daytona/sandbox.d.ts +0 -222
- package/dist/daytona/sandbox.js +0 -505
- package/dist/desktops/index.d.ts +0 -9
- package/dist/desktops/index.js +0 -10
- package/dist/domains/index.d.ts +0 -9
- package/dist/domains/index.js +0 -10
- package/dist/e2b/client.d.ts +0 -76
- package/dist/e2b/client.js +0 -124
- package/dist/e2b/code-interpreter.d.ts +0 -134
- package/dist/e2b/code-interpreter.js +0 -239
- package/dist/e2b/commands.d.ts +0 -135
- package/dist/e2b/commands.js +0 -452
- package/dist/e2b/errors.d.ts +0 -125
- package/dist/e2b/errors.js +0 -247
- package/dist/e2b/filesystem.d.ts +0 -111
- package/dist/e2b/filesystem.js +0 -374
- package/dist/e2b/index.d.ts +0 -35
- package/dist/e2b/index.js +0 -69
- package/dist/e2b/pty.d.ts +0 -24
- package/dist/e2b/pty.js +0 -83
- package/dist/e2b/sandbox.d.ts +0 -248
- package/dist/e2b/sandbox.js +0 -781
- package/dist/e2b/unsupported.d.ts +0 -38
- package/dist/e2b/unsupported.js +0 -48
- package/dist/e2b/users.d.ts +0 -39
- package/dist/e2b/users.js +0 -207
- package/dist/errors.d.ts +0 -100
- package/dist/errors.js +0 -198
- package/dist/file-source.d.ts +0 -16
- package/dist/file-source.js +0 -162
- package/dist/fly/index.d.ts +0 -102
- package/dist/fly/index.js +0 -930
- package/dist/freestyle/index.d.ts +0 -629
- package/dist/freestyle/index.js +0 -1187
- package/dist/functions/index.d.ts +0 -10
- package/dist/functions/index.js +0 -11
- package/dist/identity.d.ts +0 -30
- package/dist/identity.js +0 -141
- package/dist/image-context.d.ts +0 -39
- package/dist/image-context.js +0 -176
- package/dist/images/index.d.ts +0 -9
- package/dist/images/index.js +0 -10
- package/dist/index.d.ts +0 -87
- package/dist/index.js +0 -51
- package/dist/inngest/index.d.ts +0 -96
- package/dist/inngest/index.js +0 -183
- package/dist/ips/index.d.ts +0 -9
- package/dist/ips/index.js +0 -10
- package/dist/jobs/index.d.ts +0 -9
- package/dist/jobs/index.js +0 -10
- package/dist/jobs-code/cron.d.ts +0 -12
- package/dist/jobs-code/cron.js +0 -93
- package/dist/jobs-code/registry.d.ts +0 -31
- package/dist/jobs-code/registry.js +0 -30
- package/dist/jobs-code/runner.d.ts +0 -1
- package/dist/jobs-code/runner.js +0 -74
- package/dist/jobs-deploy.d.ts +0 -22
- package/dist/jobs-deploy.js +0 -247
- package/dist/jobs-import.d.ts +0 -15
- package/dist/jobs-import.js +0 -231
- package/dist/keys.d.ts +0 -55
- package/dist/keys.js +0 -211
- package/dist/login.d.ts +0 -88
- package/dist/login.js +0 -369
- package/dist/machine.d.ts +0 -222
- package/dist/machine.js +0 -578
- package/dist/mcp.d.ts +0 -16
- package/dist/mcp.js +0 -377
- package/dist/modal/index.d.ts +0 -532
- package/dist/modal/index.js +0 -1284
- package/dist/morph/index.d.ts +0 -255
- package/dist/morph/index.js +0 -641
- package/dist/network-cli.d.ts +0 -17
- package/dist/network-cli.js +0 -523
- package/dist/networks/index.d.ts +0 -11
- package/dist/networks/index.js +0 -10
- package/dist/openai-agents/index.d.ts +0 -132
- package/dist/openai-agents/index.js +0 -738
- package/dist/page.d.ts +0 -13
- package/dist/page.js +0 -39
- package/dist/ports/index.d.ts +0 -9
- package/dist/ports/index.js +0 -10
- package/dist/product-names.d.ts +0 -52
- package/dist/product-names.js +0 -174
- package/dist/product-renames.d.ts +0 -103
- package/dist/product-renames.js +0 -189
- package/dist/products/agents.d.ts +0 -935
- package/dist/products/agents.js +0 -581
- package/dist/products/api-keys.d.ts +0 -113
- package/dist/products/api-keys.js +0 -32
- package/dist/products/apps.d.ts +0 -409
- package/dist/products/apps.js +0 -231
- package/dist/products/audit.d.ts +0 -51
- package/dist/products/audit.js +0 -10
- package/dist/products/balancers.d.ts +0 -87
- package/dist/products/balancers.js +0 -8
- package/dist/products/billing.d.ts +0 -76
- package/dist/products/billing.js +0 -26
- package/dist/products/browser.d.ts +0 -73
- package/dist/products/browser.js +0 -29
- package/dist/products/browsers.d.ts +0 -184
- package/dist/products/browsers.js +0 -180
- package/dist/products/builds.d.ts +0 -175
- package/dist/products/builds.js +0 -234
- package/dist/products/ci.d.ts +0 -406
- package/dist/products/ci.js +0 -133
- package/dist/products/containers.d.ts +0 -71
- package/dist/products/containers.js +0 -6
- package/dist/products/desktop.d.ts +0 -141
- package/dist/products/desktop.js +0 -83
- package/dist/products/desktops.d.ts +0 -71
- package/dist/products/desktops.js +0 -27
- package/dist/products/functions.d.ts +0 -2
- package/dist/products/functions.js +0 -5
- package/dist/products/images.d.ts +0 -229
- package/dist/products/images.js +0 -235
- package/dist/products/index.d.ts +0 -84
- package/dist/products/index.js +0 -105
- package/dist/products/interpreter.d.ts +0 -97
- package/dist/products/interpreter.js +0 -106
- package/dist/products/jobs.d.ts +0 -408
- package/dist/products/jobs.js +0 -256
- package/dist/products/limits.d.ts +0 -61
- package/dist/products/limits.js +0 -10
- package/dist/products/logs.d.ts +0 -245
- package/dist/products/logs.js +0 -274
- package/dist/products/mcp.d.ts +0 -88
- package/dist/products/mcp.js +0 -58
- package/dist/products/mounts.d.ts +0 -49
- package/dist/products/mounts.js +0 -21
- package/dist/products/network-products.d.ts +0 -268
- package/dist/products/network-products.js +0 -127
- package/dist/products/network.d.ts +0 -62
- package/dist/products/network.js +0 -19
- package/dist/products/observability.d.ts +0 -264
- package/dist/products/observability.js +0 -123
- package/dist/products/plans.d.ts +0 -133
- package/dist/products/plans.js +0 -24
- package/dist/products/previews.d.ts +0 -70
- package/dist/products/previews.js +0 -39
- package/dist/products/private-network.d.ts +0 -22
- package/dist/products/private-network.js +0 -10
- package/dist/products/queues.d.ts +0 -149
- package/dist/products/queues.js +0 -213
- package/dist/products/referrals.d.ts +0 -50
- package/dist/products/referrals.js +0 -8
- package/dist/products/reservations.d.ts +0 -108
- package/dist/products/reservations.js +0 -41
- package/dist/products/resources.d.ts +0 -55
- package/dist/products/resources.js +0 -122
- package/dist/products/secrets.d.ts +0 -316
- package/dist/products/secrets.js +0 -333
- package/dist/products/signed-requests.d.ts +0 -21
- package/dist/products/signed-requests.js +0 -11
- package/dist/products/sso.d.ts +0 -44
- package/dist/products/sso.js +0 -8
- package/dist/products/streams.d.ts +0 -73
- package/dist/products/streams.js +0 -17
- package/dist/products/switching.d.ts +0 -123
- package/dist/products/switching.js +0 -26
- package/dist/products/tailscale.d.ts +0 -57
- package/dist/products/tailscale.js +0 -17
- package/dist/products/uptime.d.ts +0 -97
- package/dist/products/uptime.js +0 -42
- package/dist/products/volumes.d.ts +0 -190
- package/dist/products/volumes.js +0 -114
- package/dist/products/watch.d.ts +0 -112
- package/dist/products/watch.js +0 -249
- package/dist/proxy.d.ts +0 -30
- package/dist/proxy.js +0 -200
- package/dist/record-cli.d.ts +0 -23
- package/dist/record-cli.js +0 -308
- package/dist/renamed-products.d.ts +0 -38
- package/dist/renamed-products.js +0 -34
- package/dist/resumable.d.ts +0 -32
- package/dist/resumable.js +0 -201
- package/dist/runloop/index.d.ts +0 -333
- package/dist/runloop/index.js +0 -560
- package/dist/runloop/pagination.d.ts +0 -46
- package/dist/runloop/pagination.js +0 -82
- package/dist/runloop/sdk.d.ts +0 -131
- package/dist/runloop/sdk.js +0 -167
- package/dist/runloop/snapshots.d.ts +0 -52
- package/dist/runloop/snapshots.js +0 -244
- package/dist/sandbox.d.ts +0 -371
- package/dist/sandbox.js +0 -1500
- package/dist/sandboxes/index.d.ts +0 -11
- package/dist/sandboxes/index.js +0 -12
- package/dist/sealing.d.ts +0 -6
- package/dist/sealing.js +0 -14
- package/dist/snapshots/index.d.ts +0 -9
- package/dist/snapshots/index.js +0 -11
- package/dist/snapshots.d.ts +0 -82
- package/dist/snapshots.js +0 -65
- package/dist/sprites/index.d.ts +0 -563
- package/dist/sprites/index.js +0 -1370
- package/dist/ssh.d.ts +0 -56
- package/dist/ssh.js +0 -416
- package/dist/switched-off.d.ts +0 -5
- package/dist/switched-off.js +0 -8
- package/dist/tar.d.ts +0 -15
- package/dist/tar.js +0 -526
- package/dist/terminal.d.ts +0 -24
- package/dist/terminal.js +0 -32
- package/dist/tools/index.d.ts +0 -62
- package/dist/tools/index.js +0 -181
- package/dist/transport.d.ts +0 -165
- package/dist/transport.js +0 -883
- package/dist/trigger/index.d.ts +0 -202
- package/dist/trigger/index.js +0 -244
- package/dist/tunnel.d.ts +0 -70
- package/dist/tunnel.js +0 -316
- package/dist/types.d.ts +0 -574
- package/dist/types.js +0 -1
- package/dist/vercel/client.d.ts +0 -40
- package/dist/vercel/client.js +0 -39
- package/dist/vercel/command.d.ts +0 -117
- package/dist/vercel/command.js +0 -391
- package/dist/vercel/errors.d.ts +0 -53
- package/dist/vercel/errors.js +0 -131
- package/dist/vercel/filesystem.d.ts +0 -153
- package/dist/vercel/filesystem.js +0 -297
- package/dist/vercel/index.d.ts +0 -24
- package/dist/vercel/index.js +0 -36
- package/dist/vercel/sandbox-user.d.ts +0 -70
- package/dist/vercel/sandbox-user.js +0 -207
- package/dist/vercel/sandbox.d.ts +0 -372
- package/dist/vercel/sandbox.js +0 -1037
- package/dist/vercel/snapshot.d.ts +0 -94
- package/dist/vercel/snapshot.js +0 -137
- package/dist/volumes/index.d.ts +0 -9
- package/dist/volumes/index.js +0 -10
- package/dist/wait.d.ts +0 -12
- package/dist/wait.js +0 -47
- package/dist/wallet.d.ts +0 -45
- package/dist/wallet.js +0 -56
- package/dist/wireguard.d.ts +0 -7
- package/dist/wireguard.js +0 -13
package/dist/cloudflare/index.js
DELETED
|
@@ -1,1106 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { Runtime } from "../client.js";
|
|
3
|
-
import { NotFoundError } from "../errors.js";
|
|
4
|
-
import { timeoutSignal } from "../transport.js";
|
|
5
|
-
import { quote, client, closeShell, CompatibilityError, refuse, destroy, environment, execOptions, only, pathAt, saveEnvironment, shellCommand, shellRoot, } from "../compat/core.js";
|
|
6
|
-
import { BucketUnmountError, InvalidMountConfigError, ProcessExitedBeforeReadyError, ProcessReadyTimeoutError, S3FSMountError, ValidationFailedError, response, } from "./errors.js";
|
|
7
|
-
import { CodeInterpreter, hasEnv, RUN_ENV, } from "./interpreter.js";
|
|
8
|
-
import { sseStream } from "./streams.js";
|
|
9
|
-
import { mimeType } from "./mime.js";
|
|
10
|
-
import { CHANGES, watchFilters } from "./watch.js";
|
|
11
|
-
export { CompatibilityError };
|
|
12
|
-
export * from "./errors.js";
|
|
13
|
-
export { CodeInterpreter } from "./interpreter.js";
|
|
14
|
-
export { collectFile, isExecResult, isProcess, isProcessStatus, parseSSEStream, streamFile, asyncIterableToSSEStream, responseToAsyncIterable, } from "./streams.js";
|
|
15
|
-
export * from "./types.js";
|
|
16
|
-
export class SandboxSecurityError extends Error {
|
|
17
|
-
code;
|
|
18
|
-
constructor(message, code) {
|
|
19
|
-
super(message);
|
|
20
|
-
this.code = code;
|
|
21
|
-
this.name = "SandboxSecurityError";
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
export function sanitizeSandboxId(id) {
|
|
25
|
-
if (!id || id.length > 63)
|
|
26
|
-
throw new SandboxSecurityError("Sandbox ID must be 1-63 characters long.", "INVALID_SANDBOX_ID_LENGTH");
|
|
27
|
-
if (id.startsWith("-") || id.endsWith("-"))
|
|
28
|
-
throw new SandboxSecurityError("Sandbox ID cannot start or end with hyphens (DNS requirement).", "INVALID_SANDBOX_ID_HYPHENS");
|
|
29
|
-
if (["www", "api", "admin", "root", "system", "cloudflare", "workers"].includes(id.toLowerCase()))
|
|
30
|
-
throw new SandboxSecurityError(`Reserved sandbox ID '${id}' is not allowed.`, "RESERVED_SANDBOX_ID");
|
|
31
|
-
return id;
|
|
32
|
-
}
|
|
33
|
-
const cleanEnv = (env = {}) => Object.fromEntries(Object.entries(env).filter((entry) => entry[1] !== undefined));
|
|
34
|
-
function sleepSeconds(value = 600) {
|
|
35
|
-
let seconds;
|
|
36
|
-
if (typeof value === "number")
|
|
37
|
-
seconds = value;
|
|
38
|
-
else {
|
|
39
|
-
const match = /^(\d+)(s|m|h)$/.exec(value);
|
|
40
|
-
if (!match)
|
|
41
|
-
throw new TypeError("sleepAfter must be seconds or a duration such as 5m");
|
|
42
|
-
seconds = Number(match[1]) * ({ s: 1, m: 60, h: 3600 }[match[2]] ?? 1);
|
|
43
|
-
}
|
|
44
|
-
if (!Number.isSafeInteger(seconds) || seconds < 0 || seconds > 86_400)
|
|
45
|
-
throw new CompatibilityError("Cloudflare", "sleepAfter outside whole seconds from 0 to 86400");
|
|
46
|
-
return seconds;
|
|
47
|
-
}
|
|
48
|
-
export function validateSandboxConfiguration(options) {
|
|
49
|
-
if (options.sleepAfter !== undefined)
|
|
50
|
-
sleepSeconds(options.sleepAfter);
|
|
51
|
-
if (options.keepAlive !== undefined && typeof options.keepAlive !== "boolean")
|
|
52
|
-
throw new TypeError("keepAlive must be a boolean");
|
|
53
|
-
}
|
|
54
|
-
/** Worker bindings are deliberately not inspected or invoked. Supply Runtime credentials
|
|
55
|
-
* through options.runtime in Workers; Node also supports RUNTIME_API_KEY/login. */
|
|
56
|
-
export function getSandbox(namespace, id, options = {}) {
|
|
57
|
-
sanitizeSandboxId(id);
|
|
58
|
-
only("Cloudflare getSandbox", options, ["sleepAfter", "keepAlive", "normalizeId", "runtime"]);
|
|
59
|
-
const runtime = namespace instanceof Runtime ? namespace : client(options.runtime);
|
|
60
|
-
return new Sandbox(runtime, options.normalizeId ? id.toLowerCase() : id, options);
|
|
61
|
-
}
|
|
62
|
-
export class Sandbox {
|
|
63
|
-
runtime;
|
|
64
|
-
options;
|
|
65
|
-
pending;
|
|
66
|
-
existing;
|
|
67
|
-
id;
|
|
68
|
-
/** The port Cloudflare's container serves on; Runtime previews any port. */
|
|
69
|
-
defaultPort = 3000;
|
|
70
|
-
/** What setEnvVars gave this handle; the sandbox keeps them for every command. */
|
|
71
|
-
envVars = {};
|
|
72
|
-
#interpreter = new CodeInterpreter(this);
|
|
73
|
-
constructor(runtime, id, options = {}) {
|
|
74
|
-
this.runtime = runtime;
|
|
75
|
-
this.options = options;
|
|
76
|
-
validateSandboxConfiguration(options);
|
|
77
|
-
this.id = id;
|
|
78
|
-
}
|
|
79
|
-
get sleepAfter() {
|
|
80
|
-
return this.options.sleepAfter ?? "10m";
|
|
81
|
-
}
|
|
82
|
-
setSleepAfter(sleepAfter) {
|
|
83
|
-
return this.configure({ sleepAfter });
|
|
84
|
-
}
|
|
85
|
-
setKeepAlive(keepAlive) {
|
|
86
|
-
return this.configure({ keepAlive });
|
|
87
|
-
}
|
|
88
|
-
/** Replaces the sandbox's labels, as Cloudflare's does. */
|
|
89
|
-
async setLabels(labels) {
|
|
90
|
-
for (const [key, value] of Object.entries(labels))
|
|
91
|
-
if (typeof value !== "string")
|
|
92
|
-
throw new TypeError(`Label ${key} must be a string`);
|
|
93
|
-
await (await this.native()).update({ labels: { ...labels, "compat.provider": "cloudflare" } });
|
|
94
|
-
}
|
|
95
|
-
async configure(options) {
|
|
96
|
-
only("Cloudflare configure", options, ["sleepAfter", "keepAlive"]);
|
|
97
|
-
validateSandboxConfiguration(options);
|
|
98
|
-
const changed = (options.sleepAfter !== undefined && options.sleepAfter !== this.options.sleepAfter) ||
|
|
99
|
-
(options.keepAlive !== undefined && options.keepAlive !== this.options.keepAlive);
|
|
100
|
-
const next = { ...this.options, ...options };
|
|
101
|
-
if (changed && this.pending) {
|
|
102
|
-
const s = await this.pending;
|
|
103
|
-
await s.update({
|
|
104
|
-
idlePauseSeconds: next.keepAlive ? 0 : sleepSeconds(next.sleepAfter),
|
|
105
|
-
...(options.keepAlive !== undefined ? { persistent: options.keepAlive } : {}),
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
Object.assign(this.options, options);
|
|
109
|
-
}
|
|
110
|
-
/** Only a successfully initialized native handle can supply a recovery id. */
|
|
111
|
-
existingNativeId() {
|
|
112
|
-
return this.existing?.id;
|
|
113
|
-
}
|
|
114
|
-
/** Reconcile an uncertain update from an authoritative read, without allocation. */
|
|
115
|
-
async reconcileConfiguration(nativeId = this.existingNativeId()) {
|
|
116
|
-
if (!nativeId)
|
|
117
|
-
throw new Error("The uncertain Runtime configuration has no existing sandbox identity.");
|
|
118
|
-
const sandbox = await this.runtime.sandboxes.get(nativeId);
|
|
119
|
-
const { idlePauseSeconds, persistent } = sandbox.info;
|
|
120
|
-
if (typeof idlePauseSeconds !== "number" ||
|
|
121
|
-
!Number.isSafeInteger(idlePauseSeconds) ||
|
|
122
|
-
idlePauseSeconds < 0 ||
|
|
123
|
-
idlePauseSeconds > 86_400 ||
|
|
124
|
-
typeof persistent !== "boolean")
|
|
125
|
-
throw new Error("Runtime did not return authoritative idle and persistence settings.");
|
|
126
|
-
const configuration = { sleepAfter: idlePauseSeconds, keepAlive: persistent };
|
|
127
|
-
Object.assign(this.options, configuration);
|
|
128
|
-
this.existing = sandbox;
|
|
129
|
-
this.pending = Promise.resolve(sandbox);
|
|
130
|
-
return configuration;
|
|
131
|
-
}
|
|
132
|
-
async native() {
|
|
133
|
-
if (!this.pending) {
|
|
134
|
-
this.pending = (async () => {
|
|
135
|
-
const s = await this.runtime.sandboxes.create({
|
|
136
|
-
name: /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/.test(this.id)
|
|
137
|
-
? this.id
|
|
138
|
-
: `cf-${createHash("sha256").update(this.id).digest("hex").slice(0, 40)}`,
|
|
139
|
-
getOrCreate: true,
|
|
140
|
-
labels: { "compat.provider": "cloudflare" },
|
|
141
|
-
idlePauseSeconds: this.options.keepAlive ? 0 : sleepSeconds(this.options.sleepAfter),
|
|
142
|
-
persistent: !!this.options.keepAlive,
|
|
143
|
-
});
|
|
144
|
-
if (!s.info.reused) {
|
|
145
|
-
try {
|
|
146
|
-
await saveEnvironment(s, {});
|
|
147
|
-
}
|
|
148
|
-
catch (e) {
|
|
149
|
-
await destroy(s);
|
|
150
|
-
throw e;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
this.existing = s;
|
|
154
|
-
return s;
|
|
155
|
-
})();
|
|
156
|
-
this.pending.catch(() => {
|
|
157
|
-
this.pending = undefined;
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
return this.pending;
|
|
161
|
-
}
|
|
162
|
-
session(id = `sandbox-${this.id}`, options = {}) {
|
|
163
|
-
return new ExecutionSession(this, id, options);
|
|
164
|
-
}
|
|
165
|
-
/** A session by id: its saved settings, or a fresh session of that id, as
|
|
166
|
-
* Cloudflare starts one again after its shell ended. */
|
|
167
|
-
async sessionFor(id) {
|
|
168
|
-
if (id === undefined || id === `sandbox-${this.id}`)
|
|
169
|
-
return this.session();
|
|
170
|
-
try {
|
|
171
|
-
return await this.getSession(id);
|
|
172
|
-
}
|
|
173
|
-
catch (error) {
|
|
174
|
-
if (error instanceof NotFoundError)
|
|
175
|
-
return this.session(id);
|
|
176
|
-
throw error;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
exec(command, options) {
|
|
180
|
-
return this.session().exec(command, options);
|
|
181
|
-
}
|
|
182
|
-
async execWithSessionToken(command, sessionId, options) {
|
|
183
|
-
return (await this.sessionFor(sessionId)).exec(command, options);
|
|
184
|
-
}
|
|
185
|
-
async execStream(command, options = {}) {
|
|
186
|
-
const { sessionId, bufferSize, ...rest } = options;
|
|
187
|
-
if (bufferSize !== undefined && (!Number.isSafeInteger(bufferSize) || bufferSize <= 0))
|
|
188
|
-
throw new TypeError("bufferSize must be a positive integer");
|
|
189
|
-
return (await this.sessionFor(sessionId)).execStream(command, rest);
|
|
190
|
-
}
|
|
191
|
-
execStreamWithSessionToken(command, sessionId, options = {}) {
|
|
192
|
-
return this.execStream(command, { ...options, sessionId });
|
|
193
|
-
}
|
|
194
|
-
async startProcess(command, options = {}) {
|
|
195
|
-
if (options.processId !== undefined)
|
|
196
|
-
throw new CompatibilityError("Cloudflare", "processId; Runtime names each process itself");
|
|
197
|
-
if (options.autoCleanup)
|
|
198
|
-
throw new CompatibilityError("Cloudflare", "autoCleanup; Runtime keeps a finished process's record and output until the sandbox ends");
|
|
199
|
-
const { autoCleanup: _kept, ...rest } = options;
|
|
200
|
-
return this.session(options.sessionId).startProcess(command, rest);
|
|
201
|
-
}
|
|
202
|
-
async createSession(options = {}) {
|
|
203
|
-
only("Cloudflare session", options, [
|
|
204
|
-
"id",
|
|
205
|
-
"name",
|
|
206
|
-
"env",
|
|
207
|
-
"cwd",
|
|
208
|
-
"isolation",
|
|
209
|
-
"commandTimeoutMs",
|
|
210
|
-
]);
|
|
211
|
-
if (options.isolation)
|
|
212
|
-
throw new CompatibilityError("Cloudflare", "session PID namespace isolation");
|
|
213
|
-
const id = options.id ?? crypto.randomUUID();
|
|
214
|
-
await (await this.native()).files.write(`/workspace/.runtime-compat/cloudflare/${encodeURIComponent(id)}.json`, JSON.stringify(options), { mode: 0o600 });
|
|
215
|
-
return this.session(id, options);
|
|
216
|
-
}
|
|
217
|
-
async getSession(id) {
|
|
218
|
-
if (id === `sandbox-${this.id}`)
|
|
219
|
-
return this.session();
|
|
220
|
-
const options = JSON.parse(await (await this.native()).files.readText(`/workspace/.runtime-compat/cloudflare/${encodeURIComponent(id)}.json`));
|
|
221
|
-
return this.session(id, options);
|
|
222
|
-
}
|
|
223
|
-
async deleteSession(id) {
|
|
224
|
-
if (id === `sandbox-${this.id}`)
|
|
225
|
-
throw new Error("The default session cannot be deleted; destroy the sandbox instead");
|
|
226
|
-
await closeShell(await this.native(), `cf:${id}`);
|
|
227
|
-
await (await this.native()).files.remove(`/workspace/.runtime-compat/cloudflare/${encodeURIComponent(id)}.json`);
|
|
228
|
-
return { success: true, sessionId: id, timestamp: new Date().toISOString() };
|
|
229
|
-
}
|
|
230
|
-
async destroy() {
|
|
231
|
-
await destroy(await this.native());
|
|
232
|
-
this.pending = undefined;
|
|
233
|
-
}
|
|
234
|
-
async setEnvVars(env) {
|
|
235
|
-
const s = await this.native();
|
|
236
|
-
await saveEnvironment(s, { ...(await environment(s)), ...cleanEnv(env) });
|
|
237
|
-
this.envVars = { ...this.envVars, ...cleanEnv(env) };
|
|
238
|
-
}
|
|
239
|
-
/** Stops the sandbox; its files stay, and the next call starts it again. */
|
|
240
|
-
async stop(signal) {
|
|
241
|
-
if (signal !== undefined)
|
|
242
|
-
throw new CompatibilityError("Cloudflare", "a stop signal; Runtime stops every process");
|
|
243
|
-
await (await this.native()).stop();
|
|
244
|
-
}
|
|
245
|
-
async writeFile(path, content, options = {}) {
|
|
246
|
-
const { sessionId, ...rest } = options;
|
|
247
|
-
return (await this.sessionFor(sessionId)).writeFile(path, content, rest);
|
|
248
|
-
}
|
|
249
|
-
async readFile(path, options = {}) {
|
|
250
|
-
const { sessionId, encoding } = options;
|
|
251
|
-
const session = await this.sessionFor(sessionId);
|
|
252
|
-
return encoding === "none"
|
|
253
|
-
? session.readFile(path, { encoding: "none" })
|
|
254
|
-
: session.readFile(path, { encoding });
|
|
255
|
-
}
|
|
256
|
-
/** The file as Cloudflare streams it: `data:` lines of a metadata event,
|
|
257
|
-
* chunks (base64 for a binary file) and a complete event; read it with
|
|
258
|
-
* streamFile() or collectFile(). */
|
|
259
|
-
async readFileStream(path, options = {}) {
|
|
260
|
-
const read = await this.readFile(path, { encoding: "none", sessionId: options.sessionId });
|
|
261
|
-
const bytes = new Uint8Array(await new Response(read.content).arrayBuffer());
|
|
262
|
-
const text = new TextDecoder().decode(bytes);
|
|
263
|
-
const isBinary = text.includes("\0") || text.includes("\ufffd");
|
|
264
|
-
return sseStream(async (send) => {
|
|
265
|
-
send({
|
|
266
|
-
type: "metadata",
|
|
267
|
-
mimeType: read.mimeType,
|
|
268
|
-
size: bytes.length,
|
|
269
|
-
isBinary,
|
|
270
|
-
encoding: isBinary ? "base64" : "utf-8",
|
|
271
|
-
});
|
|
272
|
-
const CHUNK = 65_536;
|
|
273
|
-
if (isBinary)
|
|
274
|
-
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
275
|
-
let raw = "";
|
|
276
|
-
for (const byte of bytes.subarray(i, i + CHUNK))
|
|
277
|
-
raw += String.fromCharCode(byte);
|
|
278
|
-
send({ type: "chunk", data: btoa(raw) });
|
|
279
|
-
}
|
|
280
|
-
else
|
|
281
|
-
for (let i = 0; i < text.length; i += CHUNK)
|
|
282
|
-
send({ type: "chunk", data: text.slice(i, i + CHUNK) });
|
|
283
|
-
send({ type: "complete", bytesRead: bytes.length });
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
async mkdir(path, options = {}) {
|
|
287
|
-
const { sessionId, ...rest } = options;
|
|
288
|
-
return (await this.sessionFor(sessionId)).mkdir(path, rest);
|
|
289
|
-
}
|
|
290
|
-
async deleteFile(path, sessionId) {
|
|
291
|
-
return (await this.sessionFor(sessionId)).deleteFile(path);
|
|
292
|
-
}
|
|
293
|
-
async renameFile(from, to, sessionId) {
|
|
294
|
-
return (await this.sessionFor(sessionId)).renameFile(from, to);
|
|
295
|
-
}
|
|
296
|
-
moveFile(from, to, sessionId) {
|
|
297
|
-
return this.renameFile(from, to, sessionId);
|
|
298
|
-
}
|
|
299
|
-
async listFiles(path, options = {}) {
|
|
300
|
-
const { sessionId, ...rest } = options;
|
|
301
|
-
return (await this.sessionFor(sessionId)).listFiles(path, rest);
|
|
302
|
-
}
|
|
303
|
-
async exists(path, sessionId) {
|
|
304
|
-
return (await this.sessionFor(sessionId)).exists(path);
|
|
305
|
-
}
|
|
306
|
-
async listProcesses() {
|
|
307
|
-
const s = await this.native();
|
|
308
|
-
return Promise.all((await s.processes.list()).map((p) => s.processes.get(p.id).then((p) => new Process(this, p))));
|
|
309
|
-
}
|
|
310
|
-
async getProcess(id) {
|
|
311
|
-
try {
|
|
312
|
-
return new Process(this, await (await this.native()).processes.get(id));
|
|
313
|
-
}
|
|
314
|
-
catch (e) {
|
|
315
|
-
if (e instanceof NotFoundError)
|
|
316
|
-
return null;
|
|
317
|
-
throw e;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
async killProcess(id, signal = "SIGTERM") {
|
|
321
|
-
const p = await this.getProcess(id);
|
|
322
|
-
if (!p)
|
|
323
|
-
throw new Error(`Process ${id} not found`);
|
|
324
|
-
await p.kill(signal);
|
|
325
|
-
}
|
|
326
|
-
async killAllProcesses() {
|
|
327
|
-
const processes = (await this.listProcesses()).filter((p) => p.status === "running");
|
|
328
|
-
await Promise.all(processes.map((p) => p.kill()));
|
|
329
|
-
return processes.length;
|
|
330
|
-
}
|
|
331
|
-
async getProcessLogs(id) {
|
|
332
|
-
const s = await this.native();
|
|
333
|
-
let cursor = 0;
|
|
334
|
-
let stdout = "", stderr = "";
|
|
335
|
-
for (;;) {
|
|
336
|
-
const r = await this.runtime.transport.json({
|
|
337
|
-
method: "GET",
|
|
338
|
-
path: `/v1/sandboxes/${encodeURIComponent(s.id)}/processes/${encodeURIComponent(id)}/output`,
|
|
339
|
-
query: { cursor, maxBytes: 196608 },
|
|
340
|
-
});
|
|
341
|
-
if (r.truncated)
|
|
342
|
-
throw new Error("Process logs were truncated by the host");
|
|
343
|
-
for (const c of r.chunks) {
|
|
344
|
-
if (c.stream === "stdout")
|
|
345
|
-
stdout += c.text;
|
|
346
|
-
else
|
|
347
|
-
stderr += c.text;
|
|
348
|
-
}
|
|
349
|
-
if (r.chunks.length === 0 || r.nextCursor <= cursor)
|
|
350
|
-
break;
|
|
351
|
-
cursor = r.nextCursor;
|
|
352
|
-
}
|
|
353
|
-
return { stdout, stderr, processId: id };
|
|
354
|
-
}
|
|
355
|
-
async exposePort(port, options) {
|
|
356
|
-
only("Cloudflare exposePort", options, ["name", "hostname", "token"]);
|
|
357
|
-
if (options.token)
|
|
358
|
-
throw new CompatibilityError("Cloudflare", "caller-chosen preview tokens");
|
|
359
|
-
const p = await (await this.native()).previews.create(port);
|
|
360
|
-
// Cloudflare's hostname routes through its Worker. Runtime returns its own signed URL.
|
|
361
|
-
return { url: p.urlWithToken ?? p.url, port, name: options.name };
|
|
362
|
-
}
|
|
363
|
-
async unexposePort(port) {
|
|
364
|
-
await (await this.native()).previews.delete(port);
|
|
365
|
-
}
|
|
366
|
-
/** Runtime's previews have their own addresses: hostname is not used to build them. */
|
|
367
|
-
async getExposedPorts(_hostname) {
|
|
368
|
-
return (await (await this.native()).previews.list()).map((p) => ({
|
|
369
|
-
url: p.urlWithToken ?? p.url,
|
|
370
|
-
port: p.port,
|
|
371
|
-
status: "active",
|
|
372
|
-
}));
|
|
373
|
-
}
|
|
374
|
-
async isPortExposed(port) {
|
|
375
|
-
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
376
|
-
return false;
|
|
377
|
-
return (await (await this.native()).previews.list()).some((p) => p.port === port);
|
|
378
|
-
}
|
|
379
|
-
/** Cloudflare's returns 0 too: a process's record outlives it on both. */
|
|
380
|
-
async cleanupCompletedProcesses(_sessionId) {
|
|
381
|
-
return 0;
|
|
382
|
-
}
|
|
383
|
-
/** The process's output as `data:` lines of stdout, stderr and exit events. */
|
|
384
|
-
async streamProcessLogs(processId, options = {}) {
|
|
385
|
-
only("Cloudflare streamProcessLogs", options, ["signal"]);
|
|
386
|
-
options.signal?.throwIfAborted();
|
|
387
|
-
const p = await (await this.native()).processes.get(processId);
|
|
388
|
-
return sseStream(async (send, cancelled) => {
|
|
389
|
-
const signal = options.signal ? AbortSignal.any([options.signal, cancelled]) : cancelled;
|
|
390
|
-
for await (const e of p.output({ signal })) {
|
|
391
|
-
const at = new Date().toISOString();
|
|
392
|
-
if (e.type === "stdout" || e.type === "stderr")
|
|
393
|
-
send({ type: e.type, timestamp: at, data: e.data, processId });
|
|
394
|
-
else if (e.type === "exit")
|
|
395
|
-
send({ type: "exit", timestamp: at, data: "", processId, exitCode: e.exitCode });
|
|
396
|
-
else if (e.type === "truncated")
|
|
397
|
-
send({ type: "error", timestamp: at, data: "Process output was truncated", processId });
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
/** Clones with git in the sandbox, as Cloudflare's container does. */
|
|
402
|
-
async gitCheckout(repoUrl, options = {}) {
|
|
403
|
-
only("Cloudflare gitCheckout", options, [
|
|
404
|
-
"branch",
|
|
405
|
-
"targetDir",
|
|
406
|
-
"sessionId",
|
|
407
|
-
"depth",
|
|
408
|
-
"cloneTimeoutMs",
|
|
409
|
-
]);
|
|
410
|
-
const timeout = options.cloneTimeoutMs ?? 600_000;
|
|
411
|
-
if (options.depth !== undefined && (!Number.isInteger(options.depth) || options.depth <= 0))
|
|
412
|
-
throw new Error(`Invalid depth value: ${options.depth}. Must be a positive integer (e.g., 1, 5, 10).`);
|
|
413
|
-
if (!Number.isInteger(timeout) || timeout <= 0)
|
|
414
|
-
throw new Error(`Invalid timeout value: ${timeout}. Must be a positive integer number of milliseconds.`);
|
|
415
|
-
const name = repoUrl
|
|
416
|
-
.replace(/\/+$/, "")
|
|
417
|
-
.split(/[/:]/)
|
|
418
|
-
.pop()
|
|
419
|
-
?.replace(/\.git$/, "") || "repo";
|
|
420
|
-
const targetDir = options.targetDir ?? `/workspace/${name}`;
|
|
421
|
-
const session = await this.sessionFor(options.sessionId);
|
|
422
|
-
const args = [
|
|
423
|
-
"git clone",
|
|
424
|
-
...(options.branch ? ["--branch", quote(options.branch)] : []),
|
|
425
|
-
...(options.depth ? ["--depth", String(options.depth)] : []),
|
|
426
|
-
"--",
|
|
427
|
-
quote(repoUrl),
|
|
428
|
-
quote(targetDir),
|
|
429
|
-
];
|
|
430
|
-
const clone = await session.exec(`${args.join(" ")} && git -C ${quote(targetDir)} rev-parse --abbrev-ref HEAD`, { timeout });
|
|
431
|
-
if (!clone.success)
|
|
432
|
-
throw new Error(`Failed to clone ${repoUrl}: ${clone.stderr.trim() || clone.stdout.trim()}`);
|
|
433
|
-
return {
|
|
434
|
-
success: true,
|
|
435
|
-
repoUrl,
|
|
436
|
-
branch: options.branch ?? clone.stdout.trim().split("\n").pop(),
|
|
437
|
-
targetDir,
|
|
438
|
-
timestamp: new Date().toISOString(),
|
|
439
|
-
exitCode: clone.exitCode,
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
/** Changes under a directory as `data:` lines of Cloudflare's watch events,
|
|
443
|
-
* once the watch is established, until the stream is cancelled. */
|
|
444
|
-
async watch(path, options = {}) {
|
|
445
|
-
only("Cloudflare watch", options, ["recursive", "include", "exclude", "sessionId"]);
|
|
446
|
-
const filters = watchFilters(options);
|
|
447
|
-
const directory = await (await this.sessionFor(options.sessionId)).resolve(path);
|
|
448
|
-
const encode = new TextEncoder();
|
|
449
|
-
let stream;
|
|
450
|
-
let closed = false;
|
|
451
|
-
const send = (event) => {
|
|
452
|
-
if (!closed)
|
|
453
|
-
stream.enqueue(encode.encode(`data: ${JSON.stringify(event)}\n\n`));
|
|
454
|
-
};
|
|
455
|
-
const handle = await (await this.native()).files.watch(directory, (e) => {
|
|
456
|
-
const timestamp = new Date().toISOString();
|
|
457
|
-
const event = (eventType, at) => send({ type: "event", eventType, path: at, isDirectory: e.isDir, timestamp });
|
|
458
|
-
if (e.type === "rename") {
|
|
459
|
-
if (e.oldPath)
|
|
460
|
-
event("move_from", e.oldPath);
|
|
461
|
-
event("move_to", e.path);
|
|
462
|
-
}
|
|
463
|
-
else
|
|
464
|
-
event({ create: "create", write: "modify", remove: "delete", chmod: "attrib" }[e.type] ?? e.type, e.path);
|
|
465
|
-
}, {
|
|
466
|
-
recursive: options.recursive ?? true,
|
|
467
|
-
...filters,
|
|
468
|
-
timeoutMs: 0,
|
|
469
|
-
onNotice: (n) => send({ type: "error", error: n.k === "overflow" ? "Events were lost" : `Watch ${n.k}` }),
|
|
470
|
-
onExit: (reason) => {
|
|
471
|
-
send({ type: "stopped", reason });
|
|
472
|
-
if (!closed) {
|
|
473
|
-
closed = true;
|
|
474
|
-
stream.close();
|
|
475
|
-
}
|
|
476
|
-
},
|
|
477
|
-
});
|
|
478
|
-
return new ReadableStream({
|
|
479
|
-
start(controller) {
|
|
480
|
-
stream = controller;
|
|
481
|
-
send({ type: "watching", path: directory, watchId: handle.id });
|
|
482
|
-
},
|
|
483
|
-
async cancel() {
|
|
484
|
-
closed = true;
|
|
485
|
-
await handle.stop();
|
|
486
|
-
},
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
/** Whether anything under a path changed since a version this returned:
|
|
490
|
-
* the version is a digest of every entry's path, size, time and mode, tied
|
|
491
|
-
* to this boot of the sandbox. */
|
|
492
|
-
async checkChanges(path, options = {}) {
|
|
493
|
-
only("Cloudflare checkChanges", options, [
|
|
494
|
-
"since",
|
|
495
|
-
"recursive",
|
|
496
|
-
"include",
|
|
497
|
-
"exclude",
|
|
498
|
-
"sessionId",
|
|
499
|
-
]);
|
|
500
|
-
const filters = watchFilters(options);
|
|
501
|
-
const session = await this.sessionFor(options.sessionId);
|
|
502
|
-
const r = await (await this.native()).exec([
|
|
503
|
-
"python3",
|
|
504
|
-
"-c",
|
|
505
|
-
CHANGES,
|
|
506
|
-
await session.resolve(path),
|
|
507
|
-
options.recursive === false ? "0" : "1",
|
|
508
|
-
JSON.stringify(filters.include ?? []),
|
|
509
|
-
JSON.stringify(filters.exclude ?? []),
|
|
510
|
-
]);
|
|
511
|
-
if (r.exitCode !== 0)
|
|
512
|
-
throw new Error(`checkChanges failed: ${r.stderr.trim()}`);
|
|
513
|
-
const version = r.stdout.trim();
|
|
514
|
-
const timestamp = new Date().toISOString();
|
|
515
|
-
if (options.since === undefined || options.since === version)
|
|
516
|
-
return {
|
|
517
|
-
success: true,
|
|
518
|
-
status: options.since === undefined ? "changed" : "unchanged",
|
|
519
|
-
version,
|
|
520
|
-
timestamp,
|
|
521
|
-
};
|
|
522
|
-
return options.since.split(".")[0] === version.split(".")[0]
|
|
523
|
-
? { success: true, status: "changed", version, timestamp }
|
|
524
|
-
: { success: true, status: "resync", reason: "restarted", version, timestamp };
|
|
525
|
-
}
|
|
526
|
-
/** Mounts an S3, R2 or Google Cloud Storage bucket with Runtime's mounts. A
|
|
527
|
-
* private bucket needs a Runtime secret: store its keys with
|
|
528
|
-
* runtime.secrets.set and mount with sandbox.mounts.add({ secret }). */
|
|
529
|
-
async mountBucket(bucket, mountPath, options) {
|
|
530
|
-
only("Cloudflare mountBucket", options, [
|
|
531
|
-
"endpoint",
|
|
532
|
-
"provider",
|
|
533
|
-
"credentials",
|
|
534
|
-
"readOnly",
|
|
535
|
-
"s3fsOptions",
|
|
536
|
-
"prefix",
|
|
537
|
-
"credentialProxy",
|
|
538
|
-
"localBucket",
|
|
539
|
-
]);
|
|
540
|
-
if (options.credentials || options.credentialProxy)
|
|
541
|
-
throw new CompatibilityError("Cloudflare", "credentials in mountBucket; store them with runtime.secrets.set and mount with sandbox.mounts.add({ secret })");
|
|
542
|
-
if (options.s3fsOptions?.length)
|
|
543
|
-
throw new CompatibilityError("Cloudflare", "s3fsOptions; Runtime mounts with its own client");
|
|
544
|
-
if (options.localBucket || options.endpoint === undefined)
|
|
545
|
-
throw new CompatibilityError("Cloudflare", "an R2 binding; give the bucket's endpoint, https://<account>.r2.cloudflarestorage.com");
|
|
546
|
-
if (!mountPath.startsWith("/"))
|
|
547
|
-
throw new InvalidMountConfigError(`Mount path must be absolute: ${mountPath}`);
|
|
548
|
-
let host;
|
|
549
|
-
try {
|
|
550
|
-
host = new URL(options.endpoint).hostname;
|
|
551
|
-
}
|
|
552
|
-
catch {
|
|
553
|
-
throw new InvalidMountConfigError(`Invalid endpoint URL: ${options.endpoint}`);
|
|
554
|
-
}
|
|
555
|
-
const r2 = /^([a-f0-9]{32})\.r2\.cloudflarestorage\.com$/.exec(host);
|
|
556
|
-
const provider = options.provider ?? (r2 ? "r2" : host === "storage.googleapis.com" ? "gcs" : "s3");
|
|
557
|
-
const prefix = options.prefix?.replace(/^\/+|\/+$/g, "") || undefined;
|
|
558
|
-
try {
|
|
559
|
-
await (await this.native()).mounts.add({
|
|
560
|
-
provider,
|
|
561
|
-
bucket,
|
|
562
|
-
path: mountPath,
|
|
563
|
-
...(prefix ? { prefix } : {}),
|
|
564
|
-
...(provider === "r2" && r2
|
|
565
|
-
? { accountId: r2[1] }
|
|
566
|
-
: provider === "gcs"
|
|
567
|
-
? {}
|
|
568
|
-
: { endpoint: `https://${host}` }),
|
|
569
|
-
...(options.readOnly !== undefined ? { readOnly: options.readOnly } : {}),
|
|
570
|
-
});
|
|
571
|
-
}
|
|
572
|
-
catch (error) {
|
|
573
|
-
throw new S3FSMountError(`Failed to mount ${bucket} at ${mountPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
async unmountBucket(mountPath) {
|
|
577
|
-
try {
|
|
578
|
-
await (await this.native()).mounts.remove(mountPath);
|
|
579
|
-
}
|
|
580
|
-
catch (error) {
|
|
581
|
-
throw new BucketUnmountError(`Failed to unmount ${mountPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
createCodeContext(options) {
|
|
585
|
-
return this.#interpreter.createCodeContext(options);
|
|
586
|
-
}
|
|
587
|
-
async runCode(code, options = {}) {
|
|
588
|
-
if (hasEnv(options.envVars))
|
|
589
|
-
throw new CompatibilityError("Cloudflare", RUN_ENV);
|
|
590
|
-
return (await this.#interpreter.runCode(code, options)).toJSON();
|
|
591
|
-
}
|
|
592
|
-
runCodeStream(code, options = {}) {
|
|
593
|
-
if (hasEnv(options.envVars))
|
|
594
|
-
throw new CompatibilityError("Cloudflare", RUN_ENV);
|
|
595
|
-
return this.#interpreter.runCodeStream(code, options);
|
|
596
|
-
}
|
|
597
|
-
listCodeContexts() {
|
|
598
|
-
return this.#interpreter.listCodeContexts();
|
|
599
|
-
}
|
|
600
|
-
deleteCodeContext(contextId) {
|
|
601
|
-
return this.#interpreter.deleteCodeContext(contextId);
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
refuse("Cloudflare", "Sandbox", Sandbox, {
|
|
605
|
-
client: "Runtime's API is the client; use the Sandbox methods",
|
|
606
|
-
tunnels: "use exposePort for a public URL",
|
|
607
|
-
callTunnels: "use exposePort for a public URL",
|
|
608
|
-
setSandboxName: "the name is getSandbox's id",
|
|
609
|
-
setContainerTimeouts: "Runtime starts a sandbox in under a second, with nothing to tune",
|
|
610
|
-
setTransport: "Runtime's API is the only transport",
|
|
611
|
-
onStart: "a Runtime sandbox has no Durable Object lifecycle hooks",
|
|
612
|
-
onStop: "a Runtime sandbox has no Durable Object lifecycle hooks",
|
|
613
|
-
onActivityExpired: "set sleepAfter or keepAlive instead",
|
|
614
|
-
fetch: "fetch the URL exposePort returns",
|
|
615
|
-
containerFetch: "fetch the URL exposePort returns",
|
|
616
|
-
wsConnect: "connect to the URL exposePort returns",
|
|
617
|
-
getContainerPlacementId: "a Runtime sandbox's id is its place",
|
|
618
|
-
validatePortToken: "Runtime checks preview tokens itself",
|
|
619
|
-
createBackup: "snapshot the sandbox with Runtime's snapshots, or keep the folder on a volume",
|
|
620
|
-
restoreBackup: "create a sandbox from a Runtime snapshot",
|
|
621
|
-
});
|
|
622
|
-
refuse("Cloudflare", "Sandbox", Sandbox, { onError: "a Runtime sandbox has no Durable Object lifecycle hooks" }, { sync: true });
|
|
623
|
-
/** Cloudflare's Worker-side terminal proxy has no Runtime counterpart. */
|
|
624
|
-
export async function proxyTerminal(..._args) {
|
|
625
|
-
throw new CompatibilityError("Cloudflare", "proxyTerminal; open a terminal with the native Runtime sandbox's terminal()");
|
|
626
|
-
}
|
|
627
|
-
/** Runtime previews are served by Runtime, not through your Worker. */
|
|
628
|
-
export async function proxyToSandbox(..._args) {
|
|
629
|
-
throw new CompatibilityError("Cloudflare", "proxyToSandbox; Runtime serves the URLs exposePort returns itself");
|
|
630
|
-
}
|
|
631
|
-
export class ExecutionSession {
|
|
632
|
-
sandbox;
|
|
633
|
-
id;
|
|
634
|
-
options;
|
|
635
|
-
constructor(sandbox, id, options) {
|
|
636
|
-
this.sandbox = sandbox;
|
|
637
|
-
this.id = id;
|
|
638
|
-
this.options = options;
|
|
639
|
-
}
|
|
640
|
-
async context(options = {}) {
|
|
641
|
-
only("Cloudflare exec", options, [
|
|
642
|
-
"timeout",
|
|
643
|
-
"env",
|
|
644
|
-
"cwd",
|
|
645
|
-
"encoding",
|
|
646
|
-
"stream",
|
|
647
|
-
"onOutput",
|
|
648
|
-
"onComplete",
|
|
649
|
-
"onError",
|
|
650
|
-
"signal",
|
|
651
|
-
"origin",
|
|
652
|
-
]);
|
|
653
|
-
if (options.origin !== undefined && !["user", "internal"].includes(options.origin))
|
|
654
|
-
throw new TypeError("origin is user or internal");
|
|
655
|
-
if (options.encoding && !["utf8", "utf-8"].includes(options.encoding))
|
|
656
|
-
throw new CompatibilityError("Cloudflare", `process encoding ${options.encoding}`);
|
|
657
|
-
const s = await this.sandbox.native();
|
|
658
|
-
return {
|
|
659
|
-
s,
|
|
660
|
-
options: await execOptions(s, {
|
|
661
|
-
cwd: options.cwd ?? this.options.cwd,
|
|
662
|
-
env: { ...cleanEnv(this.options.env), ...cleanEnv(options.env) },
|
|
663
|
-
timeoutMs: options.timeout ?? this.options.commandTimeoutMs,
|
|
664
|
-
signal: options.signal,
|
|
665
|
-
onStdout: (data) => {
|
|
666
|
-
if (options.stream)
|
|
667
|
-
options.onOutput?.("stdout", data);
|
|
668
|
-
},
|
|
669
|
-
onStderr: (data) => {
|
|
670
|
-
if (options.stream)
|
|
671
|
-
options.onOutput?.("stderr", data);
|
|
672
|
-
},
|
|
673
|
-
}),
|
|
674
|
-
};
|
|
675
|
-
}
|
|
676
|
-
async exec(command, options = {}) {
|
|
677
|
-
options.signal?.throwIfAborted();
|
|
678
|
-
const started = Date.now();
|
|
679
|
-
try {
|
|
680
|
-
const c = await this.context(options);
|
|
681
|
-
const r = await c.s.exec(await shellCommand(c.s, command, `cf:${this.id}`, {
|
|
682
|
-
env: { ...cleanEnv(this.options.env), ...cleanEnv(options.env) },
|
|
683
|
-
cwd: options.cwd,
|
|
684
|
-
}), c.options);
|
|
685
|
-
const result = {
|
|
686
|
-
success: r.exitCode === 0 && !r.timedOut,
|
|
687
|
-
exitCode: r.exitCode ?? -1,
|
|
688
|
-
stdout: r.stdout,
|
|
689
|
-
stderr: r.stderr,
|
|
690
|
-
command,
|
|
691
|
-
duration: Date.now() - started,
|
|
692
|
-
timestamp: new Date(started).toISOString(),
|
|
693
|
-
sessionId: this.id,
|
|
694
|
-
};
|
|
695
|
-
if (options.stream)
|
|
696
|
-
options.onComplete?.(result);
|
|
697
|
-
return result;
|
|
698
|
-
}
|
|
699
|
-
catch (e) {
|
|
700
|
-
options.onError?.(e instanceof Error ? e : new Error(String(e)));
|
|
701
|
-
throw e;
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
async execStream(command, options = {}) {
|
|
705
|
-
const encode = new TextEncoder();
|
|
706
|
-
const started = new Date().toISOString();
|
|
707
|
-
const run = this.exec.bind(this);
|
|
708
|
-
const sessionId = this.id;
|
|
709
|
-
const cancellation = new AbortController();
|
|
710
|
-
const signal = options.signal
|
|
711
|
-
? AbortSignal.any([options.signal, cancellation.signal])
|
|
712
|
-
: cancellation.signal;
|
|
713
|
-
return new ReadableStream({
|
|
714
|
-
async start(controller) {
|
|
715
|
-
const send = (event) => {
|
|
716
|
-
if (!signal.aborted)
|
|
717
|
-
controller.enqueue(encode.encode(`data: ${JSON.stringify(event)}\n\n`));
|
|
718
|
-
};
|
|
719
|
-
try {
|
|
720
|
-
send({ type: "start", command, timestamp: started, sessionId: sessionId });
|
|
721
|
-
const result = await run(command, {
|
|
722
|
-
...options,
|
|
723
|
-
signal,
|
|
724
|
-
stream: true,
|
|
725
|
-
onOutput: (type, data) => send({ type, data, timestamp: new Date().toISOString(), sessionId: sessionId }),
|
|
726
|
-
});
|
|
727
|
-
send({
|
|
728
|
-
type: "complete",
|
|
729
|
-
result,
|
|
730
|
-
exitCode: result.exitCode,
|
|
731
|
-
timestamp: new Date().toISOString(),
|
|
732
|
-
});
|
|
733
|
-
if (!cancellation.signal.aborted)
|
|
734
|
-
controller.close();
|
|
735
|
-
}
|
|
736
|
-
catch (error) {
|
|
737
|
-
if (!cancellation.signal.aborted)
|
|
738
|
-
controller.error(error);
|
|
739
|
-
}
|
|
740
|
-
},
|
|
741
|
-
cancel(reason) {
|
|
742
|
-
cancellation.abort(reason);
|
|
743
|
-
},
|
|
744
|
-
});
|
|
745
|
-
}
|
|
746
|
-
async startProcess(command, options = {}) {
|
|
747
|
-
only("Cloudflare startProcess", options, [
|
|
748
|
-
"timeout",
|
|
749
|
-
"env",
|
|
750
|
-
"cwd",
|
|
751
|
-
"encoding",
|
|
752
|
-
"sessionId",
|
|
753
|
-
"onOutput",
|
|
754
|
-
"onExit",
|
|
755
|
-
"onStart",
|
|
756
|
-
"onError",
|
|
757
|
-
]);
|
|
758
|
-
const c = await this.context({
|
|
759
|
-
timeout: options.timeout,
|
|
760
|
-
env: options.env,
|
|
761
|
-
cwd: options.cwd,
|
|
762
|
-
encoding: options.encoding,
|
|
763
|
-
});
|
|
764
|
-
const { stdin: _stdin, ...spawnOptions } = c.options;
|
|
765
|
-
const p = await c.s.spawn(await shellCommand(c.s, command, `cf:${this.id}`, {
|
|
766
|
-
env: { ...cleanEnv(this.options.env), ...cleanEnv(options.env) },
|
|
767
|
-
cwd: options.cwd,
|
|
768
|
-
persist: false,
|
|
769
|
-
}), spawnOptions);
|
|
770
|
-
const process = new Process(this.sandbox, p, this.id);
|
|
771
|
-
options.onStart?.(process);
|
|
772
|
-
if (options.onOutput || options.onExit || options.onError)
|
|
773
|
-
void (async () => {
|
|
774
|
-
for await (const e of p.output())
|
|
775
|
-
if (e.type === "stdout" || e.type === "stderr")
|
|
776
|
-
options.onOutput?.(e.type, e.data);
|
|
777
|
-
else if (e.type === "exit")
|
|
778
|
-
options.onExit?.(e.exitCode);
|
|
779
|
-
else if (e.type === "truncated")
|
|
780
|
-
throw new Error("Process output was truncated");
|
|
781
|
-
})().catch((e) => options.onError?.(e instanceof Error ? e : new Error(String(e))));
|
|
782
|
-
return process;
|
|
783
|
-
}
|
|
784
|
-
/** A path as this session sees it: relative paths from its working directory. */
|
|
785
|
-
resolve(path) {
|
|
786
|
-
return this.path(path);
|
|
787
|
-
}
|
|
788
|
-
async path(path) {
|
|
789
|
-
if (path.startsWith("/"))
|
|
790
|
-
return path;
|
|
791
|
-
let cwd = this.options.cwd ?? "/workspace";
|
|
792
|
-
try {
|
|
793
|
-
cwd = await (await this.sandbox.native()).files.readText(`${await shellRoot(`cf:${this.id}`)}/cwd`);
|
|
794
|
-
}
|
|
795
|
-
catch (error) {
|
|
796
|
-
if (!(error instanceof NotFoundError))
|
|
797
|
-
throw error;
|
|
798
|
-
}
|
|
799
|
-
return pathAt(path, cwd);
|
|
800
|
-
}
|
|
801
|
-
async writeFile(path, content, options = {}) {
|
|
802
|
-
if (options.encoding && !["utf8", "utf-8", "base64"].includes(options.encoding))
|
|
803
|
-
throw new CompatibilityError("Cloudflare", `file encoding ${options.encoding}`);
|
|
804
|
-
let bytes;
|
|
805
|
-
if (typeof content === "string" && options.encoding === "base64") {
|
|
806
|
-
// Cloudflare's container refuses anything but base64's alphabet.
|
|
807
|
-
if (!/^[A-Za-z0-9+/=]*$/.test(content))
|
|
808
|
-
throw new ValidationFailedError(response("VALIDATION_FAILED", `Invalid base64 content for '${path}': must contain only A-Z, a-z, 0-9, +, /, =`, {
|
|
809
|
-
validationErrors: [
|
|
810
|
-
{ field: "content", message: "Invalid base64 characters", code: "INVALID_BASE64" },
|
|
811
|
-
],
|
|
812
|
-
}, { httpStatus: 400 }));
|
|
813
|
-
bytes = Uint8Array.from(atob(content), (c) => c.charCodeAt(0));
|
|
814
|
-
}
|
|
815
|
-
else if (typeof content === "string")
|
|
816
|
-
bytes = new TextEncoder().encode(content);
|
|
817
|
-
else {
|
|
818
|
-
const buffer = await new Response(content).arrayBuffer();
|
|
819
|
-
bytes = new Uint8Array(buffer);
|
|
820
|
-
}
|
|
821
|
-
await (await this.sandbox.native()).files.write(await this.path(path), bytes);
|
|
822
|
-
return { success: true, path, bytesWritten: bytes.length, timestamp: new Date().toISOString() };
|
|
823
|
-
}
|
|
824
|
-
async readFile(path, options = {}) {
|
|
825
|
-
const s = await this.sandbox.native(), resolved = await this.path(path), bytes = await s.files.read(resolved), type = mimeType(resolved, bytes);
|
|
826
|
-
if (options.encoding === "none")
|
|
827
|
-
return {
|
|
828
|
-
success: true,
|
|
829
|
-
path,
|
|
830
|
-
content: new ReadableStream({
|
|
831
|
-
start(c) {
|
|
832
|
-
c.enqueue(bytes);
|
|
833
|
-
c.close();
|
|
834
|
-
},
|
|
835
|
-
}),
|
|
836
|
-
size: bytes.length,
|
|
837
|
-
mimeType: type,
|
|
838
|
-
timestamp: new Date().toISOString(),
|
|
839
|
-
};
|
|
840
|
-
const text = new TextDecoder().decode(bytes), binary = text.includes("\0") || text.includes("\ufffd"), encoded = options.encoding === "base64" || (options.encoding === undefined && binary);
|
|
841
|
-
let raw = "";
|
|
842
|
-
if (encoded)
|
|
843
|
-
for (let i = 0; i < bytes.length; i += 32768)
|
|
844
|
-
raw += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
845
|
-
return {
|
|
846
|
-
success: true,
|
|
847
|
-
path,
|
|
848
|
-
content: encoded ? btoa(raw) : text,
|
|
849
|
-
encoding: encoded ? "base64" : "utf-8",
|
|
850
|
-
isBinary: binary,
|
|
851
|
-
mimeType: type,
|
|
852
|
-
size: bytes.length,
|
|
853
|
-
timestamp: new Date().toISOString(),
|
|
854
|
-
};
|
|
855
|
-
}
|
|
856
|
-
async mkdir(path, options = {}) {
|
|
857
|
-
await (await this.sandbox.native()).files.mkdir(await this.path(path), { parents: options.recursive });
|
|
858
|
-
return { success: true, path, timestamp: new Date().toISOString() };
|
|
859
|
-
}
|
|
860
|
-
async deleteFile(path) {
|
|
861
|
-
await (await this.sandbox.native()).files.remove(await this.path(path));
|
|
862
|
-
return { success: true, path, timestamp: new Date().toISOString() };
|
|
863
|
-
}
|
|
864
|
-
async renameFile(path, newPath) {
|
|
865
|
-
await (await this.sandbox.native()).files.rename(await this.path(path), await this.path(newPath));
|
|
866
|
-
return { success: true, path, newPath, timestamp: new Date().toISOString() };
|
|
867
|
-
}
|
|
868
|
-
async exists(path) {
|
|
869
|
-
return {
|
|
870
|
-
success: true,
|
|
871
|
-
path,
|
|
872
|
-
exists: await (await this.sandbox.native()).files.exists(await this.path(path)),
|
|
873
|
-
timestamp: new Date().toISOString(),
|
|
874
|
-
};
|
|
875
|
-
}
|
|
876
|
-
async listFiles(path, options = {}) {
|
|
877
|
-
const s = await this.sandbox.native();
|
|
878
|
-
const directory = await this.path(path);
|
|
879
|
-
const entries = await s.files.list(directory, { hidden: options.includeHidden });
|
|
880
|
-
if (options.recursive) {
|
|
881
|
-
for (let i = 0; i < entries.length; i++)
|
|
882
|
-
if (entries[i].type === "directory")
|
|
883
|
-
entries.push(...(await s.files.list(entries[i].path, { hidden: options.includeHidden })));
|
|
884
|
-
}
|
|
885
|
-
const files = entries.map((e) => ({
|
|
886
|
-
...e,
|
|
887
|
-
absolutePath: e.path,
|
|
888
|
-
relativePath: e.path.slice(directory.replace(/\/$/, "").length + 1),
|
|
889
|
-
permissions: {
|
|
890
|
-
readable: !!(parseInt(e.mode, 8) & 0o444),
|
|
891
|
-
writable: !!(parseInt(e.mode, 8) & 0o222),
|
|
892
|
-
executable: !!(parseInt(e.mode, 8) & 0o111),
|
|
893
|
-
},
|
|
894
|
-
}));
|
|
895
|
-
return { success: true, path, files, count: files.length, timestamp: new Date().toISOString() };
|
|
896
|
-
}
|
|
897
|
-
async setEnvVars(env) {
|
|
898
|
-
this.options = { ...this.options, env: { ...this.options.env, ...cleanEnv(env) } };
|
|
899
|
-
await (await this.sandbox.native()).files.write(`/workspace/.runtime-compat/cloudflare/${encodeURIComponent(this.id)}.json`, JSON.stringify(this.options), { mode: 0o600 });
|
|
900
|
-
}
|
|
901
|
-
// The rest of Cloudflare's ExecutionSession: the sandbox's calls, in this session.
|
|
902
|
-
moveFile(path, newPath) {
|
|
903
|
-
return this.renameFile(path, newPath);
|
|
904
|
-
}
|
|
905
|
-
readFileStream(path) {
|
|
906
|
-
return this.sandbox.readFileStream(path, { sessionId: this.id });
|
|
907
|
-
}
|
|
908
|
-
gitCheckout(repoUrl, options = {}) {
|
|
909
|
-
return this.sandbox.gitCheckout(repoUrl, { ...options, sessionId: this.id });
|
|
910
|
-
}
|
|
911
|
-
watch(path, options = {}) {
|
|
912
|
-
return this.sandbox.watch(path, { ...options, sessionId: this.id });
|
|
913
|
-
}
|
|
914
|
-
checkChanges(path, options = {}) {
|
|
915
|
-
return this.sandbox.checkChanges(path, { ...options, sessionId: this.id });
|
|
916
|
-
}
|
|
917
|
-
listProcesses() {
|
|
918
|
-
return this.sandbox.listProcesses();
|
|
919
|
-
}
|
|
920
|
-
getProcess(id) {
|
|
921
|
-
return this.sandbox.getProcess(id);
|
|
922
|
-
}
|
|
923
|
-
killProcess(id, signal) {
|
|
924
|
-
return this.sandbox.killProcess(id, signal);
|
|
925
|
-
}
|
|
926
|
-
killAllProcesses() {
|
|
927
|
-
return this.sandbox.killAllProcesses();
|
|
928
|
-
}
|
|
929
|
-
cleanupCompletedProcesses() {
|
|
930
|
-
return this.sandbox.cleanupCompletedProcesses();
|
|
931
|
-
}
|
|
932
|
-
getProcessLogs(id) {
|
|
933
|
-
return this.sandbox.getProcessLogs(id);
|
|
934
|
-
}
|
|
935
|
-
streamProcessLogs(id, options) {
|
|
936
|
-
return this.sandbox.streamProcessLogs(id, options);
|
|
937
|
-
}
|
|
938
|
-
createCodeContext(options) {
|
|
939
|
-
return this.sandbox.createCodeContext(options);
|
|
940
|
-
}
|
|
941
|
-
runCode(code, options) {
|
|
942
|
-
return this.sandbox.runCode(code, options);
|
|
943
|
-
}
|
|
944
|
-
runCodeStream(code, options) {
|
|
945
|
-
return this.sandbox.runCodeStream(code, options);
|
|
946
|
-
}
|
|
947
|
-
listCodeContexts() {
|
|
948
|
-
return this.sandbox.listCodeContexts();
|
|
949
|
-
}
|
|
950
|
-
deleteCodeContext(contextId) {
|
|
951
|
-
return this.sandbox.deleteCodeContext(contextId);
|
|
952
|
-
}
|
|
953
|
-
mountBucket(bucket, mountPath, options) {
|
|
954
|
-
return this.sandbox.mountBucket(bucket, mountPath, options);
|
|
955
|
-
}
|
|
956
|
-
unmountBucket(mountPath) {
|
|
957
|
-
return this.sandbox.unmountBucket(mountPath);
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
refuse("Cloudflare", "ExecutionSession", ExecutionSession, {
|
|
961
|
-
terminal: "open a terminal with the native Runtime sandbox's terminal()",
|
|
962
|
-
});
|
|
963
|
-
export class Process {
|
|
964
|
-
sandbox;
|
|
965
|
-
native;
|
|
966
|
-
sessionId;
|
|
967
|
-
constructor(sandbox, native, sessionId) {
|
|
968
|
-
this.sandbox = sandbox;
|
|
969
|
-
this.native = native;
|
|
970
|
-
this.sessionId = sessionId;
|
|
971
|
-
}
|
|
972
|
-
get id() {
|
|
973
|
-
return this.native.id;
|
|
974
|
-
}
|
|
975
|
-
get command() {
|
|
976
|
-
return this.native.info.command;
|
|
977
|
-
}
|
|
978
|
-
get status() {
|
|
979
|
-
return this.native.info.state === "exited"
|
|
980
|
-
? this.native.info.exitCode === 0
|
|
981
|
-
? "completed"
|
|
982
|
-
: "failed"
|
|
983
|
-
: this.native.info.state === "timed_out"
|
|
984
|
-
? "failed"
|
|
985
|
-
: this.native.info.state;
|
|
986
|
-
}
|
|
987
|
-
get startTime() {
|
|
988
|
-
return new Date(this.native.info.startedAt);
|
|
989
|
-
}
|
|
990
|
-
get endTime() {
|
|
991
|
-
return this.native.info.endedAt ? new Date(this.native.info.endedAt) : undefined;
|
|
992
|
-
}
|
|
993
|
-
get exitCode() {
|
|
994
|
-
return this.native.info.exitCode ?? undefined;
|
|
995
|
-
}
|
|
996
|
-
async kill(signal = "SIGTERM") {
|
|
997
|
-
if (!["SIGTERM", "SIGKILL", "SIGINT", "SIGHUP", "SIGQUIT", "SIGUSR1", "SIGUSR2"].includes(signal))
|
|
998
|
-
throw new TypeError(`Unsupported signal ${signal}`);
|
|
999
|
-
await this.native.kill(signal);
|
|
1000
|
-
}
|
|
1001
|
-
async getStatus() {
|
|
1002
|
-
await this.native.refresh();
|
|
1003
|
-
return this.status;
|
|
1004
|
-
}
|
|
1005
|
-
getLogs() {
|
|
1006
|
-
return this.sandbox.getProcessLogs(this.id);
|
|
1007
|
-
}
|
|
1008
|
-
async waitForExit(timeout) {
|
|
1009
|
-
const r = await this.native.wait({
|
|
1010
|
-
signal: timeout ? timeoutSignal(timeout) : undefined,
|
|
1011
|
-
});
|
|
1012
|
-
return { exitCode: r.exitCode ?? -1 };
|
|
1013
|
-
}
|
|
1014
|
-
/** Cloudflare's errors for a wait, with its messages: a timeout, or the
|
|
1015
|
-
* process ending first. */
|
|
1016
|
-
#timedOut(condition, timeout) {
|
|
1017
|
-
return new ProcessReadyTimeoutError(response("PROCESS_READY_TIMEOUT", `Process did not become ready within ${timeout}ms. Waiting for: ${condition}`, { processId: this.id, command: this.command, condition, timeout }, {
|
|
1018
|
-
httpStatus: 408,
|
|
1019
|
-
suggestion: `Check if your process outputs ${condition}. You can increase the timeout parameter.`,
|
|
1020
|
-
}));
|
|
1021
|
-
}
|
|
1022
|
-
#exited(condition, exitCode) {
|
|
1023
|
-
return new ProcessExitedBeforeReadyError(response("PROCESS_EXITED_BEFORE_READY", `Process exited with code ${exitCode} before becoming ready. Waiting for: ${condition}`, { processId: this.id, command: this.command, condition, exitCode }, { httpStatus: 500, suggestion: "Check process logs with getLogs() for error messages" }));
|
|
1024
|
-
}
|
|
1025
|
-
/** Waits as Cloudflare does: with no limit unless `timeout` is given. */
|
|
1026
|
-
async #within(condition, timeout, wait) {
|
|
1027
|
-
if (timeout === undefined)
|
|
1028
|
-
return wait();
|
|
1029
|
-
const signal = timeoutSignal(timeout);
|
|
1030
|
-
try {
|
|
1031
|
-
return await wait(signal);
|
|
1032
|
-
}
|
|
1033
|
-
catch (error) {
|
|
1034
|
-
if (!signal.aborted)
|
|
1035
|
-
throw error;
|
|
1036
|
-
throw this.#timedOut(condition, timeout);
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
async waitForLog(pattern, timeout) {
|
|
1040
|
-
const condition = typeof pattern === "string" ? `"${pattern}"` : pattern.toString();
|
|
1041
|
-
return this.#within(condition, timeout, (signal) => this.#waitForLog(pattern, condition, signal));
|
|
1042
|
-
}
|
|
1043
|
-
async #waitForLog(pattern, condition, signal) {
|
|
1044
|
-
let stdout = "", stderr = "";
|
|
1045
|
-
for await (const e of this.native.output({ signal })) {
|
|
1046
|
-
if (e.type === "stdout")
|
|
1047
|
-
stdout += e.data;
|
|
1048
|
-
else if (e.type === "stderr")
|
|
1049
|
-
stderr += e.data;
|
|
1050
|
-
const found = matchLine(stdout, pattern) ?? matchLine(stderr, pattern);
|
|
1051
|
-
if (found)
|
|
1052
|
-
return found;
|
|
1053
|
-
if (e.type === "exit")
|
|
1054
|
-
throw this.#exited(condition, e.exitCode ?? 1);
|
|
1055
|
-
}
|
|
1056
|
-
throw this.#exited(condition, 0);
|
|
1057
|
-
}
|
|
1058
|
-
/** Resolves once the port answers: an HTTP status in range (200-399 by
|
|
1059
|
-
* default) at `path`, or a TCP connection in `tcp` mode. Throws if the
|
|
1060
|
-
* process ends first, or after `timeout` milliseconds when one is given. */
|
|
1061
|
-
async waitForPort(port, options = {}) {
|
|
1062
|
-
only("Cloudflare waitForPort", options, ["mode", "path", "status", "timeout", "interval"]);
|
|
1063
|
-
const { mode = "http", path = "/", status = { min: 200, max: 399 }, interval = 500 } = options;
|
|
1064
|
-
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
1065
|
-
throw new TypeError(`Invalid port number: ${port}`);
|
|
1066
|
-
if (mode !== "http" && mode !== "tcp")
|
|
1067
|
-
throw new TypeError("mode is http or tcp");
|
|
1068
|
-
const condition = mode === "http" ? `port ${port} (HTTP ${path})` : `port ${port} (TCP)`;
|
|
1069
|
-
const min = typeof status === "number" ? status : status.min;
|
|
1070
|
-
const max = typeof status === "number" ? status : status.max;
|
|
1071
|
-
const url = `http://127.0.0.1:${port}${path.startsWith("/") ? path : `/${path}`}`;
|
|
1072
|
-
const probe = mode === "http"
|
|
1073
|
-
? `code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 2 ${quote(url)}) && [ "$code" -ge ${min} ] && [ "$code" -le ${max} ]`
|
|
1074
|
-
: `exec 3<>/dev/tcp/127.0.0.1/${port}`;
|
|
1075
|
-
const s = await this.sandbox.native();
|
|
1076
|
-
await this.#within(condition, options.timeout, async (signal) => {
|
|
1077
|
-
for (;;) {
|
|
1078
|
-
await this.native.refresh();
|
|
1079
|
-
if (this.native.info.state !== "running")
|
|
1080
|
-
throw this.#exited(condition, this.native.info.exitCode ?? 1);
|
|
1081
|
-
if ((await s.exec(["bash", "-c", probe], { signal })).exitCode === 0)
|
|
1082
|
-
return;
|
|
1083
|
-
await new Promise((done) => setTimeout(done, interval));
|
|
1084
|
-
signal?.throwIfAborted();
|
|
1085
|
-
}
|
|
1086
|
-
});
|
|
1087
|
-
}
|
|
1088
|
-
}
|
|
1089
|
-
/** The first line holding a match, as Cloudflare's waitForLog answers. */
|
|
1090
|
-
function matchLine(text, pattern) {
|
|
1091
|
-
if (typeof pattern === "string") {
|
|
1092
|
-
if (!text.includes(pattern))
|
|
1093
|
-
return null;
|
|
1094
|
-
return { line: text.split("\n").find((line) => line.includes(pattern)) ?? pattern };
|
|
1095
|
-
}
|
|
1096
|
-
const safe = new RegExp(pattern.source, pattern.flags.replace("g", ""));
|
|
1097
|
-
const match = text.match(safe);
|
|
1098
|
-
if (!match)
|
|
1099
|
-
return null;
|
|
1100
|
-
for (const line of text.split("\n")) {
|
|
1101
|
-
const lineMatch = line.match(safe);
|
|
1102
|
-
if (lineMatch)
|
|
1103
|
-
return { line, match: lineMatch };
|
|
1104
|
-
}
|
|
1105
|
-
return { line: match[0], match };
|
|
1106
|
-
}
|