Skip to content

Commit 7b9ba01

Browse files
authored
Merge pull request #6 from Serverless-Devs/sandbox-params
refactor(sandbox): migrate to object-based API parameters with backwa…
2 parents 91f37f5 + 7739840 commit 7b9ba01

14 files changed

Lines changed: 1335 additions & 633 deletions

examples/sandbox.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
*/
1515

1616
import * as fs from 'fs/promises';
17-
import * as path from 'path';
1817

1918
import {
2019
CodeInterpreterSandbox,
2120
CodeLanguage,
22-
Sandbox,
2321
SandboxClient,
2422
Template,
25-
TemplateType,
23+
TemplateType
2624
} from '../src/index';
2725
import { logger } from '../src/utils/log';
2826

@@ -39,7 +37,7 @@ const client = new SandboxClient();
3937
async function listTemplates(): Promise<void> {
4038
log('枚举模板列表 / Listing templates');
4139

42-
const templates = await client.listAllTemplates();
40+
const templates = await client.listTemplates();
4341
log(`共有 ${templates.length} 个模板 / Total ${templates.length} templates:`);
4442

4543
for (const template of templates) {
@@ -76,10 +74,12 @@ async function codeInterpreterExample(): Promise<void> {
7674
// 创建模板 / Create template
7775
log('\n--- 创建模板 / Creating template ---');
7876
const template = await Template.create({
79-
templateName,
80-
templateType: TemplateType.CODE_INTERPRETER,
81-
description: 'Test template from Node.js SDK',
82-
sandboxIdleTimeoutInSeconds: 600,
77+
input: {
78+
templateName,
79+
templateType: TemplateType.CODE_INTERPRETER,
80+
description: 'Test template from Node.js SDK',
81+
sandboxIdleTimeoutInSeconds: 600,
82+
}
8383
});
8484

8585
log(`✓ 创建模板成功 / Template created: ${template.templateName}`);
@@ -158,9 +158,9 @@ async function codeInterpreterExample(): Promise<void> {
158158
});
159159
log(`✓ 上传文件成功 / File uploaded successfully`);
160160

161-
const filestat = await sandbox.fileSystem.stat(
162-
'/home/user/test-move/test_file.txt'
163-
);
161+
const filestat = await sandbox.fileSystem.stat({
162+
path: '/home/user/test-move/test_file.txt'
163+
});
164164
log(`✓ 上传文件详情 / Uploaded file stat:`, filestat);
165165

166166
const downloadPath = './downloaded_test_file.txt';
@@ -187,7 +187,7 @@ async function codeInterpreterExample(): Promise<void> {
187187
});
188188
log(`✓ 写入文件成功 / File written successfully`);
189189

190-
const readResult = await sandbox.file.read('/home/user/test/test.txt');
190+
const readResult = await sandbox.file.read({ path: '/home/user/test/test.txt' });
191191
log(`✓ 读取文件结果 / File read result:`, readResult);
192192

193193
// 测试文件移动 / File move test
@@ -198,19 +198,19 @@ async function codeInterpreterExample(): Promise<void> {
198198
});
199199
log(`✓ 移动文件成功 / File moved successfully`);
200200

201-
const movedContent = await sandbox.file.read(
202-
'/home/user/test-move/test2.txt'
203-
);
201+
const movedContent = await sandbox.file.read({
202+
path: '/home/user/test-move/test2.txt'
203+
});
204204
log(`✓ 读取移动后的文件 / Read moved file:`, movedContent);
205205

206206
// 测试文件详情 / File stat test
207207
log('\n--- 测试文件详情 / Testing file stat ---');
208-
const dirStat = await sandbox.fileSystem.stat('/home/user/test-move');
208+
const dirStat = await sandbox.fileSystem.stat({ path: '/home/user/test-move' });
209209
log(`✓ 文件详情 / File stat:`, dirStat);
210210

211211
// 测试删除文件 / Delete test
212212
log('\n--- 测试删除文件 / Testing file deletion ---');
213-
await sandbox.fileSystem.remove('/home/user/test-move');
213+
await sandbox.fileSystem.remove({ path: '/home/user/test-move' });
214214
log(`✓ 删除文件夹成功 / Directory deleted successfully`);
215215

216216
// 测试进程操作 / Process operations
@@ -221,7 +221,7 @@ async function codeInterpreterExample(): Promise<void> {
221221
const cmdResult = await sandbox.process.cmd({ command: 'ls', cwd: '/' });
222222
log(`✓ 进程执行结果 / Process execution result:`, cmdResult);
223223

224-
const processDetails = await sandbox.process.get('1');
224+
const processDetails = await sandbox.process.get({ pid: '1' });
225225
log(`✓ 进程详情 / Process details:`, processDetails);
226226

227227
// 清理上下文

src/sandbox/aio-sandbox.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,19 @@ export class AioSandbox extends Sandbox {
311311
},
312312
config?: Config
313313
): Promise<AioSandbox> {
314-
const sandbox = await Sandbox.create(
315-
{
314+
const sandbox = await Sandbox.create({
315+
input: {
316316
templateName,
317317
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
318318
nasConfig: options?.nasConfig,
319319
ossMountConfig: options?.ossMountConfig,
320320
polarFsConfig: options?.polarFsConfig,
321321
},
322-
config
323-
);
322+
templateType: TemplateType.AIO,
323+
config,
324+
});
324325

325-
const aioSandbox = new AioSandbox(sandbox, config);
326-
return aioSandbox;
326+
return sandbox as AioSandbox;
327327
}
328328

329329
constructor(sandbox: Sandbox, config?: Config) {

src/sandbox/browser-sandbox.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ export class BrowserSandbox extends Sandbox {
3838
},
3939
config?: Config
4040
): Promise<BrowserSandbox> {
41-
const sandbox = await Sandbox.create(
42-
{
41+
const sandbox = await Sandbox.create({
42+
input: {
4343
templateName,
4444
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
4545
nasConfig: options?.nasConfig,
4646
ossMountConfig: options?.ossMountConfig,
4747
polarFsConfig: options?.polarFsConfig,
4848
},
49-
config
50-
);
49+
templateType: TemplateType.BROWSER,
50+
config,
51+
});
5152

52-
const browserSandbox = new BrowserSandbox(sandbox, config);
53-
return browserSandbox;
53+
return sandbox as BrowserSandbox;
5454
}
5555

5656
constructor(sandbox: Sandbox, config?: Config) {

0 commit comments

Comments
 (0)