@chan-mai/sandbox 0.0.0-stage → 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/dist/client.d.ts +18 -0
- package/dist/client.js +71 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/pty.d.ts +19 -0
- package/dist/pty.js +61 -0
- package/dist/pty.js.map +1 -0
- package/dist/sandbox.d.ts +114 -0
- package/dist/sandbox.js +324 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/types.d.ts +98 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +56 -5
- package/src/client.ts +85 -0
- package/src/index.ts +14 -0
- package/src/pty.ts +76 -0
- package/src/sandbox.ts +444 -0
- package/src/types.ts +124 -0
- package/README.md +0 -3
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export type SandboxState = 'running' | 'paused' | 'stopped' | 'hibernated' | 'unknown';
|
|
2
|
+
export type Shape = {
|
|
3
|
+
vcpus: number;
|
|
4
|
+
memory_mib: number;
|
|
5
|
+
};
|
|
6
|
+
export type ImageInfo = {
|
|
7
|
+
id: string;
|
|
8
|
+
store_path?: string;
|
|
9
|
+
firecracker_version: string;
|
|
10
|
+
snapshot_format: string;
|
|
11
|
+
kernel_version: string;
|
|
12
|
+
arch: string;
|
|
13
|
+
cpu_template: string;
|
|
14
|
+
registered_at: string;
|
|
15
|
+
};
|
|
16
|
+
export type SandboxInfo = {
|
|
17
|
+
id: string;
|
|
18
|
+
created_at: string;
|
|
19
|
+
snapshot_id?: string;
|
|
20
|
+
state: SandboxState;
|
|
21
|
+
ready?: boolean;
|
|
22
|
+
shape: Shape;
|
|
23
|
+
idle_timeout?: number;
|
|
24
|
+
image: ImageInfo;
|
|
25
|
+
last_active_at?: string;
|
|
26
|
+
};
|
|
27
|
+
export type SnapshotInfo = {
|
|
28
|
+
id: string;
|
|
29
|
+
vm_id: string;
|
|
30
|
+
created_at: string;
|
|
31
|
+
shape: Shape;
|
|
32
|
+
image: ImageInfo;
|
|
33
|
+
};
|
|
34
|
+
export type CreateOptions = {
|
|
35
|
+
cpu?: number;
|
|
36
|
+
memory?: number;
|
|
37
|
+
idleTimeout?: number;
|
|
38
|
+
readyTimeout?: number;
|
|
39
|
+
};
|
|
40
|
+
export type RestoreOptions = {
|
|
41
|
+
idleTimeout?: number;
|
|
42
|
+
readyTimeout?: number;
|
|
43
|
+
};
|
|
44
|
+
export type ExecOptions = {
|
|
45
|
+
cwd?: string;
|
|
46
|
+
env?: Record<string, string>;
|
|
47
|
+
stdin?: string;
|
|
48
|
+
timeout?: number;
|
|
49
|
+
};
|
|
50
|
+
export type ExecResult = {
|
|
51
|
+
exitCode: number;
|
|
52
|
+
stdout: string;
|
|
53
|
+
stderr: string;
|
|
54
|
+
stdoutTruncated: boolean;
|
|
55
|
+
stderrTruncated: boolean;
|
|
56
|
+
timedOut: boolean;
|
|
57
|
+
};
|
|
58
|
+
export type FileEntry = {
|
|
59
|
+
name: string;
|
|
60
|
+
path?: string;
|
|
61
|
+
type: 'file' | 'directory' | 'symlink' | 'other';
|
|
62
|
+
size: number;
|
|
63
|
+
mode: string;
|
|
64
|
+
modified: string;
|
|
65
|
+
target?: string;
|
|
66
|
+
};
|
|
67
|
+
export type PortPreview = {
|
|
68
|
+
url: string;
|
|
69
|
+
token: string;
|
|
70
|
+
expiresAt: Date;
|
|
71
|
+
};
|
|
72
|
+
export type MouseButton = 'left' | 'middle' | 'right';
|
|
73
|
+
export type ClickOptions = {
|
|
74
|
+
x?: number;
|
|
75
|
+
y?: number;
|
|
76
|
+
button?: MouseButton;
|
|
77
|
+
double?: boolean;
|
|
78
|
+
};
|
|
79
|
+
export type ScrollOptions = {
|
|
80
|
+
x?: number;
|
|
81
|
+
y?: number;
|
|
82
|
+
dx?: number;
|
|
83
|
+
dy?: number;
|
|
84
|
+
};
|
|
85
|
+
export type ScreenSize = {
|
|
86
|
+
width: number;
|
|
87
|
+
height: number;
|
|
88
|
+
};
|
|
89
|
+
export type RecordingOptions = {
|
|
90
|
+
framerate?: number;
|
|
91
|
+
};
|
|
92
|
+
export type RecordingInfo = {
|
|
93
|
+
id: string;
|
|
94
|
+
path: string;
|
|
95
|
+
state: 'recording' | 'stopped';
|
|
96
|
+
size: number;
|
|
97
|
+
started_at?: string;
|
|
98
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@chan-mai/sandbox",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeScript SDK for the sandbox orchestrator",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/chan-mai/sandbox.git",
|
|
9
|
+
"directory": "sdk/typescript"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -p tsconfig.build.json",
|
|
27
|
+
"prepack": "npm run build",
|
|
28
|
+
"typecheck": "tsc --noEmit",
|
|
29
|
+
"lint": "oxlint",
|
|
30
|
+
"format": "oxfmt",
|
|
31
|
+
"format:check": "oxfmt --check",
|
|
32
|
+
"test": "vitest run test/unit",
|
|
33
|
+
"test:integration": "vitest run test/integration",
|
|
34
|
+
"test:fhs": "vitest run test/fhs"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@chan-mai/oxc-config": "^1.3.0",
|
|
38
|
+
"@types/node": "^26.6.11",
|
|
39
|
+
"oxfmt": "^0.81.0",
|
|
40
|
+
"oxlint": "^1.96.0",
|
|
41
|
+
"oxlint-tsgolint": "^7.0.2009",
|
|
42
|
+
"playwright-core": "^1.65.2",
|
|
43
|
+
"typescript": "^7.0.2",
|
|
44
|
+
"vitest": "^5.1.2"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"playwright-core": ">=1.50"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"playwright-core": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=26"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export type ClientOptions = {
|
|
2
|
+
// orchestratorのURL, 既定値は環境変数SANDBOX_API_URL
|
|
3
|
+
apiURL?: string;
|
|
4
|
+
// /v1 APIのトークン, 既定値は環境変数SANDBOX_API_TOKEN
|
|
5
|
+
apiToken?: string;
|
|
6
|
+
fetch?: typeof globalThis.fetch;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export class SandboxError extends Error {
|
|
10
|
+
readonly status: number;
|
|
11
|
+
|
|
12
|
+
constructor(status: number, message: string) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'SandboxError';
|
|
15
|
+
this.status = status;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class Client {
|
|
20
|
+
readonly apiURL: string;
|
|
21
|
+
readonly #apiToken: string;
|
|
22
|
+
readonly #fetch: typeof globalThis.fetch;
|
|
23
|
+
|
|
24
|
+
constructor(opts: ClientOptions = {}) {
|
|
25
|
+
const apiURL = opts.apiURL ?? process.env.SANDBOX_API_URL;
|
|
26
|
+
const apiToken = opts.apiToken ?? process.env.SANDBOX_API_TOKEN;
|
|
27
|
+
if (!apiURL) {
|
|
28
|
+
throw new Error('apiURL or SANDBOX_API_URL is required');
|
|
29
|
+
}
|
|
30
|
+
if (!apiToken) {
|
|
31
|
+
throw new Error('apiToken or SANDBOX_API_TOKEN is required');
|
|
32
|
+
}
|
|
33
|
+
this.apiURL = apiURL.replace(/\/+$/, '');
|
|
34
|
+
this.#apiToken = apiToken;
|
|
35
|
+
this.#fetch = opts.fetch ?? globalThis.fetch;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// /v1 API呼び出し, 204時はundefined
|
|
39
|
+
async api<T>(method: string, path: string, body?: unknown, query?: Record<string, string>): Promise<T> {
|
|
40
|
+
return this.request<T>(method, this.url(path, query), `Bearer ${this.#apiToken}`, body);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
url(path: string, query?: Record<string, string>): string {
|
|
44
|
+
const u = new URL(this.apiURL + path);
|
|
45
|
+
for (const [k, v] of Object.entries(query ?? {})) {
|
|
46
|
+
u.searchParams.set(k, v);
|
|
47
|
+
}
|
|
48
|
+
return u.toString();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async request<T>(method: string, url: string, authorization: string, body?: unknown): Promise<T> {
|
|
52
|
+
const res = await this.raw(method, url, authorization, body === undefined ? undefined : JSON.stringify(body), {
|
|
53
|
+
'content-type': 'application/json',
|
|
54
|
+
});
|
|
55
|
+
if (res.status === 204) {
|
|
56
|
+
return undefined as T;
|
|
57
|
+
}
|
|
58
|
+
return (await res.json()) as T;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async raw(method: string, url: string, authorization: string, body?: BodyInit, headers: Record<string, string> = {}): Promise<Response> {
|
|
62
|
+
const res = await this.#fetch(url, {
|
|
63
|
+
method,
|
|
64
|
+
headers: { authorization, ...headers },
|
|
65
|
+
...(body === undefined ? {} : { body }),
|
|
66
|
+
});
|
|
67
|
+
if (!res.ok) {
|
|
68
|
+
throw new SandboxError(res.status, await errorMessage(res));
|
|
69
|
+
}
|
|
70
|
+
return res;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function errorMessage(res: Response): Promise<string> {
|
|
75
|
+
const text = await res.text();
|
|
76
|
+
try {
|
|
77
|
+
const parsed = JSON.parse(text) as { error?: unknown };
|
|
78
|
+
if (typeof parsed.error === 'string') {
|
|
79
|
+
return parsed.error;
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
// JSON以外の本文
|
|
83
|
+
}
|
|
84
|
+
return text || `HTTP ${res.status}`;
|
|
85
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { Client, SandboxError, type ClientOptions } from './client.js';
|
|
2
|
+
export { Pty, type PtyOptions } from './pty.js';
|
|
3
|
+
export {
|
|
4
|
+
Recording,
|
|
5
|
+
Sandbox,
|
|
6
|
+
SandboxBrowser,
|
|
7
|
+
SandboxDesktop,
|
|
8
|
+
SandboxFiles,
|
|
9
|
+
SandboxPty,
|
|
10
|
+
type AccessScope,
|
|
11
|
+
type CDPEndpoint,
|
|
12
|
+
type SandboxOptions,
|
|
13
|
+
} from './sandbox.js';
|
|
14
|
+
export type * from './types.js';
|
package/src/pty.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export type PtyOptions = {
|
|
2
|
+
cols?: number;
|
|
3
|
+
rows?: number;
|
|
4
|
+
cwd?: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type DataListener = (data: Uint8Array) => void;
|
|
8
|
+
type ExitListener = (exitCode: number | null) => void;
|
|
9
|
+
|
|
10
|
+
// バイナリ: 端末入出力, テキスト: 制御用JSON
|
|
11
|
+
export class Pty {
|
|
12
|
+
readonly #ws: WebSocket;
|
|
13
|
+
readonly #data = new Set<DataListener>();
|
|
14
|
+
readonly #exit = new Set<ExitListener>();
|
|
15
|
+
readonly exited: Promise<number | null>;
|
|
16
|
+
|
|
17
|
+
private constructor(ws: WebSocket) {
|
|
18
|
+
this.#ws = ws;
|
|
19
|
+
ws.binaryType = 'arraybuffer';
|
|
20
|
+
let exitCode: number | null = null;
|
|
21
|
+
ws.addEventListener('message', (ev: MessageEvent) => {
|
|
22
|
+
if (typeof ev.data === 'string') {
|
|
23
|
+
const msg = JSON.parse(ev.data) as { type?: string; exit_code?: number };
|
|
24
|
+
if (msg.type === 'exit') {
|
|
25
|
+
exitCode = msg.exit_code ?? null;
|
|
26
|
+
}
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const chunk = new Uint8Array(ev.data as ArrayBuffer);
|
|
30
|
+
for (const fn of this.#data) {
|
|
31
|
+
fn(chunk);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
this.exited = new Promise(resolve => {
|
|
35
|
+
ws.addEventListener('close', () => {
|
|
36
|
+
for (const fn of this.#exit) {
|
|
37
|
+
fn(exitCode);
|
|
38
|
+
}
|
|
39
|
+
resolve(exitCode);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static async open(url: string): Promise<Pty> {
|
|
45
|
+
const ws = new WebSocket(url);
|
|
46
|
+
await new Promise<void>((resolve, reject) => {
|
|
47
|
+
ws.addEventListener('open', () => resolve(), { once: true });
|
|
48
|
+
ws.addEventListener('error', () => reject(new Error('pty websocket connection failed')), { once: true });
|
|
49
|
+
});
|
|
50
|
+
return new Pty(ws);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 返却関数で登録解除
|
|
54
|
+
onData(fn: DataListener): () => void {
|
|
55
|
+
this.#data.add(fn);
|
|
56
|
+
return () => this.#data.delete(fn);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
onExit(fn: ExitListener): () => void {
|
|
60
|
+
this.#exit.add(fn);
|
|
61
|
+
return () => this.#exit.delete(fn);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
write(data: string | Uint8Array<ArrayBuffer>): void {
|
|
65
|
+
this.#ws.send(typeof data === 'string' ? new TextEncoder().encode(data) : data);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
resize(cols: number, rows: number): void {
|
|
69
|
+
this.#ws.send(JSON.stringify({ type: 'resize', cols, rows }));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// シェルへSIGHUP送信
|
|
73
|
+
close(): void {
|
|
74
|
+
this.#ws.close();
|
|
75
|
+
}
|
|
76
|
+
}
|