@computesdk/sandbox0 1.0.1 → 1.0.2
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/dist/index.mjs
CHANGED
|
@@ -22617,6 +22617,85 @@ function commandTimeoutSeconds(value) {
|
|
|
22617
22617
|
}
|
|
22618
22618
|
return Math.max(1, Math.ceil(value / 1e3));
|
|
22619
22619
|
}
|
|
22620
|
+
var commandPollIntervalMs = 100;
|
|
22621
|
+
var Sandbox0CommandTimeoutError = class extends Error {
|
|
22622
|
+
constructor(timeout) {
|
|
22623
|
+
super(`Sandbox0 command timed out after ${timeout}ms.`);
|
|
22624
|
+
this.name = "TimeoutError";
|
|
22625
|
+
}
|
|
22626
|
+
};
|
|
22627
|
+
async function withinCommandDeadline(operation, deadline, timeout) {
|
|
22628
|
+
if (deadline === void 0 || timeout === void 0) return operation;
|
|
22629
|
+
const remaining = deadline - Date.now();
|
|
22630
|
+
if (remaining <= 0) throw new Sandbox0CommandTimeoutError(timeout);
|
|
22631
|
+
let timer;
|
|
22632
|
+
try {
|
|
22633
|
+
return await Promise.race([
|
|
22634
|
+
operation,
|
|
22635
|
+
new Promise((_, reject) => {
|
|
22636
|
+
timer = setTimeout(
|
|
22637
|
+
() => reject(new Sandbox0CommandTimeoutError(timeout)),
|
|
22638
|
+
remaining
|
|
22639
|
+
);
|
|
22640
|
+
})
|
|
22641
|
+
]);
|
|
22642
|
+
} finally {
|
|
22643
|
+
if (timer) clearTimeout(timer);
|
|
22644
|
+
}
|
|
22645
|
+
}
|
|
22646
|
+
async function captureContextOutput(stream, output) {
|
|
22647
|
+
for await (const chunk of stream.outputs()) {
|
|
22648
|
+
if (chunk.source === "stderr") {
|
|
22649
|
+
output.stderr += chunk.data;
|
|
22650
|
+
} else {
|
|
22651
|
+
output.stdout += chunk.data;
|
|
22652
|
+
}
|
|
22653
|
+
}
|
|
22654
|
+
}
|
|
22655
|
+
async function observeCommandCompletion(sandbox, contextId, deadline, timeout) {
|
|
22656
|
+
const output = { stdout: "", stderr: "" };
|
|
22657
|
+
let terminal;
|
|
22658
|
+
let stream;
|
|
22659
|
+
try {
|
|
22660
|
+
stream = await withinCommandDeadline(
|
|
22661
|
+
sandbox.connectWsContext(contextId),
|
|
22662
|
+
deadline,
|
|
22663
|
+
timeout
|
|
22664
|
+
);
|
|
22665
|
+
const outputTask = captureContextOutput(stream, output).catch(() => void 0);
|
|
22666
|
+
terminal = await withinCommandDeadline(stream.wait(), deadline, timeout);
|
|
22667
|
+
await outputTask;
|
|
22668
|
+
} catch (error) {
|
|
22669
|
+
if (error instanceof Sandbox0CommandTimeoutError) throw error;
|
|
22670
|
+
} finally {
|
|
22671
|
+
stream?.close();
|
|
22672
|
+
}
|
|
22673
|
+
let context;
|
|
22674
|
+
try {
|
|
22675
|
+
context = await withinCommandDeadline(
|
|
22676
|
+
sandbox.getContext(contextId),
|
|
22677
|
+
deadline,
|
|
22678
|
+
timeout
|
|
22679
|
+
);
|
|
22680
|
+
while (context.running && terminal?.exitCode === void 0) {
|
|
22681
|
+
const delay = deadline === void 0 ? commandPollIntervalMs : Math.min(commandPollIntervalMs, Math.max(1, deadline - Date.now()));
|
|
22682
|
+
await sleep(delay);
|
|
22683
|
+
context = await withinCommandDeadline(
|
|
22684
|
+
sandbox.getContext(contextId),
|
|
22685
|
+
deadline,
|
|
22686
|
+
timeout
|
|
22687
|
+
);
|
|
22688
|
+
}
|
|
22689
|
+
} catch (error) {
|
|
22690
|
+
if (error instanceof Sandbox0CommandTimeoutError || terminal?.exitCode === void 0) {
|
|
22691
|
+
throw error;
|
|
22692
|
+
}
|
|
22693
|
+
}
|
|
22694
|
+
return { context, terminal, output };
|
|
22695
|
+
}
|
|
22696
|
+
function deleteContextBestEffort(sandbox, contextId) {
|
|
22697
|
+
void sandbox.deleteContext(contextId).catch(() => void 0);
|
|
22698
|
+
}
|
|
22620
22699
|
async function createSandbox(config, options) {
|
|
22621
22700
|
throwIfAborted2(options?.signal);
|
|
22622
22701
|
const client = createClient(config);
|
|
@@ -22690,19 +22769,40 @@ var _provider = defineProvider({
|
|
|
22690
22769
|
runCommand: async (sandbox, command, options) => {
|
|
22691
22770
|
const startedAt = Date.now();
|
|
22692
22771
|
const timeout = options?.timeout ?? observations.get(sandbox)?.commandTimeout;
|
|
22693
|
-
const
|
|
22772
|
+
const ttlSec = commandTimeoutSeconds(timeout);
|
|
22773
|
+
const deadline = timeout !== void 0 && timeout > 0 ? startedAt + timeout : void 0;
|
|
22774
|
+
const started = await sandbox.cmd(command, {
|
|
22694
22775
|
command: ["sh", "-lc", command],
|
|
22695
|
-
wait:
|
|
22776
|
+
wait: false,
|
|
22696
22777
|
cwd: options?.cwd,
|
|
22697
22778
|
envVars: options?.env,
|
|
22698
|
-
ttlSec
|
|
22779
|
+
ttlSec
|
|
22699
22780
|
});
|
|
22700
|
-
|
|
22701
|
-
|
|
22702
|
-
|
|
22703
|
-
|
|
22704
|
-
|
|
22705
|
-
|
|
22781
|
+
if (options?.background) {
|
|
22782
|
+
return {
|
|
22783
|
+
stdout: started.stdout || "",
|
|
22784
|
+
stderr: started.stderr || "",
|
|
22785
|
+
exitCode: 0,
|
|
22786
|
+
durationMs: Date.now() - startedAt
|
|
22787
|
+
};
|
|
22788
|
+
}
|
|
22789
|
+
try {
|
|
22790
|
+
const completed = await observeCommandCompletion(
|
|
22791
|
+
sandbox,
|
|
22792
|
+
started.contextId,
|
|
22793
|
+
deadline,
|
|
22794
|
+
timeout
|
|
22795
|
+
);
|
|
22796
|
+
return {
|
|
22797
|
+
stdout: completed.context?.stdout ?? completed.output.stdout,
|
|
22798
|
+
stderr: completed.context?.stderr ?? completed.output.stderr,
|
|
22799
|
+
exitCode: completed.context?.exitCode ?? completed.terminal?.exitCode ?? 1,
|
|
22800
|
+
durationMs: Date.now() - startedAt
|
|
22801
|
+
};
|
|
22802
|
+
} catch (error) {
|
|
22803
|
+
deleteContextBestEffort(sandbox, started.contextId);
|
|
22804
|
+
throw error;
|
|
22805
|
+
}
|
|
22706
22806
|
},
|
|
22707
22807
|
getInfo: async (sandbox) => {
|
|
22708
22808
|
const observation = observations.get(sandbox);
|