@vite-hub/sandbox 0.0.1 → 0.0.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.
Files changed (36) hide show
  1. package/README.md +35 -37
  2. package/dist/cloudflare-env-2NctU8i7.js +12 -0
  3. package/dist/index-DiH4OGAU.d.ts +101 -0
  4. package/dist/index.d.ts +23 -32
  5. package/dist/index.js +688 -265
  6. package/dist/module-types-Cjqo67ay.d.ts +91 -0
  7. package/dist/provider-loader-3ml65ZFy.d.ts +21 -0
  8. package/dist/runtime/provider-loader.d.ts +2 -17
  9. package/dist/runtime/provider-loader.js +4 -10
  10. package/dist/runtime/providers/cloudflare.d.ts +6 -11
  11. package/dist/runtime/providers/cloudflare.js +14 -11
  12. package/dist/runtime/providers/vercel.d.ts +5 -12
  13. package/dist/runtime/providers/vercel.js +16 -30
  14. package/dist/runtime/state.d.ts +1 -1
  15. package/dist/vite.d.ts +12 -28
  16. package/dist/vite.js +2276 -354
  17. package/package.json +68 -59
  18. package/dist/_shared-CJ2_pS4E.js +0 -53
  19. package/dist/cloudflare-retry-Cfra7MvS.js +0 -21
  20. package/dist/common-CS4FcWrh.d.ts +0 -369
  21. package/dist/index-B4j3Mvzr.d.ts +0 -146
  22. package/dist/integration-wkNE8jMO.js +0 -2032
  23. package/dist/module-types-CUcY8iuI.d.ts +0 -88
  24. package/dist/nitro.d.ts +0 -19
  25. package/dist/nitro.js +0 -488
  26. package/dist/provider-detection-IbezCeoR.js +0 -25
  27. package/dist/runtime/nitro-plugin.d.ts +0 -4
  28. package/dist/runtime/nitro-plugin.js +0 -8
  29. package/dist/sandbox/providers/cloudflare.d.ts +0 -7
  30. package/dist/sandbox/providers/cloudflare.js +0 -339
  31. package/dist/sandbox/providers/vercel.d.ts +0 -17
  32. package/dist/sandbox/providers/vercel.js +0 -538
  33. package/dist/shared-pqTHYWg1.d.ts +0 -8
  34. package/dist/utils-V7ODsDW_.js +0 -44
  35. package/dist/vercel-runtime-BPzvh_6V.js +0 -11
  36. /package/dist/{module-types-BomzaqnM.js → module-types-Dj1MFmHT.js} +0 -0
package/README.md CHANGED
@@ -1,58 +1,56 @@
1
1
  # @vite-hub/sandbox
2
2
 
3
- <p>
4
- <a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
5
- <img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
6
- <img alt="Vite" src="https://img.shields.io/badge/Vite-discovery-646cff?style=flat-square">
7
- <img alt="Sandbox" src="https://img.shields.io/badge/Sandbox-isolated%20runs-b45309?style=flat-square">
8
- </p>
3
+ `@vite-hub/sandbox` discovers and runs named work by composing a package project and a Box. Sandbox owns discovery, orchestration, and project staging; Box owns provider-specific execution.
9
4
 
10
- `@vite-hub/sandbox` runs typed work in an isolated provider runtime without coupling callers to that provider.
5
+ ```ts
6
+ // server/sandboxes/release-notes/index.ts
7
+ interface SandboxPayload {
8
+ notes?: string
9
+ }
10
+
11
+ export default async function releaseNotes(payload: SandboxPayload = {}) {
12
+ return { summary: payload.notes?.split("\n")[0] ?? "" }
13
+ }
14
+ ```
11
15
 
12
- ## Install
16
+ The folder supplies the Definition name, and the adjacent manifest owns portable lifecycle metadata:
13
17
 
14
- ```sh
15
- pnpm add @vite-hub/sandbox
18
+ ```json
19
+ {
20
+ "private": true,
21
+ "type": "module",
22
+ "vitehub": {
23
+ "sandbox": {
24
+ "timeout": 30000
25
+ }
26
+ }
27
+ }
16
28
  ```
17
29
 
18
- Add `@cloudflare/sandbox` or `@vercel/sandbox` for the provider you use.
30
+ ViteHub calls the default function with `(payload, context)` and infers the `runSandbox()` payload and result types from it. Nested `Blob` and `Uint8Array` values cross the Box boundary through binary sidecars rather than application JSON; Node.js `Buffer` values retain their `Buffer` type. Existing non-function default exports remain supported; their optional exported `SandboxPayload` type is the payload fallback. The entrypoint needs no `@vite-hub/sandbox` runtime dependency.
19
31
 
20
- ## Minimal API
32
+ ViteHub uses the manifest's `packageManager`, then a lockfile at that package root, then npm. A matching `pnpm-workspace.yaml` selects pnpm, moves preparation to the pnpm workspace root, and carries the transitive `workspace:*` dependency closure into the Box while the entrypoint still runs from its package directory.
21
33
 
22
- ```ts
23
- // server/sandboxes/release-notes.ts
24
- import { defineSandbox } from "@vite-hub/sandbox"
34
+ Package entrypoints are ESM projects. Keep `"type": "module"`, use explicit relative ESM imports for local `.ts` and `.mts` source, and depend on packages that expose runtime-ready JavaScript. ViteHub compiles the reachable local TypeScript graph without bundling package dependencies or moving relative assets. It rejects CommonJS/CTS, package import aliases and self-references for local source, imports that escape the selected package, and workspace dependencies that expose TypeScript runtime entries.
25
35
 
26
- export default defineSandbox(async (payload: { notes?: string } = {}) => {
27
- return {
28
- summary: payload.notes?.split("\n")[0] ?? "",
29
- }
30
- })
31
- ```
36
+ Use `<path>.sandbox.ts` with `defineSandbox()` for free-form Definitions outside `server/sandboxes`.
32
37
 
33
38
  ```ts
34
- // server/api/release-notes.post.ts
35
39
  import { runSandbox } from "@vite-hub/sandbox"
36
- import { defineEventHandler, readBody } from "h3"
37
40
 
38
- export default defineEventHandler(async (event) => {
39
- const result = await runSandbox("release-notes", await readBody(event))
40
- return result.isOk() ? result.value : { error: result.error.message }
41
- })
41
+ const [error, result] = await runSandbox("release-notes", { notes: "ship it" })
42
+ if (error) throw error
42
43
  ```
43
44
 
45
+ Resolve a runner when orchestration needs to inspect the provider boundary before execution:
46
+
44
47
  ```ts
45
- // nitro.config.ts
46
- import { defineNitroConfig } from "nitro/config"
48
+ import { resolveSandboxRunner } from "@vite-hub/sandbox"
47
49
 
48
- export default defineNitroConfig({
49
- modules: ["@vite-hub/sandbox/nitro"],
50
- sandbox: { provider: "cloudflare" },
51
- })
50
+ const runner = await resolveSandboxRunner("release-notes")
51
+ console.log(runner.executionAuthority)
52
52
  ```
53
53
 
54
- ## Vite and Nitro
55
-
56
- Nitro discovers `server/sandboxes/<name>.ts`. Vite also supports `src/<name>.sandbox.ts` through `hubSandbox()`. Provider config selects [Cloudflare Sandbox SDK](https://developers.cloudflare.com/sandbox/) or [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox/).
54
+ The runner exposes the selected Box provider's complete `ExecutionAuthority` descriptor. A Sandbox Definition name and its structural command arguments choose work to run; they do not limit what that process can read, reach, inherit, or spawn.
57
55
 
58
- Learn more at [vitehub.dev](https://vitehub.dev).
56
+ Add `hubSandbox()` to Vite for discovery, typed registry generation, package preparation plans, and host output. Provider images and full Dockerfile overrides belong to the selected Box adapter or host configuration; Sandbox Definitions stay portable.
@@ -0,0 +1,12 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ const activeEnvStorage = new AsyncLocalStorage();
3
+ function getActiveCloudflareEnv() {
4
+ const scoped = activeEnvStorage.getStore();
5
+ return scoped ? scoped.env : globalThis.__env__;
6
+ }
7
+ function getCloudflareEnv(event) {
8
+ const target = event;
9
+ return target?.env || target?.context?.cloudflare?.env || target?.context?._platform?.cloudflare?.env || target?.req?.runtime?.cloudflare?.env || getActiveCloudflareEnv();
10
+ }
11
+ //#endregion
12
+ export { getCloudflareEnv as t };
@@ -0,0 +1,101 @@
1
+ import { ExecutionAuthority } from "@vite-hub/runtime";
2
+
3
+ //#region ../box/dist/index-Vc0GwE3_.d.ts
4
+ interface BoxOpenOptions {
5
+ id?: string;
6
+ initialize?: (session: BoxSession, context: {
7
+ signal?: AbortSignal;
8
+ }) => Promise<void>;
9
+ signal?: AbortSignal;
10
+ }
11
+ interface BoxExecOptions {
12
+ cwd?: string;
13
+ env?: Readonly<Record<string, string>>;
14
+ signal?: AbortSignal;
15
+ timeout?: number;
16
+ }
17
+ interface BoxExecResult {
18
+ readonly code: number;
19
+ readonly ok: boolean;
20
+ readonly stderr: string;
21
+ readonly stdout: string;
22
+ }
23
+ interface BoxProcessExit {
24
+ readonly code: number;
25
+ }
26
+ interface BoxProcess {
27
+ readonly pid?: number;
28
+ readonly stderr: ReadableStream<Uint8Array>;
29
+ readonly stdout: ReadableStream<Uint8Array>;
30
+ kill(signal?: string): Promise<void>;
31
+ wait(): Promise<BoxProcessExit>;
32
+ }
33
+ interface BoxFileEntry {
34
+ readonly path: string;
35
+ readonly size?: number;
36
+ readonly type: "directory" | "file" | "symlink";
37
+ }
38
+ interface BoxFileOptions {
39
+ signal?: AbortSignal;
40
+ }
41
+ interface BoxListFilesOptions extends BoxFileOptions {
42
+ recursive?: boolean;
43
+ }
44
+ interface BoxRemoveFileOptions extends BoxFileOptions {
45
+ recursive?: boolean;
46
+ }
47
+ interface BoxFiles {
48
+ exists(path: string, options?: BoxFileOptions): Promise<boolean>;
49
+ list(path: string, options?: BoxListFilesOptions): Promise<readonly BoxFileEntry[]>;
50
+ mkdir(path: string, options?: BoxFileOptions & {
51
+ recursive?: boolean;
52
+ }): Promise<void>;
53
+ move?(source: string, destination: string, options?: BoxFileOptions): Promise<void>;
54
+ read(path: string, options?: BoxFileOptions): Promise<Uint8Array | null>;
55
+ remove(path: string, options?: BoxRemoveFileOptions): Promise<void>;
56
+ write(path: string, contents: Uint8Array, options?: BoxFileOptions): Promise<void>;
57
+ }
58
+ interface BoxPorts {
59
+ readonly values: readonly number[];
60
+ expose(port: number, options?: {
61
+ protocol?: "http" | "https" | "ws";
62
+ }): Promise<URL>;
63
+ }
64
+ interface BoxSession {
65
+ readonly cwd: string;
66
+ readonly executionAuthority: ExecutionAuthority;
67
+ readonly files: BoxFiles;
68
+ readonly id: string;
69
+ readonly ports?: BoxPorts;
70
+ readonly spawn?: (command: string, args?: readonly string[], options?: BoxExecOptions) => Promise<BoxProcess>;
71
+ close(): Promise<void>;
72
+ exec(command: string, args?: readonly string[], options?: BoxExecOptions): Promise<BoxExecResult>;
73
+ }
74
+ interface BoxEnvironment {
75
+ readonly env: Readonly<Record<string, string | undefined>>;
76
+ }
77
+ interface BoxResolvedRequirement {
78
+ readonly command: string;
79
+ readonly name: string;
80
+ }
81
+ interface BoxPlan {
82
+ readonly cache: {
83
+ readonly state: "disposable";
84
+ };
85
+ readonly environment: BoxEnvironment;
86
+ readonly executionAuthority: ExecutionAuthority;
87
+ readonly identity: string;
88
+ readonly requirements: readonly BoxResolvedRequirement[];
89
+ readonly runtime: string;
90
+ readonly workspace: {
91
+ readonly path?: string;
92
+ readonly state: "authoritative" | "disposable";
93
+ readonly workDir?: "." | "workspace";
94
+ };
95
+ }
96
+ interface Box {
97
+ readonly plan: BoxPlan;
98
+ open(options?: BoxOpenOptions): Promise<BoxSession>;
99
+ }
100
+ //#endregion
101
+ export { Box as t };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { g as VercelSandboxProviderOptions, t as CloudflareSandboxOptions } from "./common-CS4FcWrh.js";
2
- import { n as SandboxClient } from "./index-B4j3Mvzr.js";
3
- import { a as SandboxDefinitionFromHandler, c as SandboxDefinitionProviderOptions, d as SandboxExecutionOptions, f as SandboxOkResult, h as SandboxError, i as SandboxDefinitionBundle, l as SandboxDefinitionRuntime, m as getSandboxFeatureProvider, n as CloudflareSandboxDefinitionProviderOptions, o as SandboxDefinitionHandler, p as SandboxRunResult, r as SandboxDefinition, s as SandboxDefinitionOptions, t as AgentSandboxConfig, u as SandboxErrResult } from "./module-types-CUcY8iuI.js";
1
+ import { a as SandboxDefinitionBundle, c as SandboxDefinitionInput, d as SandboxExecutionOptions, f as SandboxProjectOptions, g as getSandboxFeatureProvider, h as VercelSandboxProviderOptions, i as SandboxDefinition, l as SandboxDefinitionOptions, m as SandboxRunResult, n as CloudflareSandboxDefinitionProviderOptions, o as SandboxDefinitionFromHandler, p as SandboxProvider, r as CloudflareSandboxOptions, s as SandboxDefinitionHandler, t as AgentSandboxConfig, u as SandboxDefinitionProviderOptions } from "./module-types-Cjqo67ay.js";
4
2
  import { H3Event } from "h3";
3
+ import { ExecutionAuthority } from "@vite-hub/runtime";
5
4
  import { SandboxDefinitionModules } from "#vitehub-sandbox-registry";
6
5
 
7
6
  //#region src/runtime/registry.d.ts
8
- declare function defineSandbox<THandler extends (...args: any[]) => any>(run: THandler, options?: SandboxDefinitionOptions): SandboxDefinitionFromHandler<THandler>;
7
+ declare function defineSandbox<THandler extends (...args: any[]) => any>(input: SandboxDefinitionInput<Parameters<THandler>[0], Awaited<ReturnType<THandler>>> & {
8
+ run: THandler;
9
+ }): SandboxDefinitionFromHandler<THandler>;
9
10
  //#endregion
10
11
  //#region src/internal/shared/request-payload.d.ts
11
12
  declare function readRequestPayload<TPayload = Record<string, never>>(event: H3Event, fallback?: TPayload): Promise<TPayload | unknown>;
@@ -37,38 +38,28 @@ interface ValidationOptions<TValue = unknown> {
37
38
  declare function readValidatedPayload<TInput, TOutput>(payload: TInput, validate: StandardSchemaValidator<TInput, TOutput>, options?: ValidationOptions<TInput>): Promise<TOutput>;
38
39
  declare function readValidatedPayload<TInput, TResult>(payload: TInput, validate: ValidationFunction<TInput, TResult>, options?: ValidationOptions<TInput>): Promise<ValidationResult<TInput, Awaited<TResult>>>;
39
40
  //#endregion
40
- //#region src/runtime/provider-resolution.d.ts
41
- type SandboxEvent = {
42
- context?: {
43
- cloudflare?: {
44
- env?: Record<string, unknown>;
45
- };
46
- _platform?: {
47
- cloudflare?: {
48
- env?: Record<string, unknown>;
49
- };
50
- };
51
- };
52
- };
53
- //#endregion
54
- //#region src/runtime/runtime.d.ts
55
- declare function createSandboxWithConfig(config: AgentSandboxConfig, local?: SandboxDefinitionOptions, context?: {
56
- event?: SandboxEvent;
57
- }): Promise<SandboxClient>;
58
- //#endregion
59
41
  //#region src/runtime/registry-types.d.ts
60
42
  type SandboxDefinitionName = keyof SandboxDefinitionModules;
61
- type SandboxRegistryDefinition<TName extends keyof SandboxDefinitionModules> = SandboxDefinitionModules[TName] extends {
62
- default?: infer TDefinition;
63
- } ? NonNullable<TDefinition> : never;
64
- type SandboxRunner<TDefinition> = TDefinition extends {
65
- run: infer TRun extends (...args: any[]) => any;
66
- } ? TRun : never;
67
- type SandboxPayload<TDefinition> = SandboxRunner<TDefinition> extends ((...args: infer TArgs) => unknown) ? TArgs[0] : unknown;
68
- type SandboxResult<TDefinition> = SandboxRunner<TDefinition> extends ((...args: any[]) => infer TResult) ? Awaited<TResult> : unknown;
43
+ type SandboxRegistryDefinition<TName extends keyof SandboxDefinitionModules> = SandboxDefinitionModules[TName];
44
+ type SandboxPayload<TDefinition> = TDefinition extends {
45
+ payload: infer TPayload;
46
+ } ? TPayload : unknown;
47
+ type SandboxResult<TDefinition> = TDefinition extends {
48
+ result: infer TResult;
49
+ } ? TResult : unknown;
69
50
  //#endregion
70
51
  //#region src/runtime/public.d.ts
71
52
  declare function runSandbox<const TName extends SandboxDefinitionName>(name: TName, payload?: SandboxPayload<SandboxRegistryDefinition<TName>>, options?: SandboxExecutionOptions): Promise<SandboxRunResult<SandboxResult<SandboxRegistryDefinition<TName>>>>;
72
53
  declare function runSandbox(name: string, payload?: unknown, options?: SandboxExecutionOptions): Promise<SandboxRunResult<unknown>>;
73
54
  //#endregion
74
- export { AgentSandboxConfig, CloudflareSandboxDefinitionProviderOptions, CloudflareSandboxOptions, SandboxDefinition, SandboxDefinitionBundle, SandboxDefinitionFromHandler, SandboxDefinitionHandler, SandboxDefinitionOptions, SandboxDefinitionProviderOptions, SandboxDefinitionRuntime, SandboxErrResult, SandboxError, type SandboxExecutionOptions, SandboxOkResult, type SandboxRunResult, VercelSandboxProviderOptions, createSandboxWithConfig, defineSandbox, getSandboxFeatureProvider, readRequestPayload, readValidatedPayload, runSandbox };
55
+ //#region src/runtime/runtime.d.ts
56
+ interface SandboxRunner {
57
+ readonly executionAuthority: ExecutionAuthority;
58
+ name: string;
59
+ run: <TPayload = unknown, TResult = unknown>(payload?: TPayload, options?: SandboxExecutionOptions) => Promise<TResult>;
60
+ }
61
+ declare function resolveSandboxRunner<TPayload = unknown, TResult = unknown>(name?: string): Promise<SandboxRunner & {
62
+ run: (payload?: TPayload, options?: SandboxExecutionOptions) => Promise<TResult>;
63
+ }>;
64
+ //#endregion
65
+ export { type AgentSandboxConfig, type CloudflareSandboxDefinitionProviderOptions, type CloudflareSandboxOptions, type SandboxDefinition, type SandboxDefinitionBundle, type SandboxDefinitionFromHandler, type SandboxDefinitionHandler, type SandboxDefinitionInput, type SandboxDefinitionOptions, type SandboxDefinitionProviderOptions, type SandboxExecutionOptions, type SandboxProjectOptions, type SandboxProvider, type SandboxRunResult, type SandboxRunner, type VercelSandboxProviderOptions, defineSandbox, type getSandboxFeatureProvider, readRequestPayload, readValidatedPayload, resolveSandboxRunner, runSandbox };