@computesdk/sandbox0 1.0.1 → 1.0.3
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 +6 -0
- package/dist/index.js +109 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +109 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -78,6 +78,12 @@ Per-create `templateId`, `snapshotId`, `memory`, `envs`, `ttl`, `hardTtl`, and `
|
|
|
78
78
|
| Restore from snapshot | Yes, with `snapshotId` at create time |
|
|
79
79
|
| Snapshot management | Use the native Sandbox0 SDK |
|
|
80
80
|
|
|
81
|
+
Foreground `runCommand` calls start an asynchronous Sandbox0 Context, follow
|
|
82
|
+
its WebSocket terminal event, and read the final result from the Context API.
|
|
83
|
+
If the WebSocket disconnects, the provider polls the Context until it exits.
|
|
84
|
+
The command `timeout` is also applied as the Context TTL, and a timed-out call
|
|
85
|
+
attempts to delete the Context.
|
|
86
|
+
|
|
81
87
|
`destroy` is idempotent and retries short-lived throttling or server failures. Setting `hardTtl` is still recommended for automated workloads so a failed client cannot leave a sandbox indefinitely.
|
|
82
88
|
|
|
83
89
|
For Sandbox0-specific capabilities such as pause/resume, services, snapshots, volumes, and observability, call `sandbox.getInstance()` to access the native `sandbox0` SDK handle.
|
package/dist/index.js
CHANGED
|
@@ -22637,6 +22637,85 @@ function commandTimeoutSeconds(value) {
|
|
|
22637
22637
|
}
|
|
22638
22638
|
return Math.max(1, Math.ceil(value / 1e3));
|
|
22639
22639
|
}
|
|
22640
|
+
var commandPollIntervalMs = 100;
|
|
22641
|
+
var Sandbox0CommandTimeoutError = class extends Error {
|
|
22642
|
+
constructor(timeout) {
|
|
22643
|
+
super(`Sandbox0 command timed out after ${timeout}ms.`);
|
|
22644
|
+
this.name = "TimeoutError";
|
|
22645
|
+
}
|
|
22646
|
+
};
|
|
22647
|
+
async function withinCommandDeadline(operation, deadline, timeout) {
|
|
22648
|
+
if (deadline === void 0 || timeout === void 0) return operation;
|
|
22649
|
+
const remaining = deadline - Date.now();
|
|
22650
|
+
if (remaining <= 0) throw new Sandbox0CommandTimeoutError(timeout);
|
|
22651
|
+
let timer;
|
|
22652
|
+
try {
|
|
22653
|
+
return await Promise.race([
|
|
22654
|
+
operation,
|
|
22655
|
+
new Promise((_, reject) => {
|
|
22656
|
+
timer = setTimeout(
|
|
22657
|
+
() => reject(new Sandbox0CommandTimeoutError(timeout)),
|
|
22658
|
+
remaining
|
|
22659
|
+
);
|
|
22660
|
+
})
|
|
22661
|
+
]);
|
|
22662
|
+
} finally {
|
|
22663
|
+
if (timer) clearTimeout(timer);
|
|
22664
|
+
}
|
|
22665
|
+
}
|
|
22666
|
+
async function captureContextOutput(stream, output) {
|
|
22667
|
+
for await (const chunk of stream.outputs()) {
|
|
22668
|
+
if (chunk.source === "stderr") {
|
|
22669
|
+
output.stderr += chunk.data;
|
|
22670
|
+
} else {
|
|
22671
|
+
output.stdout += chunk.data;
|
|
22672
|
+
}
|
|
22673
|
+
}
|
|
22674
|
+
}
|
|
22675
|
+
async function observeCommandCompletion(sandbox, contextId, deadline, timeout) {
|
|
22676
|
+
const output = { stdout: "", stderr: "" };
|
|
22677
|
+
let terminal;
|
|
22678
|
+
let stream;
|
|
22679
|
+
try {
|
|
22680
|
+
stream = await withinCommandDeadline(
|
|
22681
|
+
sandbox.connectWsContext(contextId),
|
|
22682
|
+
deadline,
|
|
22683
|
+
timeout
|
|
22684
|
+
);
|
|
22685
|
+
const outputTask = captureContextOutput(stream, output).catch(() => void 0);
|
|
22686
|
+
terminal = await withinCommandDeadline(stream.wait(), deadline, timeout);
|
|
22687
|
+
await outputTask;
|
|
22688
|
+
} catch (error) {
|
|
22689
|
+
if (error instanceof Sandbox0CommandTimeoutError) throw error;
|
|
22690
|
+
} finally {
|
|
22691
|
+
stream?.close();
|
|
22692
|
+
}
|
|
22693
|
+
let context;
|
|
22694
|
+
try {
|
|
22695
|
+
context = await withinCommandDeadline(
|
|
22696
|
+
sandbox.getContext(contextId),
|
|
22697
|
+
deadline,
|
|
22698
|
+
timeout
|
|
22699
|
+
);
|
|
22700
|
+
while (context.running && terminal?.exitCode === void 0) {
|
|
22701
|
+
const delay = deadline === void 0 ? commandPollIntervalMs : Math.min(commandPollIntervalMs, Math.max(1, deadline - Date.now()));
|
|
22702
|
+
await sleep(delay);
|
|
22703
|
+
context = await withinCommandDeadline(
|
|
22704
|
+
sandbox.getContext(contextId),
|
|
22705
|
+
deadline,
|
|
22706
|
+
timeout
|
|
22707
|
+
);
|
|
22708
|
+
}
|
|
22709
|
+
} catch (error) {
|
|
22710
|
+
if (error instanceof Sandbox0CommandTimeoutError || terminal?.exitCode === void 0) {
|
|
22711
|
+
throw error;
|
|
22712
|
+
}
|
|
22713
|
+
}
|
|
22714
|
+
return { context, terminal, output };
|
|
22715
|
+
}
|
|
22716
|
+
function deleteContextBestEffort(sandbox, contextId) {
|
|
22717
|
+
void sandbox.deleteContext(contextId).catch(() => void 0);
|
|
22718
|
+
}
|
|
22640
22719
|
async function createSandbox(config, options) {
|
|
22641
22720
|
throwIfAborted2(options?.signal);
|
|
22642
22721
|
const client = createClient(config);
|
|
@@ -22710,19 +22789,40 @@ var _provider = (0, import_provider.defineProvider)({
|
|
|
22710
22789
|
runCommand: async (sandbox, command, options) => {
|
|
22711
22790
|
const startedAt = Date.now();
|
|
22712
22791
|
const timeout = options?.timeout ?? observations.get(sandbox)?.commandTimeout;
|
|
22713
|
-
const
|
|
22792
|
+
const ttlSec = commandTimeoutSeconds(timeout);
|
|
22793
|
+
const deadline = timeout !== void 0 && timeout > 0 ? startedAt + timeout : void 0;
|
|
22794
|
+
const started = await sandbox.cmd(command, {
|
|
22714
22795
|
command: ["sh", "-lc", command],
|
|
22715
|
-
wait:
|
|
22796
|
+
wait: false,
|
|
22716
22797
|
cwd: options?.cwd,
|
|
22717
22798
|
envVars: options?.env,
|
|
22718
|
-
ttlSec
|
|
22799
|
+
ttlSec
|
|
22719
22800
|
});
|
|
22720
|
-
|
|
22721
|
-
|
|
22722
|
-
|
|
22723
|
-
|
|
22724
|
-
|
|
22725
|
-
|
|
22801
|
+
if (options?.background) {
|
|
22802
|
+
return {
|
|
22803
|
+
stdout: started.stdout || "",
|
|
22804
|
+
stderr: started.stderr || "",
|
|
22805
|
+
exitCode: 0,
|
|
22806
|
+
durationMs: Date.now() - startedAt
|
|
22807
|
+
};
|
|
22808
|
+
}
|
|
22809
|
+
try {
|
|
22810
|
+
const completed = await observeCommandCompletion(
|
|
22811
|
+
sandbox,
|
|
22812
|
+
started.contextId,
|
|
22813
|
+
deadline,
|
|
22814
|
+
timeout
|
|
22815
|
+
);
|
|
22816
|
+
return {
|
|
22817
|
+
stdout: completed.context?.stdout ?? completed.output.stdout,
|
|
22818
|
+
stderr: completed.context?.stderr ?? completed.output.stderr,
|
|
22819
|
+
exitCode: completed.context?.exitCode ?? completed.terminal?.exitCode ?? 1,
|
|
22820
|
+
durationMs: Date.now() - startedAt
|
|
22821
|
+
};
|
|
22822
|
+
} catch (error) {
|
|
22823
|
+
deleteContextBestEffort(sandbox, started.contextId);
|
|
22824
|
+
throw error;
|
|
22825
|
+
}
|
|
22726
22826
|
},
|
|
22727
22827
|
getInfo: async (sandbox) => {
|
|
22728
22828
|
const observation = observations.get(sandbox);
|