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/freestyle/index.js
DELETED
|
@@ -1,1187 +0,0 @@
|
|
|
1
|
-
import { ConnectionError, RuntimeError } from "../errors.js";
|
|
2
|
-
import { all, client, CompatibilityError, create, destroy, execOptions, lookup, only, quote, ready, refuse, refused, validateEnvironment, } from "../compat/core.js";
|
|
3
|
-
import { GuestFiles } from "../compat/files.js";
|
|
4
|
-
export { CompatibilityError };
|
|
5
|
-
/** What every failed Runtime call becomes, as Freestyle throws it for a
|
|
6
|
-
* non-2xx response: `code` is Runtime's error code in upper case
|
|
7
|
-
* (`NOT_FOUND`), `status` its HTTP status, and `cause` the RuntimeError. */
|
|
8
|
-
export class FreestyleApiError extends Error {
|
|
9
|
-
code;
|
|
10
|
-
status;
|
|
11
|
-
path;
|
|
12
|
-
constructor(status, body, path) {
|
|
13
|
-
super(body.message || `Freestyle API error: ${body.code}`);
|
|
14
|
-
this.name = "FreestyleApiError";
|
|
15
|
-
this.code = body.code;
|
|
16
|
-
this.status = status;
|
|
17
|
-
this.path = path;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
/** What a call that got no answer from Runtime becomes, as Freestyle 0.2.17
|
|
21
|
-
* throws a failed fetch: a TypeError whose `cause` is the RuntimeError
|
|
22
|
-
* ConnectionError and whose `errors` carry it as one entry. */
|
|
23
|
-
export class FreestyleTransportError extends TypeError {
|
|
24
|
-
errors;
|
|
25
|
-
constructor(cause, errors) {
|
|
26
|
-
super(`${cause instanceof Error ? cause.message : "fetch failed"} (${errors.map((e) => e.code ?? e.name ?? "Error").join("; ")})`, { cause });
|
|
27
|
-
this.name = "FreestyleTransportError";
|
|
28
|
-
this.errors = errors;
|
|
29
|
-
}
|
|
30
|
-
toJSON() {
|
|
31
|
-
return { name: this.name, message: this.message, errors: this.errors };
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/** A Runtime error as Freestyle's client throws it. */
|
|
35
|
-
function translate(error) {
|
|
36
|
-
if (error instanceof ConnectionError)
|
|
37
|
-
return new FreestyleTransportError(error, [
|
|
38
|
-
{ name: error.name, message: error.message, code: error.code },
|
|
39
|
-
]);
|
|
40
|
-
if (!(error instanceof RuntimeError))
|
|
41
|
-
return error;
|
|
42
|
-
const translated = new FreestyleApiError(error.status, {
|
|
43
|
-
code: error.code.toUpperCase(),
|
|
44
|
-
message: error.message,
|
|
45
|
-
});
|
|
46
|
-
Object.defineProperty(translated, "cause", { value: error });
|
|
47
|
-
return translated;
|
|
48
|
-
}
|
|
49
|
-
async function api(call) {
|
|
50
|
-
try {
|
|
51
|
-
return await call();
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
throw translate(error);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const notFound = (what) => new FreestyleApiError(404, { code: "NOT_FOUND", message: `${what} not found` });
|
|
58
|
-
export class FirewallRuleValidationError extends Error {
|
|
59
|
-
path;
|
|
60
|
-
constructor(path, message) {
|
|
61
|
-
super(message);
|
|
62
|
-
this.path = path;
|
|
63
|
-
this.name = "FirewallRuleValidationError";
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
export class TlsRuleValidationError extends Error {
|
|
67
|
-
path;
|
|
68
|
-
constructor(path, message) {
|
|
69
|
-
super(message);
|
|
70
|
-
this.path = path;
|
|
71
|
-
this.name = "TlsRuleValidationError";
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
export class TlsForwardAuthValidationError extends Error {
|
|
75
|
-
path;
|
|
76
|
-
constructor(path, message) {
|
|
77
|
-
super(message);
|
|
78
|
-
this.path = path;
|
|
79
|
-
this.name = "TlsForwardAuthValidationError";
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
// Freestyle products Runtime does not sell as they are --------------------------
|
|
83
|
-
const FIREWALL = "give a VM's outbound rules at create (firewall.rules to public or CIDR destinations), or use sandbox.network";
|
|
84
|
-
const TLS = "serve HTTPS from a sandbox with Runtime previews (sandbox.previews)";
|
|
85
|
-
const TUNNELS = "reach a sandbox's ports with sandbox.tunnel()";
|
|
86
|
-
const VPC = "let sandboxes reach each other with Runtime private networks (runtime.networks.private)";
|
|
87
|
-
export const FirewallNamespace = refused("Freestyle", "FirewallNamespace", FIREWALL);
|
|
88
|
-
export const FirewallRulesNamespace = refused("Freestyle", "FirewallRulesNamespace", FIREWALL);
|
|
89
|
-
export const validateFirewallEndpoint = refused("Freestyle", "validateFirewallEndpoint", FIREWALL);
|
|
90
|
-
export const validateFirewallRule = refused("Freestyle", "validateFirewallRule", FIREWALL);
|
|
91
|
-
export const validateFirewallSpec = refused("Freestyle", "validateFirewallSpec", FIREWALL);
|
|
92
|
-
export const TlsNamespace = refused("Freestyle", "TlsNamespace", TLS);
|
|
93
|
-
export const TlsRulesNamespace = refused("Freestyle", "TlsRulesNamespace", TLS);
|
|
94
|
-
export const TlsForwardAuthNamespace = refused("Freestyle", "TlsForwardAuthNamespace", TLS);
|
|
95
|
-
export const hasTlsIdentity = refused("Freestyle", "hasTlsIdentity", TLS);
|
|
96
|
-
export const resolveTlsProtocol = refused("Freestyle", "resolveTlsProtocol", TLS);
|
|
97
|
-
export const validateTlsDomain = refused("Freestyle", "validateTlsDomain", TLS);
|
|
98
|
-
export const validateTlsEndpoint = refused("Freestyle", "validateTlsEndpoint", TLS);
|
|
99
|
-
export const validateTlsForwardAuth = refused("Freestyle", "validateTlsForwardAuth", TLS);
|
|
100
|
-
export const validateTlsMailRoute = refused("Freestyle", "validateTlsMailRoute", TLS);
|
|
101
|
-
export const validateTlsRule = refused("Freestyle", "validateTlsRule", TLS);
|
|
102
|
-
export const validateTlsSpec = refused("Freestyle", "validateTlsSpec", TLS);
|
|
103
|
-
export const TunnelsNamespace = refused("Freestyle", "TunnelsNamespace", TUNNELS);
|
|
104
|
-
export const VpcNamespace = refused("Freestyle", "VpcNamespace", VPC);
|
|
105
|
-
export const Vpc = refused("Freestyle", "Vpc", VPC);
|
|
106
|
-
export const VpcTunnelNamespace = refused("Freestyle", "VpcTunnelNamespace", VPC);
|
|
107
|
-
const ADDRESS = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(?:\/(\d{1,2}))?$/;
|
|
108
|
-
/** An IPv4 address or CIDR, as Runtime's network allow list takes it. */
|
|
109
|
-
function cidrOf(value) {
|
|
110
|
-
const match = ADDRESS.exec(value);
|
|
111
|
-
if (!match || match.slice(1, 5).some((octet) => Number(octet) > 255))
|
|
112
|
-
return undefined;
|
|
113
|
-
if (match[5] !== undefined && Number(match[5]) > 32)
|
|
114
|
-
return undefined;
|
|
115
|
-
return match[5] === undefined ? `${value}/32` : value;
|
|
116
|
-
}
|
|
117
|
-
/** The network settings for a VM's inline rules: every rule allows this VM
|
|
118
|
-
* outbound, to the public internet or to a CIDR, over the web ports Runtime
|
|
119
|
-
* opens (any, 80 or 443 over TCP). Runtime opens 80 and 443 together, so a
|
|
120
|
-
* destination named with one port alone refuses rather than gets the other.
|
|
121
|
-
* Anything else refuses before allocation. */
|
|
122
|
-
function networkFor(rules) {
|
|
123
|
-
const allow = [];
|
|
124
|
-
let open = false;
|
|
125
|
-
const ports = new Map();
|
|
126
|
-
for (const [index, rule] of rules.entries()) {
|
|
127
|
-
only("Freestyle firewall rule", rule, ["action", "source", "destination", "description"]);
|
|
128
|
-
const { destination } = rule;
|
|
129
|
-
if (rule.action !== "allow" || Object.keys(rule.source ?? {}).length)
|
|
130
|
-
throw new CompatibilityError("Freestyle", `firewall.rules[${index}]: rules other than this VM's outbound access`);
|
|
131
|
-
only("Freestyle firewall destination", destination ?? {}, [
|
|
132
|
-
"public",
|
|
133
|
-
"cidr",
|
|
134
|
-
"port",
|
|
135
|
-
"protocol",
|
|
136
|
-
]);
|
|
137
|
-
if ((destination.port !== undefined && destination.port !== 80 && destination.port !== 443) ||
|
|
138
|
-
(destination.protocol !== undefined && destination.protocol !== "tcp") ||
|
|
139
|
-
(destination.port !== undefined && destination.protocol === undefined))
|
|
140
|
-
throw new CompatibilityError("Freestyle", `firewall.rules[${index}]: ports other than 80 and 443 over TCP; open host:port pairs with sandbox.network.set({ connect })`);
|
|
141
|
-
let target = "public";
|
|
142
|
-
if (destination.public === true && destination.cidr === undefined)
|
|
143
|
-
open = true;
|
|
144
|
-
else if (destination.public === undefined && typeof destination.cidr === "string") {
|
|
145
|
-
const cidr = cidrOf(destination.cidr);
|
|
146
|
-
if (!cidr)
|
|
147
|
-
throw new FirewallRuleValidationError(`firewall.rules[${index}].destination.cidr`, `firewall.rules[${index}].destination.cidr \`${destination.cidr}\` is not an IPv4 CIDR`);
|
|
148
|
-
if (!allow.includes(cidr))
|
|
149
|
-
allow.push(cidr);
|
|
150
|
-
target = cidr;
|
|
151
|
-
}
|
|
152
|
-
else
|
|
153
|
-
throw new CompatibilityError("Freestyle", `firewall.rules[${index}]: destinations other than public or one CIDR`);
|
|
154
|
-
ports.set(target, (ports.get(target) ?? new Set()).add(destination.port));
|
|
155
|
-
}
|
|
156
|
-
for (const [target, named] of ports)
|
|
157
|
-
if (!named.has(undefined) && !(named.has(80) && named.has(443)))
|
|
158
|
-
throw new CompatibilityError("Freestyle", `firewall rules opening ${target} on port ${[...named].join(" or ")} alone; Runtime opens 80 and 443 together`);
|
|
159
|
-
if (open)
|
|
160
|
-
return { internet: true };
|
|
161
|
-
if (allow.length)
|
|
162
|
-
return { internet: true, allow };
|
|
163
|
-
return { internet: false };
|
|
164
|
-
}
|
|
165
|
-
function ruleData(vmId, index, destination, at) {
|
|
166
|
-
return {
|
|
167
|
-
id: `runtime-network-${vmId}-${index}`,
|
|
168
|
-
action: "allow",
|
|
169
|
-
source: { vmId },
|
|
170
|
-
destination,
|
|
171
|
-
description: null,
|
|
172
|
-
dependencies: [],
|
|
173
|
-
createdAt: at,
|
|
174
|
-
updatedAt: at,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
function validateMetadata(metadata) {
|
|
178
|
-
for (const key of ["compat.provider", "fs.displayName"])
|
|
179
|
-
if (metadata !== undefined && Object.prototype.hasOwnProperty.call(metadata, key))
|
|
180
|
-
throw new CompatibilityError("Freestyle", `metadata key ${key} reserved by this adapter`);
|
|
181
|
-
}
|
|
182
|
-
const LINUX_USER = /^[a-z_][a-z0-9_-]{0,31}$/;
|
|
183
|
-
function validateLinuxUser(user) {
|
|
184
|
-
if (typeof user !== "string" || !LINUX_USER.test(user))
|
|
185
|
-
throw new TypeError("linuxUser must be a Linux user name: lowercase letters, digits, _ and -");
|
|
186
|
-
return user;
|
|
187
|
-
}
|
|
188
|
-
/** Runs `command` as `user` through sudo. The environment travels in the
|
|
189
|
-
* exec's own environment and sudo keeps it by name: values never enter the
|
|
190
|
-
* command line, which the process list and Runtime's records show. */
|
|
191
|
-
function asUser(user, command, env = {}) {
|
|
192
|
-
validateEnvironment(env);
|
|
193
|
-
const names = Object.keys(env);
|
|
194
|
-
const keep = names.length ? ` --preserve-env=${names.join(",")}` : "";
|
|
195
|
-
return `exec sudo -n -H${keep} -u ${quote(user)} -- bash -c ${quote(command)}`;
|
|
196
|
-
}
|
|
197
|
-
const data = (s) => ({
|
|
198
|
-
id: s.id,
|
|
199
|
-
state: s.state,
|
|
200
|
-
slug: s.info.name,
|
|
201
|
-
displayName: s.info.labels["fs.displayName"] ?? null,
|
|
202
|
-
resources: { cpu: s.info.vcpu, memory: s.info.memoryMiB, storage: s.info.diskMiB },
|
|
203
|
-
metadata: Object.fromEntries(Object.entries(s.info.labels).filter(([k]) => !["compat.provider", "fs.displayName"].includes(k))),
|
|
204
|
-
idleTimeoutSeconds: s.info.idlePauseSeconds === 0 ? -1 : s.info.idlePauseSeconds,
|
|
205
|
-
vpcs: [],
|
|
206
|
-
networks: [],
|
|
207
|
-
createdAt: s.info.createdAt,
|
|
208
|
-
updatedAt: s.info.createdAt,
|
|
209
|
-
});
|
|
210
|
-
/** Labels the snapshot API returns beside the typed fields. */
|
|
211
|
-
const labelsOf = (s) => s.labels ?? {};
|
|
212
|
-
const snapshotData = (s) => ({
|
|
213
|
-
id: s.id,
|
|
214
|
-
sourceVmId: s.sourceSandboxId,
|
|
215
|
-
slug: s.name,
|
|
216
|
-
displayName: labelsOf(s)["fs.displayName"] ?? null,
|
|
217
|
-
public: false,
|
|
218
|
-
createdAt: s.createdAt,
|
|
219
|
-
updatedAt: s.createdAt,
|
|
220
|
-
ttlSeconds: s.retentionDays === null ? null : s.retentionDays * 86400,
|
|
221
|
-
});
|
|
222
|
-
/** Freestyle keeps a snapshot for a time in seconds; Runtime for whole days. */
|
|
223
|
-
function retentionDays(ttlSeconds) {
|
|
224
|
-
if (ttlSeconds === undefined)
|
|
225
|
-
return undefined;
|
|
226
|
-
if (!(ttlSeconds > 0) ||
|
|
227
|
-
ttlSeconds > 365 * 86400 ||
|
|
228
|
-
ttlSeconds % 86400 !== 0 ||
|
|
229
|
-
!Number.isFinite(ttlSeconds))
|
|
230
|
-
throw new CompatibilityError("Freestyle", "snapshot retention outside 1 to 365 whole days");
|
|
231
|
-
return ttlSeconds / 86400;
|
|
232
|
-
}
|
|
233
|
-
export class Freestyle {
|
|
234
|
-
vms;
|
|
235
|
-
constructor(options = {}) {
|
|
236
|
-
const { teamId, stackAccessToken, identityAccessToken, ...rest } = options;
|
|
237
|
-
if (teamId !== undefined)
|
|
238
|
-
throw new CompatibilityError("Freestyle", "teamId; a Runtime API key is its organization's");
|
|
239
|
-
if (stackAccessToken !== undefined || identityAccessToken !== undefined)
|
|
240
|
-
throw new CompatibilityError("Freestyle", "stackAccessToken and identityAccessToken; sign in with a Runtime API key");
|
|
241
|
-
this.vms = new VmsNamespace(client(rest));
|
|
242
|
-
}
|
|
243
|
-
get firewall() {
|
|
244
|
-
throw new CompatibilityError("Freestyle", `freestyle.firewall; ${FIREWALL}`);
|
|
245
|
-
}
|
|
246
|
-
get tls() {
|
|
247
|
-
throw new CompatibilityError("Freestyle", `freestyle.tls; ${TLS}`);
|
|
248
|
-
}
|
|
249
|
-
get tunnels() {
|
|
250
|
-
throw new CompatibilityError("Freestyle", `freestyle.tunnels; ${TUNNELS}`);
|
|
251
|
-
}
|
|
252
|
-
get vpc() {
|
|
253
|
-
throw new CompatibilityError("Freestyle", `freestyle.vpc; ${VPC}`);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
refuse("Freestyle", "Freestyle", Freestyle, {
|
|
257
|
-
fetch: "call Runtime's API with its own client (new Runtime())",
|
|
258
|
-
});
|
|
259
|
-
export class VmSnapshotsNamespace {
|
|
260
|
-
runtime;
|
|
261
|
-
constructor(runtime) {
|
|
262
|
-
this.runtime = runtime;
|
|
263
|
-
}
|
|
264
|
-
/** A snapshot by id or by its slug (Runtime's snapshot name). */
|
|
265
|
-
async resolve(id) {
|
|
266
|
-
if (/^[a-f0-9-]{36}$/i.test(id))
|
|
267
|
-
return this.runtime.snapshots.get(id);
|
|
268
|
-
const found = (await this.runtime.snapshots.list({ name: id })).data.find((s) => s.name === id);
|
|
269
|
-
if (!found)
|
|
270
|
-
throw new CompatibilityError("Freestyle", `snapshot ${id}; import the source environment into Runtime first`);
|
|
271
|
-
return found;
|
|
272
|
-
}
|
|
273
|
-
get(snapshotId) {
|
|
274
|
-
return api(async () => snapshotData(await this.resolve(snapshotId)));
|
|
275
|
-
}
|
|
276
|
-
delete(snapshotId) {
|
|
277
|
-
return api(async () => this.runtime.snapshots.delete((await this.resolve(snapshotId)).id));
|
|
278
|
-
}
|
|
279
|
-
async list(options = {}) {
|
|
280
|
-
only("Freestyle snapshot list", options, ["sourceVmId", "limit", "offset"]);
|
|
281
|
-
if (options.sourceVmId !== undefined && typeof options.sourceVmId !== "string")
|
|
282
|
-
throw new TypeError("sourceVmId must be a string");
|
|
283
|
-
if (options.limit !== undefined && (!Number.isSafeInteger(options.limit) || options.limit <= 0))
|
|
284
|
-
throw new TypeError("limit must be a positive safe integer");
|
|
285
|
-
if (options.offset !== undefined &&
|
|
286
|
-
(!Number.isSafeInteger(options.offset) || options.offset < 0))
|
|
287
|
-
throw new TypeError("offset must be a nonnegative safe integer");
|
|
288
|
-
return api(async () => {
|
|
289
|
-
const offset = options.offset ?? 0;
|
|
290
|
-
const snapshots = [];
|
|
291
|
-
let totalCount = 0;
|
|
292
|
-
// Count the filtered catalog across every native page without retaining
|
|
293
|
-
// snapshots outside the requested page or Page.toArray's default cap.
|
|
294
|
-
const page = await this.runtime.snapshots.list(options.sourceVmId === undefined ? {} : { sandboxId: options.sourceVmId });
|
|
295
|
-
for await (const snapshot of page) {
|
|
296
|
-
if (options.sourceVmId !== undefined && snapshot.sourceSandboxId !== options.sourceVmId)
|
|
297
|
-
continue;
|
|
298
|
-
if (totalCount >= offset &&
|
|
299
|
-
(options.limit === undefined || snapshots.length < options.limit))
|
|
300
|
-
snapshots.push(snapshotData(snapshot));
|
|
301
|
-
totalCount++;
|
|
302
|
-
}
|
|
303
|
-
return { snapshots, totalCount };
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
/** Renames, relabels and re-times a snapshot: slug is its Runtime name,
|
|
307
|
-
* ttlSeconds whole days from now. */
|
|
308
|
-
async update(snapshotId, options) {
|
|
309
|
-
only("Freestyle snapshot update", options, ["slug", "displayName", "ttlSeconds"]);
|
|
310
|
-
const days = retentionDays(options.ttlSeconds);
|
|
311
|
-
if (options.slug !== undefined && (typeof options.slug !== "string" || !options.slug))
|
|
312
|
-
throw new TypeError("slug must be a nonempty string");
|
|
313
|
-
if (options.displayName !== undefined && typeof options.displayName !== "string")
|
|
314
|
-
throw new TypeError("displayName must be a string");
|
|
315
|
-
return api(async () => {
|
|
316
|
-
let snapshot = await this.resolve(snapshotId);
|
|
317
|
-
if (options.slug !== undefined || options.displayName !== undefined)
|
|
318
|
-
snapshot = await this.runtime.snapshots.update(snapshot.id, {
|
|
319
|
-
...(options.slug === undefined ? {} : { name: options.slug }),
|
|
320
|
-
...(options.displayName === undefined
|
|
321
|
-
? {}
|
|
322
|
-
: { labels: { ...labelsOf(snapshot), "fs.displayName": options.displayName } }),
|
|
323
|
-
});
|
|
324
|
-
if (days !== undefined)
|
|
325
|
-
snapshot = await this.runtime.snapshots.extend(snapshot.id, days);
|
|
326
|
-
return snapshotData(snapshot);
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
export class VmsNamespace {
|
|
331
|
-
runtime;
|
|
332
|
-
snapshots;
|
|
333
|
-
constructor(runtime) {
|
|
334
|
-
this.runtime = runtime;
|
|
335
|
-
this.snapshots = new VmSnapshotsNamespace(runtime);
|
|
336
|
-
}
|
|
337
|
-
async create(options) {
|
|
338
|
-
only("Freestyle create", options, [
|
|
339
|
-
"snapshotId",
|
|
340
|
-
"slug",
|
|
341
|
-
"displayName",
|
|
342
|
-
"idleTimeoutSeconds",
|
|
343
|
-
"metadata",
|
|
344
|
-
"firewall",
|
|
345
|
-
]);
|
|
346
|
-
validateMetadata(options.metadata);
|
|
347
|
-
if (!options.firewall || !Array.isArray(options.firewall.rules))
|
|
348
|
-
throw new TypeError("firewall.rules is required");
|
|
349
|
-
only("Freestyle firewall", options.firewall, ["rules"]);
|
|
350
|
-
const network = networkFor(options.firewall.rules);
|
|
351
|
-
return api(async () => {
|
|
352
|
-
const snapshot = options.snapshotId
|
|
353
|
-
? (await this.snapshots.resolve(options.snapshotId)).id
|
|
354
|
-
: undefined;
|
|
355
|
-
const s = await create(this.runtime, "freestyle", {
|
|
356
|
-
snapshot,
|
|
357
|
-
name: options.slug ?? undefined,
|
|
358
|
-
labels: { ...options.metadata, "fs.displayName": options.displayName ?? "" },
|
|
359
|
-
persistent: true,
|
|
360
|
-
idlePauseSeconds: options.idleTimeoutSeconds === -1 || options.idleTimeoutSeconds == null
|
|
361
|
-
? 0
|
|
362
|
-
: options.idleTimeoutSeconds,
|
|
363
|
-
network,
|
|
364
|
-
});
|
|
365
|
-
return {
|
|
366
|
-
vm: new Vm(this.runtime, s.id),
|
|
367
|
-
vmId: s.id,
|
|
368
|
-
data: data(s),
|
|
369
|
-
firewallRules: options.firewall.rules.map((rule, index) => ruleData(s.id, index, rule.destination, s.info.createdAt)),
|
|
370
|
-
tlsRules: [],
|
|
371
|
-
};
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
ref(id) {
|
|
375
|
-
return new Vm(this.runtime, id);
|
|
376
|
-
}
|
|
377
|
-
get(id) {
|
|
378
|
-
return api(async () => data(await lookup(this.runtime, id)));
|
|
379
|
-
}
|
|
380
|
-
delete(id) {
|
|
381
|
-
return api(async () => destroy(await lookup(this.runtime, id)));
|
|
382
|
-
}
|
|
383
|
-
async list(options = {}) {
|
|
384
|
-
only("Freestyle list", options, ["state", "slug", "metadata", "limit", "offset"]);
|
|
385
|
-
const filters = options.metadata?.split(",").map((x) => {
|
|
386
|
-
const i = x.indexOf(":");
|
|
387
|
-
return [x.slice(0, i), x.slice(i + 1)];
|
|
388
|
-
}) ?? [];
|
|
389
|
-
const values = (await api(() => all(this.runtime, "freestyle"))).filter((s) => (!options.state || s.state === options.state) &&
|
|
390
|
-
(!options.slug || s.info.name === options.slug) &&
|
|
391
|
-
filters.every(([k, v]) => s.info.labels[k] === v));
|
|
392
|
-
return {
|
|
393
|
-
vms: values
|
|
394
|
-
.slice(options.offset ?? 0, (options.offset ?? 0) + (options.limit ?? 100))
|
|
395
|
-
.map(data),
|
|
396
|
-
totalCount: values.length,
|
|
397
|
-
runningCount: values.filter((s) => s.state === "running").length,
|
|
398
|
-
startingCount: values.filter((s) => s.state === "starting").length,
|
|
399
|
-
pausingCount: values.filter((s) => s.state === "pausing").length,
|
|
400
|
-
pausedCount: values.filter((s) => s.state === "paused").length,
|
|
401
|
-
stoppedCount: values.filter((s) => s.state === "stopped").length,
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
export class VmFilesystem {
|
|
406
|
-
get;
|
|
407
|
-
files;
|
|
408
|
-
constructor(get) {
|
|
409
|
-
this.get = get;
|
|
410
|
-
this.files = new GuestFiles(get, "/");
|
|
411
|
-
}
|
|
412
|
-
async readFile(path, options = {}) {
|
|
413
|
-
const stream = await this.readFileStream(path, options);
|
|
414
|
-
return api(async () => new Uint8Array(await new Response(stream).arrayBuffer()));
|
|
415
|
-
}
|
|
416
|
-
async readTextFile(path, options = {}) {
|
|
417
|
-
return new TextDecoder().decode(await this.readFile(path, options));
|
|
418
|
-
}
|
|
419
|
-
async readFileStream(path, options = {}) {
|
|
420
|
-
only("Freestyle file read", options, ["offset", "length", "signal"]);
|
|
421
|
-
for (const value of [options.offset, options.length])
|
|
422
|
-
if (value !== undefined && (!Number.isSafeInteger(value) || value < 0))
|
|
423
|
-
throw new TypeError("File range must contain nonnegative safe integers");
|
|
424
|
-
options.signal?.throwIfAborted();
|
|
425
|
-
const source = await api(async () => (await this.get()).files.readStream(this.files.path(path), { signal: options.signal }));
|
|
426
|
-
if (options.offset === undefined && options.length === undefined)
|
|
427
|
-
return source;
|
|
428
|
-
const reader = source.getReader();
|
|
429
|
-
let skip = options.offset ?? 0, remaining = options.length ?? Infinity;
|
|
430
|
-
return new ReadableStream({
|
|
431
|
-
async pull(controller) {
|
|
432
|
-
try {
|
|
433
|
-
options.signal?.throwIfAborted();
|
|
434
|
-
if (!remaining) {
|
|
435
|
-
await reader.cancel();
|
|
436
|
-
controller.close();
|
|
437
|
-
return;
|
|
438
|
-
}
|
|
439
|
-
for (;;) {
|
|
440
|
-
const next = await reader.read();
|
|
441
|
-
if (next.done) {
|
|
442
|
-
controller.close();
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
const start = Math.min(skip, next.value.length);
|
|
446
|
-
skip -= start;
|
|
447
|
-
const chunk = next.value.subarray(start, start + remaining);
|
|
448
|
-
if (chunk.length) {
|
|
449
|
-
remaining -= chunk.length;
|
|
450
|
-
controller.enqueue(chunk);
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
catch (error) {
|
|
456
|
-
await reader.cancel(error);
|
|
457
|
-
controller.error(error);
|
|
458
|
-
}
|
|
459
|
-
},
|
|
460
|
-
cancel(reason) {
|
|
461
|
-
return reader.cancel(reason);
|
|
462
|
-
},
|
|
463
|
-
});
|
|
464
|
-
}
|
|
465
|
-
async writeFile(path, content, options = {}) {
|
|
466
|
-
only("Freestyle file write", options, [
|
|
467
|
-
"mode",
|
|
468
|
-
"onProgress",
|
|
469
|
-
"signal",
|
|
470
|
-
"chunkSize",
|
|
471
|
-
"maxAttempts",
|
|
472
|
-
]);
|
|
473
|
-
if (options.chunkSize !== undefined || options.maxAttempts !== undefined)
|
|
474
|
-
throw new CompatibilityError("Freestyle", "custom upload chunk sizing/retry budgets");
|
|
475
|
-
options.signal?.throwIfAborted();
|
|
476
|
-
const bytes = typeof content === "string"
|
|
477
|
-
? new TextEncoder().encode(content)
|
|
478
|
-
: content instanceof Uint8Array
|
|
479
|
-
? content
|
|
480
|
-
: new Uint8Array(await content.arrayBuffer());
|
|
481
|
-
await api(async () => {
|
|
482
|
-
let mode = options.mode;
|
|
483
|
-
if (mode === undefined)
|
|
484
|
-
mode = (await this.files.exists(path))
|
|
485
|
-
? parseInt((await this.files.stat(path)).mode, 8)
|
|
486
|
-
: 0o600;
|
|
487
|
-
await this.files.writeFile(path, bytes, { mode, signal: options.signal });
|
|
488
|
-
});
|
|
489
|
-
options.onProgress?.({ completedBytes: bytes.length, totalBytes: bytes.length });
|
|
490
|
-
}
|
|
491
|
-
writeTextFile(path, content, options = {}) {
|
|
492
|
-
return this.writeFile(path, content, options);
|
|
493
|
-
}
|
|
494
|
-
mkdir(path) {
|
|
495
|
-
return api(() => this.files.mkdir(path, true));
|
|
496
|
-
}
|
|
497
|
-
remove(path) {
|
|
498
|
-
return api(() => this.files.remove(path, true));
|
|
499
|
-
}
|
|
500
|
-
exists(path) {
|
|
501
|
-
return api(() => this.files.exists(path));
|
|
502
|
-
}
|
|
503
|
-
readDir(path) {
|
|
504
|
-
return api(async () => (await this.files.readdir(path)).map((e) => ({ name: e.name, kind: e.type })));
|
|
505
|
-
}
|
|
506
|
-
stat(path) {
|
|
507
|
-
return api(async () => {
|
|
508
|
-
const e = await this.files.stat(path);
|
|
509
|
-
const s = await this.get();
|
|
510
|
-
const r = await s.exec(["stat", "-c", "%U:%G", "--", this.files.path(path)], { check: true });
|
|
511
|
-
const [owner, group] = r.stdout.trim().split(":");
|
|
512
|
-
return {
|
|
513
|
-
size: e.size,
|
|
514
|
-
isFile: e.type === "file",
|
|
515
|
-
isDirectory: e.type === "directory",
|
|
516
|
-
isSymlink: e.type === "symlink",
|
|
517
|
-
permissions: e.mode,
|
|
518
|
-
owner: owner,
|
|
519
|
-
group: group,
|
|
520
|
-
modified: e.modifiedAt,
|
|
521
|
-
};
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
const PTY = /^: freestyle-pty slug=([a-z0-9-]*) user=([a-z0-9_-]*) cols=(\d+) rows=(\d+); /;
|
|
526
|
-
const SLUG = /^(?=.*[a-z-])[a-z0-9-]{1,63}$/;
|
|
527
|
-
/** A session's number: FNV-1a of its process id, 31 bits, never 0. */
|
|
528
|
-
export function sessionNumber(processId) {
|
|
529
|
-
let hash = 0x811c9dc5;
|
|
530
|
-
for (const char of processId)
|
|
531
|
-
hash = Math.imul(hash ^ char.charCodeAt(0), 0x01000193);
|
|
532
|
-
return hash >>> 1 || 1;
|
|
533
|
-
}
|
|
534
|
-
function session(process) {
|
|
535
|
-
const mark = PTY.exec(process.command);
|
|
536
|
-
if (!process.pty || !mark)
|
|
537
|
-
return undefined;
|
|
538
|
-
const running = process.state === "running";
|
|
539
|
-
return {
|
|
540
|
-
processId: process.id,
|
|
541
|
-
running,
|
|
542
|
-
info: {
|
|
543
|
-
sessionId: sessionNumber(process.id),
|
|
544
|
-
state: running ? "running" : "exited",
|
|
545
|
-
createdUnix: Math.floor(Date.parse(process.startedAt) / 1000),
|
|
546
|
-
cols: Number(mark[3]),
|
|
547
|
-
rows: Number(mark[4]),
|
|
548
|
-
...(running ? {} : { exitCode: process.exitCode }),
|
|
549
|
-
linuxUser: mark[2] || null,
|
|
550
|
-
// A slug is released when its shell exits.
|
|
551
|
-
slug: running && mark[1] ? mark[1] : null,
|
|
552
|
-
},
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
const dimension = (value, fallback, name) => {
|
|
556
|
-
if (value === undefined)
|
|
557
|
-
return fallback;
|
|
558
|
-
if (!Number.isSafeInteger(value) || value < 1 || value > 1000)
|
|
559
|
-
throw new TypeError(`${name} must be a whole number from 1 to 1000`);
|
|
560
|
-
return value;
|
|
561
|
-
};
|
|
562
|
-
const PTY_EVENTS = [
|
|
563
|
-
"reconnect",
|
|
564
|
-
"connectTimeoutMs",
|
|
565
|
-
"onStateChange",
|
|
566
|
-
"onReplayStart",
|
|
567
|
-
"onReplayEnd",
|
|
568
|
-
"onData",
|
|
569
|
-
"onExit",
|
|
570
|
-
"onClose",
|
|
571
|
-
"onError",
|
|
572
|
-
];
|
|
573
|
-
const PTY_OPEN = ["exec", "cols", "rows", "linuxUser", "slug", ...PTY_EVENTS];
|
|
574
|
-
/** Freestyle 0.2.18 checks its timers before anything is opened. */
|
|
575
|
-
function connectionOptions(options) {
|
|
576
|
-
const { reconnect } = options;
|
|
577
|
-
if (reconnect !== undefined && reconnect !== null && typeof reconnect !== "boolean") {
|
|
578
|
-
if (typeof reconnect !== "object")
|
|
579
|
-
throw new TypeError("reconnect must be true, false or reconnect options");
|
|
580
|
-
only("Freestyle pty reconnect", reconnect, ["timeoutMs", "initialDelayMs", "maxDelayMs"]);
|
|
581
|
-
}
|
|
582
|
-
const timers = {
|
|
583
|
-
connectTimeoutMs: options.connectTimeoutMs,
|
|
584
|
-
...(typeof reconnect === "object" && reconnect ? reconnect : {}),
|
|
585
|
-
};
|
|
586
|
-
for (const [name, value] of Object.entries(timers))
|
|
587
|
-
if (value !== undefined &&
|
|
588
|
-
(typeof value !== "number" || !Number.isFinite(value) || value <= 0 || value > 2_147_483_647))
|
|
589
|
-
throw new RangeError(`${name} must be a positive finite timer duration`);
|
|
590
|
-
}
|
|
591
|
-
const LOST = { wasClean: false, code: 1006, reason: "Connection lost" };
|
|
592
|
-
/** A 4xx other than 409 and 429 will not get better by retrying. */
|
|
593
|
-
const fatal = (error) => error instanceof RuntimeError &&
|
|
594
|
-
error.status >= 400 &&
|
|
595
|
-
error.status < 500 &&
|
|
596
|
-
error.status !== 409 &&
|
|
597
|
-
error.status !== 429;
|
|
598
|
-
/** A stable handle across reattachments; input is never retried or buffered.
|
|
599
|
-
* Each connection is a Runtime terminal attached to the session's process. */
|
|
600
|
-
export class PtySession {
|
|
601
|
-
sandbox;
|
|
602
|
-
processId;
|
|
603
|
-
sessionId;
|
|
604
|
-
slug;
|
|
605
|
-
created;
|
|
606
|
-
events;
|
|
607
|
-
#state = "connecting";
|
|
608
|
-
#readyState = 0;
|
|
609
|
-
#terminal;
|
|
610
|
-
#generation = 0;
|
|
611
|
-
#attempt = 0;
|
|
612
|
-
#deadline = 0;
|
|
613
|
-
#retry;
|
|
614
|
-
#stopped = false;
|
|
615
|
-
#exited = false;
|
|
616
|
-
#fatal = false;
|
|
617
|
-
#established = false;
|
|
618
|
-
#closeReported = false;
|
|
619
|
-
#dimensions;
|
|
620
|
-
#removeWake;
|
|
621
|
-
#resolveReady;
|
|
622
|
-
#rejectReady;
|
|
623
|
-
/** Resolves once the first connection is up. */
|
|
624
|
-
ready;
|
|
625
|
-
constructor(sandbox, processId, sessionId, slug, created, events, dimensions) {
|
|
626
|
-
this.sandbox = sandbox;
|
|
627
|
-
this.processId = processId;
|
|
628
|
-
this.sessionId = sessionId;
|
|
629
|
-
this.slug = slug;
|
|
630
|
-
this.created = created;
|
|
631
|
-
this.events = events;
|
|
632
|
-
this.#dimensions = dimensions;
|
|
633
|
-
this.ready = new Promise((resolve, reject) => {
|
|
634
|
-
this.#resolveReady = resolve;
|
|
635
|
-
this.#rejectReady = reject;
|
|
636
|
-
});
|
|
637
|
-
void this.ready.catch(() => { });
|
|
638
|
-
if (events.reconnect && typeof window !== "undefined" && typeof document !== "undefined") {
|
|
639
|
-
// A browser back online or back in view retries a dropped session now.
|
|
640
|
-
const wake = () => {
|
|
641
|
-
if (document.visibilityState !== "hidden" && this.#state === "closed")
|
|
642
|
-
this.reconnect();
|
|
643
|
-
};
|
|
644
|
-
window.addEventListener("online", wake);
|
|
645
|
-
window.addEventListener("pageshow", wake);
|
|
646
|
-
document.addEventListener("visibilitychange", wake);
|
|
647
|
-
this.#removeWake = () => {
|
|
648
|
-
window.removeEventListener("online", wake);
|
|
649
|
-
window.removeEventListener("pageshow", wake);
|
|
650
|
-
document.removeEventListener("visibilitychange", wake);
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
void this.#dial();
|
|
654
|
-
}
|
|
655
|
-
#setState(state) {
|
|
656
|
-
if (this.#state === state)
|
|
657
|
-
return;
|
|
658
|
-
this.#state = state;
|
|
659
|
-
this.events.onStateChange?.(state);
|
|
660
|
-
}
|
|
661
|
-
async #dial() {
|
|
662
|
-
const generation = ++this.#generation;
|
|
663
|
-
const current = () => generation === this.#generation && !this.#stopped;
|
|
664
|
-
const limit = this.#established
|
|
665
|
-
? Math.min(this.events.connectTimeoutMs ?? 15_000, Math.max(1, this.#deadline - Date.now()))
|
|
666
|
-
: this.events.connectTimeoutMs;
|
|
667
|
-
try {
|
|
668
|
-
// Before a reattachment, as Freestyle's preflight: a session that is
|
|
669
|
-
// gone, or credentials that no longer work, end it instead of retrying.
|
|
670
|
-
if (this.#established)
|
|
671
|
-
await this.sandbox.processes.get(this.processId);
|
|
672
|
-
if (!current())
|
|
673
|
-
return;
|
|
674
|
-
const opening = this.sandbox.terminal({
|
|
675
|
-
processId: this.processId,
|
|
676
|
-
onData: (data) => {
|
|
677
|
-
if (current())
|
|
678
|
-
this.events.onData?.(data);
|
|
679
|
-
},
|
|
680
|
-
});
|
|
681
|
-
let timer;
|
|
682
|
-
const terminal = await (limit === undefined
|
|
683
|
-
? opening
|
|
684
|
-
: Promise.race([
|
|
685
|
-
opening,
|
|
686
|
-
new Promise((_, reject) => {
|
|
687
|
-
timer = setTimeout(() => {
|
|
688
|
-
void opening.then((late) => late.close()).catch(() => { });
|
|
689
|
-
reject(new Error("PTY handshake timed out"));
|
|
690
|
-
}, limit);
|
|
691
|
-
}),
|
|
692
|
-
]).finally(() => clearTimeout(timer)));
|
|
693
|
-
if (!current()) {
|
|
694
|
-
terminal.close();
|
|
695
|
-
return;
|
|
696
|
-
}
|
|
697
|
-
this.#bind(terminal, generation);
|
|
698
|
-
}
|
|
699
|
-
catch (error) {
|
|
700
|
-
if (!current())
|
|
701
|
-
return;
|
|
702
|
-
if (!this.#established)
|
|
703
|
-
return this.#finish(translate(error));
|
|
704
|
-
if (fatal(error)) {
|
|
705
|
-
this.#fatal = true;
|
|
706
|
-
this.#finish(translate(error));
|
|
707
|
-
}
|
|
708
|
-
else
|
|
709
|
-
this.#scheduleRetry(translate(error));
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
#bind(terminal, generation) {
|
|
713
|
-
this.#terminal = terminal;
|
|
714
|
-
this.#readyState = 1;
|
|
715
|
-
const recovering = this.#established;
|
|
716
|
-
this.#established = true;
|
|
717
|
-
this.#attempt = 0;
|
|
718
|
-
this.#deadline = 0;
|
|
719
|
-
this.#closeReported = false;
|
|
720
|
-
this.#resolveReady();
|
|
721
|
-
this.#setState("connected");
|
|
722
|
-
if (recovering && this.#dimensions)
|
|
723
|
-
terminal.resize(this.#dimensions.cols, this.#dimensions.rows);
|
|
724
|
-
void terminal.exited.then((exitCode) => this.#ended(generation, exitCode));
|
|
725
|
-
}
|
|
726
|
-
/** The connection ended: with the shell's exit code, or without one when the
|
|
727
|
-
* shell was killed by a signal or the connection dropped. */
|
|
728
|
-
#ended(generation, exitCode) {
|
|
729
|
-
const current = () => generation === this.#generation && !this.#stopped;
|
|
730
|
-
if (!current())
|
|
731
|
-
return;
|
|
732
|
-
this.#readyState = 3;
|
|
733
|
-
if (exitCode !== null)
|
|
734
|
-
return this.#exit(exitCode);
|
|
735
|
-
this.sandbox.processes.get(this.processId).then((process) => {
|
|
736
|
-
if (!current())
|
|
737
|
-
return;
|
|
738
|
-
if (process.state === "running")
|
|
739
|
-
this.#lost();
|
|
740
|
-
else
|
|
741
|
-
this.#exit(process.exitCode);
|
|
742
|
-
}, (error) => {
|
|
743
|
-
if (current())
|
|
744
|
-
this.#lost(translate(error));
|
|
745
|
-
});
|
|
746
|
-
}
|
|
747
|
-
#retire() {
|
|
748
|
-
++this.#generation;
|
|
749
|
-
clearTimeout(this.#retry);
|
|
750
|
-
if (this.#readyState === 1)
|
|
751
|
-
this.#readyState = 2;
|
|
752
|
-
this.#terminal?.close();
|
|
753
|
-
this.#terminal = undefined;
|
|
754
|
-
}
|
|
755
|
-
#exit(exitCode) {
|
|
756
|
-
this.#exited = true;
|
|
757
|
-
this.#retire();
|
|
758
|
-
this.#readyState = 3;
|
|
759
|
-
this.#setState("exited");
|
|
760
|
-
this.#removeWake?.();
|
|
761
|
-
if (exitCode !== null)
|
|
762
|
-
this.events.onExit?.(exitCode);
|
|
763
|
-
this.#report({ wasClean: true, code: 1000, reason: "" });
|
|
764
|
-
}
|
|
765
|
-
#lost(error) {
|
|
766
|
-
this.#retire();
|
|
767
|
-
this.#readyState = 3;
|
|
768
|
-
if (this.#stopped)
|
|
769
|
-
return;
|
|
770
|
-
if (!this.events.reconnect || this.#exited || this.#fatal)
|
|
771
|
-
return this.#finish(error);
|
|
772
|
-
this.#scheduleRetry(error);
|
|
773
|
-
}
|
|
774
|
-
#scheduleRetry(error) {
|
|
775
|
-
const options = typeof this.events.reconnect === "object" ? this.events.reconnect : null;
|
|
776
|
-
this.#deadline ||= Date.now() + (options?.timeoutMs ?? 300_000);
|
|
777
|
-
if (Date.now() >= this.#deadline)
|
|
778
|
-
return this.#finish(error ?? new Error("PTY reconnect timed out"));
|
|
779
|
-
this.#setState("reconnecting");
|
|
780
|
-
const base = Math.min((options?.initialDelayMs ?? 400) * 2 ** this.#attempt++, options?.maxDelayMs ?? 10_000);
|
|
781
|
-
const delay = Math.min(Math.round(base * (0.7 + Math.random() * 0.6)), Math.max(0, this.#deadline - Date.now()));
|
|
782
|
-
this.#retry = setTimeout(() => void this.#dial(), delay);
|
|
783
|
-
}
|
|
784
|
-
#finish(error, info = LOST) {
|
|
785
|
-
clearTimeout(this.#retry);
|
|
786
|
-
if (!this.#established)
|
|
787
|
-
this.#rejectReady(error ?? new Error("PTY connection failed"));
|
|
788
|
-
this.#setState(this.#exited ? "exited" : "closed");
|
|
789
|
-
if (this.#fatal || this.#exited || !this.#established)
|
|
790
|
-
this.#removeWake?.();
|
|
791
|
-
this.#report(info, error);
|
|
792
|
-
}
|
|
793
|
-
#report(info, error) {
|
|
794
|
-
if (this.#closeReported)
|
|
795
|
-
return;
|
|
796
|
-
this.#closeReported = true;
|
|
797
|
-
if (error)
|
|
798
|
-
this.events.onError?.(error);
|
|
799
|
-
this.events.onClose?.(info);
|
|
800
|
-
}
|
|
801
|
-
/** Retry now after a network change or a spent retry budget. Never opens a new shell. */
|
|
802
|
-
reconnect() {
|
|
803
|
-
if (this.#stopped || this.#exited || this.#fatal || !this.#established)
|
|
804
|
-
return;
|
|
805
|
-
this.#retire();
|
|
806
|
-
this.#attempt = 0;
|
|
807
|
-
this.#deadline = 0;
|
|
808
|
-
this.#closeReported = false;
|
|
809
|
-
this.#scheduleRetry();
|
|
810
|
-
}
|
|
811
|
-
/** Send input once. Throws while disconnected; uncertain keystrokes are never replayed. */
|
|
812
|
-
write(data) {
|
|
813
|
-
if (this.#state !== "connected" || !this.#terminal)
|
|
814
|
-
throw new Error("PTY is not connected");
|
|
815
|
-
this.#terminal.write(data);
|
|
816
|
-
}
|
|
817
|
-
/** Remembers the size and applies it again after a reattachment. */
|
|
818
|
-
resize(options) {
|
|
819
|
-
const cols = dimension(options.cols, 80, "cols");
|
|
820
|
-
const rows = dimension(options.rows, 24, "rows");
|
|
821
|
-
this.#dimensions = { cols, rows };
|
|
822
|
-
if (this.#state === "connected")
|
|
823
|
-
this.#terminal?.resize(cols, rows);
|
|
824
|
-
}
|
|
825
|
-
signal(sig) {
|
|
826
|
-
if (sig !== "sigint" && sig !== "sigkill")
|
|
827
|
-
throw new TypeError('signal must be "sigint" or "sigkill"');
|
|
828
|
-
if (this.#state !== "connected")
|
|
829
|
-
throw new Error("PTY is not connected");
|
|
830
|
-
void (async () => {
|
|
831
|
-
const process = await this.sandbox.processes.get(this.processId);
|
|
832
|
-
await process.kill(sig === "sigint" ? "SIGINT" : "SIGKILL");
|
|
833
|
-
})().catch((error) => this.events.onError?.(translate(error)));
|
|
834
|
-
}
|
|
835
|
-
/** Cancels recovery and closes the connection; the shell keeps running and
|
|
836
|
-
* attach() finds it. */
|
|
837
|
-
detach() {
|
|
838
|
-
if (this.#stopped)
|
|
839
|
-
return;
|
|
840
|
-
this.#stopped = true;
|
|
841
|
-
this.#retire();
|
|
842
|
-
this.#removeWake?.();
|
|
843
|
-
this.#finish(undefined, { wasClean: true, code: 1000, reason: "Detached" });
|
|
844
|
-
}
|
|
845
|
-
get readyState() {
|
|
846
|
-
return this.#readyState;
|
|
847
|
-
}
|
|
848
|
-
get state() {
|
|
849
|
-
return this.#state;
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
export class VmPty {
|
|
853
|
-
get;
|
|
854
|
-
constructor(get) {
|
|
855
|
-
this.get = get;
|
|
856
|
-
}
|
|
857
|
-
async sessions(sandbox) {
|
|
858
|
-
return (await sandbox.processes.list()).flatMap((process) => session(process) ?? []);
|
|
859
|
-
}
|
|
860
|
-
async find(sandbox, selector, linuxUser) {
|
|
861
|
-
if (typeof selector !== "number" && typeof selector !== "string")
|
|
862
|
-
throw new TypeError("session must be a session id or a slug");
|
|
863
|
-
const found = (await this.sessions(sandbox))
|
|
864
|
-
.filter((s) => typeof selector === "number" ? s.info.sessionId === selector : s.info.slug === selector)
|
|
865
|
-
.sort((a, b) => a.info.createdUnix - b.info.createdUnix)[0];
|
|
866
|
-
if (!found)
|
|
867
|
-
throw notFound(`PTY session ${selector}`);
|
|
868
|
-
if (linuxUser !== undefined && found.info.linuxUser !== linuxUser)
|
|
869
|
-
throw notFound(`PTY session ${selector} of ${linuxUser}`);
|
|
870
|
-
return found;
|
|
871
|
-
}
|
|
872
|
-
async connect(sandbox, found, created, events, dimensions) {
|
|
873
|
-
const session = new PtySession(sandbox, found.processId, found.info.sessionId, found.info.slug ?? undefined, created, events, dimensions);
|
|
874
|
-
await session.ready;
|
|
875
|
-
return session;
|
|
876
|
-
}
|
|
877
|
-
async open(options = {}) {
|
|
878
|
-
only("Freestyle pty open", options, PTY_OPEN);
|
|
879
|
-
connectionOptions(options);
|
|
880
|
-
const cols = dimension(options.cols, 80, "cols");
|
|
881
|
-
const rows = dimension(options.rows, 24, "rows");
|
|
882
|
-
const user = options.linuxUser === undefined ? "" : validateLinuxUser(options.linuxUser);
|
|
883
|
-
if (options.slug !== undefined &&
|
|
884
|
-
(typeof options.slug !== "string" || !SLUG.test(options.slug)))
|
|
885
|
-
throw new TypeError("slug must be lowercase letters, digits and hyphens, not all digits");
|
|
886
|
-
if (options.exec !== undefined &&
|
|
887
|
-
(typeof options.exec !== "string" || options.exec.includes("\0")))
|
|
888
|
-
throw new TypeError("exec must be a command without NUL bytes");
|
|
889
|
-
return api(async () => {
|
|
890
|
-
const sandbox = await this.get();
|
|
891
|
-
if (options.slug !== undefined) {
|
|
892
|
-
const existing = (await this.sessions(sandbox)).find((s) => s.info.slug === options.slug);
|
|
893
|
-
// Get-or-create: a running session with the slug is handed back, exec unrun.
|
|
894
|
-
if (existing)
|
|
895
|
-
return this.connect(sandbox, existing, false, options, { cols, rows });
|
|
896
|
-
}
|
|
897
|
-
const shell = options.exec === undefined
|
|
898
|
-
? user
|
|
899
|
-
? `exec sudo -n -H -u ${quote(user)} -- bash -l`
|
|
900
|
-
: "exec bash -l"
|
|
901
|
-
: user
|
|
902
|
-
? `exec sudo -n -H -u ${quote(user)} -- bash -c ${quote(options.exec)}`
|
|
903
|
-
: options.exec;
|
|
904
|
-
const process = await sandbox.spawn(`: freestyle-pty slug=${options.slug ?? ""} user=${user} cols=${cols} rows=${rows}; ${shell}`, { pty: { cols, rows } });
|
|
905
|
-
const started = session(process.toJSON());
|
|
906
|
-
if (!started)
|
|
907
|
-
throw new Error("The started PTY session has no session line");
|
|
908
|
-
return this.connect(sandbox, started, true, options, { cols, rows });
|
|
909
|
-
});
|
|
910
|
-
}
|
|
911
|
-
async attach(options) {
|
|
912
|
-
only("Freestyle pty attach", options, ["session", "linuxUser", ...PTY_EVENTS]);
|
|
913
|
-
connectionOptions(options);
|
|
914
|
-
if (options.linuxUser !== undefined)
|
|
915
|
-
validateLinuxUser(options.linuxUser);
|
|
916
|
-
return api(async () => {
|
|
917
|
-
const sandbox = await this.get();
|
|
918
|
-
return this.connect(sandbox, await this.find(sandbox, options.session, options.linuxUser), false, options);
|
|
919
|
-
});
|
|
920
|
-
}
|
|
921
|
-
async list(options = {}) {
|
|
922
|
-
only("Freestyle pty list", options, ["linuxUser"]);
|
|
923
|
-
if (options.linuxUser !== undefined)
|
|
924
|
-
validateLinuxUser(options.linuxUser);
|
|
925
|
-
return api(async () => ({
|
|
926
|
-
sessions: (await this.sessions(await this.get()))
|
|
927
|
-
.filter((s) => options.linuxUser === undefined || s.info.linuxUser === options.linuxUser)
|
|
928
|
-
.map((s) => s.info),
|
|
929
|
-
}));
|
|
930
|
-
}
|
|
931
|
-
/** Ends a session's shell. An exited session stays in the list. */
|
|
932
|
-
async close(session, options = {}) {
|
|
933
|
-
only("Freestyle pty close", options, ["linuxUser"]);
|
|
934
|
-
if (options.linuxUser !== undefined)
|
|
935
|
-
validateLinuxUser(options.linuxUser);
|
|
936
|
-
return api(async () => {
|
|
937
|
-
const sandbox = await this.get();
|
|
938
|
-
const found = await this.find(sandbox, session, options.linuxUser);
|
|
939
|
-
if (!found.running)
|
|
940
|
-
return { sessionId: found.info.sessionId, exitCode: found.info.exitCode };
|
|
941
|
-
await (await sandbox.processes.get(found.processId)).kill("SIGKILL");
|
|
942
|
-
return { sessionId: found.info.sessionId };
|
|
943
|
-
});
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
export class VmLinuxUserPty {
|
|
947
|
-
pty;
|
|
948
|
-
linuxUser;
|
|
949
|
-
constructor(pty, linuxUser) {
|
|
950
|
-
this.pty = pty;
|
|
951
|
-
this.linuxUser = linuxUser;
|
|
952
|
-
}
|
|
953
|
-
async open(options = {}) {
|
|
954
|
-
only("Freestyle pty open", options, PTY_OPEN.filter((key) => key !== "linuxUser"));
|
|
955
|
-
return this.pty.open({ ...options, linuxUser: this.linuxUser });
|
|
956
|
-
}
|
|
957
|
-
attach(options) {
|
|
958
|
-
return this.pty.attach({ ...options, linuxUser: this.linuxUser });
|
|
959
|
-
}
|
|
960
|
-
list() {
|
|
961
|
-
return this.pty.list({ linuxUser: this.linuxUser });
|
|
962
|
-
}
|
|
963
|
-
close(session) {
|
|
964
|
-
return this.pty.close(session, { linuxUser: this.linuxUser });
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
/** Terminal queries a replay may hold (device attributes, status and mode
|
|
968
|
-
* reports, window ops): answering them again would type the answers. */
|
|
969
|
-
const XTERM_QUERIES = [
|
|
970
|
-
{ final: "c" },
|
|
971
|
-
{ prefix: ">", final: "c" },
|
|
972
|
-
{ final: "n" },
|
|
973
|
-
{ prefix: "?", final: "n" },
|
|
974
|
-
{ final: "t" },
|
|
975
|
-
{ intermediates: "$", final: "p" },
|
|
976
|
-
{ prefix: "?", intermediates: "$", final: "p" },
|
|
977
|
-
];
|
|
978
|
-
/** Freestyle 0.2.18's connectXterm: xterm.js on a named session that
|
|
979
|
-
* reattaches by itself unless `reconnect: false`. Each connection replays the
|
|
980
|
-
* session's output from its start, so the screen is reset first and the
|
|
981
|
-
* replay's terminal queries go unanswered until it has been written (150ms
|
|
982
|
-
* quiet, at most 2 seconds). dispose() detaches; the shell keeps running. */
|
|
983
|
-
export async function connectXterm({ pty, terminal, ...options }) {
|
|
984
|
-
only("Freestyle connectXterm", options, PTY_OPEN);
|
|
985
|
-
let muted = [];
|
|
986
|
-
let quiet;
|
|
987
|
-
let cap;
|
|
988
|
-
let replay = 0;
|
|
989
|
-
let disposed = false;
|
|
990
|
-
const unmute = () => {
|
|
991
|
-
clearTimeout(quiet);
|
|
992
|
-
clearTimeout(cap);
|
|
993
|
-
for (const handler of muted)
|
|
994
|
-
handler.dispose();
|
|
995
|
-
muted = [];
|
|
996
|
-
};
|
|
997
|
-
const endReplay = () => {
|
|
998
|
-
clearTimeout(quiet);
|
|
999
|
-
clearTimeout(cap);
|
|
1000
|
-
const current = replay;
|
|
1001
|
-
// Unmute once xterm has parsed everything written before this point.
|
|
1002
|
-
terminal.write("", () => {
|
|
1003
|
-
if (current === replay)
|
|
1004
|
-
unmute();
|
|
1005
|
-
});
|
|
1006
|
-
};
|
|
1007
|
-
const beginReplay = () => {
|
|
1008
|
-
++replay;
|
|
1009
|
-
unmute();
|
|
1010
|
-
terminal.write("\x1bc");
|
|
1011
|
-
muted = XTERM_QUERIES.map((id) => terminal.parser.registerCsiHandler(id, () => true));
|
|
1012
|
-
muted.push(terminal.parser.registerDcsHandler({ intermediates: "$", final: "q" }, () => true));
|
|
1013
|
-
quiet = setTimeout(endReplay, 150);
|
|
1014
|
-
cap = setTimeout(endReplay, 2_000);
|
|
1015
|
-
};
|
|
1016
|
-
const stopReplay = () => {
|
|
1017
|
-
++replay;
|
|
1018
|
-
unmute();
|
|
1019
|
-
};
|
|
1020
|
-
try {
|
|
1021
|
-
const session = await pty.open({
|
|
1022
|
-
...options,
|
|
1023
|
-
reconnect: options.reconnect ?? true,
|
|
1024
|
-
cols: options.cols ?? terminal.cols,
|
|
1025
|
-
rows: options.rows ?? terminal.rows,
|
|
1026
|
-
onStateChange(state) {
|
|
1027
|
-
if (state === "connected")
|
|
1028
|
-
beginReplay();
|
|
1029
|
-
if (state === "closed" || state === "exited")
|
|
1030
|
-
endReplay();
|
|
1031
|
-
options.onStateChange?.(state);
|
|
1032
|
-
},
|
|
1033
|
-
onData(bytes) {
|
|
1034
|
-
if (disposed)
|
|
1035
|
-
return;
|
|
1036
|
-
if (muted.length) {
|
|
1037
|
-
clearTimeout(quiet);
|
|
1038
|
-
quiet = setTimeout(endReplay, 150);
|
|
1039
|
-
}
|
|
1040
|
-
terminal.write(bytes);
|
|
1041
|
-
options.onData?.(bytes);
|
|
1042
|
-
},
|
|
1043
|
-
});
|
|
1044
|
-
const input = terminal.onData((data) => {
|
|
1045
|
-
if (session.state === "connected")
|
|
1046
|
-
session.write(data);
|
|
1047
|
-
});
|
|
1048
|
-
const resize = terminal.onResize((size) => session.resize(size));
|
|
1049
|
-
return {
|
|
1050
|
-
session,
|
|
1051
|
-
dispose() {
|
|
1052
|
-
if (disposed)
|
|
1053
|
-
return;
|
|
1054
|
-
disposed = true;
|
|
1055
|
-
input.dispose();
|
|
1056
|
-
resize.dispose();
|
|
1057
|
-
session.detach();
|
|
1058
|
-
stopReplay();
|
|
1059
|
-
},
|
|
1060
|
-
};
|
|
1061
|
-
}
|
|
1062
|
-
catch (error) {
|
|
1063
|
-
stopReplay();
|
|
1064
|
-
throw error;
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
export class VmLinuxUser {
|
|
1068
|
-
vm;
|
|
1069
|
-
linuxUser;
|
|
1070
|
-
pty;
|
|
1071
|
-
constructor(vm, linuxUser) {
|
|
1072
|
-
this.vm = vm;
|
|
1073
|
-
this.linuxUser = linuxUser;
|
|
1074
|
-
validateLinuxUser(linuxUser);
|
|
1075
|
-
this.pty = new VmLinuxUserPty(vm.pty, linuxUser);
|
|
1076
|
-
}
|
|
1077
|
-
exec(options) {
|
|
1078
|
-
const request = typeof options === "string" ? { command: options } : options;
|
|
1079
|
-
return this.vm.exec({ ...request, linuxUser: this.linuxUser });
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
export class Vm {
|
|
1083
|
-
runtime;
|
|
1084
|
-
id;
|
|
1085
|
-
fs;
|
|
1086
|
-
pty;
|
|
1087
|
-
constructor(runtime, id) {
|
|
1088
|
-
this.runtime = runtime;
|
|
1089
|
-
this.id = id;
|
|
1090
|
-
this.fs = new VmFilesystem(() => lookup(runtime, id));
|
|
1091
|
-
this.pty = new VmPty(() => lookup(runtime, id));
|
|
1092
|
-
}
|
|
1093
|
-
data() {
|
|
1094
|
-
return api(async () => data(await lookup(this.runtime, this.id)));
|
|
1095
|
-
}
|
|
1096
|
-
start() {
|
|
1097
|
-
return api(async () => data(await ready(await lookup(this.runtime, this.id))));
|
|
1098
|
-
}
|
|
1099
|
-
pause() {
|
|
1100
|
-
return api(async () => {
|
|
1101
|
-
const s = await lookup(this.runtime, this.id);
|
|
1102
|
-
await s.pause();
|
|
1103
|
-
return data(s);
|
|
1104
|
-
});
|
|
1105
|
-
}
|
|
1106
|
-
delete() {
|
|
1107
|
-
return api(async () => destroy(await lookup(this.runtime, this.id)));
|
|
1108
|
-
}
|
|
1109
|
-
/** The VM's outbound rules, read back from its network settings. */
|
|
1110
|
-
firewallRules() {
|
|
1111
|
-
return api(async () => {
|
|
1112
|
-
const s = await lookup(this.runtime, this.id);
|
|
1113
|
-
const policy = await s.network.get();
|
|
1114
|
-
if (policy.deny.length || policy.connect.length)
|
|
1115
|
-
throw new CompatibilityError("Freestyle", "network rules that deny or open ports; read them with sandbox.network.get()");
|
|
1116
|
-
const destinations = !policy.internet
|
|
1117
|
-
? []
|
|
1118
|
-
: !policy.allow.length
|
|
1119
|
-
? [{ public: true }]
|
|
1120
|
-
: policy.allow.map((entry) => {
|
|
1121
|
-
const cidr = cidrOf(entry);
|
|
1122
|
-
if (!cidr)
|
|
1123
|
-
throw new CompatibilityError("Freestyle", "network rules that name hosts; read them with sandbox.network.get()");
|
|
1124
|
-
return { cidr };
|
|
1125
|
-
});
|
|
1126
|
-
const at = policy.updatedAt ?? s.info.createdAt;
|
|
1127
|
-
const rules = destinations.map((destination, index) => ruleData(s.id, index, destination, at));
|
|
1128
|
-
return { rules, totalCount: rules.length };
|
|
1129
|
-
});
|
|
1130
|
-
}
|
|
1131
|
-
linuxUser(linuxUser) {
|
|
1132
|
-
return new VmLinuxUser(this, linuxUser);
|
|
1133
|
-
}
|
|
1134
|
-
async update(options) {
|
|
1135
|
-
only("Freestyle update", options, ["slug", "displayName", "idleTimeoutSeconds", "metadata"]);
|
|
1136
|
-
validateMetadata(options.metadata);
|
|
1137
|
-
return api(async () => {
|
|
1138
|
-
const s = await lookup(this.runtime, this.id);
|
|
1139
|
-
await s.update({
|
|
1140
|
-
name: options.slug,
|
|
1141
|
-
idlePauseSeconds: options.idleTimeoutSeconds === -1 ? 0 : options.idleTimeoutSeconds,
|
|
1142
|
-
labels: {
|
|
1143
|
-
...s.info.labels,
|
|
1144
|
-
...options.metadata,
|
|
1145
|
-
...(options.displayName === undefined ? {} : { "fs.displayName": options.displayName }),
|
|
1146
|
-
},
|
|
1147
|
-
});
|
|
1148
|
-
return data(s);
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
async exec(options) {
|
|
1152
|
-
const o = typeof options === "string" ? { command: options } : options;
|
|
1153
|
-
only("Freestyle exec", o, ["command", "linuxUser", "timeoutMs", "env", "stdin"]);
|
|
1154
|
-
const user = o.linuxUser === undefined ? undefined : validateLinuxUser(o.linuxUser);
|
|
1155
|
-
return api(async () => {
|
|
1156
|
-
const s = await lookup(this.runtime, this.id);
|
|
1157
|
-
const native = await execOptions(s, {
|
|
1158
|
-
onStdout: () => undefined,
|
|
1159
|
-
onStderr: () => undefined,
|
|
1160
|
-
timeoutMs: o.timeoutMs,
|
|
1161
|
-
env: o.env,
|
|
1162
|
-
stdin: o.stdin === undefined
|
|
1163
|
-
? undefined
|
|
1164
|
-
: Uint8Array.from(atob(o.stdin), (c) => c.charCodeAt(0)),
|
|
1165
|
-
});
|
|
1166
|
-
const r = user
|
|
1167
|
-
? await s.exec(asUser(user, o.command, native.env), native)
|
|
1168
|
-
: await s.exec(o.command, native);
|
|
1169
|
-
return { stdout: r.stdout, stderr: r.stderr, statusCode: r.timedOut ? null : r.exitCode };
|
|
1170
|
-
});
|
|
1171
|
-
}
|
|
1172
|
-
async snapshot(options = {}) {
|
|
1173
|
-
only("Freestyle snapshot", options, ["slug", "displayName", "ttlSeconds"]);
|
|
1174
|
-
const days = retentionDays(options.ttlSeconds);
|
|
1175
|
-
return api(async () => {
|
|
1176
|
-
const s = await (await lookup(this.runtime, this.id)).snapshot({
|
|
1177
|
-
name: options.slug,
|
|
1178
|
-
labels: options.displayName ? { "fs.displayName": options.displayName } : undefined,
|
|
1179
|
-
retentionDays: days,
|
|
1180
|
-
});
|
|
1181
|
-
return { snapshotId: s.id, sourceVmId: s.sourceSandboxId, snapshot: snapshotData(s) };
|
|
1182
|
-
});
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
refuse("Freestyle", "Vm", Vm, {
|
|
1186
|
-
resize: "Runtime resizes by restarting, which ends running processes: call sandbox.resize() on the native client",
|
|
1187
|
-
});
|