microsandbox 0.1.0 → 0.1.1
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 +121 -16
- package/dist/base-sandbox.d.ts +91 -0
- package/dist/base-sandbox.js +264 -0
- package/dist/base-sandbox.js.map +1 -0
- package/dist/command-execution.d.ts +59 -0
- package/dist/command-execution.js +102 -0
- package/dist/command-execution.js.map +1 -0
- package/dist/command.d.ts +24 -0
- package/dist/command.js +84 -0
- package/dist/command.js.map +1 -0
- package/dist/execution.d.ts +52 -0
- package/dist/execution.js +103 -0
- package/dist/execution.js.map +1 -0
- package/dist/index.d.ts +10 -9
- package/dist/index.js +20 -14
- package/dist/index.js.map +1 -0
- package/dist/metrics.d.ts +69 -0
- package/dist/metrics.js +142 -0
- package/dist/metrics.js.map +1 -0
- package/dist/node-sandbox.d.ts +32 -0
- package/dist/node-sandbox.js +92 -0
- package/dist/node-sandbox.js.map +1 -0
- package/dist/python-sandbox.d.ts +32 -0
- package/dist/python-sandbox.js +92 -0
- package/dist/python-sandbox.js.map +1 -0
- package/dist/types.d.ts +110 -0
- package/dist/types.js +88 -0
- package/dist/types.js.map +1 -0
- package/package.json +23 -4
package/README.md
CHANGED
|
@@ -1,33 +1,138 @@
|
|
|
1
|
-
# Microsandbox JavaScript SDK
|
|
1
|
+
# Microsandbox JavaScript/TypeScript SDK
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A secure, efficient sandbox for executing untrusted code from TypeScript/JavaScript applications.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
```
|
|
7
|
+
```sh
|
|
8
8
|
npm install microsandbox
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Requirements
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
- Node.js 14.x or later
|
|
14
|
+
- A running Microsandbox server with a valid API key
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
greet("World");
|
|
16
|
+
## Configuration
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
Before using the SDK, you need to set your Microsandbox API key:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
export MSB_API_KEY=msb_***
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
Here's a simple example using the NodeSandbox to execute JavaScript code:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { NodeSandbox } from "microsandbox";
|
|
30
|
+
|
|
31
|
+
async function main() {
|
|
32
|
+
// Create and start a Node.js sandbox
|
|
33
|
+
const sb = await NodeSandbox.create({ name: "test" });
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
// Execute JavaScript code in the sandbox
|
|
37
|
+
let exec = await sb.run("var name = 'JavaScript'");
|
|
38
|
+
exec = await sb.run("console.log(`Hello ${name}!`)");
|
|
39
|
+
|
|
40
|
+
// Get the output
|
|
41
|
+
console.log(await exec.output()); // prints Hello JavaScript!
|
|
42
|
+
} finally {
|
|
43
|
+
// Stop the sandbox when done
|
|
44
|
+
await sb.stop();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
main().catch(console.error);
|
|
22
49
|
```
|
|
23
50
|
|
|
24
|
-
##
|
|
51
|
+
## Available Sandbox Types
|
|
52
|
+
|
|
53
|
+
The SDK provides several specialized sandbox environments:
|
|
54
|
+
|
|
55
|
+
- `NodeSandbox`: For executing JavaScript/TypeScript code
|
|
56
|
+
- `PythonSandbox`: For executing Python code
|
|
57
|
+
|
|
58
|
+
Each sandbox type is optimized for its respective language environment.
|
|
59
|
+
|
|
60
|
+
## Creating a Sandbox
|
|
61
|
+
|
|
62
|
+
You can create a sandbox with custom options:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { NodeSandbox, SandboxOptions } from "microsandbox";
|
|
66
|
+
|
|
67
|
+
// Using builder pattern
|
|
68
|
+
const options = SandboxOptions.builder()
|
|
69
|
+
.name("my-sandbox")
|
|
70
|
+
.memory(1024)
|
|
71
|
+
.cpus(2)
|
|
72
|
+
.build();
|
|
73
|
+
|
|
74
|
+
// Create and start the sandbox
|
|
75
|
+
const sandbox = await NodeSandbox.create(options);
|
|
76
|
+
|
|
77
|
+
// Don't forget to stop the sandbox when done
|
|
78
|
+
await sandbox.stop();
|
|
79
|
+
```
|
|
25
80
|
|
|
26
|
-
|
|
81
|
+
## Running Shell Commands
|
|
27
82
|
|
|
28
|
-
|
|
29
|
-
|
|
83
|
+
You can execute shell commands in the sandbox:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const cmd = await sandbox.command.run("ls", ["-la"]);
|
|
87
|
+
console.log(await cmd.output());
|
|
88
|
+
console.log("Exit code:", cmd.exitCode);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Monitoring Sandbox Metrics
|
|
92
|
+
|
|
93
|
+
Get resource usage metrics for your sandbox:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// Get all metrics
|
|
97
|
+
const metrics = await sandbox.metrics.all();
|
|
98
|
+
console.log(metrics);
|
|
99
|
+
|
|
100
|
+
// Get specific metrics
|
|
101
|
+
const cpuUsage = await sandbox.metrics.cpu();
|
|
102
|
+
const memoryUsage = await sandbox.metrics.memory();
|
|
103
|
+
const diskUsage = await sandbox.metrics.disk();
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Error Handling
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
try {
|
|
110
|
+
const exec = await sandbox.run("invalid code");
|
|
111
|
+
|
|
112
|
+
if (exec.hasError()) {
|
|
113
|
+
console.error("Execution error:", await exec.error());
|
|
114
|
+
} else {
|
|
115
|
+
console.log("Result:", await exec.output());
|
|
116
|
+
}
|
|
117
|
+
} catch (err) {
|
|
118
|
+
console.error("Sandbox error:", err.message);
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Development
|
|
123
|
+
|
|
124
|
+
### Building the SDK
|
|
125
|
+
|
|
126
|
+
```sh
|
|
127
|
+
npm run build
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Running Tests
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
npm test
|
|
134
|
+
```
|
|
30
135
|
|
|
31
136
|
## License
|
|
32
137
|
|
|
33
|
-
|
|
138
|
+
Apache License 2.0
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base sandbox implementation for the Microsandbox TypeScript SDK.
|
|
3
|
+
*/
|
|
4
|
+
import { Command } from "./command";
|
|
5
|
+
import { Metrics } from "./metrics";
|
|
6
|
+
import { Execution } from "./execution";
|
|
7
|
+
import { SandboxOptions } from "./types";
|
|
8
|
+
export declare abstract class BaseSandbox {
|
|
9
|
+
protected _serverUrl: string;
|
|
10
|
+
protected _namespace: string;
|
|
11
|
+
protected _name: string;
|
|
12
|
+
protected _apiKey: string | undefined;
|
|
13
|
+
protected _isStarted: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Initialize a base sandbox instance.
|
|
16
|
+
*
|
|
17
|
+
* @param options - Configuration options for the sandbox
|
|
18
|
+
*/
|
|
19
|
+
constructor(options?: SandboxOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Create and initialize a new sandbox instance.
|
|
22
|
+
*
|
|
23
|
+
* This is the base implementation that subclasses should call.
|
|
24
|
+
*
|
|
25
|
+
* @param ctor - The constructor function for the subclass
|
|
26
|
+
* @param options - Configuration options for the sandbox
|
|
27
|
+
* @returns A Promise resolving to a new sandbox instance
|
|
28
|
+
*/
|
|
29
|
+
protected static createBase<T extends BaseSandbox>(ctor: new (options?: SandboxOptions) => T, options?: SandboxOptions): Promise<T>;
|
|
30
|
+
static create(options?: SandboxOptions): Promise<BaseSandbox>;
|
|
31
|
+
/**
|
|
32
|
+
* Get the default Docker image for this sandbox type.
|
|
33
|
+
*
|
|
34
|
+
* @returns A string containing the Docker image name and tag
|
|
35
|
+
*/
|
|
36
|
+
abstract getDefaultImage(): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Start the sandbox container.
|
|
39
|
+
*
|
|
40
|
+
* @param image - Docker image to use for the sandbox (defaults to language-specific image)
|
|
41
|
+
* @param memory - Memory limit in MB
|
|
42
|
+
* @param cpus - CPU limit (will be rounded to nearest integer)
|
|
43
|
+
* @param timeout - Maximum time in seconds to wait for the sandbox to start (default: 180 seconds)
|
|
44
|
+
*
|
|
45
|
+
* @throws Error if the sandbox fails to start
|
|
46
|
+
* @throws Error if the sandbox doesn't start within the specified timeout
|
|
47
|
+
*/
|
|
48
|
+
start(image?: string, memory?: number, cpus?: number, timeout?: number): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Stop the sandbox container.
|
|
51
|
+
*
|
|
52
|
+
* @throws Error if the sandbox fails to stop
|
|
53
|
+
*/
|
|
54
|
+
stop(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Execute code in the sandbox.
|
|
57
|
+
*
|
|
58
|
+
* @param code - Code to execute
|
|
59
|
+
* @returns An Execution object representing the executed code
|
|
60
|
+
* @throws Error if execution fails
|
|
61
|
+
*/
|
|
62
|
+
abstract run(code: string): Promise<Execution>;
|
|
63
|
+
/**
|
|
64
|
+
* Access the command namespace for executing shell commands in the sandbox.
|
|
65
|
+
*/
|
|
66
|
+
get command(): Command;
|
|
67
|
+
/**
|
|
68
|
+
* Access the metrics namespace for retrieving sandbox metrics.
|
|
69
|
+
*/
|
|
70
|
+
get metrics(): Metrics;
|
|
71
|
+
/**
|
|
72
|
+
* Check if the sandbox is started.
|
|
73
|
+
*/
|
|
74
|
+
get isStarted(): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Get the server URL.
|
|
77
|
+
*/
|
|
78
|
+
get serverUrl(): string;
|
|
79
|
+
/**
|
|
80
|
+
* Get the namespace.
|
|
81
|
+
*/
|
|
82
|
+
get namespace(): string;
|
|
83
|
+
/**
|
|
84
|
+
* Get the sandbox name.
|
|
85
|
+
*/
|
|
86
|
+
get name(): string;
|
|
87
|
+
/**
|
|
88
|
+
* Get the API key.
|
|
89
|
+
*/
|
|
90
|
+
get apiKey(): string | undefined;
|
|
91
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Base sandbox implementation for the Microsandbox TypeScript SDK.
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
17
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
18
|
+
}) : function(o, v) {
|
|
19
|
+
o["default"] = v;
|
|
20
|
+
});
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = [];
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
return ownKeys(o);
|
|
29
|
+
};
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod;
|
|
32
|
+
var result = {};
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
34
|
+
__setModuleDefault(result, mod);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.BaseSandbox = void 0;
|
|
43
|
+
const uuid_1 = require("uuid");
|
|
44
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
45
|
+
const dotenv = __importStar(require("dotenv"));
|
|
46
|
+
const command_1 = require("./command");
|
|
47
|
+
const metrics_1 = require("./metrics");
|
|
48
|
+
class BaseSandbox {
|
|
49
|
+
/**
|
|
50
|
+
* Initialize a base sandbox instance.
|
|
51
|
+
*
|
|
52
|
+
* @param options - Configuration options for the sandbox
|
|
53
|
+
*/
|
|
54
|
+
constructor(options) {
|
|
55
|
+
this._isStarted = false;
|
|
56
|
+
// Try to load from .env if MSB_API_KEY is not already set
|
|
57
|
+
if (!process.env.MSB_API_KEY) {
|
|
58
|
+
try {
|
|
59
|
+
dotenv.config();
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
// Ignore errors if .env file doesn't exist
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
this._serverUrl =
|
|
66
|
+
options?.serverUrl ||
|
|
67
|
+
process.env.MSB_SERVER_URL ||
|
|
68
|
+
"http://127.0.0.1:5555";
|
|
69
|
+
this._namespace = options?.namespace || "default";
|
|
70
|
+
this._name = options?.name || `sandbox-${(0, uuid_1.v4)().substring(0, 8)}`;
|
|
71
|
+
this._apiKey = options?.apiKey || process.env.MSB_API_KEY;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create and initialize a new sandbox instance.
|
|
75
|
+
*
|
|
76
|
+
* This is the base implementation that subclasses should call.
|
|
77
|
+
*
|
|
78
|
+
* @param ctor - The constructor function for the subclass
|
|
79
|
+
* @param options - Configuration options for the sandbox
|
|
80
|
+
* @returns A Promise resolving to a new sandbox instance
|
|
81
|
+
*/
|
|
82
|
+
static async createBase(ctor, options) {
|
|
83
|
+
// Try to load from .env if MSB_API_KEY is not already set
|
|
84
|
+
if (!process.env.MSB_API_KEY) {
|
|
85
|
+
try {
|
|
86
|
+
dotenv.config();
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
// Ignore errors if .env file doesn't exist
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const sandbox = new ctor(options);
|
|
93
|
+
// Start the sandbox
|
|
94
|
+
await sandbox.start(options?.image, options?.memory, options?.cpus, options?.timeout);
|
|
95
|
+
return sandbox;
|
|
96
|
+
}
|
|
97
|
+
// Abstract static method signature that subclasses must implement
|
|
98
|
+
static create(options) {
|
|
99
|
+
throw new Error("Static method 'create' must be implemented by subclass");
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Start the sandbox container.
|
|
103
|
+
*
|
|
104
|
+
* @param image - Docker image to use for the sandbox (defaults to language-specific image)
|
|
105
|
+
* @param memory - Memory limit in MB
|
|
106
|
+
* @param cpus - CPU limit (will be rounded to nearest integer)
|
|
107
|
+
* @param timeout - Maximum time in seconds to wait for the sandbox to start (default: 180 seconds)
|
|
108
|
+
*
|
|
109
|
+
* @throws Error if the sandbox fails to start
|
|
110
|
+
* @throws Error if the sandbox doesn't start within the specified timeout
|
|
111
|
+
*/
|
|
112
|
+
async start(image, memory = 512, cpus = 1.0, timeout = 180.0) {
|
|
113
|
+
if (this._isStarted) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const sandboxImage = image || (await this.getDefaultImage());
|
|
117
|
+
const requestData = {
|
|
118
|
+
jsonrpc: "2.0",
|
|
119
|
+
method: "sandbox.start",
|
|
120
|
+
params: {
|
|
121
|
+
namespace: this._namespace,
|
|
122
|
+
sandbox: this._name,
|
|
123
|
+
config: {
|
|
124
|
+
image: sandboxImage,
|
|
125
|
+
memory,
|
|
126
|
+
cpus: Math.round(cpus),
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
id: (0, uuid_1.v4)(),
|
|
130
|
+
};
|
|
131
|
+
const headers = {
|
|
132
|
+
"Content-Type": "application/json",
|
|
133
|
+
};
|
|
134
|
+
if (this._apiKey) {
|
|
135
|
+
headers["Authorization"] = `Bearer ${this._apiKey}`;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
// We don't have a client-side timeout like the Python SDK, but we can implement one if needed
|
|
139
|
+
const response = await (0, node_fetch_1.default)(`${this._serverUrl}/api/v1/rpc`, {
|
|
140
|
+
method: "POST",
|
|
141
|
+
headers,
|
|
142
|
+
body: JSON.stringify(requestData),
|
|
143
|
+
// We'd need to use a more sophisticated fetch library for proper timeout handling
|
|
144
|
+
// Node-fetch doesn't have a timeout option that works the same way as Python's aiohttp
|
|
145
|
+
});
|
|
146
|
+
if (!response.ok) {
|
|
147
|
+
const errorText = await response.text();
|
|
148
|
+
throw new Error(`Failed to start sandbox: ${errorText}`);
|
|
149
|
+
}
|
|
150
|
+
const responseData = await response.json();
|
|
151
|
+
if ("error" in responseData) {
|
|
152
|
+
throw new Error(`Failed to start sandbox: ${responseData.error.message}`);
|
|
153
|
+
}
|
|
154
|
+
// Check the result message - it might indicate the sandbox is still initializing
|
|
155
|
+
const result = responseData.result;
|
|
156
|
+
if (typeof result === "string" && result.includes("timed out waiting")) {
|
|
157
|
+
// Server timed out but still started the sandbox
|
|
158
|
+
// We'll log a warning but still consider it started
|
|
159
|
+
console.warn(`Sandbox start warning: ${result}`);
|
|
160
|
+
}
|
|
161
|
+
this._isStarted = true;
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
if (e instanceof Error) {
|
|
165
|
+
if (e.message.includes("timeout")) {
|
|
166
|
+
throw new Error(`Timed out waiting for sandbox to start after ${timeout} seconds`);
|
|
167
|
+
}
|
|
168
|
+
throw new Error(`Failed to communicate with Microsandbox server: ${e.message}`);
|
|
169
|
+
}
|
|
170
|
+
throw new Error("Failed to communicate with Microsandbox server: Unknown error");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Stop the sandbox container.
|
|
175
|
+
*
|
|
176
|
+
* @throws Error if the sandbox fails to stop
|
|
177
|
+
*/
|
|
178
|
+
async stop() {
|
|
179
|
+
if (!this._isStarted) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const requestData = {
|
|
183
|
+
jsonrpc: "2.0",
|
|
184
|
+
method: "sandbox.stop",
|
|
185
|
+
params: {
|
|
186
|
+
namespace: this._namespace,
|
|
187
|
+
sandbox: this._name,
|
|
188
|
+
},
|
|
189
|
+
id: (0, uuid_1.v4)(),
|
|
190
|
+
};
|
|
191
|
+
const headers = {
|
|
192
|
+
"Content-Type": "application/json",
|
|
193
|
+
};
|
|
194
|
+
if (this._apiKey) {
|
|
195
|
+
headers["Authorization"] = `Bearer ${this._apiKey}`;
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
const response = await (0, node_fetch_1.default)(`${this._serverUrl}/api/v1/rpc`, {
|
|
199
|
+
method: "POST",
|
|
200
|
+
headers,
|
|
201
|
+
body: JSON.stringify(requestData),
|
|
202
|
+
});
|
|
203
|
+
if (!response.ok) {
|
|
204
|
+
const errorText = await response.text();
|
|
205
|
+
throw new Error(`Failed to stop sandbox: ${errorText}`);
|
|
206
|
+
}
|
|
207
|
+
const responseData = await response.json();
|
|
208
|
+
if ("error" in responseData) {
|
|
209
|
+
throw new Error(`Failed to stop sandbox: ${responseData.error.message}`);
|
|
210
|
+
}
|
|
211
|
+
this._isStarted = false;
|
|
212
|
+
}
|
|
213
|
+
catch (e) {
|
|
214
|
+
if (e instanceof Error) {
|
|
215
|
+
throw new Error(`Failed to communicate with Microsandbox server: ${e.message}`);
|
|
216
|
+
}
|
|
217
|
+
throw new Error("Failed to communicate with Microsandbox server: Unknown error");
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Access the command namespace for executing shell commands in the sandbox.
|
|
222
|
+
*/
|
|
223
|
+
get command() {
|
|
224
|
+
return new command_1.Command(this);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Access the metrics namespace for retrieving sandbox metrics.
|
|
228
|
+
*/
|
|
229
|
+
get metrics() {
|
|
230
|
+
return new metrics_1.Metrics(this);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Check if the sandbox is started.
|
|
234
|
+
*/
|
|
235
|
+
get isStarted() {
|
|
236
|
+
return this._isStarted;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Get the server URL.
|
|
240
|
+
*/
|
|
241
|
+
get serverUrl() {
|
|
242
|
+
return this._serverUrl;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Get the namespace.
|
|
246
|
+
*/
|
|
247
|
+
get namespace() {
|
|
248
|
+
return this._namespace;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Get the sandbox name.
|
|
252
|
+
*/
|
|
253
|
+
get name() {
|
|
254
|
+
return this._name;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get the API key.
|
|
258
|
+
*/
|
|
259
|
+
get apiKey() {
|
|
260
|
+
return this._apiKey;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
exports.BaseSandbox = BaseSandbox;
|
|
264
|
+
//# sourceMappingURL=base-sandbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-sandbox.js","sourceRoot":"","sources":["../src/base-sandbox.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,+BAAoC;AACpC,4DAA+B;AAC/B,+CAAiC;AAEjC,uCAAoC;AACpC,uCAAoC;AAIpC,MAAsB,WAAW;IAO/B;;;;OAI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,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,iBAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED;;OAEG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF;AAlSD,kCAkSC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class representing command execution results in a sandbox environment.
|
|
3
|
+
*/
|
|
4
|
+
import { OutputData } from "./types";
|
|
5
|
+
export declare class CommandExecution {
|
|
6
|
+
private outputLines;
|
|
7
|
+
private _command;
|
|
8
|
+
private _args;
|
|
9
|
+
private _exitCode;
|
|
10
|
+
private _success;
|
|
11
|
+
/**
|
|
12
|
+
* Initialize a command execution instance.
|
|
13
|
+
*
|
|
14
|
+
* @param outputData - Output data from the sandbox.command.run response
|
|
15
|
+
*/
|
|
16
|
+
constructor(outputData?: OutputData);
|
|
17
|
+
/**
|
|
18
|
+
* Process output data from the sandbox.command.run response.
|
|
19
|
+
*
|
|
20
|
+
* @param outputData - Dictionary containing the output data
|
|
21
|
+
*/
|
|
22
|
+
private processOutputData;
|
|
23
|
+
/**
|
|
24
|
+
* Get the standard output from the command execution.
|
|
25
|
+
*
|
|
26
|
+
* @returns String containing the stdout output of the command
|
|
27
|
+
*/
|
|
28
|
+
output(): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Get the error output from the command execution.
|
|
31
|
+
*
|
|
32
|
+
* @returns String containing the stderr output of the command
|
|
33
|
+
*/
|
|
34
|
+
error(): Promise<string>;
|
|
35
|
+
/**
|
|
36
|
+
* Get the exit code of the command execution.
|
|
37
|
+
*
|
|
38
|
+
* @returns Integer containing the exit code
|
|
39
|
+
*/
|
|
40
|
+
get exitCode(): number;
|
|
41
|
+
/**
|
|
42
|
+
* Check if the command executed successfully.
|
|
43
|
+
*
|
|
44
|
+
* @returns Boolean indicating whether the command succeeded (exit code 0)
|
|
45
|
+
*/
|
|
46
|
+
get success(): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Get the command that was executed.
|
|
49
|
+
*
|
|
50
|
+
* @returns String containing the command
|
|
51
|
+
*/
|
|
52
|
+
get command(): string;
|
|
53
|
+
/**
|
|
54
|
+
* Get the arguments used for the command.
|
|
55
|
+
*
|
|
56
|
+
* @returns List of strings containing the command arguments
|
|
57
|
+
*/
|
|
58
|
+
get args(): string[];
|
|
59
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Class representing command execution results in a sandbox environment.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CommandExecution = void 0;
|
|
7
|
+
class CommandExecution {
|
|
8
|
+
/**
|
|
9
|
+
* Initialize a command execution instance.
|
|
10
|
+
*
|
|
11
|
+
* @param outputData - Output data from the sandbox.command.run response
|
|
12
|
+
*/
|
|
13
|
+
constructor(outputData) {
|
|
14
|
+
this.outputLines = [];
|
|
15
|
+
this._command = "";
|
|
16
|
+
this._args = [];
|
|
17
|
+
this._exitCode = -1;
|
|
18
|
+
this._success = false;
|
|
19
|
+
if (outputData) {
|
|
20
|
+
this.processOutputData(outputData);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Process output data from the sandbox.command.run response.
|
|
25
|
+
*
|
|
26
|
+
* @param outputData - Dictionary containing the output data
|
|
27
|
+
*/
|
|
28
|
+
processOutputData(outputData) {
|
|
29
|
+
// Extract output lines from the response
|
|
30
|
+
this.outputLines = outputData.output || [];
|
|
31
|
+
// Store command-specific metadata
|
|
32
|
+
this._command = outputData.command || "";
|
|
33
|
+
this._args = outputData.args || [];
|
|
34
|
+
this._exitCode =
|
|
35
|
+
outputData.exit_code !== undefined ? outputData.exit_code : -1;
|
|
36
|
+
this._success = outputData.success || false;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get the standard output from the command execution.
|
|
40
|
+
*
|
|
41
|
+
* @returns String containing the stdout output of the command
|
|
42
|
+
*/
|
|
43
|
+
async output() {
|
|
44
|
+
// Combine the stdout output lines into a single string
|
|
45
|
+
let outputText = "";
|
|
46
|
+
for (const line of this.outputLines) {
|
|
47
|
+
if (line.stream === "stdout") {
|
|
48
|
+
outputText += line.text + "\n";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return outputText.trim();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get the error output from the command execution.
|
|
55
|
+
*
|
|
56
|
+
* @returns String containing the stderr output of the command
|
|
57
|
+
*/
|
|
58
|
+
async error() {
|
|
59
|
+
// Combine the stderr output lines into a single string
|
|
60
|
+
let errorText = "";
|
|
61
|
+
for (const line of this.outputLines) {
|
|
62
|
+
if (line.stream === "stderr") {
|
|
63
|
+
errorText += line.text + "\n";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return errorText.trim();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get the exit code of the command execution.
|
|
70
|
+
*
|
|
71
|
+
* @returns Integer containing the exit code
|
|
72
|
+
*/
|
|
73
|
+
get exitCode() {
|
|
74
|
+
return this._exitCode;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Check if the command executed successfully.
|
|
78
|
+
*
|
|
79
|
+
* @returns Boolean indicating whether the command succeeded (exit code 0)
|
|
80
|
+
*/
|
|
81
|
+
get success() {
|
|
82
|
+
return this._success;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get the command that was executed.
|
|
86
|
+
*
|
|
87
|
+
* @returns String containing the command
|
|
88
|
+
*/
|
|
89
|
+
get command() {
|
|
90
|
+
return this._command;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Get the arguments used for the command.
|
|
94
|
+
*
|
|
95
|
+
* @returns List of strings containing the command arguments
|
|
96
|
+
*/
|
|
97
|
+
get args() {
|
|
98
|
+
return this._args;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.CommandExecution = CommandExecution;
|
|
102
|
+
//# sourceMappingURL=command-execution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-execution.js","sourceRoot":"","sources":["../src/command-execution.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAIH,MAAa,gBAAgB;IAO3B;;;;OAIG;IACH,YAAY,UAAuB;QAX3B,gBAAW,GAAiB,EAAE,CAAC;QAC/B,aAAQ,GAAW,EAAE,CAAC;QACtB,UAAK,GAAa,EAAE,CAAC;QACrB,cAAS,GAAW,CAAC,CAAC,CAAC;QACvB,aAAQ,GAAY,KAAK,CAAC;QAQhC,IAAI,UAAU,EAAE,CAAC;YACf,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IAED;;;;OAIG;IACK,iBAAiB,CAAC,UAAsB;QAC9C,yCAAyC;QACzC,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC,MAAM,IAAI,EAAE,CAAC;QAE3C,kCAAkC;QAClC,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,OAAO,IAAI,EAAE,CAAC;QACzC,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC,IAAI,IAAI,EAAE,CAAC;QACnC,IAAI,CAAC,SAAS;YACZ,UAAU,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,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