sandlot 0.1.2 → 0.1.3
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 +138 -408
- package/dist/build-emitter.d.ts +31 -13
- package/dist/build-emitter.d.ts.map +1 -1
- package/dist/builder.d.ts +370 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/bundler.d.ts +1 -1
- package/dist/bundler.d.ts.map +1 -1
- package/dist/commands/compile.d.ts +13 -0
- package/dist/commands/compile.d.ts.map +1 -0
- package/dist/commands/index.d.ts +17 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/packages.d.ts +17 -0
- package/dist/commands/packages.d.ts.map +1 -0
- package/dist/commands/run.d.ts +40 -0
- package/dist/commands/run.d.ts.map +1 -0
- package/dist/commands/types.d.ts +141 -0
- package/dist/commands/types.d.ts.map +1 -0
- package/dist/fs.d.ts +53 -49
- package/dist/fs.d.ts.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +249 -427
- package/dist/internal.js +111 -87
- package/dist/runner.d.ts +314 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/sandbox-manager.d.ts +45 -21
- package/dist/sandbox-manager.d.ts.map +1 -1
- package/dist/sandbox.d.ts +144 -62
- package/dist/sandbox.d.ts.map +1 -1
- package/dist/shared-modules.d.ts +22 -3
- package/dist/shared-modules.d.ts.map +1 -1
- package/dist/shared-resources.d.ts +0 -3
- package/dist/shared-resources.d.ts.map +1 -1
- package/dist/typechecker.d.ts +1 -1
- package/package.json +2 -11
- package/src/build-emitter.ts +32 -29
- package/src/builder.ts +498 -0
- package/src/bundler.ts +24 -36
- package/src/commands/compile.ts +236 -0
- package/src/commands/index.ts +51 -0
- package/src/commands/packages.ts +154 -0
- package/src/commands/run.ts +245 -0
- package/src/commands/types.ts +172 -0
- package/src/fs.ts +82 -221
- package/src/index.ts +17 -12
- package/src/sandbox.ts +217 -149
- package/src/shared-modules.ts +74 -4
- package/src/shared-resources.ts +0 -3
- package/src/typechecker.ts +1 -1
- package/dist/react.d.ts +0 -159
- package/dist/react.d.ts.map +0 -1
- package/dist/react.js +0 -149
- package/src/commands.ts +0 -733
- package/src/sandbox-manager.ts +0 -409
|
@@ -1,31 +1,40 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SandboxManager - Manages
|
|
2
|
+
* SandboxManager - Manages lifecycle and orchestration for multiple sandboxes.
|
|
3
3
|
*
|
|
4
4
|
* When running many sandboxes concurrently (e.g., for multiple AI agents),
|
|
5
|
-
* this manager
|
|
5
|
+
* this manager provides:
|
|
6
6
|
*
|
|
7
|
-
* -
|
|
8
|
-
* -
|
|
7
|
+
* - ID-based tracking and lookup of sandboxes
|
|
8
|
+
* - Batch operations (saveAll, destroyAll, getDirtySandboxes)
|
|
9
|
+
* - Manager-level default options inherited by all sandboxes
|
|
10
|
+
* - Statistics and introspection
|
|
11
|
+
*
|
|
12
|
+
* Note: Resources (TypeScript libs, bundler) are shared via a global singleton
|
|
13
|
+
* regardless of whether you use the manager or standalone sandbox functions.
|
|
14
|
+
* The manager's value is lifecycle management, not resource efficiency.
|
|
9
15
|
*
|
|
10
16
|
* Usage:
|
|
11
17
|
* ```ts
|
|
12
18
|
* const manager = await createSandboxManager();
|
|
13
19
|
*
|
|
14
|
-
* // Create multiple sandboxes
|
|
15
|
-
* const sandbox1 = await manager.createSandbox({ ... });
|
|
16
|
-
* const sandbox2 = await manager.createSandbox({ ... });
|
|
20
|
+
* // Create multiple sandboxes with automatic ID tracking
|
|
21
|
+
* const sandbox1 = await manager.createSandbox({ id: "agent-1", ... });
|
|
22
|
+
* const sandbox2 = await manager.createSandbox({ id: "agent-2", ... });
|
|
17
23
|
*
|
|
18
|
-
* //
|
|
24
|
+
* // Lookup, batch operations, etc.
|
|
25
|
+
* manager.getSandbox("agent-1");
|
|
26
|
+
* manager.getDirtySandboxes();
|
|
27
|
+
* await manager.saveAll();
|
|
19
28
|
*
|
|
20
|
-
* // Clean up
|
|
29
|
+
* // Clean up all at once
|
|
21
30
|
* manager.destroyAll();
|
|
22
31
|
* ```
|
|
23
32
|
*/
|
|
24
33
|
import { defineCommand } from "just-bash/browser";
|
|
25
34
|
import { type IndexedDbFsOptions } from "./fs";
|
|
26
35
|
import { type BundleResult } from "./bundler";
|
|
27
|
-
import { type SharedResources
|
|
28
|
-
import type { Sandbox } from "./sandbox";
|
|
36
|
+
import { type SharedResources } from "./shared-resources";
|
|
37
|
+
import type { Sandbox, SandboxBashOptions } from "./sandbox";
|
|
29
38
|
/**
|
|
30
39
|
* Options for creating a sandbox via the manager
|
|
31
40
|
*/
|
|
@@ -76,6 +85,13 @@ export interface ManagedSandboxOptions {
|
|
|
76
85
|
* @see SandboxOptions.sharedModules for full documentation
|
|
77
86
|
*/
|
|
78
87
|
sharedModules?: string[];
|
|
88
|
+
/**
|
|
89
|
+
* Options passed through to the just-bash Bash constructor.
|
|
90
|
+
* Overrides manager-level bashOptions for this sandbox.
|
|
91
|
+
*
|
|
92
|
+
* @see SandboxOptions.bashOptions for full documentation
|
|
93
|
+
*/
|
|
94
|
+
bashOptions?: SandboxBashOptions;
|
|
79
95
|
}
|
|
80
96
|
/**
|
|
81
97
|
* A sandbox instance managed by the SandboxManager.
|
|
@@ -114,11 +130,7 @@ export interface SandboxManagerStats {
|
|
|
114
130
|
/**
|
|
115
131
|
* Options for creating a SandboxManager
|
|
116
132
|
*/
|
|
117
|
-
export interface SandboxManagerOptions
|
|
118
|
-
/**
|
|
119
|
-
* TypeScript libs to load. Defaults to browser libs (ES2020 + DOM).
|
|
120
|
-
*/
|
|
121
|
-
libs?: string[];
|
|
133
|
+
export interface SandboxManagerOptions {
|
|
122
134
|
/**
|
|
123
135
|
* Default shared modules for all sandboxes created by this manager.
|
|
124
136
|
* Individual sandboxes can override with their own sharedModules option.
|
|
@@ -135,9 +147,20 @@ export interface SandboxManagerOptions extends SharedResourcesOptions {
|
|
|
135
147
|
* ```
|
|
136
148
|
*/
|
|
137
149
|
sharedModules?: string[];
|
|
150
|
+
/**
|
|
151
|
+
* Default bash options for all sandboxes created by this manager.
|
|
152
|
+
* Individual sandboxes can override with their own bashOptions.
|
|
153
|
+
*
|
|
154
|
+
* @see SandboxOptions.bashOptions for full documentation
|
|
155
|
+
*/
|
|
156
|
+
bashOptions?: SandboxBashOptions;
|
|
138
157
|
}
|
|
139
158
|
/**
|
|
140
|
-
* Manager for creating and managing multiple sandboxes
|
|
159
|
+
* Manager for creating and managing multiple sandboxes.
|
|
160
|
+
*
|
|
161
|
+
* Provides lifecycle management, ID-based tracking, and batch operations
|
|
162
|
+
* for multiple concurrent sandboxes. Resources (TypeScript libs, bundler)
|
|
163
|
+
* are shared via a global singleton.
|
|
141
164
|
*/
|
|
142
165
|
export declare class SandboxManager {
|
|
143
166
|
private resources;
|
|
@@ -148,15 +171,15 @@ export declare class SandboxManager {
|
|
|
148
171
|
private options;
|
|
149
172
|
constructor(options?: SandboxManagerOptions);
|
|
150
173
|
/**
|
|
151
|
-
* Initialize
|
|
174
|
+
* Initialize the manager by loading shared resources.
|
|
152
175
|
* Called automatically on first sandbox creation, but can be called
|
|
153
|
-
* explicitly to pre-warm.
|
|
176
|
+
* explicitly to pre-warm the global resource cache.
|
|
154
177
|
*/
|
|
155
178
|
initialize(): Promise<void>;
|
|
156
179
|
private doInitialize;
|
|
157
180
|
/**
|
|
158
181
|
* Create a new managed sandbox.
|
|
159
|
-
*
|
|
182
|
+
* The sandbox is tracked by the manager and can be looked up by ID.
|
|
160
183
|
*/
|
|
161
184
|
createSandbox(options?: ManagedSandboxOptions): Promise<ManagedSandbox>;
|
|
162
185
|
/**
|
|
@@ -172,7 +195,8 @@ export declare class SandboxManager {
|
|
|
172
195
|
*/
|
|
173
196
|
closeSandbox(id: string): boolean;
|
|
174
197
|
/**
|
|
175
|
-
* Close all sandboxes
|
|
198
|
+
* Close all sandboxes managed by this manager.
|
|
199
|
+
* Does not affect the global shared resources.
|
|
176
200
|
*/
|
|
177
201
|
destroyAll(): void;
|
|
178
202
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox-manager.d.ts","sourceRoot":"","sources":["../src/sandbox-manager.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"sandbox-manager.d.ts","sourceRoot":"","sources":["../src/sandbox-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,EAAQ,aAAa,EAAoB,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAe,KAAK,kBAAkB,EAAE,MAAM,MAAM,CAAC;AAC5D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAEL,KAAK,eAAe,EACrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAE7D;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;;;;OAOG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IAEZ;;;;;OAKG;IACH,SAAS,CAAC,EAAE,kBAAkB,CAAC;IAE/B;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEtC;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzD;;OAEG;IACH,cAAc,CAAC,EAAE,UAAU,CAAC,OAAO,aAAa,CAAC,EAAE,CAAC;IAEpD;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IAEzB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC;AAED;;;;;;GAM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I,MAAM,EAAE;IAM7B;;OAEG;IACH,QAAQ,IAAI,mBAAmB;IAS/B;;OAEG;IACH,YAAY,IAAI,eAAe,GAAG,IAAI;CAGvC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,cAAc,CAAC,CAIzB"}
|
package/dist/sandbox.d.ts
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
|
-
import { Bash, defineCommand } from "just-bash/browser";
|
|
2
|
-
import {
|
|
3
|
-
import { type
|
|
1
|
+
import { Bash, defineCommand, type BashOptions } from "just-bash/browser";
|
|
2
|
+
import { Filesystem } from "./fs";
|
|
3
|
+
import { type BuildOutput, type ValidateFn } from "./commands";
|
|
4
4
|
import { type SharedResources } from "./shared-resources";
|
|
5
|
+
/**
|
|
6
|
+
* Options that can be passed through to the just-bash Bash constructor.
|
|
7
|
+
* Excludes options that sandlot controls internally (fs, customCommands, files).
|
|
8
|
+
*/
|
|
9
|
+
export type SandboxBashOptions = Omit<BashOptions, 'fs' | 'customCommands' | 'files'>;
|
|
5
10
|
/**
|
|
6
11
|
* Options for creating a sandbox environment
|
|
7
12
|
*/
|
|
8
13
|
export interface SandboxOptions {
|
|
9
14
|
/**
|
|
10
|
-
*
|
|
15
|
+
* Initial files to populate the filesystem with.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const sandbox = await createSandbox({
|
|
20
|
+
* initialFiles: {
|
|
21
|
+
* '/src/index.ts': 'export const x = 1;',
|
|
22
|
+
* '/tsconfig.json': JSON.stringify({ compilerOptions: { strict: true } }),
|
|
23
|
+
* },
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
initialFiles?: Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum filesystem size in bytes (default: 50MB)
|
|
11
30
|
*/
|
|
12
|
-
|
|
31
|
+
maxFilesystemSize?: number;
|
|
13
32
|
/**
|
|
14
33
|
* Path to tsconfig.json in the virtual filesystem.
|
|
15
34
|
* Default: "/tsconfig.json"
|
|
@@ -25,10 +44,12 @@ export interface SandboxOptions {
|
|
|
25
44
|
resources?: SharedResources;
|
|
26
45
|
/**
|
|
27
46
|
* Callback invoked when a build succeeds.
|
|
28
|
-
* Receives the
|
|
29
|
-
*
|
|
47
|
+
* Receives the build output with the bundle and loaded module.
|
|
48
|
+
*
|
|
49
|
+
* For agent workflows, prefer using `createBuilder()` which handles
|
|
50
|
+
* build capture automatically.
|
|
30
51
|
*/
|
|
31
|
-
onBuild?: (result:
|
|
52
|
+
onBuild?: (result: BuildOutput) => void | Promise<void>;
|
|
32
53
|
/**
|
|
33
54
|
* Additional custom commands to add to the bash environment
|
|
34
55
|
*/
|
|
@@ -41,6 +62,9 @@ export interface SandboxOptions {
|
|
|
41
62
|
* This solves the "multiple React instances" problem by allowing dynamic
|
|
42
63
|
* components to share the same React instance as the host application.
|
|
43
64
|
*
|
|
65
|
+
* Type definitions are automatically installed for these modules so that
|
|
66
|
+
* TypeScript can typecheck code that imports them.
|
|
67
|
+
*
|
|
44
68
|
* @example
|
|
45
69
|
* ```ts
|
|
46
70
|
* // Host setup
|
|
@@ -53,73 +77,141 @@ export interface SandboxOptions {
|
|
|
53
77
|
* 'react-dom/client': ReactDOM,
|
|
54
78
|
* });
|
|
55
79
|
*
|
|
56
|
-
* // Create sandbox with shared modules
|
|
57
|
-
* const sandbox = await
|
|
80
|
+
* // Create sandbox with shared modules (types auto-installed)
|
|
81
|
+
* const sandbox = await createSandbox({
|
|
58
82
|
* sharedModules: ['react', 'react-dom/client'],
|
|
59
83
|
* });
|
|
60
84
|
* ```
|
|
61
85
|
*/
|
|
62
86
|
sharedModules?: string[];
|
|
87
|
+
/**
|
|
88
|
+
* Options passed through to the just-bash Bash constructor.
|
|
89
|
+
* Use this to configure environment variables, execution limits,
|
|
90
|
+
* network access, logging, and other bash-level settings.
|
|
91
|
+
*
|
|
92
|
+
* Note: `fs`, `customCommands`, and `files` are controlled by sandlot
|
|
93
|
+
* and cannot be overridden here.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* const sandbox = await createSandbox({
|
|
98
|
+
* bashOptions: {
|
|
99
|
+
* cwd: '/src',
|
|
100
|
+
* env: { NODE_ENV: 'development' },
|
|
101
|
+
* executionLimits: { maxCommandCount: 1000 },
|
|
102
|
+
* },
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
bashOptions?: SandboxBashOptions;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Sandbox state that can be serialized for persistence.
|
|
110
|
+
*/
|
|
111
|
+
export interface SandboxState {
|
|
112
|
+
/**
|
|
113
|
+
* All files in the filesystem as path -> content mapping.
|
|
114
|
+
* Can be passed as `initialFiles` when creating a new sandbox.
|
|
115
|
+
*/
|
|
116
|
+
files: Record<string, string>;
|
|
63
117
|
}
|
|
64
118
|
/**
|
|
65
119
|
* The sandbox environment containing the filesystem and bash shell
|
|
66
120
|
*/
|
|
67
121
|
export interface Sandbox {
|
|
68
122
|
/**
|
|
69
|
-
* The virtual filesystem
|
|
123
|
+
* The virtual filesystem
|
|
70
124
|
*/
|
|
71
|
-
fs:
|
|
125
|
+
fs: Filesystem;
|
|
72
126
|
/**
|
|
73
127
|
* The just-bash shell environment
|
|
74
128
|
*/
|
|
75
129
|
bash: Bash;
|
|
76
130
|
/**
|
|
77
|
-
*
|
|
131
|
+
* The last successful build output, or null if no build has succeeded yet.
|
|
132
|
+
*
|
|
133
|
+
* This is updated automatically whenever a `build` command succeeds.
|
|
134
|
+
* Contains both the bundle and the loaded (and validated, if applicable) module.
|
|
78
135
|
*
|
|
79
136
|
* @example
|
|
80
137
|
* ```ts
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* await
|
|
84
|
-
*
|
|
138
|
+
* // Agent loop pattern
|
|
139
|
+
* while (!sandbox.lastBuild) {
|
|
140
|
+
* const response = await agent.step();
|
|
141
|
+
* await sandbox.bash.exec(response.command);
|
|
142
|
+
* }
|
|
143
|
+
* // Build succeeded, sandbox.lastBuild contains bundle + module
|
|
144
|
+
* const App = sandbox.lastBuild.module.App;
|
|
85
145
|
* ```
|
|
86
146
|
*/
|
|
87
|
-
|
|
147
|
+
lastBuild: BuildOutput | null;
|
|
88
148
|
/**
|
|
89
|
-
*
|
|
149
|
+
* Get the current sandbox state for persistence.
|
|
90
150
|
*
|
|
91
|
-
*
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
*
|
|
151
|
+
* Returns a serializable object containing all files that can be
|
|
152
|
+
* JSON-serialized and used to restore the sandbox later.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* // Save sandbox state
|
|
157
|
+
* const state = sandbox.getState();
|
|
158
|
+
* localStorage.setItem('my-project', JSON.stringify(state));
|
|
159
|
+
*
|
|
160
|
+
* // Later, restore the sandbox
|
|
161
|
+
* const saved = JSON.parse(localStorage.getItem('my-project'));
|
|
162
|
+
* const sandbox2 = await createSandbox({ initialFiles: saved.files });
|
|
163
|
+
* ```
|
|
96
164
|
*/
|
|
97
|
-
|
|
165
|
+
getState(): SandboxState;
|
|
98
166
|
/**
|
|
99
167
|
* Subscribe to build events. Called whenever a build succeeds.
|
|
100
168
|
* Returns an unsubscribe function.
|
|
101
169
|
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
170
|
+
* For agent workflows, prefer using `createBuilder()` which handles
|
|
171
|
+
* build capture automatically. Use `onBuild` directly when you need
|
|
172
|
+
* more control over the subscription lifecycle.
|
|
104
173
|
*
|
|
105
174
|
* @example
|
|
106
175
|
* ```ts
|
|
107
|
-
* let
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* onBuild: (result) => {
|
|
111
|
-
* bundle = result;
|
|
112
|
-
* console.log('Built:', result.code.length, 'bytes');
|
|
113
|
-
* },
|
|
176
|
+
* let lastBuild: BuildOutput | null = null;
|
|
177
|
+
* const unsubscribe = sandbox.onBuild((result) => {
|
|
178
|
+
* lastBuild = result;
|
|
114
179
|
* });
|
|
115
180
|
*
|
|
116
|
-
*
|
|
181
|
+
* await sandbox.bash.exec('build /src/index.ts');
|
|
182
|
+
* unsubscribe();
|
|
183
|
+
*
|
|
184
|
+
* if (lastBuild) {
|
|
185
|
+
* const App = lastBuild.module.App as React.ComponentType;
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
onBuild(callback: (result: BuildOutput) => void | Promise<void>): () => void;
|
|
190
|
+
/**
|
|
191
|
+
* Set a validation function for the build command.
|
|
192
|
+
*
|
|
193
|
+
* When set, the build command will run this function after loading
|
|
194
|
+
* the module. If validation throws, the build fails and the agent
|
|
195
|
+
* sees the error. If validation passes, the validated module is
|
|
196
|
+
* available in the build output.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts
|
|
200
|
+
* sandbox.setValidation((mod) => {
|
|
201
|
+
* if (!mod.App) throw new Error("Must export App component");
|
|
202
|
+
* return { App: mod.App as React.ComponentType };
|
|
203
|
+
* });
|
|
117
204
|
*
|
|
118
|
-
* //
|
|
119
|
-
*
|
|
205
|
+
* // Now build will fail if App is missing
|
|
206
|
+
* await sandbox.bash.exec('build /src/index.ts');
|
|
120
207
|
* ```
|
|
121
208
|
*/
|
|
122
|
-
|
|
209
|
+
setValidation(fn: ValidateFn): void;
|
|
210
|
+
/**
|
|
211
|
+
* Clear the validation function.
|
|
212
|
+
* After calling this, builds will not perform validation.
|
|
213
|
+
*/
|
|
214
|
+
clearValidation(): void;
|
|
123
215
|
}
|
|
124
216
|
/**
|
|
125
217
|
* Create an in-browser agent sandbox with a virtual filesystem, TypeScript
|
|
@@ -128,9 +220,12 @@ export interface Sandbox {
|
|
|
128
220
|
* The sandbox provides a just-bash shell with custom commands:
|
|
129
221
|
* - `tsc [entry]` - Type check the project
|
|
130
222
|
* - `build [entry] [options]` - Build the project (runs typecheck first)
|
|
223
|
+
* - `install <pkg>` - Install npm packages
|
|
224
|
+
* - `uninstall <pkg>` - Remove packages
|
|
225
|
+
* - `list` - List installed packages
|
|
226
|
+
* - `run <entry>` - Run a script
|
|
131
227
|
*
|
|
132
228
|
* Build options:
|
|
133
|
-
* - `--output, -o <path>` - Output path (default: /dist/bundle.js)
|
|
134
229
|
* - `--format, -f <esm|iife|cjs>` - Output format (default: esm)
|
|
135
230
|
* - `--minify, -m` - Enable minification
|
|
136
231
|
* - `--skip-typecheck, -s` - Skip type checking
|
|
@@ -140,18 +235,14 @@ export interface Sandbox {
|
|
|
140
235
|
* let bundleResult: BundleResult | null = null;
|
|
141
236
|
*
|
|
142
237
|
* const sandbox = await createSandbox({
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* compilerOptions: { target: "ES2020", strict: true }
|
|
149
|
-
* }),
|
|
150
|
-
* },
|
|
238
|
+
* initialFiles: {
|
|
239
|
+
* '/src/index.ts': 'export const hello = "world";',
|
|
240
|
+
* '/tsconfig.json': JSON.stringify({
|
|
241
|
+
* compilerOptions: { target: 'ES2020', strict: true }
|
|
242
|
+
* }),
|
|
151
243
|
* },
|
|
152
244
|
* onBuild: (result) => {
|
|
153
245
|
* bundleResult = result;
|
|
154
|
-
* // Could also: dynamically import, halt agent, etc.
|
|
155
246
|
* },
|
|
156
247
|
* });
|
|
157
248
|
*
|
|
@@ -159,27 +250,18 @@ export interface Sandbox {
|
|
|
159
250
|
* await sandbox.bash.exec('echo "console.log(1);" > /src/index.ts');
|
|
160
251
|
*
|
|
161
252
|
* // Type check
|
|
162
|
-
* const tscResult = await sandbox.bash.exec(
|
|
253
|
+
* const tscResult = await sandbox.bash.exec('tsc /src/index.ts');
|
|
163
254
|
* console.log(tscResult.stdout);
|
|
164
255
|
*
|
|
165
256
|
* // Build (includes typecheck, triggers onBuild callback)
|
|
166
|
-
* const buildResult = await sandbox.bash.exec(
|
|
257
|
+
* const buildResult = await sandbox.bash.exec('build /src/index.ts');
|
|
167
258
|
* console.log(buildResult.stdout);
|
|
168
259
|
* console.log(bundleResult?.code); // The compiled bundle
|
|
169
260
|
*
|
|
170
|
-
* // Save
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* // Clean up
|
|
174
|
-
* sandbox.close();
|
|
261
|
+
* // Save state for later
|
|
262
|
+
* const state = sandbox.getState();
|
|
263
|
+
* localStorage.setItem('my-project', JSON.stringify(state));
|
|
175
264
|
* ```
|
|
176
265
|
*/
|
|
177
266
|
export declare function createSandbox(options?: SandboxOptions): Promise<Sandbox>;
|
|
178
|
-
/**
|
|
179
|
-
* Create an in-memory sandbox (no IndexedDB persistence).
|
|
180
|
-
* Useful for testing or temporary workspaces.
|
|
181
|
-
*/
|
|
182
|
-
export declare function createInMemorySandbox(options?: Omit<SandboxOptions, "fsOptions"> & {
|
|
183
|
-
initialFiles?: Record<string, string>;
|
|
184
|
-
}): Promise<Sandbox>;
|
|
185
267
|
//# sourceMappingURL=sandbox.d.ts.map
|
package/dist/sandbox.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC1E,OAAO,EAAE,UAAU,EAA0B,MAAM,MAAM,CAAC;AAE1D,OAAO,EAA2C,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AACxG,OAAO,EAAuB,KAAK,eAAe,EAAE,M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AAE,WAAW,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC;IAE7E;;;;;;;;;;;;;;;;;;OAkBG;IACH,aAAa,CAAC,EAAE,EAAE,UAAU,GAAG,IAAI,CAAC;IAEpC;;;OAGG;IACH,eAAe,IAAI,IAAI,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,wBAAsB,aAAa,CAAC,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,OAAO,CAAC,CA4GlF"}
|
package/dist/shared-modules.d.ts
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
* });
|
|
22
22
|
*
|
|
23
23
|
* // Now create sandbox with sharedModules option
|
|
24
|
-
* const sandbox = await
|
|
24
|
+
* const sandbox = await createSandbox({
|
|
25
25
|
* sharedModules: ['react', 'react-dom/client'],
|
|
26
26
|
* });
|
|
27
27
|
* ```
|
|
@@ -33,9 +33,11 @@
|
|
|
33
33
|
*/
|
|
34
34
|
export declare class SharedModuleRegistry {
|
|
35
35
|
private modules;
|
|
36
|
+
private exportNames;
|
|
36
37
|
constructor();
|
|
37
38
|
/**
|
|
38
|
-
* Register a module to be shared with dynamic bundles
|
|
39
|
+
* Register a module to be shared with dynamic bundles.
|
|
40
|
+
* Automatically introspects the module to discover its exports.
|
|
39
41
|
*
|
|
40
42
|
* @param moduleId - The import specifier (e.g., 'react', 'react-dom/client')
|
|
41
43
|
* @param module - The module's exports object
|
|
@@ -43,7 +45,8 @@ export declare class SharedModuleRegistry {
|
|
|
43
45
|
*/
|
|
44
46
|
register(moduleId: string, module: unknown): this;
|
|
45
47
|
/**
|
|
46
|
-
* Register multiple modules at once
|
|
48
|
+
* Register multiple modules at once.
|
|
49
|
+
* Automatically introspects each module to discover its exports.
|
|
47
50
|
*
|
|
48
51
|
* @param modules - Object mapping module IDs to their exports
|
|
49
52
|
* @returns this for chaining
|
|
@@ -74,6 +77,14 @@ export declare class SharedModuleRegistry {
|
|
|
74
77
|
* Get list of all registered module IDs
|
|
75
78
|
*/
|
|
76
79
|
list(): string[];
|
|
80
|
+
/**
|
|
81
|
+
* Get the export names for a registered module.
|
|
82
|
+
* These were discovered by introspecting the module at registration time.
|
|
83
|
+
*
|
|
84
|
+
* @param moduleId - The import specifier
|
|
85
|
+
* @returns Array of export names, or empty array if not registered
|
|
86
|
+
*/
|
|
87
|
+
getExportNames(moduleId: string): string[];
|
|
77
88
|
/**
|
|
78
89
|
* Clear all registrations
|
|
79
90
|
*/
|
|
@@ -121,6 +132,14 @@ export declare function unregisterSharedModule(moduleId: string): boolean;
|
|
|
121
132
|
* Clear all shared modules from the default registry.
|
|
122
133
|
*/
|
|
123
134
|
export declare function clearSharedModules(): void;
|
|
135
|
+
/**
|
|
136
|
+
* Get the export names for a registered shared module.
|
|
137
|
+
* Used by the bundler to generate proper re-export statements.
|
|
138
|
+
*
|
|
139
|
+
* @param moduleId - The import specifier
|
|
140
|
+
* @returns Array of export names, or empty array if not registered
|
|
141
|
+
*/
|
|
142
|
+
export declare function getSharedModuleExports(moduleId: string): string[];
|
|
124
143
|
/**
|
|
125
144
|
* Generate the runtime code that dynamic bundles use to access shared modules.
|
|
126
145
|
* This is injected into bundles when they import from shared modules.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-modules.d.ts","sourceRoot":"","sources":["../src/shared-modules.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAOH;;;;GAIG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,OAAO,CAA8B;;
|
|
1
|
+
{"version":3,"file":"shared-modules.d.ts","sourceRoot":"","sources":["../src/shared-modules.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAOH;;;;GAIG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,C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7
7
|
* - Types cache - avoids redundant network fetches when multiple sandboxes
|
|
8
8
|
* install the same packages
|
|
9
|
-
*
|
|
10
|
-
* This module consolidates what was previously scattered across
|
|
11
|
-
* sandbox.ts and sandbox-manager.ts into a single source of truth.
|
|
12
9
|
*/
|
|
13
10
|
import { type TypesCache } from "./packages";
|
|
14
11
|
export type { TypesCache } from "./packages";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-resources.d.ts","sourceRoot":"","sources":["../src/shared-resources.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"shared-resources.d.ts","sourceRoot":"","sources":["../src/shared-resources.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EAAsB,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAGjE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE9B;;OAEG;IACH,YAAY,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B;;;;OAIG;IACH,UAAU,EAAE,UAAU,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAEhB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;;OAIG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,eAAe,CAAC,CAqB1B;AAWD;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,eAAe,CAAC,CAapE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAG5C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAE7C"}
|
package/dist/typechecker.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sandlot",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Browser-based TypeScript sandbox with esbuild bundling and type checking",
|
|
5
5
|
"author": "blindmansion",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,17 +43,8 @@
|
|
|
43
43
|
"just-bash": "^2.6.0",
|
|
44
44
|
"typescript": "^5.9.3"
|
|
45
45
|
},
|
|
46
|
-
"peerDependencies": {
|
|
47
|
-
"react": ">=17.0.0"
|
|
48
|
-
},
|
|
49
|
-
"peerDependenciesMeta": {
|
|
50
|
-
"react": {
|
|
51
|
-
"optional": true
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
46
|
"devDependencies": {
|
|
55
47
|
"@types/bun": "latest",
|
|
56
|
-
"@types/react": "^19"
|
|
57
|
-
"react": "^19.2.3"
|
|
48
|
+
"@types/react": "^19"
|
|
58
49
|
}
|
|
59
50
|
}
|