gitx.do 0.1.0 → 0.1.2
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/README.md +40 -353
- package/dist/do/logger.d.ts +50 -0
- package/dist/do/logger.d.ts.map +1 -0
- package/dist/do/logger.js +122 -0
- package/dist/do/logger.js.map +1 -0
- package/dist/{durable-object → do}/schema.d.ts +3 -3
- package/dist/do/schema.d.ts.map +1 -0
- package/dist/{durable-object → do}/schema.js +4 -3
- package/dist/do/schema.js.map +1 -0
- package/dist/do/types.d.ts +267 -0
- package/dist/do/types.d.ts.map +1 -0
- package/dist/do/types.js +62 -0
- package/dist/do/types.js.map +1 -0
- package/dist/index.d.ts +15 -415
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -483
- package/dist/index.js.map +1 -1
- package/package.json +13 -21
- package/dist/cli/commands/add.d.ts +0 -174
- package/dist/cli/commands/add.d.ts.map +0 -1
- package/dist/cli/commands/add.js +0 -131
- package/dist/cli/commands/add.js.map +0 -1
- package/dist/cli/commands/blame.d.ts +0 -259
- package/dist/cli/commands/blame.d.ts.map +0 -1
- package/dist/cli/commands/blame.js +0 -609
- package/dist/cli/commands/blame.js.map +0 -1
- package/dist/cli/commands/branch.d.ts +0 -249
- package/dist/cli/commands/branch.d.ts.map +0 -1
- package/dist/cli/commands/branch.js +0 -693
- package/dist/cli/commands/branch.js.map +0 -1
- package/dist/cli/commands/commit.d.ts +0 -182
- package/dist/cli/commands/commit.d.ts.map +0 -1
- package/dist/cli/commands/commit.js +0 -437
- package/dist/cli/commands/commit.js.map +0 -1
- package/dist/cli/commands/diff.d.ts +0 -464
- package/dist/cli/commands/diff.d.ts.map +0 -1
- package/dist/cli/commands/diff.js +0 -958
- package/dist/cli/commands/diff.js.map +0 -1
- package/dist/cli/commands/log.d.ts +0 -239
- package/dist/cli/commands/log.d.ts.map +0 -1
- package/dist/cli/commands/log.js +0 -535
- package/dist/cli/commands/log.js.map +0 -1
- package/dist/cli/commands/merge.d.ts +0 -106
- package/dist/cli/commands/merge.d.ts.map +0 -1
- package/dist/cli/commands/merge.js +0 -55
- package/dist/cli/commands/merge.js.map +0 -1
- package/dist/cli/commands/review.d.ts +0 -457
- package/dist/cli/commands/review.d.ts.map +0 -1
- package/dist/cli/commands/review.js +0 -533
- package/dist/cli/commands/review.js.map +0 -1
- package/dist/cli/commands/status.d.ts +0 -269
- package/dist/cli/commands/status.d.ts.map +0 -1
- package/dist/cli/commands/status.js +0 -493
- package/dist/cli/commands/status.js.map +0 -1
- package/dist/cli/commands/web.d.ts +0 -199
- package/dist/cli/commands/web.d.ts.map +0 -1
- package/dist/cli/commands/web.js +0 -696
- package/dist/cli/commands/web.js.map +0 -1
- package/dist/cli/fs-adapter.d.ts +0 -656
- package/dist/cli/fs-adapter.d.ts.map +0 -1
- package/dist/cli/fs-adapter.js +0 -1179
- package/dist/cli/fs-adapter.js.map +0 -1
- package/dist/cli/fsx-cli-adapter.d.ts +0 -359
- package/dist/cli/fsx-cli-adapter.d.ts.map +0 -1
- package/dist/cli/fsx-cli-adapter.js +0 -619
- package/dist/cli/fsx-cli-adapter.js.map +0 -1
- package/dist/cli/index.d.ts +0 -387
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/index.js +0 -523
- package/dist/cli/index.js.map +0 -1
- package/dist/cli/ui/components/DiffView.d.ts +0 -7
- package/dist/cli/ui/components/DiffView.d.ts.map +0 -1
- package/dist/cli/ui/components/DiffView.js +0 -11
- package/dist/cli/ui/components/DiffView.js.map +0 -1
- package/dist/cli/ui/components/ErrorDisplay.d.ts +0 -6
- package/dist/cli/ui/components/ErrorDisplay.d.ts.map +0 -1
- package/dist/cli/ui/components/ErrorDisplay.js +0 -11
- package/dist/cli/ui/components/ErrorDisplay.js.map +0 -1
- package/dist/cli/ui/components/FuzzySearch.d.ts +0 -9
- package/dist/cli/ui/components/FuzzySearch.d.ts.map +0 -1
- package/dist/cli/ui/components/FuzzySearch.js +0 -12
- package/dist/cli/ui/components/FuzzySearch.js.map +0 -1
- package/dist/cli/ui/components/LoadingSpinner.d.ts +0 -6
- package/dist/cli/ui/components/LoadingSpinner.d.ts.map +0 -1
- package/dist/cli/ui/components/LoadingSpinner.js +0 -10
- package/dist/cli/ui/components/LoadingSpinner.js.map +0 -1
- package/dist/cli/ui/components/NavigationList.d.ts +0 -9
- package/dist/cli/ui/components/NavigationList.d.ts.map +0 -1
- package/dist/cli/ui/components/NavigationList.js +0 -11
- package/dist/cli/ui/components/NavigationList.js.map +0 -1
- package/dist/cli/ui/components/ScrollableContent.d.ts +0 -8
- package/dist/cli/ui/components/ScrollableContent.d.ts.map +0 -1
- package/dist/cli/ui/components/ScrollableContent.js +0 -11
- package/dist/cli/ui/components/ScrollableContent.js.map +0 -1
- package/dist/cli/ui/components/index.d.ts +0 -7
- package/dist/cli/ui/components/index.d.ts.map +0 -1
- package/dist/cli/ui/components/index.js +0 -9
- package/dist/cli/ui/components/index.js.map +0 -1
- package/dist/cli/ui/terminal-ui.d.ts +0 -52
- package/dist/cli/ui/terminal-ui.d.ts.map +0 -1
- package/dist/cli/ui/terminal-ui.js +0 -121
- package/dist/cli/ui/terminal-ui.js.map +0 -1
- package/dist/do/BashModule.d.ts +0 -871
- package/dist/do/BashModule.d.ts.map +0 -1
- package/dist/do/BashModule.js +0 -1143
- package/dist/do/BashModule.js.map +0 -1
- package/dist/do/FsModule.d.ts +0 -601
- package/dist/do/FsModule.d.ts.map +0 -1
- package/dist/do/FsModule.js +0 -1120
- package/dist/do/FsModule.js.map +0 -1
- package/dist/do/GitModule.d.ts +0 -635
- package/dist/do/GitModule.d.ts.map +0 -1
- package/dist/do/GitModule.js +0 -781
- package/dist/do/GitModule.js.map +0 -1
- package/dist/do/GitRepoDO.d.ts +0 -281
- package/dist/do/GitRepoDO.d.ts.map +0 -1
- package/dist/do/GitRepoDO.js +0 -479
- package/dist/do/GitRepoDO.js.map +0 -1
- package/dist/do/bash-ast.d.ts +0 -246
- package/dist/do/bash-ast.d.ts.map +0 -1
- package/dist/do/bash-ast.js +0 -888
- package/dist/do/bash-ast.js.map +0 -1
- package/dist/do/container-executor.d.ts +0 -491
- package/dist/do/container-executor.d.ts.map +0 -1
- package/dist/do/container-executor.js +0 -730
- package/dist/do/container-executor.js.map +0 -1
- package/dist/do/index.d.ts +0 -53
- package/dist/do/index.d.ts.map +0 -1
- package/dist/do/index.js +0 -91
- package/dist/do/index.js.map +0 -1
- package/dist/do/tiered-storage.d.ts +0 -403
- package/dist/do/tiered-storage.d.ts.map +0 -1
- package/dist/do/tiered-storage.js +0 -689
- package/dist/do/tiered-storage.js.map +0 -1
- package/dist/do/withBash.d.ts +0 -231
- package/dist/do/withBash.d.ts.map +0 -1
- package/dist/do/withBash.js +0 -244
- package/dist/do/withBash.js.map +0 -1
- package/dist/do/withFs.d.ts +0 -237
- package/dist/do/withFs.d.ts.map +0 -1
- package/dist/do/withFs.js +0 -387
- package/dist/do/withFs.js.map +0 -1
- package/dist/do/withGit.d.ts +0 -180
- package/dist/do/withGit.d.ts.map +0 -1
- package/dist/do/withGit.js +0 -271
- package/dist/do/withGit.js.map +0 -1
- package/dist/durable-object/object-store.d.ts +0 -633
- package/dist/durable-object/object-store.d.ts.map +0 -1
- package/dist/durable-object/object-store.js +0 -1161
- package/dist/durable-object/object-store.js.map +0 -1
- package/dist/durable-object/schema.d.ts.map +0 -1
- package/dist/durable-object/schema.js.map +0 -1
- package/dist/durable-object/wal.d.ts +0 -416
- package/dist/durable-object/wal.d.ts.map +0 -1
- package/dist/durable-object/wal.js +0 -445
- package/dist/durable-object/wal.js.map +0 -1
- package/dist/mcp/adapter.d.ts +0 -772
- package/dist/mcp/adapter.d.ts.map +0 -1
- package/dist/mcp/adapter.js +0 -895
- package/dist/mcp/adapter.js.map +0 -1
- package/dist/mcp/sandbox/miniflare-evaluator.d.ts +0 -22
- package/dist/mcp/sandbox/miniflare-evaluator.d.ts.map +0 -1
- package/dist/mcp/sandbox/miniflare-evaluator.js +0 -140
- package/dist/mcp/sandbox/miniflare-evaluator.js.map +0 -1
- package/dist/mcp/sandbox/object-store-proxy.d.ts +0 -32
- package/dist/mcp/sandbox/object-store-proxy.d.ts.map +0 -1
- package/dist/mcp/sandbox/object-store-proxy.js +0 -30
- package/dist/mcp/sandbox/object-store-proxy.js.map +0 -1
- package/dist/mcp/sandbox/template.d.ts +0 -17
- package/dist/mcp/sandbox/template.d.ts.map +0 -1
- package/dist/mcp/sandbox/template.js +0 -71
- package/dist/mcp/sandbox/template.js.map +0 -1
- package/dist/mcp/sandbox.d.ts +0 -764
- package/dist/mcp/sandbox.d.ts.map +0 -1
- package/dist/mcp/sandbox.js +0 -1362
- package/dist/mcp/sandbox.js.map +0 -1
- package/dist/mcp/sdk-adapter.d.ts +0 -835
- package/dist/mcp/sdk-adapter.d.ts.map +0 -1
- package/dist/mcp/sdk-adapter.js +0 -974
- package/dist/mcp/sdk-adapter.js.map +0 -1
- package/dist/mcp/tools/do.d.ts +0 -32
- package/dist/mcp/tools/do.d.ts.map +0 -1
- package/dist/mcp/tools/do.js +0 -115
- package/dist/mcp/tools/do.js.map +0 -1
- package/dist/mcp/tools.d.ts +0 -548
- package/dist/mcp/tools.d.ts.map +0 -1
- package/dist/mcp/tools.js +0 -1934
- package/dist/mcp/tools.js.map +0 -1
- package/dist/ops/blame.d.ts +0 -551
- package/dist/ops/blame.d.ts.map +0 -1
- package/dist/ops/blame.js +0 -1037
- package/dist/ops/blame.js.map +0 -1
- package/dist/ops/branch.d.ts +0 -766
- package/dist/ops/branch.d.ts.map +0 -1
- package/dist/ops/branch.js +0 -950
- package/dist/ops/branch.js.map +0 -1
- package/dist/ops/commit-traversal.d.ts +0 -349
- package/dist/ops/commit-traversal.d.ts.map +0 -1
- package/dist/ops/commit-traversal.js +0 -821
- package/dist/ops/commit-traversal.js.map +0 -1
- package/dist/ops/commit.d.ts +0 -555
- package/dist/ops/commit.d.ts.map +0 -1
- package/dist/ops/commit.js +0 -826
- package/dist/ops/commit.js.map +0 -1
- package/dist/ops/merge-base.d.ts +0 -397
- package/dist/ops/merge-base.d.ts.map +0 -1
- package/dist/ops/merge-base.js +0 -691
- package/dist/ops/merge-base.js.map +0 -1
- package/dist/ops/merge.d.ts +0 -855
- package/dist/ops/merge.d.ts.map +0 -1
- package/dist/ops/merge.js +0 -1551
- package/dist/ops/merge.js.map +0 -1
- package/dist/ops/tag.d.ts +0 -247
- package/dist/ops/tag.d.ts.map +0 -1
- package/dist/ops/tag.js +0 -649
- package/dist/ops/tag.js.map +0 -1
- package/dist/ops/tree-builder.d.ts +0 -178
- package/dist/ops/tree-builder.d.ts.map +0 -1
- package/dist/ops/tree-builder.js +0 -271
- package/dist/ops/tree-builder.js.map +0 -1
- package/dist/ops/tree-diff.d.ts +0 -291
- package/dist/ops/tree-diff.d.ts.map +0 -1
- package/dist/ops/tree-diff.js +0 -705
- package/dist/ops/tree-diff.js.map +0 -1
- package/dist/pack/delta.d.ts +0 -248
- package/dist/pack/delta.d.ts.map +0 -1
- package/dist/pack/delta.js +0 -736
- package/dist/pack/delta.js.map +0 -1
- package/dist/pack/format.d.ts +0 -446
- package/dist/pack/format.d.ts.map +0 -1
- package/dist/pack/format.js +0 -572
- package/dist/pack/format.js.map +0 -1
- package/dist/pack/full-generation.d.ts +0 -612
- package/dist/pack/full-generation.d.ts.map +0 -1
- package/dist/pack/full-generation.js +0 -1378
- package/dist/pack/full-generation.js.map +0 -1
- package/dist/pack/generation.d.ts +0 -441
- package/dist/pack/generation.d.ts.map +0 -1
- package/dist/pack/generation.js +0 -707
- package/dist/pack/generation.js.map +0 -1
- package/dist/pack/index.d.ts +0 -502
- package/dist/pack/index.d.ts.map +0 -1
- package/dist/pack/index.js +0 -833
- package/dist/pack/index.js.map +0 -1
- package/dist/refs/branch.d.ts +0 -668
- package/dist/refs/branch.d.ts.map +0 -1
- package/dist/refs/branch.js +0 -897
- package/dist/refs/branch.js.map +0 -1
- package/dist/refs/storage.d.ts +0 -833
- package/dist/refs/storage.d.ts.map +0 -1
- package/dist/refs/storage.js +0 -1023
- package/dist/refs/storage.js.map +0 -1
- package/dist/refs/tag.d.ts +0 -860
- package/dist/refs/tag.d.ts.map +0 -1
- package/dist/refs/tag.js +0 -996
- package/dist/refs/tag.js.map +0 -1
- package/dist/storage/backend.d.ts +0 -425
- package/dist/storage/backend.d.ts.map +0 -1
- package/dist/storage/backend.js +0 -41
- package/dist/storage/backend.js.map +0 -1
- package/dist/storage/fsx-adapter.d.ts +0 -204
- package/dist/storage/fsx-adapter.d.ts.map +0 -1
- package/dist/storage/fsx-adapter.js +0 -470
- package/dist/storage/fsx-adapter.js.map +0 -1
- package/dist/storage/lru-cache.d.ts +0 -691
- package/dist/storage/lru-cache.d.ts.map +0 -1
- package/dist/storage/lru-cache.js +0 -813
- package/dist/storage/lru-cache.js.map +0 -1
- package/dist/storage/object-index.d.ts +0 -585
- package/dist/storage/object-index.d.ts.map +0 -1
- package/dist/storage/object-index.js +0 -532
- package/dist/storage/object-index.js.map +0 -1
- package/dist/storage/r2-pack.d.ts +0 -1257
- package/dist/storage/r2-pack.d.ts.map +0 -1
- package/dist/storage/r2-pack.js +0 -1770
- package/dist/storage/r2-pack.js.map +0 -1
- package/dist/tiered/cdc-pipeline.d.ts +0 -1888
- package/dist/tiered/cdc-pipeline.d.ts.map +0 -1
- package/dist/tiered/cdc-pipeline.js +0 -1880
- package/dist/tiered/cdc-pipeline.js.map +0 -1
- package/dist/tiered/migration.d.ts +0 -1104
- package/dist/tiered/migration.d.ts.map +0 -1
- package/dist/tiered/migration.js +0 -1214
- package/dist/tiered/migration.js.map +0 -1
- package/dist/tiered/parquet-writer.d.ts +0 -1145
- package/dist/tiered/parquet-writer.d.ts.map +0 -1
- package/dist/tiered/parquet-writer.js +0 -1183
- package/dist/tiered/parquet-writer.js.map +0 -1
- package/dist/tiered/read-path.d.ts +0 -835
- package/dist/tiered/read-path.d.ts.map +0 -1
- package/dist/tiered/read-path.js +0 -487
- package/dist/tiered/read-path.js.map +0 -1
- package/dist/types/capability.d.ts +0 -1385
- package/dist/types/capability.d.ts.map +0 -1
- package/dist/types/capability.js +0 -36
- package/dist/types/capability.js.map +0 -1
- package/dist/types/index.d.ts +0 -13
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -18
- package/dist/types/index.js.map +0 -1
- package/dist/types/objects.d.ts +0 -692
- package/dist/types/objects.d.ts.map +0 -1
- package/dist/types/objects.js +0 -837
- package/dist/types/objects.js.map +0 -1
- package/dist/types/storage.d.ts +0 -603
- package/dist/types/storage.d.ts.map +0 -1
- package/dist/types/storage.js +0 -191
- package/dist/types/storage.js.map +0 -1
- package/dist/types/worker-loader.d.ts +0 -60
- package/dist/types/worker-loader.d.ts.map +0 -1
- package/dist/types/worker-loader.js +0 -62
- package/dist/types/worker-loader.js.map +0 -1
- package/dist/utils/hash.d.ts +0 -197
- package/dist/utils/hash.d.ts.map +0 -1
- package/dist/utils/hash.js +0 -268
- package/dist/utils/hash.js.map +0 -1
- package/dist/utils/sha1.d.ts +0 -290
- package/dist/utils/sha1.d.ts.map +0 -1
- package/dist/utils/sha1.js +0 -582
- package/dist/utils/sha1.js.map +0 -1
- package/dist/wire/capabilities.d.ts +0 -1044
- package/dist/wire/capabilities.d.ts.map +0 -1
- package/dist/wire/capabilities.js +0 -941
- package/dist/wire/capabilities.js.map +0 -1
- package/dist/wire/path-security.d.ts +0 -157
- package/dist/wire/path-security.d.ts.map +0 -1
- package/dist/wire/path-security.js +0 -307
- package/dist/wire/path-security.js.map +0 -1
- package/dist/wire/pkt-line.d.ts +0 -345
- package/dist/wire/pkt-line.d.ts.map +0 -1
- package/dist/wire/pkt-line.js +0 -381
- package/dist/wire/pkt-line.js.map +0 -1
- package/dist/wire/receive-pack.d.ts +0 -1059
- package/dist/wire/receive-pack.d.ts.map +0 -1
- package/dist/wire/receive-pack.js +0 -1414
- package/dist/wire/receive-pack.js.map +0 -1
- package/dist/wire/smart-http.d.ts +0 -799
- package/dist/wire/smart-http.d.ts.map +0 -1
- package/dist/wire/smart-http.js +0 -945
- package/dist/wire/smart-http.js.map +0 -1
- package/dist/wire/upload-pack.d.ts +0 -727
- package/dist/wire/upload-pack.d.ts.map +0 -1
- package/dist/wire/upload-pack.js +0 -1138
- package/dist/wire/upload-pack.js.map +0 -1
|
@@ -1,730 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Cloudflare Container Executor Integration
|
|
3
|
-
*
|
|
4
|
-
* This module provides executor implementations for running bash commands
|
|
5
|
-
* in Cloudflare Containers. It supports multiple execution modes:
|
|
6
|
-
*
|
|
7
|
-
* - HTTP-based execution with the Sandbox SDK (exec/execStream)
|
|
8
|
-
* - WebSocket streaming for interactive sessions
|
|
9
|
-
* - Session isolation for multi-tenant environments
|
|
10
|
-
*
|
|
11
|
-
* @module do/container-executor
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { CloudflareContainerExecutor } from 'gitx.do/do'
|
|
16
|
-
*
|
|
17
|
-
* const executor = new CloudflareContainerExecutor({
|
|
18
|
-
* sandbox: env.Sandbox,
|
|
19
|
-
* sessionId: 'user-123',
|
|
20
|
-
* })
|
|
21
|
-
*
|
|
22
|
-
* const result = await executor.execute('ls -la')
|
|
23
|
-
* console.log(result.stdout)
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
// ============================================================================
|
|
27
|
-
// CloudflareContainerExecutor Class
|
|
28
|
-
// ============================================================================
|
|
29
|
-
/**
|
|
30
|
-
* CloudflareContainerExecutor - Execute bash commands in Cloudflare Containers.
|
|
31
|
-
*
|
|
32
|
-
* This executor implements the BashExecutor interface and supports multiple
|
|
33
|
-
* execution backends:
|
|
34
|
-
*
|
|
35
|
-
* 1. **Sandbox SDK**: Uses `exec()` and `execStream()` from @cloudflare/sandbox
|
|
36
|
-
* 2. **HTTP Exec**: Sends commands to an HTTP endpoint in the container
|
|
37
|
-
* 3. **WebSocket Streaming**: Connects to container via WebSocket for streaming
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* ```typescript
|
|
41
|
-
* // Using Sandbox SDK
|
|
42
|
-
* const executor = new CloudflareContainerExecutor({
|
|
43
|
-
* sandbox: env.Sandbox,
|
|
44
|
-
* sessionId: 'session-123',
|
|
45
|
-
* })
|
|
46
|
-
*
|
|
47
|
-
* // Execute a command
|
|
48
|
-
* const result = await executor.execute('npm install')
|
|
49
|
-
*
|
|
50
|
-
* // Spawn for streaming
|
|
51
|
-
* const handle = await executor.spawn('npm', ['run', 'dev'], {
|
|
52
|
-
* onStdout: (chunk) => console.log(chunk),
|
|
53
|
-
* onStderr: (chunk) => console.error(chunk),
|
|
54
|
-
* })
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```typescript
|
|
59
|
-
* // Using HTTP exec endpoint
|
|
60
|
-
* const executor = new CloudflareContainerExecutor({
|
|
61
|
-
* httpExecEndpoint: 'https://container.example.com/exec',
|
|
62
|
-
* sessionId: 'session-456',
|
|
63
|
-
* })
|
|
64
|
-
*
|
|
65
|
-
* const result = await executor.execute('ls -la')
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
export class CloudflareContainerExecutor {
|
|
69
|
-
sandbox;
|
|
70
|
-
container;
|
|
71
|
-
sessionId;
|
|
72
|
-
defaultCwd;
|
|
73
|
-
defaultTimeout;
|
|
74
|
-
defaultEnv;
|
|
75
|
-
wsEndpoint;
|
|
76
|
-
httpExecEndpoint;
|
|
77
|
-
fetchFn;
|
|
78
|
-
/**
|
|
79
|
-
* Session state for isolation.
|
|
80
|
-
*/
|
|
81
|
-
session;
|
|
82
|
-
/**
|
|
83
|
-
* Process ID counter for spawn handles.
|
|
84
|
-
*/
|
|
85
|
-
pidCounter = 1000;
|
|
86
|
-
/**
|
|
87
|
-
* Create a new CloudflareContainerExecutor.
|
|
88
|
-
*
|
|
89
|
-
* @param options - Configuration options
|
|
90
|
-
*/
|
|
91
|
-
constructor(options = {}) {
|
|
92
|
-
this.sandbox = options.sandbox;
|
|
93
|
-
this.container = options.container;
|
|
94
|
-
this.sessionId = options.sessionId ?? crypto.randomUUID();
|
|
95
|
-
this.defaultCwd = options.cwd ?? '/';
|
|
96
|
-
this.defaultTimeout = options.timeout ?? 30000;
|
|
97
|
-
this.defaultEnv = options.env ?? {};
|
|
98
|
-
this.wsEndpoint = options.wsEndpoint;
|
|
99
|
-
this.httpExecEndpoint = options.httpExecEndpoint;
|
|
100
|
-
this.fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
101
|
-
// Initialize session state
|
|
102
|
-
this.session = {
|
|
103
|
-
id: this.sessionId,
|
|
104
|
-
cwd: this.defaultCwd,
|
|
105
|
-
env: { ...this.defaultEnv },
|
|
106
|
-
processes: new Map(),
|
|
107
|
-
lastActivity: Date.now(),
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
// ===========================================================================
|
|
111
|
-
// BashExecutor Interface
|
|
112
|
-
// ===========================================================================
|
|
113
|
-
/**
|
|
114
|
-
* Execute a command and return the result.
|
|
115
|
-
*
|
|
116
|
-
* @param command - The command to execute
|
|
117
|
-
* @param options - Execution options
|
|
118
|
-
* @returns Promise resolving to the execution result
|
|
119
|
-
*/
|
|
120
|
-
async execute(command, options) {
|
|
121
|
-
this.session.lastActivity = Date.now();
|
|
122
|
-
const cwd = options?.cwd ?? this.session.cwd;
|
|
123
|
-
const timeout = options?.timeout ?? this.defaultTimeout;
|
|
124
|
-
const env = { ...this.session.env, ...options?.env };
|
|
125
|
-
// Try sandbox SDK first
|
|
126
|
-
if (this.sandbox) {
|
|
127
|
-
return this.executeViaSandbox(command, { cwd, timeout, env, stdin: options?.stdin });
|
|
128
|
-
}
|
|
129
|
-
// Try HTTP exec endpoint
|
|
130
|
-
if (this.httpExecEndpoint) {
|
|
131
|
-
return this.executeViaHttp(command, { cwd, timeout, env, stdin: options?.stdin });
|
|
132
|
-
}
|
|
133
|
-
// Try container fetch with custom exec endpoint
|
|
134
|
-
if (this.container) {
|
|
135
|
-
return this.executeViaContainer(command, { cwd, timeout, env, stdin: options?.stdin });
|
|
136
|
-
}
|
|
137
|
-
// No execution backend available
|
|
138
|
-
return {
|
|
139
|
-
command,
|
|
140
|
-
stdout: '',
|
|
141
|
-
stderr: 'No execution backend configured (sandbox, httpExecEndpoint, or container required)',
|
|
142
|
-
exitCode: 1,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Spawn a command for streaming execution.
|
|
147
|
-
*
|
|
148
|
-
* @param command - The command to spawn
|
|
149
|
-
* @param args - Command arguments
|
|
150
|
-
* @param options - Spawn options
|
|
151
|
-
* @returns Promise resolving to a spawn handle
|
|
152
|
-
*/
|
|
153
|
-
async spawn(command, args, options) {
|
|
154
|
-
this.session.lastActivity = Date.now();
|
|
155
|
-
const fullCommand = args?.length ? `${command} ${args.join(' ')}` : command;
|
|
156
|
-
const cwd = options?.cwd ?? this.session.cwd;
|
|
157
|
-
const timeout = options?.timeout ?? this.defaultTimeout;
|
|
158
|
-
const env = { ...this.session.env, ...options?.env };
|
|
159
|
-
// Try sandbox SDK streaming
|
|
160
|
-
if (this.sandbox?.execStream) {
|
|
161
|
-
return this.spawnViaSandbox(fullCommand, { cwd, timeout, env }, options);
|
|
162
|
-
}
|
|
163
|
-
// Try WebSocket streaming
|
|
164
|
-
if (this.wsEndpoint) {
|
|
165
|
-
return this.spawnViaWebSocket(fullCommand, { cwd, timeout, env }, options);
|
|
166
|
-
}
|
|
167
|
-
// Try sandbox startProcess
|
|
168
|
-
if (this.sandbox?.startProcess) {
|
|
169
|
-
return this.spawnViaProcess(fullCommand, { cwd, timeout, env }, options);
|
|
170
|
-
}
|
|
171
|
-
// Fallback: execute and simulate streaming
|
|
172
|
-
return this.spawnViaExec(fullCommand, { cwd, timeout, env }, options);
|
|
173
|
-
}
|
|
174
|
-
// ===========================================================================
|
|
175
|
-
// Sandbox SDK Execution
|
|
176
|
-
// ===========================================================================
|
|
177
|
-
/**
|
|
178
|
-
* Execute a command using the Sandbox SDK.
|
|
179
|
-
*/
|
|
180
|
-
async executeViaSandbox(command, options) {
|
|
181
|
-
try {
|
|
182
|
-
const result = await this.sandbox.exec(command, options);
|
|
183
|
-
return {
|
|
184
|
-
command,
|
|
185
|
-
stdout: result.stdout,
|
|
186
|
-
stderr: result.stderr,
|
|
187
|
-
exitCode: result.exitCode,
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
catch (error) {
|
|
191
|
-
return {
|
|
192
|
-
command,
|
|
193
|
-
stdout: '',
|
|
194
|
-
stderr: error instanceof Error ? error.message : String(error),
|
|
195
|
-
exitCode: 1,
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Spawn a command using Sandbox SDK streaming.
|
|
201
|
-
*/
|
|
202
|
-
async spawnViaSandbox(command, execOptions, spawnOptions) {
|
|
203
|
-
const pid = this.pidCounter++;
|
|
204
|
-
let abortController = null;
|
|
205
|
-
const streamResult = await this.sandbox.execStream(command, execOptions);
|
|
206
|
-
abortController = { abort: streamResult.abort };
|
|
207
|
-
// Process the stream
|
|
208
|
-
const reader = streamResult.stream.getReader();
|
|
209
|
-
const processStream = async () => {
|
|
210
|
-
let stdout = '';
|
|
211
|
-
let stderr = '';
|
|
212
|
-
let exitCode = 0;
|
|
213
|
-
try {
|
|
214
|
-
while (true) {
|
|
215
|
-
const { done, value } = await reader.read();
|
|
216
|
-
if (done)
|
|
217
|
-
break;
|
|
218
|
-
if (value.type === 'stdout') {
|
|
219
|
-
const data = String(value.data);
|
|
220
|
-
stdout += data;
|
|
221
|
-
spawnOptions?.onStdout?.(data);
|
|
222
|
-
}
|
|
223
|
-
else if (value.type === 'stderr') {
|
|
224
|
-
const data = String(value.data);
|
|
225
|
-
stderr += data;
|
|
226
|
-
spawnOptions?.onStderr?.(data);
|
|
227
|
-
}
|
|
228
|
-
else if (value.type === 'exit') {
|
|
229
|
-
exitCode = Number(value.data);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
catch (error) {
|
|
234
|
-
stderr += error instanceof Error ? error.message : String(error);
|
|
235
|
-
exitCode = 1;
|
|
236
|
-
}
|
|
237
|
-
spawnOptions?.onExit?.(exitCode);
|
|
238
|
-
return { command, stdout, stderr, exitCode };
|
|
239
|
-
};
|
|
240
|
-
const donePromise = processStream();
|
|
241
|
-
const handle = {
|
|
242
|
-
pid,
|
|
243
|
-
done: donePromise,
|
|
244
|
-
kill: (_signal) => {
|
|
245
|
-
abortController?.abort();
|
|
246
|
-
},
|
|
247
|
-
write: (_data) => {
|
|
248
|
-
// Sandbox stream doesn't support stdin writing
|
|
249
|
-
},
|
|
250
|
-
closeStdin: () => {
|
|
251
|
-
// No-op for sandbox stream
|
|
252
|
-
},
|
|
253
|
-
};
|
|
254
|
-
return handle;
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Spawn a command using Sandbox SDK startProcess.
|
|
258
|
-
*/
|
|
259
|
-
async spawnViaProcess(command, execOptions, spawnOptions) {
|
|
260
|
-
const processHandle = await this.sandbox.startProcess(command, execOptions);
|
|
261
|
-
const pid = processHandle.pid;
|
|
262
|
-
// Track the process
|
|
263
|
-
this.session.processes.set(pid, processHandle);
|
|
264
|
-
// Set up exit handling
|
|
265
|
-
const donePromise = processHandle.exited.then(result => {
|
|
266
|
-
this.session.processes.delete(pid);
|
|
267
|
-
spawnOptions?.onExit?.(result.exitCode);
|
|
268
|
-
return {
|
|
269
|
-
command,
|
|
270
|
-
stdout: result.stdout,
|
|
271
|
-
stderr: result.stderr,
|
|
272
|
-
exitCode: result.exitCode,
|
|
273
|
-
};
|
|
274
|
-
});
|
|
275
|
-
const handle = {
|
|
276
|
-
pid,
|
|
277
|
-
done: donePromise,
|
|
278
|
-
kill: (signal) => {
|
|
279
|
-
processHandle.kill(signal);
|
|
280
|
-
},
|
|
281
|
-
write: (data) => {
|
|
282
|
-
processHandle.write(data);
|
|
283
|
-
},
|
|
284
|
-
closeStdin: () => {
|
|
285
|
-
processHandle.closeStdin();
|
|
286
|
-
},
|
|
287
|
-
};
|
|
288
|
-
return handle;
|
|
289
|
-
}
|
|
290
|
-
// ===========================================================================
|
|
291
|
-
// HTTP Exec Execution
|
|
292
|
-
// ===========================================================================
|
|
293
|
-
/**
|
|
294
|
-
* Execute a command via HTTP exec endpoint.
|
|
295
|
-
*/
|
|
296
|
-
async executeViaHttp(command, options) {
|
|
297
|
-
try {
|
|
298
|
-
const response = await this.fetchFn(this.httpExecEndpoint, {
|
|
299
|
-
method: 'POST',
|
|
300
|
-
headers: {
|
|
301
|
-
'Content-Type': 'application/json',
|
|
302
|
-
'X-Session-Id': this.sessionId,
|
|
303
|
-
},
|
|
304
|
-
body: JSON.stringify({
|
|
305
|
-
command,
|
|
306
|
-
cwd: options.cwd,
|
|
307
|
-
env: options.env,
|
|
308
|
-
timeout: options.timeout,
|
|
309
|
-
stdin: options.stdin,
|
|
310
|
-
}),
|
|
311
|
-
});
|
|
312
|
-
if (!response.ok) {
|
|
313
|
-
const errorText = await response.text();
|
|
314
|
-
return {
|
|
315
|
-
command,
|
|
316
|
-
stdout: '',
|
|
317
|
-
stderr: `HTTP exec failed: ${response.status} ${errorText}`,
|
|
318
|
-
exitCode: 1,
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
const result = await response.json();
|
|
322
|
-
return {
|
|
323
|
-
command,
|
|
324
|
-
stdout: result.stdout ?? '',
|
|
325
|
-
stderr: result.stderr ?? '',
|
|
326
|
-
exitCode: result.exitCode ?? (result.success ? 0 : 1),
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
catch (error) {
|
|
330
|
-
return {
|
|
331
|
-
command,
|
|
332
|
-
stdout: '',
|
|
333
|
-
stderr: error instanceof Error ? error.message : String(error),
|
|
334
|
-
exitCode: 1,
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
// ===========================================================================
|
|
339
|
-
// Container Fetch Execution
|
|
340
|
-
// ===========================================================================
|
|
341
|
-
/**
|
|
342
|
-
* Execute a command via container fetch.
|
|
343
|
-
*/
|
|
344
|
-
async executeViaContainer(command, options) {
|
|
345
|
-
try {
|
|
346
|
-
// Build the request to the container's exec endpoint
|
|
347
|
-
const container = this.container;
|
|
348
|
-
const request = new Request('http://container/exec', {
|
|
349
|
-
method: 'POST',
|
|
350
|
-
headers: {
|
|
351
|
-
'Content-Type': 'application/json',
|
|
352
|
-
'X-Session-Id': this.sessionId,
|
|
353
|
-
},
|
|
354
|
-
body: JSON.stringify({
|
|
355
|
-
command,
|
|
356
|
-
cwd: options.cwd,
|
|
357
|
-
env: options.env,
|
|
358
|
-
timeout: options.timeout,
|
|
359
|
-
stdin: options.stdin,
|
|
360
|
-
}),
|
|
361
|
-
});
|
|
362
|
-
const response = await container.fetch(request);
|
|
363
|
-
if (!response.ok) {
|
|
364
|
-
const errorText = await response.text();
|
|
365
|
-
return {
|
|
366
|
-
command,
|
|
367
|
-
stdout: '',
|
|
368
|
-
stderr: `Container exec failed: ${response.status} ${errorText}`,
|
|
369
|
-
exitCode: 1,
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
const result = await response.json();
|
|
373
|
-
return {
|
|
374
|
-
command,
|
|
375
|
-
stdout: result.stdout ?? '',
|
|
376
|
-
stderr: result.stderr ?? '',
|
|
377
|
-
exitCode: result.exitCode ?? (result.success ? 0 : 1),
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
catch (error) {
|
|
381
|
-
return {
|
|
382
|
-
command,
|
|
383
|
-
stdout: '',
|
|
384
|
-
stderr: error instanceof Error ? error.message : String(error),
|
|
385
|
-
exitCode: 1,
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
// ===========================================================================
|
|
390
|
-
// WebSocket Streaming
|
|
391
|
-
// ===========================================================================
|
|
392
|
-
/**
|
|
393
|
-
* Spawn a command via WebSocket connection.
|
|
394
|
-
*/
|
|
395
|
-
async spawnViaWebSocket(command, execOptions, spawnOptions) {
|
|
396
|
-
const pid = this.pidCounter++;
|
|
397
|
-
let ws = null;
|
|
398
|
-
const { promise: donePromise, resolve: resolveDone } = withResolvers();
|
|
399
|
-
let stdout = '';
|
|
400
|
-
let stderr = '';
|
|
401
|
-
let exitCode = 0;
|
|
402
|
-
try {
|
|
403
|
-
// Build WebSocket URL with session and command info
|
|
404
|
-
const wsUrl = new URL(this.wsEndpoint);
|
|
405
|
-
wsUrl.searchParams.set('session', this.sessionId);
|
|
406
|
-
wsUrl.searchParams.set('command', command);
|
|
407
|
-
if (execOptions.cwd)
|
|
408
|
-
wsUrl.searchParams.set('cwd', execOptions.cwd);
|
|
409
|
-
// Create WebSocket connection
|
|
410
|
-
ws = new WebSocket(wsUrl.toString());
|
|
411
|
-
ws.addEventListener('open', () => {
|
|
412
|
-
// Send initial configuration
|
|
413
|
-
ws.send(JSON.stringify({
|
|
414
|
-
type: 'init',
|
|
415
|
-
env: execOptions.env,
|
|
416
|
-
timeout: execOptions.timeout,
|
|
417
|
-
}));
|
|
418
|
-
});
|
|
419
|
-
ws.addEventListener('message', (event) => {
|
|
420
|
-
try {
|
|
421
|
-
const message = JSON.parse(String(event.data));
|
|
422
|
-
switch (message.type) {
|
|
423
|
-
case 'stdout': {
|
|
424
|
-
const outData = String(message.data ?? '');
|
|
425
|
-
stdout += outData;
|
|
426
|
-
spawnOptions?.onStdout?.(outData);
|
|
427
|
-
break;
|
|
428
|
-
}
|
|
429
|
-
case 'stderr': {
|
|
430
|
-
const errData = String(message.data ?? '');
|
|
431
|
-
stderr += errData;
|
|
432
|
-
spawnOptions?.onStderr?.(errData);
|
|
433
|
-
break;
|
|
434
|
-
}
|
|
435
|
-
case 'exit':
|
|
436
|
-
exitCode = Number(message.data ?? 0);
|
|
437
|
-
ws?.close();
|
|
438
|
-
break;
|
|
439
|
-
case 'error':
|
|
440
|
-
stderr += String(message.data ?? '');
|
|
441
|
-
exitCode = 1;
|
|
442
|
-
ws?.close();
|
|
443
|
-
break;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
catch {
|
|
447
|
-
// Handle non-JSON messages as stdout
|
|
448
|
-
const data = String(event.data);
|
|
449
|
-
stdout += data;
|
|
450
|
-
spawnOptions?.onStdout?.(data);
|
|
451
|
-
}
|
|
452
|
-
});
|
|
453
|
-
ws.addEventListener('close', () => {
|
|
454
|
-
spawnOptions?.onExit?.(exitCode);
|
|
455
|
-
resolveDone({ command, stdout, stderr, exitCode });
|
|
456
|
-
});
|
|
457
|
-
ws.addEventListener('error', (event) => {
|
|
458
|
-
stderr += `WebSocket error: ${event}`;
|
|
459
|
-
exitCode = 1;
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
|
-
catch (error) {
|
|
463
|
-
stderr = error instanceof Error ? error.message : String(error);
|
|
464
|
-
exitCode = 1;
|
|
465
|
-
resolveDone({ command, stdout, stderr, exitCode });
|
|
466
|
-
}
|
|
467
|
-
const handle = {
|
|
468
|
-
pid,
|
|
469
|
-
done: donePromise,
|
|
470
|
-
kill: (signal) => {
|
|
471
|
-
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
472
|
-
ws.send(JSON.stringify({ type: 'signal', signal: signal ?? 'SIGTERM' }));
|
|
473
|
-
ws.close();
|
|
474
|
-
}
|
|
475
|
-
},
|
|
476
|
-
write: (data) => {
|
|
477
|
-
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
478
|
-
ws.send(JSON.stringify({ type: 'stdin', data }));
|
|
479
|
-
}
|
|
480
|
-
},
|
|
481
|
-
closeStdin: () => {
|
|
482
|
-
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
483
|
-
ws.send(JSON.stringify({ type: 'stdin_close' }));
|
|
484
|
-
}
|
|
485
|
-
},
|
|
486
|
-
};
|
|
487
|
-
return handle;
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Spawn a command by executing and simulating streaming output.
|
|
491
|
-
*/
|
|
492
|
-
async spawnViaExec(command, execOptions, spawnOptions) {
|
|
493
|
-
const pid = this.pidCounter++;
|
|
494
|
-
const executeAndStream = async () => {
|
|
495
|
-
const result = await this.execute(command, {
|
|
496
|
-
cwd: execOptions.cwd,
|
|
497
|
-
timeout: execOptions.timeout,
|
|
498
|
-
env: execOptions.env,
|
|
499
|
-
stdin: execOptions.stdin,
|
|
500
|
-
});
|
|
501
|
-
// Simulate streaming by emitting all output at once
|
|
502
|
-
if (result.stdout) {
|
|
503
|
-
spawnOptions?.onStdout?.(result.stdout);
|
|
504
|
-
}
|
|
505
|
-
if (result.stderr) {
|
|
506
|
-
spawnOptions?.onStderr?.(result.stderr);
|
|
507
|
-
}
|
|
508
|
-
spawnOptions?.onExit?.(result.exitCode);
|
|
509
|
-
return result;
|
|
510
|
-
};
|
|
511
|
-
const handle = {
|
|
512
|
-
pid,
|
|
513
|
-
done: executeAndStream(),
|
|
514
|
-
kill: () => {
|
|
515
|
-
// Cannot kill a non-streaming execution
|
|
516
|
-
},
|
|
517
|
-
write: () => {
|
|
518
|
-
// Cannot write to a non-streaming execution
|
|
519
|
-
},
|
|
520
|
-
closeStdin: () => {
|
|
521
|
-
// No-op
|
|
522
|
-
},
|
|
523
|
-
};
|
|
524
|
-
return handle;
|
|
525
|
-
}
|
|
526
|
-
// ===========================================================================
|
|
527
|
-
// Session Management
|
|
528
|
-
// ===========================================================================
|
|
529
|
-
/**
|
|
530
|
-
* Get the current session ID.
|
|
531
|
-
*
|
|
532
|
-
* @returns The session ID
|
|
533
|
-
*/
|
|
534
|
-
getSessionId() {
|
|
535
|
-
return this.sessionId;
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Get the current session state.
|
|
539
|
-
*
|
|
540
|
-
* @returns Copy of the session state
|
|
541
|
-
*/
|
|
542
|
-
getSessionState() {
|
|
543
|
-
return {
|
|
544
|
-
id: this.session.id,
|
|
545
|
-
cwd: this.session.cwd,
|
|
546
|
-
env: { ...this.session.env },
|
|
547
|
-
lastActivity: this.session.lastActivity,
|
|
548
|
-
processCount: this.session.processes.size,
|
|
549
|
-
};
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Update the session working directory.
|
|
553
|
-
*
|
|
554
|
-
* @param cwd - New working directory
|
|
555
|
-
*/
|
|
556
|
-
setCwd(cwd) {
|
|
557
|
-
this.session.cwd = cwd;
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* Update session environment variables.
|
|
561
|
-
*
|
|
562
|
-
* @param env - Environment variables to set
|
|
563
|
-
*/
|
|
564
|
-
setEnv(env) {
|
|
565
|
-
Object.assign(this.session.env, env);
|
|
566
|
-
}
|
|
567
|
-
/**
|
|
568
|
-
* Unset session environment variables.
|
|
569
|
-
*
|
|
570
|
-
* @param keys - Environment variable keys to unset
|
|
571
|
-
*/
|
|
572
|
-
unsetEnv(keys) {
|
|
573
|
-
for (const key of keys) {
|
|
574
|
-
delete this.session.env[key];
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Get the number of active processes.
|
|
579
|
-
*
|
|
580
|
-
* @returns Number of active processes
|
|
581
|
-
*/
|
|
582
|
-
getActiveProcessCount() {
|
|
583
|
-
return this.session.processes.size;
|
|
584
|
-
}
|
|
585
|
-
/**
|
|
586
|
-
* Kill all active processes.
|
|
587
|
-
*
|
|
588
|
-
* @param signal - Signal to send (default: SIGTERM)
|
|
589
|
-
*/
|
|
590
|
-
async killAll(signal = 'SIGTERM') {
|
|
591
|
-
const kills = Array.from(this.session.processes.values()).map(handle => handle.kill(signal));
|
|
592
|
-
await Promise.all(kills);
|
|
593
|
-
this.session.processes.clear();
|
|
594
|
-
}
|
|
595
|
-
// ===========================================================================
|
|
596
|
-
// File Operations (when sandbox supports it)
|
|
597
|
-
// ===========================================================================
|
|
598
|
-
/**
|
|
599
|
-
* Write a file to the container filesystem.
|
|
600
|
-
*
|
|
601
|
-
* @param path - File path
|
|
602
|
-
* @param content - File content
|
|
603
|
-
*/
|
|
604
|
-
async writeFile(path, content) {
|
|
605
|
-
if (this.sandbox?.writeFile) {
|
|
606
|
-
await this.sandbox.writeFile(path, content);
|
|
607
|
-
}
|
|
608
|
-
else if (this.httpExecEndpoint) {
|
|
609
|
-
// Use HTTP endpoint for file writing
|
|
610
|
-
await this.fetchFn(this.httpExecEndpoint.replace('/exec', '/file'), {
|
|
611
|
-
method: 'PUT',
|
|
612
|
-
headers: {
|
|
613
|
-
'Content-Type': 'application/octet-stream',
|
|
614
|
-
'X-Session-Id': this.sessionId,
|
|
615
|
-
'X-File-Path': path,
|
|
616
|
-
},
|
|
617
|
-
body: content,
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
else {
|
|
621
|
-
throw new Error('File operations not supported by this executor');
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* Read a file from the container filesystem.
|
|
626
|
-
*
|
|
627
|
-
* @param path - File path
|
|
628
|
-
* @returns File content
|
|
629
|
-
*/
|
|
630
|
-
async readFile(path) {
|
|
631
|
-
if (this.sandbox?.readFile) {
|
|
632
|
-
return await this.sandbox.readFile(path);
|
|
633
|
-
}
|
|
634
|
-
else if (this.httpExecEndpoint) {
|
|
635
|
-
// Use HTTP endpoint for file reading
|
|
636
|
-
const response = await this.fetchFn(this.httpExecEndpoint.replace('/exec', '/file'), {
|
|
637
|
-
method: 'GET',
|
|
638
|
-
headers: {
|
|
639
|
-
'X-Session-Id': this.sessionId,
|
|
640
|
-
'X-File-Path': path,
|
|
641
|
-
},
|
|
642
|
-
});
|
|
643
|
-
if (!response.ok) {
|
|
644
|
-
throw new Error(`File read failed: ${response.status}`);
|
|
645
|
-
}
|
|
646
|
-
return await response.text();
|
|
647
|
-
}
|
|
648
|
-
else {
|
|
649
|
-
throw new Error('File operations not supported by this executor');
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
// ============================================================================
|
|
654
|
-
// Factory Functions
|
|
655
|
-
// ============================================================================
|
|
656
|
-
/**
|
|
657
|
-
* Create a CloudflareContainerExecutor instance.
|
|
658
|
-
*
|
|
659
|
-
* @param options - Configuration options
|
|
660
|
-
* @returns A new CloudflareContainerExecutor instance
|
|
661
|
-
*
|
|
662
|
-
* @example
|
|
663
|
-
* ```typescript
|
|
664
|
-
* const executor = createContainerExecutor({
|
|
665
|
-
* sandbox: env.Sandbox,
|
|
666
|
-
* sessionId: 'user-123',
|
|
667
|
-
* })
|
|
668
|
-
* ```
|
|
669
|
-
*/
|
|
670
|
-
export function createContainerExecutor(options = {}) {
|
|
671
|
-
return new CloudflareContainerExecutor(options);
|
|
672
|
-
}
|
|
673
|
-
/**
|
|
674
|
-
* Create an executor from a Sandbox binding.
|
|
675
|
-
*
|
|
676
|
-
* @param sandbox - Cloudflare Sandbox binding
|
|
677
|
-
* @param sessionId - Optional session ID for isolation
|
|
678
|
-
* @returns A new CloudflareContainerExecutor instance
|
|
679
|
-
*/
|
|
680
|
-
export function createSandboxExecutor(sandbox, sessionId) {
|
|
681
|
-
return new CloudflareContainerExecutor({ sandbox, sessionId });
|
|
682
|
-
}
|
|
683
|
-
/**
|
|
684
|
-
* Create an executor from an HTTP exec endpoint.
|
|
685
|
-
*
|
|
686
|
-
* @param endpoint - HTTP exec endpoint URL
|
|
687
|
-
* @param sessionId - Optional session ID for isolation
|
|
688
|
-
* @returns A new CloudflareContainerExecutor instance
|
|
689
|
-
*/
|
|
690
|
-
export function createHttpExecutor(endpoint, sessionId) {
|
|
691
|
-
return new CloudflareContainerExecutor({ httpExecEndpoint: endpoint, sessionId });
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* Create an executor from a WebSocket endpoint.
|
|
695
|
-
*
|
|
696
|
-
* @param endpoint - WebSocket endpoint URL
|
|
697
|
-
* @param sessionId - Optional session ID for isolation
|
|
698
|
-
* @returns A new CloudflareContainerExecutor instance
|
|
699
|
-
*/
|
|
700
|
-
export function createWebSocketExecutor(endpoint, sessionId) {
|
|
701
|
-
return new CloudflareContainerExecutor({ wsEndpoint: endpoint, sessionId });
|
|
702
|
-
}
|
|
703
|
-
// ============================================================================
|
|
704
|
-
// Type Guards
|
|
705
|
-
// ============================================================================
|
|
706
|
-
/**
|
|
707
|
-
* Check if a value is a CloudflareContainerExecutor instance.
|
|
708
|
-
*
|
|
709
|
-
* @param value - Value to check
|
|
710
|
-
* @returns True if value is a CloudflareContainerExecutor
|
|
711
|
-
*/
|
|
712
|
-
export function isContainerExecutor(value) {
|
|
713
|
-
return value instanceof CloudflareContainerExecutor;
|
|
714
|
-
}
|
|
715
|
-
// ============================================================================
|
|
716
|
-
// Helper Functions
|
|
717
|
-
// ============================================================================
|
|
718
|
-
/**
|
|
719
|
-
* Promise.withResolvers polyfill for older environments.
|
|
720
|
-
*/
|
|
721
|
-
function withResolvers() {
|
|
722
|
-
let resolve;
|
|
723
|
-
let reject;
|
|
724
|
-
const promise = new Promise((res, rej) => {
|
|
725
|
-
resolve = res;
|
|
726
|
-
reject = rej;
|
|
727
|
-
});
|
|
728
|
-
return { promise, resolve: resolve, reject: reject };
|
|
729
|
-
}
|
|
730
|
-
//# sourceMappingURL=container-executor.js.map
|