sandlot 0.1.3 → 0.2.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.
Files changed (115) hide show
  1. package/dist/browser/bundler.d.ts +68 -0
  2. package/dist/browser/bundler.d.ts.map +1 -0
  3. package/dist/browser/executor.d.ts +46 -0
  4. package/dist/browser/executor.d.ts.map +1 -0
  5. package/dist/browser/index.d.ts +9 -0
  6. package/dist/browser/index.d.ts.map +1 -0
  7. package/dist/browser/index.js +2692 -0
  8. package/dist/browser/preset.d.ts +63 -0
  9. package/dist/browser/preset.d.ts.map +1 -0
  10. package/dist/commands/index.d.ts +20 -11
  11. package/dist/commands/index.d.ts.map +1 -1
  12. package/dist/commands/types.d.ts +31 -132
  13. package/dist/commands/types.d.ts.map +1 -1
  14. package/dist/core/bundler-utils.d.ts +142 -0
  15. package/dist/core/bundler-utils.d.ts.map +1 -0
  16. package/dist/core/esm-types-resolver.d.ts +125 -0
  17. package/dist/core/esm-types-resolver.d.ts.map +1 -0
  18. package/dist/core/executor.d.ts +35 -0
  19. package/dist/core/executor.d.ts.map +1 -0
  20. package/dist/{fs.d.ts → core/fs.d.ts} +27 -29
  21. package/dist/core/fs.d.ts.map +1 -0
  22. package/dist/core/sandbox.d.ts +30 -0
  23. package/dist/core/sandbox.d.ts.map +1 -0
  24. package/dist/core/sandlot.d.ts +30 -0
  25. package/dist/core/sandlot.d.ts.map +1 -0
  26. package/dist/core/shared-module-registry.d.ts +46 -0
  27. package/dist/core/shared-module-registry.d.ts.map +1 -0
  28. package/dist/core/typechecker.d.ts +60 -0
  29. package/dist/core/typechecker.d.ts.map +1 -0
  30. package/dist/index.d.ts +11 -16
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +1405 -2049
  33. package/dist/node/bundler.d.ts +48 -0
  34. package/dist/node/bundler.d.ts.map +1 -0
  35. package/dist/node/executor.d.ts +48 -0
  36. package/dist/node/executor.d.ts.map +1 -0
  37. package/dist/node/index.d.ts +9 -0
  38. package/dist/node/index.d.ts.map +1 -0
  39. package/dist/node/index.js +2646 -0
  40. package/dist/node/preset.d.ts +62 -0
  41. package/dist/node/preset.d.ts.map +1 -0
  42. package/dist/types.d.ts +525 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/package.json +27 -8
  45. package/src/browser/bundler.ts +294 -0
  46. package/src/browser/executor.ts +71 -0
  47. package/src/browser/index.ts +57 -0
  48. package/src/browser/preset.ts +179 -0
  49. package/src/commands/index.ts +526 -43
  50. package/src/commands/types.ts +82 -146
  51. package/src/core/bundler-utils.ts +630 -0
  52. package/src/core/esm-types-resolver.ts +432 -0
  53. package/src/core/executor.ts +161 -0
  54. package/src/{fs.ts → core/fs.ts} +59 -37
  55. package/src/core/sandbox.ts +621 -0
  56. package/src/core/sandlot.ts +77 -0
  57. package/src/core/shared-module-registry.ts +138 -0
  58. package/src/core/typechecker.ts +607 -0
  59. package/src/index.ts +104 -139
  60. package/src/node/bundler.ts +194 -0
  61. package/src/node/executor.ts +87 -0
  62. package/src/node/index.ts +39 -0
  63. package/src/node/preset.ts +178 -0
  64. package/src/types.ts +668 -0
  65. package/README.md +0 -243
  66. package/dist/build-emitter.d.ts +0 -47
  67. package/dist/build-emitter.d.ts.map +0 -1
  68. package/dist/builder.d.ts +0 -370
  69. package/dist/builder.d.ts.map +0 -1
  70. package/dist/bundler.d.ts +0 -148
  71. package/dist/bundler.d.ts.map +0 -1
  72. package/dist/commands/compile.d.ts +0 -13
  73. package/dist/commands/compile.d.ts.map +0 -1
  74. package/dist/commands/packages.d.ts +0 -17
  75. package/dist/commands/packages.d.ts.map +0 -1
  76. package/dist/commands/run.d.ts +0 -40
  77. package/dist/commands/run.d.ts.map +0 -1
  78. package/dist/commands.d.ts +0 -179
  79. package/dist/commands.d.ts.map +0 -1
  80. package/dist/fs.d.ts.map +0 -1
  81. package/dist/internal.d.ts +0 -79
  82. package/dist/internal.d.ts.map +0 -1
  83. package/dist/internal.js +0 -1976
  84. package/dist/loader.d.ts +0 -164
  85. package/dist/loader.d.ts.map +0 -1
  86. package/dist/packages.d.ts +0 -199
  87. package/dist/packages.d.ts.map +0 -1
  88. package/dist/runner.d.ts +0 -314
  89. package/dist/runner.d.ts.map +0 -1
  90. package/dist/sandbox-manager.d.ts +0 -261
  91. package/dist/sandbox-manager.d.ts.map +0 -1
  92. package/dist/sandbox.d.ts +0 -267
  93. package/dist/sandbox.d.ts.map +0 -1
  94. package/dist/shared-modules.d.ts +0 -148
  95. package/dist/shared-modules.d.ts.map +0 -1
  96. package/dist/shared-resources.d.ts +0 -102
  97. package/dist/shared-resources.d.ts.map +0 -1
  98. package/dist/ts-libs.d.ts +0 -98
  99. package/dist/ts-libs.d.ts.map +0 -1
  100. package/dist/typechecker.d.ts +0 -127
  101. package/dist/typechecker.d.ts.map +0 -1
  102. package/src/build-emitter.ts +0 -64
  103. package/src/builder.ts +0 -498
  104. package/src/bundler.ts +0 -542
  105. package/src/commands/compile.ts +0 -236
  106. package/src/commands/packages.ts +0 -154
  107. package/src/commands/run.ts +0 -245
  108. package/src/internal.ts +0 -119
  109. package/src/loader.ts +0 -229
  110. package/src/packages.ts +0 -936
  111. package/src/sandbox.ts +0 -396
  112. package/src/shared-modules.ts +0 -280
  113. package/src/shared-resources.ts +0 -166
  114. package/src/ts-libs.ts +0 -320
  115. package/src/typechecker.ts +0 -635
@@ -0,0 +1,62 @@
1
+ import { type EsmTypesResolverOptions } from "../core/esm-types-resolver";
2
+ import type { Sandlot, SandlotOptions } from "../types";
3
+ import { type EsbuildNativeBundlerOptions } from "./bundler";
4
+ import { type TypecheckerOptions } from "../core/typechecker";
5
+ import { type NodeExecutorOptions } from "./executor";
6
+ export interface CreateNodeSandlotOptions extends Omit<SandlotOptions, "bundler" | "typechecker" | "typesResolver" | "executor"> {
7
+ /**
8
+ * Custom bundler options, or a pre-configured bundler instance.
9
+ */
10
+ bundler?: EsbuildNativeBundlerOptions | SandlotOptions["bundler"];
11
+ /**
12
+ * Custom typechecker options, or a pre-configured typechecker instance.
13
+ * Set to `false` to disable type checking.
14
+ */
15
+ typechecker?: TypecheckerOptions | SandlotOptions["typechecker"] | false;
16
+ /**
17
+ * Custom types resolver options, or a pre-configured resolver instance.
18
+ * Set to `false` to disable type resolution.
19
+ */
20
+ typesResolver?: EsmTypesResolverOptions | SandlotOptions["typesResolver"] | false;
21
+ /**
22
+ * Custom executor options, or a pre-configured executor instance.
23
+ * Set to `false` to disable execution (sandbox.run() will throw).
24
+ * Defaults to NodeExecutor.
25
+ */
26
+ executor?: NodeExecutorOptions | SandlotOptions["executor"] | false;
27
+ }
28
+ /**
29
+ * Create a Sandlot instance pre-configured for Node.js/Bun/Deno environments.
30
+ *
31
+ * This is a convenience function that sets up sensible defaults:
32
+ * - EsbuildNativeBundler for bundling (uses native esbuild)
33
+ * - Typechecker for type checking (fetches libs from CDN)
34
+ * - EsmTypesResolver for npm type resolution
35
+ * - NodeExecutor for code execution
36
+ *
37
+ * @example Basic usage
38
+ * ```ts
39
+ * const sandlot = await createNodeSandlot();
40
+ * const sandbox = await sandlot.createSandbox();
41
+ * ```
42
+ *
43
+ * @example With shared modules
44
+ * ```ts
45
+ * import express from "express";
46
+ *
47
+ * const sandlot = await createNodeSandlot({
48
+ * sharedModules: {
49
+ * express,
50
+ * },
51
+ * });
52
+ * ```
53
+ *
54
+ * @example Disable type checking for faster builds
55
+ * ```ts
56
+ * const sandlot = await createNodeSandlot({
57
+ * typechecker: false,
58
+ * });
59
+ * ```
60
+ */
61
+ export declare function createNodeSandlot(options?: CreateNodeSandlotOptions): Promise<Sandlot>;
62
+ //# sourceMappingURL=preset.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preset.d.ts","sourceRoot":"","sources":["../../src/node/preset.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAwB,KAAK,2BAA2B,EAAE,MAAM,WAAW,CAAC;AACnF,OAAO,EAEL,KAAK,kBAAkB,EACxB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,KAAK,mBAAmB,EACzB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,wBACf,SAAQ,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,aAAa,GAAG,eAAe,GAAG,UAAU,CAAC;IACtF;;OAEG;IACH,OAAO,CAAC,EAAE,2BAA2B,GAAG,cAAc,CAAC,SAAS,CAAC,CAAC;IAElE;;;OAGG;IACH,WAAW,CAAC,EACV,kBAAkB,GAClB,cAAc,CAAC,aAAa,CAAC,GAC7B,KAAK,CAAC;IAER;;;OAGG;IACH,aAAa,CAAC,EACZ,uBAAuB,GACvB,cAAc,CAAC,eAAe,CAAC,GAC/B,KAAK,CAAC;IAER;;;;OAIG;IACH,QAAQ,CAAC,EACP,mBAAmB,GACnB,cAAc,CAAC,UAAU,CAAC,GAC1B,KAAK,CAAC;CACT;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,GAAE,wBAA6B,GACrC,OAAO,CAAC,OAAO,CAAC,CAgDlB"}
@@ -0,0 +1,525 @@
1
+ import type { Filesystem } from "./core/fs";
2
+ export type { IFileSystem, FsEntry, FsStat } from "just-bash/browser";
3
+ export type { Filesystem } from "./core/fs";
4
+ /**
5
+ * Bundler interface - transforms source files into executable JavaScript.
6
+ *
7
+ * Implementations handle their own initialization and caching (e.g., WASM loading).
8
+ */
9
+ export interface IBundler {
10
+ /**
11
+ * Bundle source files from a filesystem into a single output
12
+ */
13
+ bundle(options: BundleOptions): Promise<BundleResult>;
14
+ }
15
+ export interface BundleOptions {
16
+ fs: Filesystem;
17
+ entryPoint: string;
18
+ /**
19
+ * Map of installed package names to versions.
20
+ * Used by the bundler to resolve imports to specific versions.
21
+ *
22
+ * @example { "lodash": "4.17.21", "react": "18.2.0" }
23
+ */
24
+ installedPackages?: Record<string, string>;
25
+ /**
26
+ * Module IDs that resolve to shared modules instead of bundling.
27
+ * These are provided by the host environment at runtime.
28
+ */
29
+ sharedModules?: string[];
30
+ /** Shared module registry for resolving shared imports */
31
+ sharedModuleRegistry?: ISharedModuleRegistry;
32
+ /** Modules to treat as external (don't bundle or rewrite) */
33
+ external?: string[];
34
+ format?: "esm" | "iife" | "cjs";
35
+ minify?: boolean;
36
+ sourcemap?: boolean;
37
+ target?: string[];
38
+ }
39
+ /**
40
+ * Bundle result - success or failure with structured errors.
41
+ */
42
+ export type BundleResult = BundleSuccess | BundleFailure;
43
+ export interface BundleSuccess {
44
+ success: true;
45
+ code: string;
46
+ warnings: BundleWarning[];
47
+ includedFiles: string[];
48
+ }
49
+ export interface BundleFailure {
50
+ success: false;
51
+ errors: BundleError[];
52
+ warnings: BundleWarning[];
53
+ }
54
+ export interface BundleWarning {
55
+ text: string;
56
+ location?: BundleLocation;
57
+ }
58
+ export interface BundleError {
59
+ text: string;
60
+ location?: BundleLocation;
61
+ }
62
+ export interface BundleLocation {
63
+ file: string;
64
+ line: number;
65
+ column?: number;
66
+ /** The source line text (if available from esbuild) */
67
+ lineText?: string;
68
+ }
69
+ /**
70
+ * Typechecker interface - validates TypeScript code.
71
+ *
72
+ * Implementations handle their own lib file loading and caching.
73
+ */
74
+ export interface ITypechecker {
75
+ /**
76
+ * Type check files against a virtual filesystem
77
+ */
78
+ typecheck(options: TypecheckOptions): Promise<TypecheckResult>;
79
+ }
80
+ export interface TypecheckOptions {
81
+ /** Sync filesystem to read source files from */
82
+ fs: Filesystem;
83
+ /** Entry point path (absolute path in the filesystem) */
84
+ entryPoint: string;
85
+ /** Path to tsconfig.json (default: "/tsconfig.json") */
86
+ tsconfigPath?: string;
87
+ }
88
+ export interface TypecheckResult {
89
+ success: boolean;
90
+ diagnostics: Diagnostic[];
91
+ }
92
+ export interface Diagnostic {
93
+ file?: string;
94
+ line?: number;
95
+ column?: number;
96
+ message: string;
97
+ severity: "error" | "warning" | "info";
98
+ }
99
+ /**
100
+ * Types resolver interface - fetches type definitions for npm packages.
101
+ *
102
+ * Implementations handle their own caching (in-memory, KV, R2, etc.).
103
+ */
104
+ export interface ITypesResolver {
105
+ /**
106
+ * Fetch type definitions for a package.
107
+ * Returns a map of file paths to content for .d.ts files.
108
+ */
109
+ resolveTypes(packageName: string, version?: string): Promise<Record<string, string>>;
110
+ }
111
+ /**
112
+ * Shared module registry interface - provides host modules to bundled code.
113
+ *
114
+ * This allows dynamic code to use the same React/library instances as the host,
115
+ * avoiding the "multiple React instances" problem.
116
+ *
117
+ * Each registry instance has a unique key for global exposure, allowing multiple
118
+ * Sandlot instances to coexist without sharing module state.
119
+ */
120
+ export interface ISharedModuleRegistry {
121
+ /**
122
+ * The unique global key where this registry is exposed.
123
+ * Bundled code accesses the registry via `globalThis[registryKey]`.
124
+ */
125
+ readonly registryKey: string;
126
+ /**
127
+ * Get a registered module by ID
128
+ */
129
+ get(moduleId: string): unknown;
130
+ /**
131
+ * Check if a module is registered
132
+ */
133
+ has(moduleId: string): boolean;
134
+ /**
135
+ * Get export names for a module (for generating re-exports)
136
+ */
137
+ getExportNames(moduleId: string): string[];
138
+ /**
139
+ * List all registered module IDs
140
+ */
141
+ list(): string[];
142
+ }
143
+ /**
144
+ * Executor interface - runs bundled code in an isolated context.
145
+ *
146
+ * Different implementations provide different isolation levels:
147
+ * - MainThreadExecutor: Runs in the main thread (no isolation, for trusted code)
148
+ * - WorkerExecutor: Runs in a Web Worker (memory isolation, can be terminated)
149
+ * - IframeExecutor: Runs in a sandboxed iframe (DOM isolation, CSP control)
150
+ *
151
+ * The executor receives a code string (bundled JavaScript) and options,
152
+ * and returns the execution result including captured logs and return value.
153
+ */
154
+ export interface IExecutor {
155
+ /**
156
+ * Execute bundled code and return the result.
157
+ *
158
+ * @param code - The bundled JavaScript code to execute
159
+ * @param options - Execution options (entry export, context, timeout)
160
+ * @returns Execution result with logs, return value, and any error
161
+ */
162
+ execute(code: string, options?: ExecuteOptions): Promise<ExecuteResult>;
163
+ }
164
+ /**
165
+ * Options for code execution.
166
+ */
167
+ export interface ExecuteOptions {
168
+ /**
169
+ * Which export to call:
170
+ * - 'main': Calls `main(context)` export with the provided context
171
+ * - 'default': Calls the default export (no arguments)
172
+ *
173
+ * If neither export exists, top-level code still runs on import.
174
+ * @default 'main'
175
+ */
176
+ entryExport?: "main" | "default";
177
+ /**
178
+ * Context object passed to `main(context)` when entryExport is 'main'.
179
+ * Typically includes things like args, env, logging functions.
180
+ */
181
+ context?: Record<string, unknown>;
182
+ /**
183
+ * Execution timeout in milliseconds.
184
+ * Set to 0 to disable timeout.
185
+ * @default 30000
186
+ */
187
+ timeout?: number;
188
+ }
189
+ /**
190
+ * Result of code execution.
191
+ */
192
+ export interface ExecuteResult {
193
+ /** Whether execution completed successfully */
194
+ success: boolean;
195
+ /** Captured console output (log, warn, error, info, debug) */
196
+ logs: string[];
197
+ /** Return value from the executed function (if any) */
198
+ returnValue?: unknown;
199
+ /** Error message if execution failed */
200
+ error?: string;
201
+ /** Execution time in milliseconds */
202
+ executionTimeMs?: number;
203
+ }
204
+ export interface SandlotOptions {
205
+ /**
206
+ * Bundler implementation.
207
+ * Handles its own initialization and WASM loading.
208
+ */
209
+ bundler: IBundler;
210
+ /**
211
+ * Executor implementation (optional).
212
+ * Handles running bundled code in an appropriate context.
213
+ * If not provided, sandbox.run() will throw an error.
214
+ */
215
+ executor?: IExecutor;
216
+ /**
217
+ * Typechecker implementation (optional - skip type checking if not provided).
218
+ * Handles its own TypeScript lib file loading and caching.
219
+ */
220
+ typechecker?: ITypechecker;
221
+ /**
222
+ * Types resolver for npm packages (optional).
223
+ * Handles its own caching. If not provided, `install` command won't fetch types.
224
+ */
225
+ typesResolver?: ITypesResolver;
226
+ /**
227
+ * Shared modules - host modules to share with sandboxed code.
228
+ * Keys are import specifiers, values are the actual module objects.
229
+ *
230
+ * @example { 'react': React, 'react-dom/client': ReactDOM }
231
+ */
232
+ sharedModules?: Record<string, unknown>;
233
+ /**
234
+ * Default options for sandboxes created from this instance
235
+ */
236
+ sandboxDefaults?: {
237
+ maxFilesystemSize?: number;
238
+ };
239
+ }
240
+ export interface SandboxOptions {
241
+ /**
242
+ * Initial files to populate the filesystem with.
243
+ *
244
+ * If `/package.json` is not provided, a default one will be created with:
245
+ * ```json
246
+ * { "main": "./index.ts", "dependencies": {} }
247
+ * ```
248
+ *
249
+ * If `/tsconfig.json` is not provided, sensible defaults will be created.
250
+ *
251
+ * The `main` field in package.json determines the entry point for build/typecheck.
252
+ */
253
+ initialFiles?: Record<string, string>;
254
+ /**
255
+ * Maximum filesystem size in bytes
256
+ */
257
+ maxFilesystemSize?: number;
258
+ /**
259
+ * Callback invoked when a build succeeds
260
+ */
261
+ onBuild?: (result: BuildSuccess) => void | Promise<void>;
262
+ }
263
+ /**
264
+ * Build phases that can fail.
265
+ */
266
+ export type BuildPhase = "entry" | "typecheck" | "bundle";
267
+ /**
268
+ * Build result - success or failure with structured errors.
269
+ *
270
+ * On success, contains the bundled code string.
271
+ * On failure, contains the phase that failed and structured error information.
272
+ *
273
+ * Note: Build does NOT load or execute the module. Use an executor to run the code.
274
+ * This keeps the build phase pure (no code execution) and allows different execution
275
+ * contexts (main thread, web worker, iframe, remote server).
276
+ */
277
+ export type BuildResult = BuildSuccess | BuildFailure;
278
+ export interface BuildSuccess {
279
+ success: true;
280
+ /** The bundled JavaScript code */
281
+ code: string;
282
+ /** Files that were included in the bundle */
283
+ includedFiles: string[];
284
+ /** Any warnings from the bundler */
285
+ warnings: BundleWarning[];
286
+ }
287
+ export interface BuildFailure {
288
+ success: false;
289
+ /** Which phase of the build failed */
290
+ phase: BuildPhase;
291
+ /** Error message (for entry failures) */
292
+ message?: string;
293
+ /** Type check diagnostics (for typecheck failures) */
294
+ diagnostics?: Diagnostic[];
295
+ /** Bundle errors (for bundle failures) */
296
+ bundleErrors?: BundleError[];
297
+ /** Bundle warnings (may be present even on failure) */
298
+ bundleWarnings?: BundleWarning[];
299
+ }
300
+ export interface InstallResult {
301
+ /** Package name */
302
+ name: string;
303
+ /** Resolved version */
304
+ version: string;
305
+ /** Whether type definitions were installed */
306
+ typesInstalled: boolean;
307
+ /** Number of .d.ts files written */
308
+ typeFilesCount: number;
309
+ /** Whether types came from cache */
310
+ fromCache?: boolean;
311
+ /** Error message if types failed to install */
312
+ typesError?: string;
313
+ }
314
+ export interface UninstallResult {
315
+ /** Package name */
316
+ name: string;
317
+ /** Whether the package was installed (and thus removed) */
318
+ removed: boolean;
319
+ }
320
+ export interface SandboxBuildOptions {
321
+ /**
322
+ * Entry point to build.
323
+ * If not specified, reads from `main` field in /package.json.
324
+ * Falls back to "./index.ts" if not found.
325
+ */
326
+ entryPoint?: string;
327
+ /**
328
+ * Skip type checking before bundling.
329
+ * @default false
330
+ */
331
+ skipTypecheck?: boolean;
332
+ /**
333
+ * Minify the output.
334
+ * @default false
335
+ */
336
+ minify?: boolean;
337
+ /**
338
+ * Output format.
339
+ * @default "esm"
340
+ */
341
+ format?: "esm" | "iife" | "cjs";
342
+ }
343
+ export interface SandboxTypecheckOptions {
344
+ /**
345
+ * Entry point to typecheck.
346
+ * If not specified, reads from `main` field in /package.json.
347
+ * Falls back to "./index.ts" if not found.
348
+ */
349
+ entryPoint?: string;
350
+ }
351
+ /**
352
+ * Options for running code in the sandbox.
353
+ */
354
+ export interface RunOptions {
355
+ /**
356
+ * Entry point to build and run.
357
+ * If not specified, reads from `main` field in /package.json.
358
+ * Falls back to "./index.ts" if not found.
359
+ */
360
+ entryPoint?: string;
361
+ /**
362
+ * Skip type checking before building.
363
+ * @default false
364
+ */
365
+ skipTypecheck?: boolean;
366
+ /**
367
+ * Which export to call:
368
+ * - 'main': Calls `main(context)` export with the provided context
369
+ * - 'default': Calls the default export (no arguments)
370
+ * @default 'main'
371
+ */
372
+ entryExport?: "main" | "default";
373
+ /**
374
+ * Context object passed to `main(context)` when entryExport is 'main'.
375
+ */
376
+ context?: Record<string, unknown>;
377
+ /**
378
+ * Execution timeout in milliseconds.
379
+ * Set to 0 to disable timeout.
380
+ * @default 30000
381
+ */
382
+ timeout?: number;
383
+ }
384
+ /**
385
+ * Result of running code in the sandbox.
386
+ *
387
+ * Extends ExecuteResult with build failure information.
388
+ * If `buildFailure` is present, the build failed before execution.
389
+ */
390
+ export interface RunResult extends ExecuteResult {
391
+ /** If build failed, contains failure details */
392
+ buildFailure?: {
393
+ phase: BuildPhase;
394
+ message?: string;
395
+ };
396
+ }
397
+ export interface Sandbox {
398
+ /**
399
+ * The virtual filesystem (sync)
400
+ */
401
+ readonly fs: Filesystem;
402
+ /**
403
+ * Execute a shell command
404
+ */
405
+ exec(command: string): Promise<ExecResult>;
406
+ /**
407
+ * The last successful build result (code string + metadata)
408
+ */
409
+ readonly lastBuild: BuildSuccess | null;
410
+ /**
411
+ * Get the current sandbox state for persistence
412
+ */
413
+ getState(): SandboxState;
414
+ /**
415
+ * Read a file from the virtual filesystem.
416
+ *
417
+ * @param path - Absolute path to the file (e.g., "/src/app.ts")
418
+ * @returns File contents as a string
419
+ * @throws If the file does not exist
420
+ */
421
+ readFile(path: string): string;
422
+ /**
423
+ * Write a file to the virtual filesystem.
424
+ * Creates parent directories automatically if they don't exist.
425
+ *
426
+ * @param path - Absolute path to the file (e.g., "/src/app.ts")
427
+ * @param content - File contents to write
428
+ */
429
+ writeFile(path: string, content: string): void;
430
+ /**
431
+ * Subscribe to build events
432
+ */
433
+ onBuild(callback: (result: BuildSuccess) => void | Promise<void>): () => void;
434
+ /**
435
+ * Install a package from npm.
436
+ * Updates /package.json and fetches type definitions if typesResolver is configured.
437
+ *
438
+ * @param packageSpec - Package specifier (e.g., "lodash", "lodash@4.17.21", "@types/node@20")
439
+ * @returns Installation result with version and types info
440
+ *
441
+ * @example
442
+ * await sandbox.install("lodash@4.17.21");
443
+ * await sandbox.install("@tanstack/react-query");
444
+ */
445
+ install(packageSpec: string): Promise<InstallResult>;
446
+ /**
447
+ * Uninstall a package.
448
+ * Removes from /package.json and deletes type definition files.
449
+ *
450
+ * @param packageName - Package name (e.g., "lodash", "@tanstack/react-query")
451
+ * @returns Whether the package was removed
452
+ */
453
+ uninstall(packageName: string): Promise<UninstallResult>;
454
+ /**
455
+ * Build the project.
456
+ * Reads dependencies from /package.json, optionally typechecks, and bundles.
457
+ * Returns the bundled code string (does NOT execute it).
458
+ *
459
+ * @param options - Build options
460
+ * @returns Build result - check `success` field to determine outcome
461
+ */
462
+ build(options?: SandboxBuildOptions): Promise<BuildResult>;
463
+ /**
464
+ * Type check the project.
465
+ * Reads tsconfig from filesystem and runs the typechecker.
466
+ *
467
+ * @param options - Typecheck options
468
+ * @returns Typecheck result with diagnostics
469
+ */
470
+ typecheck(options?: SandboxTypecheckOptions): Promise<TypecheckResult>;
471
+ /**
472
+ * Build and run code using the configured executor.
473
+ *
474
+ * This is a convenience method that:
475
+ * 1. Builds the code (typecheck + bundle)
476
+ * 2. Passes the bundled code to the executor
477
+ * 3. Returns the execution result
478
+ *
479
+ * Requires an executor to be configured when creating Sandlot.
480
+ *
481
+ * @param options - Run options (entry point, context, timeout, etc.)
482
+ * @returns Run result with logs, return value, and any error
483
+ * @throws If no executor was configured
484
+ *
485
+ * @example
486
+ * ```ts
487
+ * // Script style - top-level code runs on import
488
+ * sandbox.writeFile('/index.ts', 'console.log("Hello!")');
489
+ * const result = await sandbox.run();
490
+ * console.log(result.logs); // ['Hello!']
491
+ *
492
+ * // Main function style - gets context
493
+ * sandbox.writeFile('/index.ts', `
494
+ * export function main(ctx) {
495
+ * ctx.log("Args:", ctx.args);
496
+ * return { success: true };
497
+ * }
498
+ * `);
499
+ * const result = await sandbox.run({
500
+ * context: { args: ['--verbose'] }
501
+ * });
502
+ * console.log(result.returnValue); // { success: true }
503
+ * ```
504
+ */
505
+ run(options?: RunOptions): Promise<RunResult>;
506
+ }
507
+ export interface ExecResult {
508
+ exitCode: number;
509
+ stdout: string;
510
+ stderr: string;
511
+ }
512
+ export interface SandboxState {
513
+ files: Record<string, string>;
514
+ }
515
+ export interface Sandlot {
516
+ /**
517
+ * Create a new sandbox environment
518
+ */
519
+ createSandbox(options?: SandboxOptions): Promise<Sandbox>;
520
+ /**
521
+ * The shared module registry (if shared modules were provided)
522
+ */
523
+ readonly sharedModules: ISharedModuleRegistry | null;
524
+ }
525
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAM5C,YAAY,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACtE,YAAY,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAM5C;;;;GAIG;AACH,MAAM,WAAW,QAAQ;IACvB;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CACvD;AAED,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,UAAU,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IAEnB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE3C;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IAEzB,0DAA0D;IAC1D,oBAAoB,CAAC,EAAE,qBAAqB,CAAC;IAE7C,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IAEpB,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,CAAC;IAChC,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,aAAa,GAAG,aAAa,CAAC;AAEzD,MAAM,WAAW,aAAa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;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,GAAG,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;CAC/C;AAED,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC/B;AAMD,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,aAAa,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE1D;;OAEG;IACH,QAAQ,CAAC,aAAa,EAAE,qBAAqB,GAAG,IAAI,CAAC;CACtD"}
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "sandlot",
3
- "version": "0.1.3",
4
- "description": "Browser-based TypeScript sandbox with esbuild bundling and type checking",
3
+ "version": "0.2.0",
4
+ "description": "TypeScript sandbox with esbuild bundling and type checking for browser and server",
5
5
  "author": "blindmansion",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/blindmansion/sandlot.git",
9
+ "directory": "packages/sandlot"
10
+ },
6
11
  "license": "MIT",
7
12
  "keywords": [
8
13
  "sandbox",
@@ -22,10 +27,15 @@
22
27
  "import": "./dist/index.js",
23
28
  "default": "./dist/index.js"
24
29
  },
25
- "./internal": {
26
- "types": "./dist/internal.d.ts",
27
- "import": "./dist/internal.js",
28
- "default": "./dist/internal.js"
30
+ "./browser": {
31
+ "types": "./dist/browser/index.d.ts",
32
+ "import": "./dist/browser/index.js",
33
+ "default": "./dist/browser/index.js"
34
+ },
35
+ "./node": {
36
+ "types": "./dist/node/index.d.ts",
37
+ "import": "./dist/node/index.js",
38
+ "default": "./dist/node/index.js"
29
39
  }
30
40
  },
31
41
  "files": [
@@ -34,7 +44,7 @@
34
44
  ],
35
45
  "scripts": {
36
46
  "build": "bun run build:js && bun run build:types",
37
- "build:js": "bun build ./src/index.ts ./src/internal.ts --outdir ./dist --format esm --packages external",
47
+ "build:js": "bun build ./src/index.ts ./src/browser/index.ts ./src/node/index.ts --outdir ./dist --format esm --packages external",
38
48
  "build:types": "tsc --emitDeclarationOnly",
39
49
  "prepublishOnly": "bun run build"
40
50
  },
@@ -43,8 +53,17 @@
43
53
  "just-bash": "^2.6.0",
44
54
  "typescript": "^5.9.3"
45
55
  },
56
+ "peerDependencies": {
57
+ "esbuild": ">=0.20.0"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "esbuild": {
61
+ "optional": true
62
+ }
63
+ },
46
64
  "devDependencies": {
47
65
  "@types/bun": "latest",
48
- "@types/react": "^19"
66
+ "@types/react": "^19",
67
+ "esbuild": "^0.27.2"
49
68
  }
50
69
  }