@saikrishnaambeti/docker-sandbox 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +178 -0
- package/dist/cli/cpufeatures-h04yv4n7.node +0 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +52803 -0
- package/dist/cli/sshcrypto-npryzwb9.node +0 -0
- package/dist/cpufeatures-h04yv4n7.node +0 -0
- package/dist/index-rff3tvyv.js +21 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/sdk/index.d.ts +67 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/index.js +128 -0
- package/dist/server/index.d.ts +18 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +52727 -0
- package/dist/sshcrypto-npryzwb9.node +0 -0
- package/package.json +79 -0
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
+
for (let key of __getOwnPropNames(mod))
|
|
11
|
+
if (!__hasOwnProp.call(to, key))
|
|
12
|
+
__defProp(to, key, {
|
|
13
|
+
get: () => mod[key],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
19
|
+
var __require = import.meta.require;
|
|
20
|
+
|
|
21
|
+
export { __toESM, __commonJS, __require };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,OAAO,EACP,eAAe,EACf,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,eAAe,GACrB,MAAM,OAAO,CAAC;AAGf,OAAO,EAAE,WAAW,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export interface SandboxOptions {
|
|
2
|
+
/** Server URL (defaults to http://localhost:4000) */
|
|
3
|
+
serverUrl?: string;
|
|
4
|
+
/** Git source to clone into the sandbox */
|
|
5
|
+
source?: {
|
|
6
|
+
url: string;
|
|
7
|
+
type: "git";
|
|
8
|
+
};
|
|
9
|
+
/** Timeout in milliseconds */
|
|
10
|
+
timeout?: number;
|
|
11
|
+
/** Ports to expose from the container */
|
|
12
|
+
ports?: number[];
|
|
13
|
+
/** Runtime environment (e.g., "node22") */
|
|
14
|
+
runtime?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface CommandOptions {
|
|
17
|
+
/** Command to run */
|
|
18
|
+
cmd: string;
|
|
19
|
+
/** Arguments for the command */
|
|
20
|
+
args: string[];
|
|
21
|
+
/** Environment variables */
|
|
22
|
+
env?: Record<string, string>;
|
|
23
|
+
/** Stream to write stderr output */
|
|
24
|
+
stderr?: WritableStream<Uint8Array>;
|
|
25
|
+
/** Stream to write stdout output */
|
|
26
|
+
stdout?: WritableStream<Uint8Array>;
|
|
27
|
+
/** Run in background without waiting */
|
|
28
|
+
detached?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface CommandFinished {
|
|
31
|
+
/** Exit code of the command */
|
|
32
|
+
exitCode: number;
|
|
33
|
+
}
|
|
34
|
+
export declare class Sandbox {
|
|
35
|
+
/** Container ID */
|
|
36
|
+
id: string;
|
|
37
|
+
/** Mapped ports (container port -> host port) */
|
|
38
|
+
ports: Record<number, number>;
|
|
39
|
+
/** Server URL */
|
|
40
|
+
private serverUrl;
|
|
41
|
+
constructor(id: string, ports: Record<number, number>, serverUrl?: string);
|
|
42
|
+
/**
|
|
43
|
+
* Create a new sandbox container
|
|
44
|
+
*/
|
|
45
|
+
static create(opts?: SandboxOptions): Promise<Sandbox>;
|
|
46
|
+
/**
|
|
47
|
+
* Run a command in the sandbox
|
|
48
|
+
*/
|
|
49
|
+
runCommand(opts: CommandOptions): Promise<CommandFinished>;
|
|
50
|
+
/**
|
|
51
|
+
* Get the info about the sandbox container
|
|
52
|
+
*/
|
|
53
|
+
getInfo(): Promise<unknown>;
|
|
54
|
+
/**
|
|
55
|
+
* Stop and remove the sandbox container
|
|
56
|
+
*/
|
|
57
|
+
destroy(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Get the URL for an exposed port
|
|
60
|
+
*/
|
|
61
|
+
domain(port: number): string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Helper to create a WritableStream from Node.js stdout/stderr
|
|
65
|
+
*/
|
|
66
|
+
export declare function createLogStream(stream: NodeJS.WriteStream): WritableStream<Uint8Array>;
|
|
67
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sdk/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,qDAAqD;IACrD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2CAA2C;IAC3C,MAAM,CAAC,EAAE;QACP,GAAG,EAAE,MAAM,CAAC;QACZ,IAAI,EAAE,KAAK,CAAC;KACb,CAAC;IACF,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,yCA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eAAe,CAC7B,MAAM,EAAE,MAAM,CAAC,WAAW,GACzB,cAAc,CAAC,UAAU,CAAC,CAM5B"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import"../index-rff3tvyv.js";
|
|
3
|
+
|
|
4
|
+
// src/sdk/index.ts
|
|
5
|
+
var DEFAULT_SERVER_URL = "http://localhost:4000";
|
|
6
|
+
|
|
7
|
+
class Sandbox {
|
|
8
|
+
id;
|
|
9
|
+
ports;
|
|
10
|
+
serverUrl;
|
|
11
|
+
constructor(id, ports, serverUrl = DEFAULT_SERVER_URL) {
|
|
12
|
+
this.id = id;
|
|
13
|
+
this.ports = ports;
|
|
14
|
+
this.serverUrl = serverUrl;
|
|
15
|
+
}
|
|
16
|
+
static async create(opts = {}) {
|
|
17
|
+
const serverUrl = opts.serverUrl || DEFAULT_SERVER_URL;
|
|
18
|
+
const res = await fetch(`${serverUrl}/sandbox`, {
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: { "Content-Type": "application/json" },
|
|
21
|
+
body: JSON.stringify({
|
|
22
|
+
source: opts.source,
|
|
23
|
+
ports: opts.ports,
|
|
24
|
+
runtime: opts.runtime
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
if (!res.ok) {
|
|
28
|
+
throw new Error(`Failed to create sandbox: ${await res.text()}`);
|
|
29
|
+
}
|
|
30
|
+
const data = await res.json();
|
|
31
|
+
return new Sandbox(data.id, data.ports, serverUrl);
|
|
32
|
+
}
|
|
33
|
+
async runCommand(opts) {
|
|
34
|
+
const res = await fetch(`${this.serverUrl}/sandbox/${this.id}/exec`, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: { "Content-Type": "application/json" },
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
cmd: opts.cmd,
|
|
39
|
+
args: opts.args,
|
|
40
|
+
detached: opts.detached
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
if (!res.ok) {
|
|
44
|
+
throw new Error(`Failed to run command: ${await res.text()}`);
|
|
45
|
+
}
|
|
46
|
+
if (opts.detached) {
|
|
47
|
+
return { exitCode: 0 };
|
|
48
|
+
}
|
|
49
|
+
if (!res.body)
|
|
50
|
+
return { exitCode: 0 };
|
|
51
|
+
const reader = res.body.getReader();
|
|
52
|
+
const stdoutWriter = opts.stdout ? opts.stdout.getWriter() : null;
|
|
53
|
+
const stderrWriter = opts.stderr ? opts.stderr.getWriter() : null;
|
|
54
|
+
let buffer = new Uint8Array(0);
|
|
55
|
+
try {
|
|
56
|
+
while (true) {
|
|
57
|
+
const { done, value } = await reader.read();
|
|
58
|
+
if (done)
|
|
59
|
+
break;
|
|
60
|
+
const newBuffer = new Uint8Array(buffer.length + value.length);
|
|
61
|
+
newBuffer.set(buffer);
|
|
62
|
+
newBuffer.set(value, buffer.length);
|
|
63
|
+
buffer = newBuffer;
|
|
64
|
+
while (buffer.length >= 8) {
|
|
65
|
+
const type = buffer[0];
|
|
66
|
+
const b4 = buffer[4];
|
|
67
|
+
const b5 = buffer[5];
|
|
68
|
+
const b6 = buffer[6];
|
|
69
|
+
const b7 = buffer[7];
|
|
70
|
+
const size = b4 << 24 | b5 << 16 | b6 << 8 | b7;
|
|
71
|
+
if (buffer.length < 8 + size) {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
const payload = buffer.slice(8, 8 + size);
|
|
75
|
+
buffer = buffer.slice(8 + size);
|
|
76
|
+
if (type === 1 && stdoutWriter) {
|
|
77
|
+
await stdoutWriter.write(payload);
|
|
78
|
+
} else if (type === 2 && stderrWriter) {
|
|
79
|
+
await stderrWriter.write(payload);
|
|
80
|
+
} else {
|
|
81
|
+
if (stdoutWriter)
|
|
82
|
+
await stdoutWriter.write(payload);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
if (stdoutWriter)
|
|
88
|
+
stdoutWriter.releaseLock();
|
|
89
|
+
if (stderrWriter)
|
|
90
|
+
stderrWriter.releaseLock();
|
|
91
|
+
}
|
|
92
|
+
return { exitCode: 0 };
|
|
93
|
+
}
|
|
94
|
+
async getInfo() {
|
|
95
|
+
const res = await fetch(`${this.serverUrl}/sandbox/${this.id}`);
|
|
96
|
+
if (!res.ok) {
|
|
97
|
+
throw new Error(`Failed to get sandbox info: ${await res.text()}`);
|
|
98
|
+
}
|
|
99
|
+
return res.json();
|
|
100
|
+
}
|
|
101
|
+
async destroy() {
|
|
102
|
+
const res = await fetch(`${this.serverUrl}/sandbox/${this.id}`, {
|
|
103
|
+
method: "DELETE"
|
|
104
|
+
});
|
|
105
|
+
if (!res.ok) {
|
|
106
|
+
throw new Error(`Failed to destroy sandbox: ${await res.text()}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
domain(port) {
|
|
110
|
+
const mapped = this.ports[port];
|
|
111
|
+
if (!mapped)
|
|
112
|
+
throw new Error(`Port ${port} not exposed`);
|
|
113
|
+
return `http://localhost:${mapped}`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function createLogStream(stream) {
|
|
117
|
+
return new WritableStream({
|
|
118
|
+
write(chunk) {
|
|
119
|
+
stream.write(chunk);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
export {
|
|
124
|
+
createLogStream,
|
|
125
|
+
Sandbox
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export { Sandbox, createLogStream };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface ServerOptions {
|
|
2
|
+
port?: number;
|
|
3
|
+
}
|
|
4
|
+
export interface SandboxCreateRequest {
|
|
5
|
+
source?: {
|
|
6
|
+
url: string;
|
|
7
|
+
type: "git";
|
|
8
|
+
};
|
|
9
|
+
ports?: number[];
|
|
10
|
+
runtime?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface ExecRequest {
|
|
13
|
+
cmd: string;
|
|
14
|
+
args?: string[];
|
|
15
|
+
detached?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function startServer(options?: ServerOptions): Bun.Server<undefined>;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AASA,MAAM,WAAW,aAAa;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE;QACP,GAAG,EAAE,MAAM,CAAC;QACZ,IAAI,EAAE,KAAK,CAAC;KACb,CAAC;IACF,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AASD,wBAAgB,WAAW,CAAC,OAAO,GAAE,aAAkB,yBAoOtD"}
|