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
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command execution interface for the Microsandbox TypeScript SDK.
|
|
3
|
+
*/
|
|
4
|
+
import { CommandExecution } from "./command-execution";
|
|
5
|
+
import { BaseSandbox } from "./base-sandbox";
|
|
6
|
+
export declare class Command {
|
|
7
|
+
private sandbox;
|
|
8
|
+
/**
|
|
9
|
+
* Initialize the command instance.
|
|
10
|
+
*
|
|
11
|
+
* @param sandboxInstance - The sandbox instance this command belongs to
|
|
12
|
+
*/
|
|
13
|
+
constructor(sandboxInstance: BaseSandbox);
|
|
14
|
+
/**
|
|
15
|
+
* Execute a shell command in the sandbox.
|
|
16
|
+
*
|
|
17
|
+
* @param command - The command to execute
|
|
18
|
+
* @param args - Optional list of command arguments
|
|
19
|
+
* @param timeout - Optional timeout in seconds
|
|
20
|
+
* @returns A CommandExecution object containing the results
|
|
21
|
+
* @throws Error if the sandbox is not started or execution fails
|
|
22
|
+
*/
|
|
23
|
+
run(command: string, args?: string[], timeout?: number): Promise<CommandExecution>;
|
|
24
|
+
}
|
package/dist/command.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Command execution interface for the Microsandbox TypeScript SDK.
|
|
4
|
+
*/
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.Command = void 0;
|
|
10
|
+
const uuid_1 = require("uuid");
|
|
11
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
12
|
+
const command_execution_1 = require("./command-execution");
|
|
13
|
+
class Command {
|
|
14
|
+
/**
|
|
15
|
+
* Initialize the command instance.
|
|
16
|
+
*
|
|
17
|
+
* @param sandboxInstance - The sandbox instance this command belongs to
|
|
18
|
+
*/
|
|
19
|
+
constructor(sandboxInstance) {
|
|
20
|
+
this.sandbox = sandboxInstance;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Execute a shell command in the sandbox.
|
|
24
|
+
*
|
|
25
|
+
* @param command - The command to execute
|
|
26
|
+
* @param args - Optional list of command arguments
|
|
27
|
+
* @param timeout - Optional timeout in seconds
|
|
28
|
+
* @returns A CommandExecution object containing the results
|
|
29
|
+
* @throws Error if the sandbox is not started or execution fails
|
|
30
|
+
*/
|
|
31
|
+
async run(command, args, timeout) {
|
|
32
|
+
if (!this.sandbox.isStarted) {
|
|
33
|
+
throw new Error("Sandbox is not started. Call start() first.");
|
|
34
|
+
}
|
|
35
|
+
const headers = {
|
|
36
|
+
"Content-Type": "application/json",
|
|
37
|
+
};
|
|
38
|
+
if (this.sandbox.apiKey) {
|
|
39
|
+
headers["Authorization"] = `Bearer ${this.sandbox.apiKey}`;
|
|
40
|
+
}
|
|
41
|
+
// Prepare the request data
|
|
42
|
+
const requestData = {
|
|
43
|
+
jsonrpc: "2.0",
|
|
44
|
+
method: "sandbox.command.run",
|
|
45
|
+
params: {
|
|
46
|
+
sandbox: this.sandbox.name,
|
|
47
|
+
namespace: this.sandbox.namespace,
|
|
48
|
+
command,
|
|
49
|
+
args: args || [],
|
|
50
|
+
},
|
|
51
|
+
id: (0, uuid_1.v4)(),
|
|
52
|
+
};
|
|
53
|
+
// Add timeout if specified
|
|
54
|
+
if (timeout !== undefined) {
|
|
55
|
+
requestData.params.timeout = timeout;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const response = await (0, node_fetch_1.default)(`${this.sandbox.serverUrl}/api/v1/rpc`, {
|
|
59
|
+
method: "POST",
|
|
60
|
+
headers,
|
|
61
|
+
body: JSON.stringify(requestData),
|
|
62
|
+
});
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
const errorText = await response.text();
|
|
65
|
+
throw new Error(`Failed to execute command: ${errorText}`);
|
|
66
|
+
}
|
|
67
|
+
const responseData = await response.json();
|
|
68
|
+
if ("error" in responseData) {
|
|
69
|
+
throw new Error(`Failed to execute command: ${responseData.error.message}`);
|
|
70
|
+
}
|
|
71
|
+
const result = responseData.result || {};
|
|
72
|
+
// Create and return a CommandExecution object with the output data
|
|
73
|
+
return new command_execution_1.CommandExecution(result);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
if (e instanceof Error) {
|
|
77
|
+
throw new Error(`Failed to execute command: ${e.message}`);
|
|
78
|
+
}
|
|
79
|
+
throw new Error("Failed to execute command: Unknown error");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.Command = Command;
|
|
84
|
+
//# sourceMappingURL=command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command.js","source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AC,SAAS,CAAC,WAAW,CAAC;aAClC,CAAC,CAAC;YAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;gBACxC,MAAM,IAAI,KAAK,CAAC,8BAA8B,SAAS,EAAE,CAAC,CAAC;YAC7D,CAAC;YAED,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YAE3C,IAAI,OAAO,IAAI,YAAY,EAAE,CAAC;gBAC5B,MAAM,IAAI,KAAK,CACb,8BAA8B,YAAY,CAAC,KAAK,CAAC,OAAO,EAAE,CAC3D,CAAC;YACJ,CAAC;YAED,MAAM,MAAM,GAAG,YAAY,CAAC,MAAM,IAAI,EAAE,CAAC;YAEzC,mEAAmE;YACnE,OAAO,IAAI,oCAAgB,CAAC,MAAM,CAAC,CAAC;QACtC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,YAAY,KAAK,EAAE,CAAC;gBACvB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;YAC7D,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC;CACF;AAlGD,0BAkGC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class representing code execution results in a sandbox environment.
|
|
3
|
+
*/
|
|
4
|
+
import { OutputData } from "./types";
|
|
5
|
+
export declare class Execution {
|
|
6
|
+
private outputLines;
|
|
7
|
+
private _status;
|
|
8
|
+
private _language;
|
|
9
|
+
private _hasError;
|
|
10
|
+
/**
|
|
11
|
+
* Initialize an execution instance.
|
|
12
|
+
*
|
|
13
|
+
* @param outputData - Output data from the sandbox.repl.run response
|
|
14
|
+
*/
|
|
15
|
+
constructor(outputData?: OutputData);
|
|
16
|
+
/**
|
|
17
|
+
* Process output data from the sandbox.repl.run response.
|
|
18
|
+
*
|
|
19
|
+
* @param outputData - Dictionary containing the output data
|
|
20
|
+
*/
|
|
21
|
+
private processOutputData;
|
|
22
|
+
/**
|
|
23
|
+
* Get the standard output from the execution.
|
|
24
|
+
*
|
|
25
|
+
* @returns String containing the stdout output of the execution
|
|
26
|
+
*/
|
|
27
|
+
output(): Promise<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Get the error output from the execution.
|
|
30
|
+
*
|
|
31
|
+
* @returns String containing the stderr output of the execution
|
|
32
|
+
*/
|
|
33
|
+
error(): Promise<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Check if the execution contains an error.
|
|
36
|
+
*
|
|
37
|
+
* @returns Boolean indicating whether the execution encountered an error
|
|
38
|
+
*/
|
|
39
|
+
hasError(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Get the status of the execution.
|
|
42
|
+
*
|
|
43
|
+
* @returns String containing the execution status (e.g., "success")
|
|
44
|
+
*/
|
|
45
|
+
get status(): string;
|
|
46
|
+
/**
|
|
47
|
+
* Get the language used for the execution.
|
|
48
|
+
*
|
|
49
|
+
* @returns String containing the execution language (e.g., "python")
|
|
50
|
+
*/
|
|
51
|
+
get language(): string;
|
|
52
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Class representing code execution results in a sandbox environment.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Execution = void 0;
|
|
7
|
+
class Execution {
|
|
8
|
+
/**
|
|
9
|
+
* Initialize an execution instance.
|
|
10
|
+
*
|
|
11
|
+
* @param outputData - Output data from the sandbox.repl.run response
|
|
12
|
+
*/
|
|
13
|
+
constructor(outputData) {
|
|
14
|
+
this.outputLines = [];
|
|
15
|
+
this._status = "unknown";
|
|
16
|
+
this._language = "unknown";
|
|
17
|
+
this._hasError = false;
|
|
18
|
+
if (outputData) {
|
|
19
|
+
this.processOutputData(outputData);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Process output data from the sandbox.repl.run response.
|
|
24
|
+
*
|
|
25
|
+
* @param outputData - Dictionary containing the output data
|
|
26
|
+
*/
|
|
27
|
+
processOutputData(outputData) {
|
|
28
|
+
// Extract output lines from the response
|
|
29
|
+
this.outputLines = outputData.output || [];
|
|
30
|
+
// Store additional metadata that might be useful
|
|
31
|
+
this._status = outputData.status || "unknown";
|
|
32
|
+
this._language = outputData.language || "unknown";
|
|
33
|
+
// Check for errors in the output or status
|
|
34
|
+
if (this._status === "error" || this._status === "exception") {
|
|
35
|
+
this._hasError = true;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
// Check if there's any stderr output
|
|
39
|
+
for (const line of this.outputLines) {
|
|
40
|
+
if (line.stream === "stderr" && line.text) {
|
|
41
|
+
this._hasError = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get the standard output from the execution.
|
|
49
|
+
*
|
|
50
|
+
* @returns String containing the stdout output of the execution
|
|
51
|
+
*/
|
|
52
|
+
async output() {
|
|
53
|
+
// Combine the stdout output lines into a single string
|
|
54
|
+
let outputText = "";
|
|
55
|
+
for (const line of this.outputLines) {
|
|
56
|
+
if (line.stream === "stdout") {
|
|
57
|
+
outputText += line.text + "\n";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return outputText.trim();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get the error output from the execution.
|
|
64
|
+
*
|
|
65
|
+
* @returns String containing the stderr output of the execution
|
|
66
|
+
*/
|
|
67
|
+
async error() {
|
|
68
|
+
// Combine the stderr output lines into a single string
|
|
69
|
+
let errorText = "";
|
|
70
|
+
for (const line of this.outputLines) {
|
|
71
|
+
if (line.stream === "stderr") {
|
|
72
|
+
errorText += line.text + "\n";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return errorText.trim();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Check if the execution contains an error.
|
|
79
|
+
*
|
|
80
|
+
* @returns Boolean indicating whether the execution encountered an error
|
|
81
|
+
*/
|
|
82
|
+
hasError() {
|
|
83
|
+
return this._hasError;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get the status of the execution.
|
|
87
|
+
*
|
|
88
|
+
* @returns String containing the execution status (e.g., "success")
|
|
89
|
+
*/
|
|
90
|
+
get status() {
|
|
91
|
+
return this._status;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Get the language used for the execution.
|
|
95
|
+
*
|
|
96
|
+
* @returns String containing the execution language (e.g., "python")
|
|
97
|
+
*/
|
|
98
|
+
get language() {
|
|
99
|
+
return this._language;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.Execution = Execution;
|
|
103
|
+
//# sourceMappingURL=execution.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execution.js","sourceRoot":"","sources":["../src/execution.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAIH,MAAa,SAAS;IAMpB;;;;OAIG;IACH,YAAY,UAAuB;QAV3B,gBAAW,GAAiB,EAAE,CAAC;QAC/B,YAAO,GAAW,SAAS,CAAC;QAC5B,cAAS,GAAW,SAAS,CAAC;QAC9B,cAAS,GAAY,KAAK,CAAC;QAQjC,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,iDAAiD;QACjD,IAAI,CAAC,OAAO,GAAG,UAAU,CAAC,MAAM,IAAI,SAAS,CAAC;QAC9C,IAAI,CA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;;;OAIG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;CACF;AAxGD,8BAwGC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Microsandbox
|
|
2
|
+
* Microsandbox TypeScript SDK
|
|
3
3
|
*
|
|
4
|
-
* A
|
|
4
|
+
* A TypeScript SDK for creating and managing secure sandboxes for code execution.
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export
|
|
6
|
+
export { BaseSandbox } from "./base-sandbox";
|
|
7
|
+
export { Command } from "./command";
|
|
8
|
+
export { CommandExecution } from "./command-execution";
|
|
9
|
+
export { Execution } from "./execution";
|
|
10
|
+
export { Metrics } from "./metrics";
|
|
11
|
+
export { NodeSandbox } from "./node-sandbox";
|
|
12
|
+
export { PythonSandbox } from "./python-sandbox";
|
|
13
|
+
export { SandboxOptions } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Microsandbox
|
|
3
|
+
* Microsandbox TypeScript SDK
|
|
4
4
|
*
|
|
5
|
-
* A
|
|
5
|
+
* A TypeScript SDK for creating and managing secure sandboxes for code execution.
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
8
|
+
exports.SandboxOptions = exports.PythonSandbox = exports.NodeSandbox = exports.Metrics = exports.Execution = exports.CommandExecution = exports.Command = exports.BaseSandbox = void 0;
|
|
9
|
+
var base_sandbox_1 = require("./base-sandbox");
|
|
10
|
+
Object.defineProperty(exports, "BaseSandbox", { enumerable: true, get: function () { return base_sandbox_1.BaseSandbox; } });
|
|
11
|
+
var command_1 = require("./command");
|
|
12
|
+
Object.defineProperty(exports, "Command", { enumerable: true, get: function () { return command_1.Command; } });
|
|
13
|
+
var command_execution_1 = require("./command-execution");
|
|
14
|
+
Object.defineProperty(exports, "CommandExecution", { enumerable: true, get: function () { return command_execution_1.CommandExecution; } });
|
|
15
|
+
var execution_1 = require("./execution");
|
|
16
|
+
Object.defineProperty(exports, "Execution", { enumerable: true, get: function () { return execution_1.Execution; } });
|
|
17
|
+
var metrics_1 = require("./metrics");
|
|
18
|
+
Object.defineProperty(exports, "Metrics", { enumerable: true, get: function () { return metrics_1.Metrics; } });
|
|
19
|
+
var node_sandbox_1 = require("./node-sandbox");
|
|
20
|
+
Object.defineProperty(exports, "NodeSandbox", { enumerable: true, get: function () { return node_sandbox_1.NodeSandbox; } });
|
|
21
|
+
var python_sandbox_1 = require("./python-sandbox");
|
|
22
|
+
Object.defineProperty(exports, "PythonSandbox", { enumerable: true, get: function () { return python_sandbox_1.PythonSandbox; } });
|
|
23
|
+
var types_1 = require("./types");
|
|
24
|
+
Object.defineProperty(exports, "SandboxOptions", { enumerable: true, get: function () { return types_1.SandboxOptions; } });
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,+CAA6C;AAApC,2GAAA,WAAW,OAAA;AACpB,qCAAoC;AAA3B,kGAAA,OAAO,OAAA;AAChB,yDAAuD;AAA9C,qHAAA,gBAAgB,OAAA;AACzB,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAClB,qCAAoC;AAA3B,kGAAA,OAAO,OAAA;AAChB,+CAA6C;AAApC,2GAAA,WAAW,OAAA;AACpB,mDAAiD;AAAxC,+GAAA,aAAa,OAAA;AACtB,iCAAyC;AAAhC,uGAAA,cAAc,OAAA"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metrics interface for the Microsandbox TypeScript SDK.
|
|
3
|
+
*/
|
|
4
|
+
import { BaseSandbox } from "./base-sandbox";
|
|
5
|
+
export declare class Metrics {
|
|
6
|
+
private sandbox;
|
|
7
|
+
/**
|
|
8
|
+
* Initialize the metrics instance.
|
|
9
|
+
*
|
|
10
|
+
* @param sandboxInstance - The sandbox instance this metrics object belongs to
|
|
11
|
+
*/
|
|
12
|
+
constructor(sandboxInstance: BaseSandbox);
|
|
13
|
+
/**
|
|
14
|
+
* Internal method to fetch current metrics from the server.
|
|
15
|
+
*
|
|
16
|
+
* @returns A dictionary containing the metrics data for the sandbox
|
|
17
|
+
* @throws Error if the request to the server fails
|
|
18
|
+
*/
|
|
19
|
+
private getMetrics;
|
|
20
|
+
/**
|
|
21
|
+
* Get all metrics for the current sandbox.
|
|
22
|
+
*
|
|
23
|
+
* @returns A dictionary containing all metrics for the sandbox:
|
|
24
|
+
* {
|
|
25
|
+
* "name": string,
|
|
26
|
+
* "namespace": string,
|
|
27
|
+
* "running": boolean,
|
|
28
|
+
* "cpu_usage": number | null,
|
|
29
|
+
* "memory_usage": number | null,
|
|
30
|
+
* "disk_usage": number | null
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
34
|
+
*/
|
|
35
|
+
all(): Promise<any>;
|
|
36
|
+
/**
|
|
37
|
+
* Get CPU usage percentage for the current sandbox.
|
|
38
|
+
*
|
|
39
|
+
* @returns CPU usage as a percentage (0-100) or undefined if not available.
|
|
40
|
+
* May return 0.0 for idle sandboxes or when metrics are not precise.
|
|
41
|
+
*
|
|
42
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
43
|
+
*/
|
|
44
|
+
cpu(): Promise<number | undefined>;
|
|
45
|
+
/**
|
|
46
|
+
* Get memory usage for the current sandbox.
|
|
47
|
+
*
|
|
48
|
+
* @returns Memory usage in MiB or undefined if not available
|
|
49
|
+
*
|
|
50
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
51
|
+
*/
|
|
52
|
+
memory(): Promise<number | undefined>;
|
|
53
|
+
/**
|
|
54
|
+
* Get disk usage for the current sandbox.
|
|
55
|
+
*
|
|
56
|
+
* @returns Disk usage in bytes or undefined if not available
|
|
57
|
+
*
|
|
58
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
59
|
+
*/
|
|
60
|
+
disk(): Promise<number | undefined>;
|
|
61
|
+
/**
|
|
62
|
+
* Check if the sandbox is currently running.
|
|
63
|
+
*
|
|
64
|
+
* @returns True if the sandbox is running, False otherwise
|
|
65
|
+
*
|
|
66
|
+
* @throws Error if the request to the server fails
|
|
67
|
+
*/
|
|
68
|
+
isRunning(): Promise<boolean>;
|
|
69
|
+
}
|
package/dist/metrics.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Metrics interface for the Microsandbox TypeScript SDK.
|
|
4
|
+
*/
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.Metrics = void 0;
|
|
10
|
+
const uuid_1 = require("uuid");
|
|
11
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
12
|
+
class Metrics {
|
|
13
|
+
/**
|
|
14
|
+
* Initialize the metrics instance.
|
|
15
|
+
*
|
|
16
|
+
* @param sandboxInstance - The sandbox instance this metrics object belongs to
|
|
17
|
+
*/
|
|
18
|
+
constructor(sandboxInstance) {
|
|
19
|
+
this.sandbox = sandboxInstance;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Internal method to fetch current metrics from the server.
|
|
23
|
+
*
|
|
24
|
+
* @returns A dictionary containing the metrics data for the sandbox
|
|
25
|
+
* @throws Error if the request to the server fails
|
|
26
|
+
*/
|
|
27
|
+
async getMetrics() {
|
|
28
|
+
if (!this.sandbox.isStarted) {
|
|
29
|
+
throw new Error("Sandbox is not started. Call start() first.");
|
|
30
|
+
}
|
|
31
|
+
const headers = {
|
|
32
|
+
"Content-Type": "application/json",
|
|
33
|
+
};
|
|
34
|
+
if (this.sandbox.apiKey) {
|
|
35
|
+
headers["Authorization"] = `Bearer ${this.sandbox.apiKey}`;
|
|
36
|
+
}
|
|
37
|
+
// Prepare the request data
|
|
38
|
+
const requestData = {
|
|
39
|
+
jsonrpc: "2.0",
|
|
40
|
+
method: "sandbox.metrics.get",
|
|
41
|
+
params: {
|
|
42
|
+
namespace: this.sandbox.namespace,
|
|
43
|
+
sandbox: this.sandbox.name,
|
|
44
|
+
},
|
|
45
|
+
id: (0, uuid_1.v4)(),
|
|
46
|
+
};
|
|
47
|
+
try {
|
|
48
|
+
const response = await (0, node_fetch_1.default)(`${this.sandbox.serverUrl}/api/v1/rpc`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers,
|
|
51
|
+
body: JSON.stringify(requestData),
|
|
52
|
+
});
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
const errorText = await response.text();
|
|
55
|
+
throw new Error(`Failed to get sandbox metrics: ${errorText}`);
|
|
56
|
+
}
|
|
57
|
+
const responseData = await response.json();
|
|
58
|
+
if ("error" in responseData) {
|
|
59
|
+
throw new Error(`Failed to get sandbox metrics: ${responseData.error.message}`);
|
|
60
|
+
}
|
|
61
|
+
const result = responseData.result || {};
|
|
62
|
+
const sandboxes = result.sandboxes || [];
|
|
63
|
+
// We expect exactly one sandbox in the response (our own)
|
|
64
|
+
if (!sandboxes.length) {
|
|
65
|
+
return {};
|
|
66
|
+
}
|
|
67
|
+
// Return the first (and should be only) sandbox data
|
|
68
|
+
return sandboxes[0];
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
if (e instanceof Error) {
|
|
72
|
+
throw new Error(`Failed to get sandbox metrics: ${e.message}`);
|
|
73
|
+
}
|
|
74
|
+
throw new Error("Failed to get sandbox metrics: Unknown error");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get all metrics for the current sandbox.
|
|
79
|
+
*
|
|
80
|
+
* @returns A dictionary containing all metrics for the sandbox:
|
|
81
|
+
* {
|
|
82
|
+
* "name": string,
|
|
83
|
+
* "namespace": string,
|
|
84
|
+
* "running": boolean,
|
|
85
|
+
* "cpu_usage": number | null,
|
|
86
|
+
* "memory_usage": number | null,
|
|
87
|
+
* "disk_usage": number | null
|
|
88
|
+
* }
|
|
89
|
+
*
|
|
90
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
91
|
+
*/
|
|
92
|
+
async all() {
|
|
93
|
+
return await this.getMetrics();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get CPU usage percentage for the current sandbox.
|
|
97
|
+
*
|
|
98
|
+
* @returns CPU usage as a percentage (0-100) or undefined if not available.
|
|
99
|
+
* May return 0.0 for idle sandboxes or when metrics are not precise.
|
|
100
|
+
*
|
|
101
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
102
|
+
*/
|
|
103
|
+
async cpu() {
|
|
104
|
+
const metrics = await this.getMetrics();
|
|
105
|
+
return metrics.cpu_usage;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get memory usage for the current sandbox.
|
|
109
|
+
*
|
|
110
|
+
* @returns Memory usage in MiB or undefined if not available
|
|
111
|
+
*
|
|
112
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
113
|
+
*/
|
|
114
|
+
async memory() {
|
|
115
|
+
const metrics = await this.getMetrics();
|
|
116
|
+
return metrics.memory_usage;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get disk usage for the current sandbox.
|
|
120
|
+
*
|
|
121
|
+
* @returns Disk usage in bytes or undefined if not available
|
|
122
|
+
*
|
|
123
|
+
* @throws Error if the sandbox is not started or if the request fails
|
|
124
|
+
*/
|
|
125
|
+
async disk() {
|
|
126
|
+
const metrics = await this.getMetrics();
|
|
127
|
+
return metrics.disk_usage;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Check if the sandbox is currently running.
|
|
131
|
+
*
|
|
132
|
+
* @returns True if the sandbox is running, False otherwise
|
|
133
|
+
*
|
|
134
|
+
* @throws Error if the request to the server fails
|
|
135
|
+
*/
|
|
136
|
+
async isRunning() {
|
|
137
|
+
const metrics = await this.getMetrics();
|
|
138
|
+
return metrics.running || false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
exports.Metrics = Metrics;
|
|
142
|
+
//# sourceMappingURL=metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../src/metrics.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;AAEH,+BAAoC;AACpC,4DAA+B;AAI/B,MAAa,OAAO;IAGlB;;;;OAIG;IACH,YAAY,eAA4B;QACtC,IAAI,CAAC,OAAO,GAAG,eAAe,CAAC;IACjC,CAAC;IAED;;;;;OAKG;IACK,KAAK,CAAC,UAAU;QACtB,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;QACjE,CAAC;QAED,MAAM,OAAO,GAA2B;YACtC,cAAc,EAAE,kBAAkB;SACnC,CAAC;QAEF,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YACxB,OAAO,CAAC,eAAe,CAAC,GAAG,UAAU,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;QAC7D,CAAC;QAED,2BAA2B;QAC3B,MAAM,WAAW,GAAG;YAClB,OAAO,EAAE,KAAK;YACd,MAAM,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AAC;YACjE,CAAC;YACD,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAClE,CAAC;IACH,CAAC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,GAAG;QACP,OAAO,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;IACjC,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,GAAG;QACP,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;QACxC,OAAO,OAAO,CAAC,SAAS,CAAC;IAC3B,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,MAAM;QACV,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;QACxC,OAAO,OAAO,CAAC,YAAY,CAAC;IAC9B,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,IAAI;QACR,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;QACxC,OAAO,OAAO,CAAC,UAAU,CAAC;IAC5B,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;QACxC,OAAO,OAAO,CAAC,OAAO,IAAI,KAAK,CAAC;IAClC,CAAC;CACF;AAnJD,0BAmJC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js-specific sandbox implementation for the Microsandbox TypeScript SDK.
|
|
3
|
+
*/
|
|
4
|
+
import { BaseSandbox } from "./base-sandbox";
|
|
5
|
+
import { Execution } from "./execution";
|
|
6
|
+
import { SandboxOptions } from "./types";
|
|
7
|
+
export declare class NodeSandbox extends BaseSandbox {
|
|
8
|
+
/**
|
|
9
|
+
* Get the default Docker image for Node.js sandbox.
|
|
10
|
+
*
|
|
11
|
+
* @returns A string containing the Docker image name and tag
|
|
12
|
+
*/
|
|
13
|
+
getDefaultImage(): Promise<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Execute JavaScript code in the sandbox.
|
|
16
|
+
*
|
|
17
|
+
* @param code - JavaScript code to execute
|
|
18
|
+
* @param options - Optional execution options like timeout
|
|
19
|
+
* @returns An Execution object that represents the executed code
|
|
20
|
+
* @throws Error if the sandbox is not started or execution fails
|
|
21
|
+
*/
|
|
22
|
+
run(code: string, options?: {
|
|
23
|
+
timeout?: number;
|
|
24
|
+
}): Promise<Execution>;
|
|
25
|
+
/**
|
|
26
|
+
* Create and initialize a new NodeSandbox instance.
|
|
27
|
+
*
|
|
28
|
+
* @param options - Configuration options for the sandbox
|
|
29
|
+
* @returns A Promise resolving to a new NodeSandbox instance
|
|
30
|
+
*/
|
|
31
|
+
static create(options?: SandboxOptions): Promise<NodeSandbox>;
|
|
32
|
+
}
|