应用构建器 SDK
JavaScript SDK 为 Adobe runtime Sandboxs 所使用.
A 类 沙盒 是一种与世隔绝的计算环境。 您创建一个, 运行命令并读取/ 写入文件, 然后销毁它 .
先决条件
要使用此库, 您必须启用 Sandboxs 为您的运行时命名空间 。 请联系迈克尔·戈伯林(Michael Goberling)[email protected] (英语)或科斯敏·斯坦西乌(Cosmin Stanciu)时间轴: [email protected]要求这样做.
安装
npm install @adobe/aio-lib-sandbox
快速启动
在运行时动作内部,不需要配置来使用SDK,因为证书是从环境中自动读取的.
const { Sandbox } = require('@adobe/aio-lib-sandbox')
async function main (params) {
const sandbox = await Sandbox.create({ name: 'my-sandbox' })
const { stdout } = await sandbox.exec('node --version', { timeout: 10_000 })
await sandbox.destroy()
return { stdout: stdout.trim() }
}
exports.main = main
配置
当运行在运行时间动作内时, SDK 自动读取来自环境的证书 :
| 变量 | 说明 |
|---|---|
__OW_API_HOST | 运行时间 API 主机 |
__OW_NAMESPACE | 运行时命名空间 |
__OW_API_KEY | 运行时间 API 密钥( 基本认证) |
你可以通过直接传递到 Sandbox.create() 或者说 Sandbox.get()数字 :
const sandbox = await Sandbox.create({
apiHost: 'https://adobeioruntime.net',
namespace: 'my-namespace',
auth: 'my-api-key',
name: 'my-sandbox'
})
使用量
创建沙盒
const { Sandbox } = require('@adobe/aio-lib-sandbox')
const sandbox = await Sandbox.create({
name: 'my-sandbox',
region: 'va6',
type: 'cpu:default',
idleTimeout: 900,
maxLifetime: 3600,
ports: [3000, 8080],
envs: { API_KEY: 'your-api-key' }
})
Canonical sandbox API主机为纬度路由并可以发送请求到外接组
区域名称。 何时 region 设置, SDK 由此引导犬类主机
直接到那个区域终点。 区域标识符使用2到4个字母,然后是1或
二位数,并改为小写。 自定义和已经区域 apiHost 值为左
不变。 选择 region 保存默认的 latency 路由 API 主机行为 .
沙盒寿命模型
沙盒总是被删除 maxLifetime 已经过去了。 还将在 idleTimeout 已经过去了,如果没有活动.
保存沙盒, 发送至少一个命令或检查状态 idleTimeout 秒数.
获取状态
const sandbox = await Sandbox.get(sandbox.id)
console.log('status:', sandbox.status)
执行
const result = await sandbox.exec('ls -al', { timeout: 10_000 })
console.log('stdout:', result.stdout.trim())
console.log('exit code:', result.exitCode)
注:在
/workspace默认目录, 无法配置
已删除的命令
过 detached: true 以运行一个长寿的背景进程.
// Start a background server
const command = await sandbox.exec('node server.js', { detached: true })
// Wait for it to exit (e.g. after you stop it)
const result = await command.wait()
console.log('exit code:', result.exitCode)
// Send a signal to stop it
await command.kill()
如果进程还在运行中, 您需要从不同的上下文获得一个手柄, 请使用 getCommand() 以重新连接 execId数字 :
const command = await sandbox.getCommand(execId, { onOutput: (data, stream) => process.stdout.write(data) })
await command.wait()
注:目前只允许5个背景进程同时运行.
文件管理
const script = "console.log('hello from sandbox script', process.version)\n"
await sandbox.writeFile('hello.js', script)
const content = await sandbox.readFile('hello.js')
console.log('readFile content:', content.trim())
const entries = await sandbox.listFiles('.')
console.log('listFiles entries:', entries)
执行文件
const result = await sandbox.exec('node hello.js', { timeout: 10_000 })
console.log('stdout:', result.stdout.trim())
console.log('stderr:', result.stderr.trim())
console.log('exit code:', result.exitCode)
写入 Stdin
开始命令
const result = await sandbox.exec('node process_csv.js', {
stdin: 'col1,col2\nval1,val2\n',
timeout: 10_000
})
console.log('stdout:', result.stdout.trim())
运行中的命令
const task = sandbox.exec('cat -n', { timeout: 10_000 })
sandbox.writeStdin(task.execId, 'line 1\n')
sandbox.writeStdin(task.execId, 'line 2\n')
sandbox.closeStdin(task.execId)
const result = await task
console.log('stdout:', result.stdout.trim())
销毁
await sandbox.destroy()
预览 URL
应向公众开放的港口必须在创建时通过 ports 阵列.
const sandbox = await Sandbox.create({
name: 'web-sandbox',
ports: [3000, 8080]
})
// Start a server inside the sandbox on the declared port
await sandbox.exec('node server.js &', { timeout: 50_000 })
// Retrieve the pre-provisioned preview URL — synchronous, no network call
const url = sandbox.getUrl(3000)
console.log('preview:', url)
// https://sb-abc123-va6-0-xK3mPq2nAeB-3000.sandbox-adobeioruntime.net
网络政策
沙盒是默认的 除非明确允许,否则所有出入境交通均被封锁.
传出 a policy.network.egress 数组在创建时允许将输出的终点、路径或HTTP动词列出.
const sandbox = await Sandbox.create({
name: 'policy-sandbox',
maxLifetime: 300,
policy: {
network: {
egress: [
{ host: 'httpbin.org', port: 443 },
{
host: 'api.github.com',
port: 443,
rules: [
{ methods: ['GET'], pathPattern: '/repos/**' }
]
}
]
}
}
})
发展
安装开发依赖性 :
npm install
要运行 CI 所用的相同检查 :
npm test
Linting由ESLint提供动力:
npm run lint
npm run lint-fix