secure-exec 0.2.20 → 0.2.21
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 +72 -38
- package/dist/context.d.ts +17 -0
- package/dist/context.js +15 -0
- package/dist/index.d.ts +13 -9
- package/dist/index.js +6 -2
- package/dist/runtime.d.ts +33 -27
- package/dist/runtime.js +27 -57
- package/dist/typescript.d.ts +11 -8
- package/dist/typescript.js +4 -0
- package/package.json +15 -16
- package/dist/npm.d.ts +0 -20
- package/dist/npm.js +0 -26
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
|
-
Secure Node.js execution without a sandbox.<br/>Run untrusted JavaScript and TypeScript in an isolated VM with real Node.js APIs, npm packages, and a virtual filesystem
|
|
6
|
+
Secure Node.js execution without a sandbox.<br/>Run untrusted JavaScript and TypeScript in an isolated VM with real Node.js APIs, npm packages, and a virtual filesystem.
|
|
7
7
|
</p>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
@@ -46,8 +46,8 @@ other than `succeeded` carries an `error`, and guest stack traces arrive on
|
|
|
46
46
|
## Configure the VM
|
|
47
47
|
|
|
48
48
|
VM options go on the call: `permissions`, `limits`, `mounts`, and the rest of
|
|
49
|
-
the agentOS VM options.
|
|
50
|
-
is merged over the defaults.
|
|
49
|
+
the agentOS VM options. External network access is denied unless you allow it,
|
|
50
|
+
and a policy is merged over the defaults.
|
|
51
51
|
|
|
52
52
|
```ts
|
|
53
53
|
await evaluate(
|
|
@@ -60,24 +60,74 @@ await evaluate(
|
|
|
60
60
|
);
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
## Keep
|
|
63
|
+
## Keep things with a VM
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
The functions above are one-shot conveniences. When files, installed packages,
|
|
66
|
+
or a running server should outlast one call, create a VM. It is an agentOS VM
|
|
67
|
+
with Secure Exec's defaults.
|
|
67
68
|
|
|
68
69
|
```ts
|
|
69
|
-
import {
|
|
70
|
-
|
|
70
|
+
import { createVm } from "secure-exec";
|
|
71
|
+
|
|
72
|
+
const vm = await createVm({ permissions: { network: "allow" } });
|
|
73
|
+
try {
|
|
74
|
+
await vm.npm.install(["zod"]);
|
|
75
|
+
await vm.filesystem.writeFile("/workspace/data.json", JSON.stringify({ n: 40 }));
|
|
76
|
+
|
|
77
|
+
const result = await vm.javascript.evaluate<number>(
|
|
78
|
+
`import("node:fs").then((fs) => JSON.parse(fs.readFileSync("/workspace/data.json", "utf8")).n + 2)`,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const server = await vm.javascript.spawn(`/* long-running code */`);
|
|
82
|
+
await vm.process.kill(server.pid);
|
|
83
|
+
} finally {
|
|
84
|
+
await vm.dispose();
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`vm.javascript`, `vm.typescript`, `vm.npm`, `vm.filesystem`, `vm.network`, and
|
|
89
|
+
`vm.process` behave exactly as they do in agentOS.
|
|
90
|
+
|
|
91
|
+
## Keep variables with a context
|
|
71
92
|
|
|
72
|
-
|
|
93
|
+
Each call on a VM starts with fresh JavaScript memory. A context keeps it, like a
|
|
94
|
+
REPL, and several contexts run in parallel in one VM.
|
|
73
95
|
|
|
74
|
-
|
|
75
|
-
const
|
|
96
|
+
```ts
|
|
97
|
+
const context = await vm.createContext();
|
|
98
|
+
|
|
99
|
+
await context.execute("globalThis.total = 40");
|
|
100
|
+
const result = await context.evaluate<number>("total + 2");
|
|
76
101
|
|
|
77
|
-
await context.reset();
|
|
102
|
+
await context.reset(); // clear state
|
|
103
|
+
await context.dispose(); // delete the context, keep the VM
|
|
78
104
|
```
|
|
79
105
|
|
|
80
|
-
|
|
106
|
+
## Host functions
|
|
107
|
+
|
|
108
|
+
Give the code your own functions without giving it your credentials. Each
|
|
109
|
+
collection is a global inside the VM, and each function is async.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { evaluate, hostFunction, hostFunctions } from "secure-exec";
|
|
113
|
+
import { z } from "zod";
|
|
114
|
+
|
|
115
|
+
const orders = hostFunctions({
|
|
116
|
+
name: "orders",
|
|
117
|
+
description: "Look up customer orders.",
|
|
118
|
+
functions: {
|
|
119
|
+
list: hostFunction({
|
|
120
|
+
description: "List a customer's orders.",
|
|
121
|
+
inputSchema: z.object({ customer: z.string() }),
|
|
122
|
+
execute: ({ customer }) => db.orders.findMany({ customer }),
|
|
123
|
+
}),
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
await evaluate(`orders.list({ customer: "c_123" }).then((rows) => rows.length)`, {
|
|
128
|
+
hostFunctions: [orders],
|
|
129
|
+
});
|
|
130
|
+
```
|
|
81
131
|
|
|
82
132
|
## TypeScript
|
|
83
133
|
|
|
@@ -92,40 +142,24 @@ const checked = await check(`const total: number = "nope";`);
|
|
|
92
142
|
for (const diagnostic of checked.diagnostics) console.log(diagnostic.message);
|
|
93
143
|
```
|
|
94
144
|
|
|
95
|
-
##
|
|
96
|
-
|
|
97
|
-
`secure-exec/npm` installs packages into a context's VM.
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
import { createContext, evaluate } from "secure-exec";
|
|
101
|
-
import { install } from "secure-exec/npm";
|
|
102
|
-
|
|
103
|
-
await using context = await createContext({ permissions: { network: "allow" } });
|
|
104
|
-
await install(["zod"], { context });
|
|
105
|
-
|
|
106
|
-
// Packages install into /workspace. Inline code resolves imports from
|
|
107
|
-
// `filePath`, so place it next to node_modules.
|
|
108
|
-
await evaluate(`import("zod").then(({ z }) => z.string().parse("ok"))`, {
|
|
109
|
-
context,
|
|
110
|
-
filePath: "/workspace/main.mjs",
|
|
111
|
-
});
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
`runScript` and `runPackage` work like `npm run` and `npx`.
|
|
115
|
-
|
|
116
|
-
## Warm up
|
|
145
|
+
## Lifecycle
|
|
117
146
|
|
|
118
147
|
The first call starts a shared sidecar process. Call `init()` at startup to pay
|
|
119
|
-
that cost ahead of time
|
|
148
|
+
that cost ahead of time, and `shutdown()` to stop it, for example in a test
|
|
149
|
+
teardown hook.
|
|
120
150
|
|
|
121
151
|
```ts
|
|
122
|
-
import { init } from "secure-exec";
|
|
152
|
+
import { init, shutdown } from "secure-exec";
|
|
123
153
|
|
|
124
154
|
await init();
|
|
155
|
+
// ...
|
|
156
|
+
await shutdown();
|
|
125
157
|
```
|
|
126
158
|
|
|
127
159
|
## More
|
|
128
160
|
|
|
129
|
-
|
|
161
|
+
Need Python? Use the [agentOS Python execution API](https://rivet.dev/agentos/docs/python), which has the same `execute` and `evaluate` shape.
|
|
162
|
+
|
|
163
|
+
For processes, filesystem access, and agent sessions, use
|
|
130
164
|
[`@rivet-dev/agentos-core`](https://rivet.dev/agentos) directly. Read the [documentation](https://rivet.dev/secure-exec/docs), or browse the examples in
|
|
131
165
|
[`secure-exec/examples`](https://github.com/rivet-dev/agentos/tree/main/secure-exec/examples).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AgentOs, CodeEvaluationResult, CodeExecutionResult, JavaScriptEvaluationOptions, JavaScriptExecutionOptions, JsonValue } from "@rivet-dev/agentos-core";
|
|
2
|
+
/**
|
|
3
|
+
* Retained JavaScript state in a VM: variables and imports survive between
|
|
4
|
+
* calls. A context runs one call at a time. For TypeScript in the same state,
|
|
5
|
+
* pass `contextId` to `vm.typescript`.
|
|
6
|
+
*/
|
|
7
|
+
export interface Context extends AsyncDisposable {
|
|
8
|
+
readonly contextId: string;
|
|
9
|
+
execute(source: string, options?: Omit<JavaScriptExecutionOptions, "contextId">): Promise<CodeExecutionResult>;
|
|
10
|
+
evaluate<T = JsonValue>(source: string, options?: Omit<JavaScriptEvaluationOptions, "contextId">): Promise<CodeEvaluationResult<T>>;
|
|
11
|
+
/** Clear the retained state and keep the context. */
|
|
12
|
+
reset(): Promise<void>;
|
|
13
|
+
/** Delete the context. The VM is left running. */
|
|
14
|
+
dispose(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/** Create a context in `vm`. A VM can hold many, and they run in parallel. */
|
|
17
|
+
export declare function createContext(vm: AgentOs): Promise<Context>;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
/** Create a context in `vm`. A VM can hold many, and they run in parallel. */
|
|
3
|
+
export async function createContext(vm) {
|
|
4
|
+
const contextId = randomUUID();
|
|
5
|
+
await vm.createContext(contextId);
|
|
6
|
+
const dispose = () => vm.contexts.delete(contextId);
|
|
7
|
+
return {
|
|
8
|
+
contextId,
|
|
9
|
+
execute: (source, options) => vm.javascript.execute(source, { ...options, contextId }),
|
|
10
|
+
evaluate: (source, options) => vm.javascript.evaluate(source, { ...options, contextId }),
|
|
11
|
+
reset: () => vm.contexts.reset(contextId),
|
|
12
|
+
dispose,
|
|
13
|
+
[Symbol.asyncDispose]: dispose,
|
|
14
|
+
};
|
|
15
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import type { CodeEvaluationResult, CodeExecutionResult, JavaScriptEvaluationOptions, JavaScriptExecutionOptions, JsonValue } from "@rivet-dev/agentos-core";
|
|
2
|
-
import { type
|
|
3
|
-
export type { CodeEvaluationResult, CodeExecutionResult, ExecutionErrorData, ExecutionOutputOptions, JsonValue, LimitWarning, MountConfig, OutputCapture, Permissions, } from "@rivet-dev/agentos-core";
|
|
4
|
-
export { createHostDirBackend, nodeModulesMount, } from "@rivet-dev/agentos-core";
|
|
5
|
-
export {
|
|
6
|
-
export type
|
|
7
|
-
export type
|
|
1
|
+
import type { CodeEvaluationResult, CodeExecutionResult, HostFunctionSchemas, JavaScriptEvaluationOptions, JavaScriptExecutionOptions, JsonValue, LanguageExecutionOptions } from "@rivet-dev/agentos-core";
|
|
2
|
+
import { type OneShot } from "./runtime.js";
|
|
3
|
+
export type { CodeEvaluationResult, CodeExecutionResult, ExecutionErrorData, ExecutionOutputOptions, HostFunction, HostFunctionCollection, HostFunctionCollections, HttpRequest, HttpResponse, JsonValue, LimitWarning, MountConfig, OutputCapture, Permissions, ProcessDescriptor, ProcessExit, SidecarRejectionDetail, } from "@rivet-dev/agentos-core";
|
|
4
|
+
export { createHostDirBackend, hostDirMount, KernelError, nodeModulesMount, SidecarProcessError, SidecarProcessExited, SidecarRejectedError, SidecarSilenceTimeout, } from "@rivet-dev/agentos-core";
|
|
5
|
+
export type { Context } from "./context.js";
|
|
6
|
+
export { createVm, init, shutdown, type Vm, type VmOptions, } from "./runtime.js";
|
|
7
|
+
export type ExecuteOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = OneShot<JavaScriptExecutionOptions, HOST_FUNCTIONS>;
|
|
8
|
+
export type EvaluateOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = OneShot<JavaScriptEvaluationOptions, HOST_FUNCTIONS>;
|
|
9
|
+
export type ExecuteFileOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = OneShot<LanguageExecutionOptions, HOST_FUNCTIONS>;
|
|
8
10
|
/** Run JavaScript for its side effects and captured output. */
|
|
9
|
-
export declare function execute(source: string, options?: ExecuteOptions): Promise<CodeExecutionResult>;
|
|
11
|
+
export declare function execute<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(source: string, options?: ExecuteOptions<HOST_FUNCTIONS>): Promise<CodeExecutionResult>;
|
|
10
12
|
/** Evaluate one JavaScript expression and return its JSON value. */
|
|
11
|
-
export declare function evaluate<T = JsonValue>(source: string, options?: EvaluateOptions): Promise<CodeEvaluationResult<T>>;
|
|
13
|
+
export declare function evaluate<T = JsonValue, HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(source: string, options?: EvaluateOptions<HOST_FUNCTIONS>): Promise<CodeEvaluationResult<T>>;
|
|
14
|
+
/** Run a JavaScript file from a mount, by its path inside the VM. */
|
|
15
|
+
export declare function executeFile<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(path: string, options?: ExecuteFileOptions<HOST_FUNCTIONS>): Promise<CodeExecutionResult>;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { run } from "./runtime.js";
|
|
2
|
-
export { createHostDirBackend, nodeModulesMount, } from "@rivet-dev/agentos-core";
|
|
3
|
-
export {
|
|
2
|
+
export { createHostDirBackend, hostDirMount, KernelError, nodeModulesMount, SidecarProcessError, SidecarProcessExited, SidecarRejectedError, SidecarSilenceTimeout, } from "@rivet-dev/agentos-core";
|
|
3
|
+
export { createVm, init, shutdown, } from "./runtime.js";
|
|
4
4
|
/** Run JavaScript for its side effects and captured output. */
|
|
5
5
|
export function execute(source, options) {
|
|
6
6
|
return run(options, (vm, operationOptions) => vm.javascript.execute(source, operationOptions));
|
|
@@ -9,3 +9,7 @@ export function execute(source, options) {
|
|
|
9
9
|
export function evaluate(source, options) {
|
|
10
10
|
return run(options, (vm, operationOptions) => vm.javascript.evaluate(source, operationOptions));
|
|
11
11
|
}
|
|
12
|
+
/** Run a JavaScript file from a mount, by its path inside the VM. */
|
|
13
|
+
export function executeFile(path, options) {
|
|
14
|
+
return run(options, (vm, operationOptions) => vm.javascript.executeFile(path, operationOptions));
|
|
15
|
+
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,33 +1,39 @@
|
|
|
1
|
-
import { AgentOs, type AgentOsOptions } from "@rivet-dev/agentos-core";
|
|
2
|
-
|
|
3
|
-
export type VmOptions = AgentOsOptions;
|
|
1
|
+
import { AgentOs, type AgentOsOptions, type HostFunctionSchemas } from "@rivet-dev/agentos-core";
|
|
2
|
+
import { type Context } from "./context.js";
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Options for the VM that runs the code: permissions, limits, mounts, and so on.
|
|
5
|
+
*
|
|
6
|
+
* The type parameter exists only so each host function's `execute` infers its
|
|
7
|
+
* input from its own `inputSchema`; callers never write it.
|
|
7
8
|
*/
|
|
8
|
-
export
|
|
9
|
-
readonly contextId: string;
|
|
10
|
-
/** Clear the retained state. The VM and its filesystem are kept. */
|
|
11
|
-
reset(): Promise<void>;
|
|
12
|
-
/** Dispose the VM that backs this context. */
|
|
13
|
-
dispose(): Promise<void>;
|
|
14
|
-
}
|
|
9
|
+
export type VmOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = AgentOsOptions<HOST_FUNCTIONS>;
|
|
15
10
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
11
|
+
* A VM that lives until you dispose it. Files, installed packages, and
|
|
12
|
+
* background processes persist across the calls you make on it. Every namespace
|
|
13
|
+
* is the agentOS one, unchanged, so the agentOS docs describe its behavior.
|
|
18
14
|
*/
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
export interface Vm extends AsyncDisposable {
|
|
16
|
+
readonly javascript: Omit<AgentOs["javascript"], "npm">;
|
|
17
|
+
readonly typescript: AgentOs["typescript"];
|
|
18
|
+
readonly npm: AgentOs["javascript"]["npm"];
|
|
19
|
+
readonly filesystem: AgentOs["filesystem"];
|
|
20
|
+
readonly network: AgentOs["network"];
|
|
21
|
+
readonly process: AgentOs["process"];
|
|
22
|
+
/** Create a context: JavaScript state that survives between calls. */
|
|
23
|
+
createContext(): Promise<Context>;
|
|
24
|
+
/** Dispose the VM and everything in it. */
|
|
25
|
+
dispose(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export declare function createVm<HOST_FUNCTIONS extends HostFunctionSchemas>(options?: VmOptions<HOST_FUNCTIONS>): Promise<Vm>;
|
|
28
|
+
/** Options for a one-shot call: the operation's own options plus VM options. */
|
|
29
|
+
export type OneShot<O, HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = Omit<O, "contextId"> & VmOptions<HOST_FUNCTIONS>;
|
|
26
30
|
/** Start the shared sidecar process now so the first operation does not pay for it. */
|
|
27
31
|
export declare function init(): Promise<void>;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Stop the shared sidecar process and every VM still running in it. Call it when
|
|
34
|
+
* your process should exit while work may be in flight, such as at the end of a
|
|
35
|
+
* test run. The next call starts a new sidecar.
|
|
36
|
+
*/
|
|
37
|
+
export declare function shutdown(): Promise<void>;
|
|
38
|
+
/** Run `operation` in a fresh VM built from the VM options, then dispose it. */
|
|
39
|
+
export declare function run<O extends object, R, HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(options: (O & VmOptions<HOST_FUNCTIONS>) | undefined, operation: (vm: Vm, options: O) => Promise<R>): Promise<R>;
|
package/dist/runtime.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AgentOs, } from "@rivet-dev/agentos-core";
|
|
2
|
+
import { createContext } from "./context.js";
|
|
2
3
|
// Keep in sync with `AgentOsOptions`. The record type makes a missing or
|
|
3
4
|
// unknown key a compile error.
|
|
4
5
|
const VM_OPTION_KEYS = Object.keys({
|
|
@@ -13,7 +14,7 @@ const VM_OPTION_KEYS = Object.keys({
|
|
|
13
14
|
mounts: true,
|
|
14
15
|
sandbox: true,
|
|
15
16
|
scheduleDriver: true,
|
|
16
|
-
|
|
17
|
+
hostFunctions: true,
|
|
17
18
|
permissions: true,
|
|
18
19
|
sidecar: true,
|
|
19
20
|
limits: true,
|
|
@@ -21,58 +22,38 @@ const VM_OPTION_KEYS = Object.keys({
|
|
|
21
22
|
onAgentExit: true,
|
|
22
23
|
onLimitWarning: true,
|
|
23
24
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
...options,
|
|
40
|
-
permissions: { ...BASE_PERMISSIONS, ...options.permissions },
|
|
41
|
-
});
|
|
25
|
+
export async function createVm(options = {}) {
|
|
26
|
+
const vm = await AgentOs.create(options);
|
|
27
|
+
const { npm, ...javascript } = vm.javascript;
|
|
28
|
+
const dispose = () => vm.dispose();
|
|
29
|
+
return {
|
|
30
|
+
javascript,
|
|
31
|
+
typescript: vm.typescript,
|
|
32
|
+
npm,
|
|
33
|
+
filesystem: vm.filesystem,
|
|
34
|
+
network: vm.network,
|
|
35
|
+
process: vm.process,
|
|
36
|
+
createContext: () => createContext(vm),
|
|
37
|
+
dispose,
|
|
38
|
+
[Symbol.asyncDispose]: dispose,
|
|
39
|
+
};
|
|
42
40
|
}
|
|
43
|
-
const contextVms = new WeakMap();
|
|
44
41
|
/** Start the shared sidecar process now so the first operation does not pay for it. */
|
|
45
42
|
export async function init() {
|
|
46
43
|
// A bare VM is enough to start the sidecar; skip projecting default software.
|
|
47
44
|
const vm = await createVm({ defaultSoftware: false });
|
|
48
45
|
await vm.dispose();
|
|
49
46
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
const context = {
|
|
60
|
-
contextId,
|
|
61
|
-
reset: () => vm.contexts.reset(contextId),
|
|
62
|
-
dispose: () => vm.dispose(),
|
|
63
|
-
[Symbol.asyncDispose]: () => vm.dispose(),
|
|
64
|
-
};
|
|
65
|
-
contextVms.set(context, vm);
|
|
66
|
-
return context;
|
|
67
|
-
}
|
|
68
|
-
export function contextVm(context) {
|
|
69
|
-
const vm = contextVms.get(context);
|
|
70
|
-
if (!vm) {
|
|
71
|
-
throw new TypeError("context must be created by createContext()");
|
|
72
|
-
}
|
|
73
|
-
return vm;
|
|
47
|
+
/**
|
|
48
|
+
* Stop the shared sidecar process and every VM still running in it. Call it when
|
|
49
|
+
* your process should exit while work may be in flight, such as at the end of a
|
|
50
|
+
* test run. The next call starts a new sidecar.
|
|
51
|
+
*/
|
|
52
|
+
export async function shutdown() {
|
|
53
|
+
const sidecar = await AgentOs.getSharedSidecar();
|
|
54
|
+
await sidecar.dispose();
|
|
74
55
|
}
|
|
75
|
-
/** Run `operation` in
|
|
56
|
+
/** Run `operation` in a fresh VM built from the VM options, then dispose it. */
|
|
76
57
|
export async function run(options, operation) {
|
|
77
58
|
const vmOptions = {};
|
|
78
59
|
const operationOptions = {};
|
|
@@ -80,21 +61,10 @@ export async function run(options, operation) {
|
|
|
80
61
|
const isVmOption = VM_OPTION_KEYS.includes(key);
|
|
81
62
|
(isVmOption ? vmOptions : operationOptions)[key] = value;
|
|
82
63
|
}
|
|
83
|
-
const { context, ...rest } = operationOptions;
|
|
84
|
-
if (context) {
|
|
85
|
-
const configured = Object.keys(vmOptions);
|
|
86
|
-
if (configured.length > 0) {
|
|
87
|
-
throw new TypeError(`${configured.join(", ")} cannot be combined with context; pass VM options to createContext()`);
|
|
88
|
-
}
|
|
89
|
-
return operation(contextVm(context), {
|
|
90
|
-
...rest,
|
|
91
|
-
contextId: context.contextId,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
64
|
const vm = await createVm(vmOptions);
|
|
95
65
|
let result;
|
|
96
66
|
try {
|
|
97
|
-
result = await operation(vm,
|
|
67
|
+
result = await operation(vm, operationOptions);
|
|
98
68
|
}
|
|
99
69
|
catch (error) {
|
|
100
70
|
throw await disposeAfterFailure(vm, error);
|
package/dist/typescript.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import type { CodeEvaluationResult, CodeExecutionResult, JsonValue, TypeScriptCheckOptions, TypeScriptCheckResult, TypeScriptEvaluationOptions, TypeScriptExecutionOptions } from "@rivet-dev/agentos-core";
|
|
2
|
-
import { type
|
|
1
|
+
import type { CodeEvaluationResult, CodeExecutionResult, HostFunctionSchemas, JsonValue, TypeScriptCheckOptions, TypeScriptCheckResult, TypeScriptEvaluationOptions, TypeScriptExecutionOptions, TypeScriptFileExecutionOptions } from "@rivet-dev/agentos-core";
|
|
2
|
+
import { type OneShot } from "./runtime.js";
|
|
3
3
|
export type { TypeScriptCheckResult, TypeScriptDiagnostic, } from "@rivet-dev/agentos-core";
|
|
4
|
-
export type ExecuteOptions =
|
|
5
|
-
export type EvaluateOptions =
|
|
6
|
-
export type
|
|
4
|
+
export type ExecuteOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = OneShot<TypeScriptExecutionOptions, HOST_FUNCTIONS>;
|
|
5
|
+
export type EvaluateOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = OneShot<TypeScriptEvaluationOptions, HOST_FUNCTIONS>;
|
|
6
|
+
export type ExecuteFileOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = OneShot<TypeScriptFileExecutionOptions, HOST_FUNCTIONS>;
|
|
7
|
+
export type CheckOptions<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas> = OneShot<TypeScriptCheckOptions, HOST_FUNCTIONS>;
|
|
7
8
|
/** Run TypeScript for its side effects and captured output. Types are stripped, not checked. */
|
|
8
|
-
export declare function execute(source: string, options?: ExecuteOptions): Promise<CodeExecutionResult>;
|
|
9
|
+
export declare function execute<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(source: string, options?: ExecuteOptions<HOST_FUNCTIONS>): Promise<CodeExecutionResult>;
|
|
9
10
|
/** Evaluate one TypeScript expression and return its JSON value. Types are stripped, not checked. */
|
|
10
|
-
export declare function evaluate<T = JsonValue>(source: string, options?: EvaluateOptions): Promise<CodeEvaluationResult<T>>;
|
|
11
|
+
export declare function evaluate<T = JsonValue, HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(source: string, options?: EvaluateOptions<HOST_FUNCTIONS>): Promise<CodeEvaluationResult<T>>;
|
|
12
|
+
/** Run a TypeScript file from a mount, by its path inside the VM. Types are stripped, not checked. */
|
|
13
|
+
export declare function executeFile<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(path: string, options?: ExecuteFileOptions<HOST_FUNCTIONS>): Promise<CodeExecutionResult>;
|
|
11
14
|
/** Type-check TypeScript without running it. */
|
|
12
|
-
export declare function check(source: string, options?: CheckOptions): Promise<TypeScriptCheckResult>;
|
|
15
|
+
export declare function check<HOST_FUNCTIONS extends HostFunctionSchemas = HostFunctionSchemas>(source: string, options?: CheckOptions<HOST_FUNCTIONS>): Promise<TypeScriptCheckResult>;
|
package/dist/typescript.js
CHANGED
|
@@ -7,6 +7,10 @@ export function execute(source, options) {
|
|
|
7
7
|
export function evaluate(source, options) {
|
|
8
8
|
return run(options, (vm, operationOptions) => vm.typescript.evaluate(source, operationOptions));
|
|
9
9
|
}
|
|
10
|
+
/** Run a TypeScript file from a mount, by its path inside the VM. Types are stripped, not checked. */
|
|
11
|
+
export function executeFile(path, options) {
|
|
12
|
+
return run(options, (vm, operationOptions) => vm.typescript.executeFile(path, operationOptions));
|
|
13
|
+
}
|
|
10
14
|
/** Type-check TypeScript without running it. */
|
|
11
15
|
export function check(source, options) {
|
|
12
16
|
return run(options, (vm, operationOptions) => vm.typescript.check(source, operationOptions));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "secure-exec",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.21",
|
|
4
4
|
"description": "Secure Node.js execution for AI agents: run untrusted JavaScript and TypeScript in an isolated VM.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -10,26 +10,19 @@
|
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
12
|
"types": "./dist/index.d.ts",
|
|
13
|
-
"import": "./dist/index.js"
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
14
15
|
},
|
|
15
16
|
"./typescript": {
|
|
16
17
|
"types": "./dist/typescript.d.ts",
|
|
17
|
-
"import": "./dist/typescript.js"
|
|
18
|
-
|
|
19
|
-
"./npm": {
|
|
20
|
-
"types": "./dist/npm.d.ts",
|
|
21
|
-
"import": "./dist/npm.js"
|
|
18
|
+
"import": "./dist/typescript.js",
|
|
19
|
+
"default": "./dist/typescript.js"
|
|
22
20
|
}
|
|
23
21
|
},
|
|
24
22
|
"files": [
|
|
25
23
|
"dist",
|
|
26
24
|
"README.md"
|
|
27
25
|
],
|
|
28
|
-
"repository": {
|
|
29
|
-
"type": "git",
|
|
30
|
-
"url": "https://github.com/rivet-dev/agentos.git",
|
|
31
|
-
"directory": "packages/secure-exec"
|
|
32
|
-
},
|
|
33
26
|
"homepage": "https://rivet.dev/secure-exec",
|
|
34
27
|
"engines": {
|
|
35
28
|
"node": ">=22.0.0"
|
|
@@ -43,11 +36,17 @@
|
|
|
43
36
|
"test": "vitest run"
|
|
44
37
|
},
|
|
45
38
|
"dependencies": {
|
|
46
|
-
"@rivet-dev/agentos-core": "0.2.
|
|
39
|
+
"@rivet-dev/agentos-core": "0.2.21"
|
|
47
40
|
},
|
|
48
41
|
"devDependencies": {
|
|
49
|
-
"@types/node": "^22.
|
|
50
|
-
"typescript": "^5.7.
|
|
51
|
-
"vitest": "^2.1.8"
|
|
42
|
+
"@types/node": "^22.10.2",
|
|
43
|
+
"typescript": "^5.7.2",
|
|
44
|
+
"vitest": "^2.1.8",
|
|
45
|
+
"zod": "^4.1.11"
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/rivet-dev/agentos.git",
|
|
50
|
+
"directory": "secure-exec"
|
|
52
51
|
}
|
|
53
52
|
}
|
package/dist/npm.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { CodeExecutionResult, LanguageExecutionOptions, NpmPackageInstallOptions, NpmProjectInstallOptions } from "@rivet-dev/agentos-core";
|
|
2
|
-
import { type Context } from "./runtime.js";
|
|
3
|
-
type InContext<O> = Omit<O, "contextId"> & {
|
|
4
|
-
context: Context;
|
|
5
|
-
};
|
|
6
|
-
export type InstallProjectOptions = InContext<NpmProjectInstallOptions>;
|
|
7
|
-
export type InstallPackagesOptions = InContext<NpmPackageInstallOptions>;
|
|
8
|
-
export type RunScriptOptions = InContext<LanguageExecutionOptions>;
|
|
9
|
-
export type RunPackageOptions = InContext<LanguageExecutionOptions & {
|
|
10
|
-
binary?: string;
|
|
11
|
-
}>;
|
|
12
|
-
/** Install the dependencies of the `package.json` in the context's working directory. */
|
|
13
|
-
export declare function install(options: InstallProjectOptions): Promise<CodeExecutionResult>;
|
|
14
|
-
/** Install packages into the context's VM. */
|
|
15
|
-
export declare function install(packages: string | string[], options: InstallPackagesOptions): Promise<CodeExecutionResult>;
|
|
16
|
-
/** Run a `package.json` script, like `npm run`. */
|
|
17
|
-
export declare function runScript(script: string, options: RunScriptOptions): Promise<CodeExecutionResult>;
|
|
18
|
-
/** Run a package's binary, like `npx`. */
|
|
19
|
-
export declare function runPackage(packageSpec: string, options: RunPackageOptions): Promise<CodeExecutionResult>;
|
|
20
|
-
export {};
|
package/dist/npm.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { contextVm } from "./runtime.js";
|
|
2
|
-
export function install(packagesOrOptions, packageOptions) {
|
|
3
|
-
if (typeof packagesOrOptions === "string" ||
|
|
4
|
-
Array.isArray(packagesOrOptions)) {
|
|
5
|
-
const { context, ...options } = requireContext(packageOptions);
|
|
6
|
-
return contextVm(context).javascript.npm.install(packagesOrOptions, options);
|
|
7
|
-
}
|
|
8
|
-
const { context, ...options } = requireContext(packagesOrOptions);
|
|
9
|
-
return contextVm(context).javascript.npm.install(options);
|
|
10
|
-
}
|
|
11
|
-
/** Run a `package.json` script, like `npm run`. */
|
|
12
|
-
export function runScript(script, options) {
|
|
13
|
-
const { context, ...rest } = requireContext(options);
|
|
14
|
-
return contextVm(context).javascript.npm.runScript(script, rest);
|
|
15
|
-
}
|
|
16
|
-
/** Run a package's binary, like `npx`. */
|
|
17
|
-
export function runPackage(packageSpec, options) {
|
|
18
|
-
const { context, ...rest } = requireContext(options);
|
|
19
|
-
return contextVm(context).javascript.npm.runPackage(packageSpec, rest);
|
|
20
|
-
}
|
|
21
|
-
function requireContext(options) {
|
|
22
|
-
if (!options?.context) {
|
|
23
|
-
throw new TypeError("npm operations require a context; create one with createContext()");
|
|
24
|
-
}
|
|
25
|
-
return options;
|
|
26
|
-
}
|