@chan-mai/sandbox 0.0.0-semantic-release → 0.0.0-stage
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 +3 -0
- package/package.json +5 -56
- package/dist/client.d.ts +0 -18
- package/dist/client.js +0 -71
- package/dist/client.js.map +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -4
- package/dist/index.js.map +0 -1
- package/dist/pty.d.ts +0 -19
- package/dist/pty.js +0 -61
- package/dist/pty.js.map +0 -1
- package/dist/sandbox.d.ts +0 -114
- package/dist/sandbox.js +0 -324
- package/dist/sandbox.js.map +0 -1
- package/dist/types.d.ts +0 -98
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/src/client.ts +0 -85
- package/src/index.ts +0 -14
- package/src/pty.ts +0 -76
- package/src/sandbox.ts +0 -444
- package/src/types.ts +0 -124
package/src/sandbox.ts
DELETED
|
@@ -1,444 +0,0 @@
|
|
|
1
|
-
import { Client } from './client.js';
|
|
2
|
-
import { Pty } from './pty.js';
|
|
3
|
-
|
|
4
|
-
import type { ClientOptions } from './client.js';
|
|
5
|
-
import type { PtyOptions } from './pty.js';
|
|
6
|
-
import type {
|
|
7
|
-
ClickOptions,
|
|
8
|
-
CreateOptions,
|
|
9
|
-
ExecOptions,
|
|
10
|
-
ExecResult,
|
|
11
|
-
FileEntry,
|
|
12
|
-
PortPreview,
|
|
13
|
-
RecordingInfo,
|
|
14
|
-
RecordingOptions,
|
|
15
|
-
RestoreOptions,
|
|
16
|
-
SandboxInfo,
|
|
17
|
-
ScreenSize,
|
|
18
|
-
ScrollOptions,
|
|
19
|
-
SnapshotInfo,
|
|
20
|
-
} from './types.js';
|
|
21
|
-
|
|
22
|
-
const accessTTLSeconds = 3600;
|
|
23
|
-
// 期限切れ直前の要求失敗回避
|
|
24
|
-
const accessRenewMarginMs = 5 * 60 * 1000;
|
|
25
|
-
const defaultReadyTimeoutMs = 120_000;
|
|
26
|
-
|
|
27
|
-
type AccessResponse = {
|
|
28
|
-
token: string;
|
|
29
|
-
expires_at: string;
|
|
30
|
-
desktop_url?: string;
|
|
31
|
-
cdp_url?: string;
|
|
32
|
-
agent_url?: string;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export type AccessScope = 'desktop' | 'cdp' | 'agent';
|
|
36
|
-
|
|
37
|
-
type Access = {
|
|
38
|
-
token: string;
|
|
39
|
-
expiresAt: number;
|
|
40
|
-
url: string;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export type SandboxOptions = ClientOptions & { client?: Client };
|
|
44
|
-
|
|
45
|
-
function clientOf(opts: SandboxOptions): Client {
|
|
46
|
-
return opts.client ?? new Client(opts);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function readyQuery(timeoutMs: number | undefined): Record<string, string> {
|
|
50
|
-
return { wait: 'ready', timeout: `${timeoutMs ?? defaultReadyTimeoutMs}ms` };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export class Sandbox {
|
|
54
|
-
readonly client: Client;
|
|
55
|
-
info: SandboxInfo;
|
|
56
|
-
readonly #access = new Map<AccessScope, Access>();
|
|
57
|
-
readonly #accessPending = new Map<AccessScope, Promise<Access>>();
|
|
58
|
-
|
|
59
|
-
readonly files: SandboxFiles;
|
|
60
|
-
readonly pty: SandboxPty;
|
|
61
|
-
readonly browser: SandboxBrowser;
|
|
62
|
-
readonly desktop: SandboxDesktop;
|
|
63
|
-
|
|
64
|
-
private constructor(client: Client, info: SandboxInfo) {
|
|
65
|
-
this.client = client;
|
|
66
|
-
this.info = info;
|
|
67
|
-
this.files = new SandboxFiles(this);
|
|
68
|
-
this.pty = new SandboxPty(this);
|
|
69
|
-
this.browser = new SandboxBrowser(this);
|
|
70
|
-
this.desktop = new SandboxDesktop(this);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
get id(): string {
|
|
74
|
-
return this.info.id;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
static async create(opts: CreateOptions & SandboxOptions = {}): Promise<Sandbox> {
|
|
78
|
-
const client = clientOf(opts);
|
|
79
|
-
const body: Record<string, number> = {};
|
|
80
|
-
if (opts.cpu !== undefined) body.vcpus = opts.cpu;
|
|
81
|
-
if (opts.memory !== undefined) body.memory_mib = opts.memory;
|
|
82
|
-
if (opts.idleTimeout !== undefined) body.idle_timeout = opts.idleTimeout;
|
|
83
|
-
const info = await client.api<SandboxInfo>('POST', '/v1/vms', body, readyQuery(opts.readyTimeout));
|
|
84
|
-
return new Sandbox(client, info);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
static async restore(snapshotId: string, opts: RestoreOptions & SandboxOptions = {}): Promise<Sandbox> {
|
|
88
|
-
const client = clientOf(opts);
|
|
89
|
-
const body = opts.idleTimeout === undefined ? {} : { idle_timeout: opts.idleTimeout };
|
|
90
|
-
const info = await client.api<SandboxInfo>(
|
|
91
|
-
'POST',
|
|
92
|
-
`/v1/snapshots/${encodeURIComponent(snapshotId)}/restore`,
|
|
93
|
-
body,
|
|
94
|
-
readyQuery(opts.readyTimeout),
|
|
95
|
-
);
|
|
96
|
-
return new Sandbox(client, info);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
static async get(id: string, opts: SandboxOptions = {}): Promise<Sandbox> {
|
|
100
|
-
const client = clientOf(opts);
|
|
101
|
-
return new Sandbox(client, await client.api<SandboxInfo>('GET', `/v1/vms/${encodeURIComponent(id)}`));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
static async list(opts: SandboxOptions = {}): Promise<Sandbox[]> {
|
|
105
|
-
const client = clientOf(opts);
|
|
106
|
-
const infos = await client.api<SandboxInfo[]>('GET', '/v1/vms');
|
|
107
|
-
return infos.map(info => new Sandbox(client, info));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
static async listSnapshots(opts: SandboxOptions = {}): Promise<SnapshotInfo[]> {
|
|
111
|
-
return clientOf(opts).api<SnapshotInfo[]>('GET', '/v1/snapshots');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
static async getSnapshot(snapshotId: string, opts: SandboxOptions = {}): Promise<SnapshotInfo> {
|
|
115
|
-
return clientOf(opts).api<SnapshotInfo>('GET', `/v1/snapshots/${encodeURIComponent(snapshotId)}`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
static async deleteSnapshot(snapshotId: string, opts: SandboxOptions = {}): Promise<void> {
|
|
119
|
-
await clientOf(opts).api<void>('DELETE', `/v1/snapshots/${encodeURIComponent(snapshotId)}`);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async refresh(): Promise<SandboxInfo> {
|
|
123
|
-
this.info = await this.client.api<SandboxInfo>('GET', this.path());
|
|
124
|
-
return this.info;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
path(suffix = ''): string {
|
|
128
|
-
return `/v1/vms/${encodeURIComponent(this.id)}${suffix}`;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// 休止, 停止中はGateway経由の要求で自動起動
|
|
132
|
-
async exec(command: string, opts: ExecOptions = {}): Promise<ExecResult> {
|
|
133
|
-
const res = await this.agent<{
|
|
134
|
-
exit_code: number;
|
|
135
|
-
stdout: string;
|
|
136
|
-
stderr: string;
|
|
137
|
-
stdout_truncated?: boolean;
|
|
138
|
-
stderr_truncated?: boolean;
|
|
139
|
-
timed_out?: boolean;
|
|
140
|
-
}>('POST', '/exec', {
|
|
141
|
-
command,
|
|
142
|
-
cwd: opts.cwd,
|
|
143
|
-
env: opts.env,
|
|
144
|
-
stdin: opts.stdin,
|
|
145
|
-
timeout: opts.timeout === undefined ? undefined : `${opts.timeout}ms`,
|
|
146
|
-
});
|
|
147
|
-
return {
|
|
148
|
-
exitCode: res.exit_code,
|
|
149
|
-
stdout: res.stdout,
|
|
150
|
-
stderr: res.stderr,
|
|
151
|
-
stdoutTruncated: res.stdout_truncated ?? false,
|
|
152
|
-
stderrTruncated: res.stderr_truncated ?? false,
|
|
153
|
-
timedOut: res.timed_out ?? false,
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async exposePort(port: number, opts: { ttlSeconds?: number } = {}): Promise<PortPreview> {
|
|
158
|
-
const res = await this.client.api<{ url: string; token: string; expires_at: string }>(
|
|
159
|
-
'POST',
|
|
160
|
-
this.path(`/ports/${port}/access`),
|
|
161
|
-
undefined,
|
|
162
|
-
{ ttl: `${opts.ttlSeconds ?? accessTTLSeconds}s` },
|
|
163
|
-
);
|
|
164
|
-
return { url: res.url, token: res.token, expiresAt: new Date(res.expires_at) };
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
async snapshot(): Promise<SnapshotInfo> {
|
|
168
|
-
return this.client.api<SnapshotInfo>('POST', this.path('/snapshots'));
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async hibernate(): Promise<SandboxInfo> {
|
|
172
|
-
return this.action('hibernate');
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async start(): Promise<SandboxInfo> {
|
|
176
|
-
return this.action('start');
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async stop(): Promise<SandboxInfo> {
|
|
180
|
-
return this.action('stop');
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async pause(): Promise<SandboxInfo> {
|
|
184
|
-
return this.action('pause');
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async resume(): Promise<SandboxInfo> {
|
|
188
|
-
return this.action('resume');
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async delete(): Promise<void> {
|
|
192
|
-
await this.client.api<void>('DELETE', this.path());
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
async [Symbol.asyncDispose](): Promise<void> {
|
|
196
|
-
await this.delete();
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
private async action(name: string): Promise<SandboxInfo> {
|
|
200
|
-
this.info = await this.client.api<SandboxInfo>('POST', this.path(`/${name}`));
|
|
201
|
-
return this.info;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// VM, スコープ単位のアクセストークン, 期限前に再発行
|
|
205
|
-
async access(scope: AccessScope): Promise<Access> {
|
|
206
|
-
const cached = this.#access.get(scope);
|
|
207
|
-
if (cached && cached.expiresAt - Date.now() > accessRenewMarginMs) {
|
|
208
|
-
return cached;
|
|
209
|
-
}
|
|
210
|
-
let pending = this.#accessPending.get(scope);
|
|
211
|
-
if (!pending) {
|
|
212
|
-
pending = this.client
|
|
213
|
-
.api<AccessResponse>('POST', this.path('/access'), undefined, { ttl: `${accessTTLSeconds}s`, scopes: scope })
|
|
214
|
-
.then(res => {
|
|
215
|
-
const url = { desktop: res.desktop_url, cdp: res.cdp_url, agent: res.agent_url }[scope];
|
|
216
|
-
if (url === undefined) throw new Error(`access response has no ${scope} url`);
|
|
217
|
-
const a = { token: res.token, expiresAt: Date.parse(res.expires_at), url };
|
|
218
|
-
this.#access.set(scope, a);
|
|
219
|
-
return a;
|
|
220
|
-
})
|
|
221
|
-
.finally(() => {
|
|
222
|
-
this.#accessPending.delete(scope);
|
|
223
|
-
});
|
|
224
|
-
this.#accessPending.set(scope, pending);
|
|
225
|
-
}
|
|
226
|
-
return pending;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
async agent<T>(method: string, path: string, body?: unknown): Promise<T> {
|
|
230
|
-
const a = await this.access('agent');
|
|
231
|
-
return this.client.request<T>(method, a.url + path, `Bearer ${a.token}`, body);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async agentRaw(method: string, path: string, body?: BodyInit): Promise<Response> {
|
|
235
|
-
const a = await this.access('agent');
|
|
236
|
-
return this.client.raw(method, a.url + path, `Bearer ${a.token}`, body);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function filesQuery(path: string, extra: Record<string, string> = {}): string {
|
|
241
|
-
return '?' + new URLSearchParams({ path, ...extra }).toString();
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export class SandboxFiles {
|
|
245
|
-
readonly #sb: Sandbox;
|
|
246
|
-
|
|
247
|
-
constructor(sb: Sandbox) {
|
|
248
|
-
this.#sb = sb;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
async read(path: string): Promise<Uint8Array> {
|
|
252
|
-
const res = await this.#sb.agentRaw('GET', '/files' + filesQuery(path));
|
|
253
|
-
return new Uint8Array(await res.arrayBuffer());
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
async readText(path: string): Promise<string> {
|
|
257
|
-
return new TextDecoder().decode(await this.read(path));
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// 親ディレクトリ自動作成, 一時ファイル経由で置換
|
|
261
|
-
async write(path: string, data: string | Uint8Array<ArrayBuffer>, opts: { mode?: number } = {}): Promise<void> {
|
|
262
|
-
const extra: Record<string, string> = opts.mode === undefined ? {} : { mode: '0' + opts.mode.toString(8) };
|
|
263
|
-
const body = typeof data === 'string' ? new TextEncoder().encode(data) : data;
|
|
264
|
-
await this.#sb.agentRaw('PUT', '/files' + filesQuery(path, extra), body);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
async mkdir(path: string): Promise<void> {
|
|
268
|
-
await this.#sb.agentRaw('PUT', '/files' + filesQuery(path, { type: 'directory' }));
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
async list(path: string): Promise<FileEntry[]> {
|
|
272
|
-
return this.#sb.agent<FileEntry[]>('GET', '/files' + filesQuery(path));
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
async stat(path: string): Promise<FileEntry> {
|
|
276
|
-
return this.#sb.agent<FileEntry>('GET', '/stat' + filesQuery(path));
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
async remove(path: string, opts: { recursive?: boolean } = {}): Promise<void> {
|
|
280
|
-
await this.#sb.agentRaw('DELETE', '/files' + filesQuery(path, opts.recursive ? { recursive: 'true' } : {}));
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export class SandboxPty {
|
|
285
|
-
readonly #sb: Sandbox;
|
|
286
|
-
|
|
287
|
-
constructor(sb: Sandbox) {
|
|
288
|
-
this.#sb = sb;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// WebSocketのヘッダ指定不可, トークンはクエリで送信
|
|
292
|
-
async create(opts: PtyOptions = {}): Promise<Pty> {
|
|
293
|
-
const a = await this.#sb.access('agent');
|
|
294
|
-
const u = new URL(a.url + '/pty');
|
|
295
|
-
u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
296
|
-
u.searchParams.set('access_token', a.token);
|
|
297
|
-
if (opts.cols !== undefined) u.searchParams.set('cols', String(opts.cols));
|
|
298
|
-
if (opts.rows !== undefined) u.searchParams.set('rows', String(opts.rows));
|
|
299
|
-
if (opts.cwd !== undefined) u.searchParams.set('cwd', opts.cwd);
|
|
300
|
-
return Pty.open(u.toString());
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
export type CDPEndpoint = {
|
|
305
|
-
url: string;
|
|
306
|
-
headers: Record<string, string>;
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
export class SandboxBrowser {
|
|
310
|
-
readonly #sb: Sandbox;
|
|
311
|
-
|
|
312
|
-
constructor(sb: Sandbox) {
|
|
313
|
-
this.#sb = sb;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
async cdpEndpoint(): Promise<CDPEndpoint> {
|
|
317
|
-
const a = await this.#sb.access('cdp');
|
|
318
|
-
return { url: a.url, headers: { authorization: `Bearer ${a.token}` } };
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
async connect(): Promise<import('playwright-core').Browser> {
|
|
322
|
-
let pw: typeof import('playwright-core');
|
|
323
|
-
try {
|
|
324
|
-
pw = await import('playwright-core');
|
|
325
|
-
} catch {
|
|
326
|
-
throw new Error('browser.connect() requires the optional peer dependency playwright-core');
|
|
327
|
-
}
|
|
328
|
-
const ep = await this.cdpEndpoint();
|
|
329
|
-
return pw.chromium.connectOverCDP(ep.url, { headers: ep.headers });
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
export class SandboxDesktop {
|
|
334
|
-
readonly #sb: Sandbox;
|
|
335
|
-
|
|
336
|
-
constructor(sb: Sandbox) {
|
|
337
|
-
this.#sb = sb;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// URLにdesktopスコープ限定のアクセストークン含む, ブラウザでそのまま表示可
|
|
341
|
-
async getURL(): Promise<string> {
|
|
342
|
-
return (await this.#sb.access('desktop')).url;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// PNG
|
|
346
|
-
async screenshot(): Promise<Uint8Array> {
|
|
347
|
-
const res = await this.#sb.agentRaw('GET', '/desktop/screenshot');
|
|
348
|
-
return new Uint8Array(await res.arrayBuffer());
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async screenSize(): Promise<ScreenSize> {
|
|
352
|
-
return this.#sb.agent<ScreenSize>('GET', '/desktop/screen');
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
async move(pos: { x: number; y: number }): Promise<void> {
|
|
356
|
-
await this.#sb.agent<void>('POST', '/desktop/move', pos);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
async click(opts: ClickOptions = {}): Promise<void> {
|
|
360
|
-
await this.#sb.agent<void>('POST', '/desktop/click', opts);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
async scroll(opts: ScrollOptions): Promise<void> {
|
|
364
|
-
await this.#sb.agent<void>('POST', '/desktop/scroll', opts);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
// ASCII文字, 改行, タブのみ, 日本語はIMEで入力
|
|
368
|
-
async type(text: string): Promise<void> {
|
|
369
|
-
await this.#sb.agent<void>('POST', '/desktop/type', { text });
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// 例: Enter, Ctrl+L, Ctrl+Shift+Tab
|
|
373
|
-
async press(key: string): Promise<void> {
|
|
374
|
-
await this.#sb.agent<void>('POST', '/desktop/press', { key });
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// 一括入力, クリップボード上書き, keyは貼付けキー(既定Ctrl+V)
|
|
378
|
-
async paste(text: string, opts: { key?: string } = {}): Promise<void> {
|
|
379
|
-
await this.#sb.agent<void>('POST', '/desktop/paste', { text, ...opts });
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
async startRecording(opts: RecordingOptions = {}): Promise<Recording> {
|
|
383
|
-
return new Recording(this.#sb, await this.#sb.agent<RecordingInfo>('POST', '/desktop/recordings', opts));
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
async listRecordings(): Promise<Recording[]> {
|
|
387
|
-
const infos = await this.#sb.agent<RecordingInfo[]>('GET', '/desktop/recordings');
|
|
388
|
-
return infos.map(info => new Recording(this.#sb, info));
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
async getRecording(id: string): Promise<Recording> {
|
|
392
|
-
return new Recording(this.#sb, await this.#sb.agent<RecordingInfo>('GET', recordingPath(id)));
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
function recordingPath(id: string, suffix = ''): string {
|
|
397
|
-
return `/desktop/recordings/${encodeURIComponent(id)}${suffix}`;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// 録画ファイルはVM内に保存, 取得はエージェント経由
|
|
401
|
-
export class Recording {
|
|
402
|
-
readonly #sb: Sandbox;
|
|
403
|
-
info: RecordingInfo;
|
|
404
|
-
|
|
405
|
-
constructor(sb: Sandbox, info: RecordingInfo) {
|
|
406
|
-
this.#sb = sb;
|
|
407
|
-
this.info = info;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
get id(): string {
|
|
411
|
-
return this.info.id;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
get path(): string {
|
|
415
|
-
return this.info.path;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
async refresh(): Promise<RecordingInfo> {
|
|
419
|
-
this.info = await this.#sb.agent<RecordingInfo>('GET', recordingPath(this.id));
|
|
420
|
-
return this.info;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
async stop(): Promise<RecordingInfo> {
|
|
424
|
-
this.info = await this.#sb.agent<RecordingInfo>('POST', recordingPath(this.id, '/stop'));
|
|
425
|
-
return this.info;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
async download(): Promise<Uint8Array> {
|
|
429
|
-
return this.#sb.files.read(this.path);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
// 大きな録画をメモリへ載せず保存する用途
|
|
433
|
-
async stream(): Promise<ReadableStream<Uint8Array>> {
|
|
434
|
-
const res = await this.#sb.agentRaw('GET', '/files' + filesQuery(this.path));
|
|
435
|
-
if (res.body === null) {
|
|
436
|
-
throw new Error('recording response has no body');
|
|
437
|
-
}
|
|
438
|
-
return res.body;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
async delete(): Promise<void> {
|
|
442
|
-
await this.#sb.agent<void>('DELETE', recordingPath(this.id));
|
|
443
|
-
}
|
|
444
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
export type SandboxState = 'running' | 'paused' | 'stopped' | 'hibernated' | 'unknown';
|
|
2
|
-
|
|
3
|
-
export type Shape = {
|
|
4
|
-
vcpus: number;
|
|
5
|
-
memory_mib: number;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type ImageInfo = {
|
|
9
|
-
id: string;
|
|
10
|
-
store_path?: string;
|
|
11
|
-
firecracker_version: string;
|
|
12
|
-
snapshot_format: string;
|
|
13
|
-
kernel_version: string;
|
|
14
|
-
arch: string;
|
|
15
|
-
cpu_template: string;
|
|
16
|
-
registered_at: string;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
// GET /v1/vms/{id}の応答
|
|
20
|
-
export type SandboxInfo = {
|
|
21
|
-
id: string;
|
|
22
|
-
created_at: string;
|
|
23
|
-
snapshot_id?: string;
|
|
24
|
-
state: SandboxState;
|
|
25
|
-
ready?: boolean;
|
|
26
|
-
shape: Shape;
|
|
27
|
-
idle_timeout?: number;
|
|
28
|
-
image: ImageInfo;
|
|
29
|
-
last_active_at?: string;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type SnapshotInfo = {
|
|
33
|
-
id: string;
|
|
34
|
-
vm_id: string;
|
|
35
|
-
created_at: string;
|
|
36
|
-
shape: Shape;
|
|
37
|
-
image: ImageInfo;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export type CreateOptions = {
|
|
41
|
-
// -shapesで許可した組み合わせのみ
|
|
42
|
-
cpu?: number;
|
|
43
|
-
// MiB
|
|
44
|
-
memory?: number;
|
|
45
|
-
// 秒, 0で自動休止無効, 省略時はorchestrator既定値
|
|
46
|
-
idleTimeout?: number;
|
|
47
|
-
// 起動完了待ち上限, ミリ秒
|
|
48
|
-
readyTimeout?: number;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export type RestoreOptions = {
|
|
52
|
-
idleTimeout?: number;
|
|
53
|
-
readyTimeout?: number;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export type ExecOptions = {
|
|
57
|
-
cwd?: string;
|
|
58
|
-
env?: Record<string, string>;
|
|
59
|
-
stdin?: string;
|
|
60
|
-
// ミリ秒
|
|
61
|
-
timeout?: number;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export type ExecResult = {
|
|
65
|
-
exitCode: number;
|
|
66
|
-
stdout: string;
|
|
67
|
-
stderr: string;
|
|
68
|
-
stdoutTruncated: boolean;
|
|
69
|
-
stderrTruncated: boolean;
|
|
70
|
-
timedOut: boolean;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export type FileEntry = {
|
|
74
|
-
name: string;
|
|
75
|
-
path?: string;
|
|
76
|
-
type: 'file' | 'directory' | 'symlink' | 'other';
|
|
77
|
-
size: number;
|
|
78
|
-
mode: string;
|
|
79
|
-
modified: string;
|
|
80
|
-
target?: string;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export type PortPreview = {
|
|
84
|
-
url: string;
|
|
85
|
-
token: string;
|
|
86
|
-
expiresAt: Date;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
export type MouseButton = 'left' | 'middle' | 'right';
|
|
90
|
-
|
|
91
|
-
// 座標省略時は現在のポインタ位置
|
|
92
|
-
export type ClickOptions = {
|
|
93
|
-
x?: number;
|
|
94
|
-
y?: number;
|
|
95
|
-
button?: MouseButton;
|
|
96
|
-
double?: boolean;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// 正の値で下, 右方向, 単位はホイールの1ステップ
|
|
100
|
-
export type ScrollOptions = {
|
|
101
|
-
x?: number;
|
|
102
|
-
y?: number;
|
|
103
|
-
dx?: number;
|
|
104
|
-
dy?: number;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
export type ScreenSize = {
|
|
108
|
-
width: number;
|
|
109
|
-
height: number;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export type RecordingOptions = {
|
|
113
|
-
// 1から60, 省略時15
|
|
114
|
-
framerate?: number;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
export type RecordingInfo = {
|
|
118
|
-
id: string;
|
|
119
|
-
// VM内のMP4ファイル
|
|
120
|
-
path: string;
|
|
121
|
-
state: 'recording' | 'stopped';
|
|
122
|
-
size: number;
|
|
123
|
-
started_at?: string;
|
|
124
|
-
};
|