@b3os/sdk 0.2.0 → 0.2.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/dist/cjs/core/sse.d.ts +2 -0
- package/dist/cjs/core/sse.js +17 -5
- package/dist/cjs/core/sse.js.map +1 -1
- package/dist/cjs/core/util.d.ts +1 -1
- package/dist/cjs/core/util.js +1 -1
- package/dist/cjs/generated/client.d.ts +13 -0
- package/dist/cjs/generated/client.js +25 -0
- package/dist/cjs/generated/client.js.map +1 -1
- package/dist/cjs/generated/errors.d.ts +1 -1
- package/dist/cjs/generated/errors.js +95 -0
- package/dist/cjs/generated/errors.js.map +1 -1
- package/dist/cjs/generated/ids.d.ts +61 -0
- package/dist/cjs/generated/ids.js +15 -0
- package/dist/cjs/generated/ids.js.map +1 -1
- package/dist/cjs/generated/jobs.js +4 -0
- package/dist/cjs/generated/jobs.js.map +1 -1
- package/dist/cjs/generated/ns/actions.d.ts +303 -4
- package/dist/cjs/generated/ns/actions.js +446 -4
- package/dist/cjs/generated/ns/actions.js.map +1 -1
- package/dist/cjs/generated/ns/agents.d.ts +229 -0
- package/dist/cjs/generated/ns/agents.js +402 -0
- package/dist/cjs/generated/ns/agents.js.map +1 -0
- package/dist/cjs/generated/ns/images.d.ts +81 -0
- package/dist/cjs/generated/ns/images.js +122 -0
- package/dist/cjs/generated/ns/images.js.map +1 -0
- package/dist/cjs/generated/ns/infer.d.ts +71 -3
- package/dist/cjs/generated/ns/infer.js +98 -3
- package/dist/cjs/generated/ns/infer.js.map +1 -1
- package/dist/cjs/generated/ns/keys.d.ts +3 -1
- package/dist/cjs/generated/ns/keys.js +3 -1
- package/dist/cjs/generated/ns/keys.js.map +1 -1
- package/dist/cjs/generated/ns/listeners.d.ts +159 -0
- package/dist/cjs/generated/ns/listeners.js +264 -0
- package/dist/cjs/generated/ns/listeners.js.map +1 -0
- package/dist/cjs/generated/ns/orgs.d.ts +10 -0
- package/dist/cjs/generated/ns/orgs.js +26 -1
- package/dist/cjs/generated/ns/orgs.js.map +1 -1
- package/dist/cjs/generated/ns/projects.d.ts +32 -0
- package/dist/cjs/generated/ns/projects.js +66 -0
- package/dist/cjs/generated/ns/projects.js.map +1 -1
- package/dist/cjs/generated/ns/render.d.ts +192 -21
- package/dist/cjs/generated/ns/render.js +267 -24
- package/dist/cjs/generated/ns/render.js.map +1 -1
- package/dist/cjs/generated/ns/requests.d.ts +3 -1
- package/dist/cjs/generated/ns/requests.js +3 -1
- package/dist/cjs/generated/ns/requests.js.map +1 -1
- package/dist/cjs/generated/ns/routines.d.ts +11 -4
- package/dist/cjs/generated/ns/routines.js +11 -4
- package/dist/cjs/generated/ns/routines.js.map +1 -1
- package/dist/cjs/generated/ns/sandboxes.d.ts +292 -7
- package/dist/cjs/generated/ns/sandboxes.js +444 -6
- package/dist/cjs/generated/ns/sandboxes.js.map +1 -1
- package/dist/cjs/generated/ns/usage.d.ts +1 -1
- package/dist/cjs/generated/ns/usage.js +1 -1
- package/dist/cjs/generated/ns/vision.d.ts +59 -17
- package/dist/cjs/generated/ns/vision.js +59 -21
- package/dist/cjs/generated/ns/vision.js.map +1 -1
- package/dist/cjs/generated/types.d.ts +2787 -85
- package/dist/cjs/helpers/sandboxes.d.ts +46 -3
- package/dist/cjs/helpers/sandboxes.js +127 -14
- package/dist/cjs/helpers/sandboxes.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/core/sse.d.ts +2 -0
- package/dist/esm/core/sse.js +17 -5
- package/dist/esm/core/sse.js.map +1 -1
- package/dist/esm/core/util.d.ts +1 -1
- package/dist/esm/core/util.js +1 -1
- package/dist/esm/generated/client.d.ts +13 -0
- package/dist/esm/generated/client.js +25 -0
- package/dist/esm/generated/client.js.map +1 -1
- package/dist/esm/generated/errors.d.ts +1 -1
- package/dist/esm/generated/errors.js +95 -0
- package/dist/esm/generated/errors.js.map +1 -1
- package/dist/esm/generated/ids.d.ts +61 -0
- package/dist/esm/generated/ids.js +15 -0
- package/dist/esm/generated/ids.js.map +1 -1
- package/dist/esm/generated/jobs.js +4 -0
- package/dist/esm/generated/jobs.js.map +1 -1
- package/dist/esm/generated/ns/actions.d.ts +303 -4
- package/dist/esm/generated/ns/actions.js +446 -4
- package/dist/esm/generated/ns/actions.js.map +1 -1
- package/dist/esm/generated/ns/agents.d.ts +229 -0
- package/dist/esm/generated/ns/agents.js +398 -0
- package/dist/esm/generated/ns/agents.js.map +1 -0
- package/dist/esm/generated/ns/images.d.ts +81 -0
- package/dist/esm/generated/ns/images.js +118 -0
- package/dist/esm/generated/ns/images.js.map +1 -0
- package/dist/esm/generated/ns/infer.d.ts +71 -3
- package/dist/esm/generated/ns/infer.js +98 -3
- package/dist/esm/generated/ns/infer.js.map +1 -1
- package/dist/esm/generated/ns/keys.d.ts +3 -1
- package/dist/esm/generated/ns/keys.js +3 -1
- package/dist/esm/generated/ns/keys.js.map +1 -1
- package/dist/esm/generated/ns/listeners.d.ts +159 -0
- package/dist/esm/generated/ns/listeners.js +260 -0
- package/dist/esm/generated/ns/listeners.js.map +1 -0
- package/dist/esm/generated/ns/orgs.d.ts +10 -0
- package/dist/esm/generated/ns/orgs.js +26 -1
- package/dist/esm/generated/ns/orgs.js.map +1 -1
- package/dist/esm/generated/ns/projects.d.ts +32 -0
- package/dist/esm/generated/ns/projects.js +66 -0
- package/dist/esm/generated/ns/projects.js.map +1 -1
- package/dist/esm/generated/ns/render.d.ts +192 -21
- package/dist/esm/generated/ns/render.js +267 -24
- package/dist/esm/generated/ns/render.js.map +1 -1
- package/dist/esm/generated/ns/requests.d.ts +3 -1
- package/dist/esm/generated/ns/requests.js +3 -1
- package/dist/esm/generated/ns/requests.js.map +1 -1
- package/dist/esm/generated/ns/routines.d.ts +11 -4
- package/dist/esm/generated/ns/routines.js +11 -4
- package/dist/esm/generated/ns/routines.js.map +1 -1
- package/dist/esm/generated/ns/sandboxes.d.ts +292 -7
- package/dist/esm/generated/ns/sandboxes.js +444 -6
- package/dist/esm/generated/ns/sandboxes.js.map +1 -1
- package/dist/esm/generated/ns/usage.d.ts +1 -1
- package/dist/esm/generated/ns/usage.js +1 -1
- package/dist/esm/generated/ns/vision.d.ts +59 -17
- package/dist/esm/generated/ns/vision.js +59 -21
- package/dist/esm/generated/ns/vision.js.map +1 -1
- package/dist/esm/generated/types.d.ts +2787 -85
- package/dist/esm/helpers/sandboxes.d.ts +46 -3
- package/dist/esm/helpers/sandboxes.js +126 -13
- package/dist/esm/helpers/sandboxes.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/sse.ts +17 -5
- package/src/core/util.ts +1 -1
- package/src/generated/client.ts +28 -0
- package/src/generated/errors.ts +190 -0
- package/src/generated/ids.ts +15 -0
- package/src/generated/jobs.ts +4 -0
- package/src/generated/ns/actions.ts +483 -4
- package/src/generated/ns/agents.ts +461 -0
- package/src/generated/ns/images.ts +128 -0
- package/src/generated/ns/infer.ts +100 -3
- package/src/generated/ns/keys.ts +3 -1
- package/src/generated/ns/listeners.ts +296 -0
- package/src/generated/ns/orgs.ts +27 -1
- package/src/generated/ns/projects.ts +74 -0
- package/src/generated/ns/render.ts +277 -24
- package/src/generated/ns/requests.ts +3 -1
- package/src/generated/ns/routines.ts +11 -4
- package/src/generated/ns/sandboxes.ts +492 -7
- package/src/generated/ns/usage.ts +1 -1
- package/src/generated/ns/vision.ts +59 -21
- package/src/generated/types.ts +3511 -575
- package/src/helpers/sandboxes.ts +167 -14
- package/src/index.ts +1 -0
package/src/helpers/sandboxes.ts
CHANGED
|
@@ -275,6 +275,12 @@ export interface ServiceOptions extends ReadyOptions {
|
|
|
275
275
|
previewPort?: number;
|
|
276
276
|
/** Lifetime of a private preview URL, in seconds. */
|
|
277
277
|
previewTtlS?: number;
|
|
278
|
+
/**
|
|
279
|
+
* The node starts the command again, in the same sandbox, when it exits: `on-failure` after a non-zero code or a
|
|
280
|
+
* signal, `always` after any exit (backoff 0 s to 60 s; `crash_loop` after 5 exits in 5 minutes). `stop()` ends
|
|
281
|
+
* the supervision. Left out, the process is not supervised.
|
|
282
|
+
*/
|
|
283
|
+
restart?: "on-failure" | "always";
|
|
278
284
|
}
|
|
279
285
|
|
|
280
286
|
export interface Service {
|
|
@@ -290,7 +296,7 @@ export interface Service {
|
|
|
290
296
|
|
|
291
297
|
/** Starts a background process (a server, a worker) and waits until it is ready. */
|
|
292
298
|
export async function startService(api: SandboxesAPI, sandboxId: string, options: ServiceOptions): Promise<Service> {
|
|
293
|
-
const { cmd, cwd, env, user, ready, previewPort, previewTtlS, ...waitOptions } = options;
|
|
299
|
+
const { cmd, cwd, env, user, ready, previewPort, previewTtlS, restart, ...waitOptions } = options;
|
|
294
300
|
const wait = requestOptions(options);
|
|
295
301
|
const t0 = performance.now();
|
|
296
302
|
const body: T.ExecRequest & { background: true } = {
|
|
@@ -299,6 +305,7 @@ export async function startService(api: SandboxesAPI, sandboxId: string, options
|
|
|
299
305
|
...(cwd ? { cwd } : {}),
|
|
300
306
|
...(env ? { env } : {}),
|
|
301
307
|
...(user ? { user } : {}),
|
|
308
|
+
...(restart ? { restart } : {}),
|
|
302
309
|
};
|
|
303
310
|
const proc = await api.exec(sandboxId, body, wait);
|
|
304
311
|
const stop = (signal: "TERM" | "KILL" | "INT" | "HUP" = "TERM") =>
|
|
@@ -471,8 +478,23 @@ export interface SandboxExecOptions extends RequestOptions {
|
|
|
471
478
|
/** Kill the command after this long (default 30 s; at most 60 s unless streamed). */
|
|
472
479
|
commandTimeoutMs?: number;
|
|
473
480
|
user?: "user" | "root";
|
|
481
|
+
/**
|
|
482
|
+
* What the result keeps of each stream, in UTF-8 bytes, never part of a character (default
|
|
483
|
+
* {@link DEFAULT_MAX_OUTPUT_BYTES}, 16 MiB; `Infinity` keeps everything, and `null` is refused). Past it a streamed
|
|
484
|
+
* call stops keeping output as it arrives while the command runs on and `onStdout`/`onStderr` still get every
|
|
485
|
+
* chunk; a buffered call gets at most the node's 1 MiB, and a smaller cap trims it here. Either way
|
|
486
|
+
* `stdoutTruncated` / `stderrTruncated` say so.
|
|
487
|
+
*/
|
|
488
|
+
maxOutputBytes?: number;
|
|
474
489
|
}
|
|
475
490
|
|
|
491
|
+
/**
|
|
492
|
+
* What `Sandbox.exec` keeps of each output stream by default: 16 MiB of stdout and 16 MiB of stderr (UTF-8 bytes).
|
|
493
|
+
* A streamed command may run for an hour, and keeping all of a chatty one (`yes`, a verbose build) would fill the
|
|
494
|
+
* caller's memory; `maxOutputBytes: Infinity` keeps everything.
|
|
495
|
+
*/
|
|
496
|
+
export const DEFAULT_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
|
|
497
|
+
|
|
476
498
|
/** A finished command. */
|
|
477
499
|
export interface SandboxExecOutput {
|
|
478
500
|
/** The exit status; `null` when a signal killed it. */
|
|
@@ -482,6 +504,13 @@ export interface SandboxExecOutput {
|
|
|
482
504
|
stdout: string;
|
|
483
505
|
stderr: string;
|
|
484
506
|
durationMs: number | null;
|
|
507
|
+
/**
|
|
508
|
+
* True when `stdout` is not all the command wrote: the node keeps at most 1 MiB of each stream on a buffered call,
|
|
509
|
+
* and `maxOutputBytes` caps what the SDK keeps on either path (a streamed call's callbacks still get every chunk).
|
|
510
|
+
*/
|
|
511
|
+
stdoutTruncated: boolean;
|
|
512
|
+
/** The same for stderr. */
|
|
513
|
+
stderrTruncated: boolean;
|
|
485
514
|
}
|
|
486
515
|
|
|
487
516
|
/** Files inside the sandbox (`/v1/sandboxes/{id}/files`, `files/stat`, `files/move`, `files/mkdir`). */
|
|
@@ -508,10 +537,17 @@ export interface SandboxFiles {
|
|
|
508
537
|
mkdir(path: string, options?: RequestOptions & { mode?: string }): Promise<T.FileEntry>;
|
|
509
538
|
}
|
|
510
539
|
|
|
511
|
-
/** Ports inside the sandbox, reached through preview URLs. */
|
|
540
|
+
/** Ports inside the sandbox, reached through preview URLs, or through a port's own host. */
|
|
512
541
|
export interface SandboxPorts {
|
|
513
542
|
/** A URL that reaches `port` (it must be declared in `ports` at create): signed, valid `ttlS` (default 600 s). */
|
|
514
543
|
url(port: number, options?: RequestOptions & { ttlS?: number }): Promise<string>;
|
|
544
|
+
/**
|
|
545
|
+
* The port's own host, `<port>-sbx-<id>.b3os.io` (`sandboxes.createPortHost`): the app at `/`, keeping its cookies,
|
|
546
|
+
* shared by link. Answers `host` (safe to show) and `url`, a link valid `ttlS` (default 600 s, at most a day) whose
|
|
547
|
+
* first visit keeps that browser in for 24 hours: share it as a secret. `rotate` ends every earlier link and cookie
|
|
548
|
+
* of this host; `cookies` sets its cookie rule. Not yet from code inside a sandbox (421 `edge_only`).
|
|
549
|
+
*/
|
|
550
|
+
host(port: number, options?: RequestOptions & SandboxPortHostOptions): Promise<T.PortHost>;
|
|
515
551
|
/**
|
|
516
552
|
* A styled screenshot of the app on `port` (render), through a fresh preview URL. `path` is appended to it;
|
|
517
553
|
* `body` takes every other screenshot field.
|
|
@@ -530,6 +566,13 @@ export interface SandboxPorts {
|
|
|
530
566
|
): Promise<T.ScrapeResult>;
|
|
531
567
|
}
|
|
532
568
|
|
|
569
|
+
/** What `ports.host` asks for: the link's lifetime, a new secret first, the host's cookie rule. */
|
|
570
|
+
export interface SandboxPortHostOptions {
|
|
571
|
+
ttlS?: number;
|
|
572
|
+
rotate?: boolean;
|
|
573
|
+
cookies?: T.AppHostCookies;
|
|
574
|
+
}
|
|
575
|
+
|
|
533
576
|
const textDecoder = new TextDecoder();
|
|
534
577
|
|
|
535
578
|
function joinPath(base: string, path: string | undefined): string {
|
|
@@ -548,10 +591,97 @@ function execBody(cmd: string | readonly string[], o: SandboxExecOptions): T.Exe
|
|
|
548
591
|
}
|
|
549
592
|
|
|
550
593
|
function execRequestOptions(o: SandboxExecOptions): RequestOptions {
|
|
551
|
-
const {
|
|
594
|
+
const {
|
|
595
|
+
onStdout: _o,
|
|
596
|
+
onStderr: _e,
|
|
597
|
+
cwd: _c,
|
|
598
|
+
env: _v,
|
|
599
|
+
stdin: _s,
|
|
600
|
+
commandTimeoutMs: _t,
|
|
601
|
+
user: _u,
|
|
602
|
+
maxOutputBytes: _m,
|
|
603
|
+
...rest
|
|
604
|
+
} = o;
|
|
552
605
|
return rest;
|
|
553
606
|
}
|
|
554
607
|
|
|
608
|
+
function capOf(maxOutputBytes: number | undefined): number {
|
|
609
|
+
// Only undefined takes the default: null (Python's None, which keeps everything) is refused, naming Infinity.
|
|
610
|
+
const cap = maxOutputBytes === undefined ? DEFAULT_MAX_OUTPUT_BYTES : maxOutputBytes;
|
|
611
|
+
if (cap !== Number.POSITIVE_INFINITY && !(Number.isInteger(cap) && cap >= 0)) {
|
|
612
|
+
throw new B3OSError({
|
|
613
|
+
code: "invalid_request",
|
|
614
|
+
status: null,
|
|
615
|
+
detail: `maxOutputBytes must be a non-negative integer or Infinity, got ${String(maxOutputBytes)}`,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
return cap;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/** UTF-8 bytes of one UTF-16 code unit at `i` of `s` (4 for a surrogate pair, 0 for its second half). */
|
|
622
|
+
function utf8Bytes(s: string, i: number): number {
|
|
623
|
+
const c = s.charCodeAt(i);
|
|
624
|
+
if (c < 0x80) return 1;
|
|
625
|
+
if (c < 0x800) return 2;
|
|
626
|
+
if (c >= 0xd800 && c <= 0xdbff) {
|
|
627
|
+
const d = s.charCodeAt(i + 1);
|
|
628
|
+
if (d >= 0xdc00 && d <= 0xdfff) return 4;
|
|
629
|
+
} else if (c >= 0xdc00 && c <= 0xdfff) {
|
|
630
|
+
const p = s.charCodeAt(i - 1);
|
|
631
|
+
if (i > 0 && p >= 0xd800 && p <= 0xdbff) return 0;
|
|
632
|
+
}
|
|
633
|
+
return 3; // the rest of the BMP; a lone surrogate is sent as U+FFFD
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** One output stream as it arrives, up to `cap` UTF-8 bytes; past the cap nothing more is kept and `cut` is set. */
|
|
637
|
+
class Kept {
|
|
638
|
+
text = "";
|
|
639
|
+
size = 0;
|
|
640
|
+
cut = false;
|
|
641
|
+
readonly cap: number;
|
|
642
|
+
constructor(cap: number) {
|
|
643
|
+
this.cap = cap;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
add(data: string): void {
|
|
647
|
+
if (this.cut || data.length === 0) return;
|
|
648
|
+
if (this.cap === Number.POSITIVE_INFINITY) {
|
|
649
|
+
this.text += data;
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
// At most 3 bytes a code unit: a chunk that fits even then needs no counting.
|
|
653
|
+
const room = this.cap - this.size;
|
|
654
|
+
if (data.length * 3 <= room) {
|
|
655
|
+
this.text += data;
|
|
656
|
+
this.size += utf8Length(data);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
let n = 0;
|
|
660
|
+
for (let i = 0; i < data.length; i++) {
|
|
661
|
+
const b = utf8Bytes(data, i);
|
|
662
|
+
if (n + b > room) {
|
|
663
|
+
// Never half a surrogate pair: `b` was 4 at its first half, so the cut falls before it. A pair sent in two
|
|
664
|
+
// events, cut between its halves, leaves its first half at the end: drop it.
|
|
665
|
+
this.text += data.slice(0, i);
|
|
666
|
+
const last = this.text.charCodeAt(this.text.length - 1);
|
|
667
|
+
if (last >= 0xd800 && last <= 0xdbff) this.text = this.text.slice(0, -1);
|
|
668
|
+
this.size = this.cap;
|
|
669
|
+
this.cut = true;
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
n += b;
|
|
673
|
+
}
|
|
674
|
+
this.text += data;
|
|
675
|
+
this.size += n;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function utf8Length(s: string): number {
|
|
680
|
+
let n = 0;
|
|
681
|
+
for (let i = 0; i < s.length; i++) n += utf8Bytes(s, i);
|
|
682
|
+
return n;
|
|
683
|
+
}
|
|
684
|
+
|
|
555
685
|
/**
|
|
556
686
|
* A sandbox you hold: every operation of `b3os.sandboxes` bound to one id, plus `files`, `ports` and disposal. The
|
|
557
687
|
* API's sandbox object (what `createSandbox` and `getSandbox` return) is the type `SandboxInfo`, in `sbx.info`.
|
|
@@ -618,6 +748,14 @@ export class Sandbox implements AsyncDisposable {
|
|
|
618
748
|
};
|
|
619
749
|
this.ports = {
|
|
620
750
|
url,
|
|
751
|
+
host: (port, options = {}) => {
|
|
752
|
+
const { ttlS, rotate, cookies, ...o } = options;
|
|
753
|
+
const body: T.PortHostRequest = {};
|
|
754
|
+
if (ttlS !== undefined) body.ttl_s = ttlS;
|
|
755
|
+
if (rotate !== undefined) body.rotate = rotate;
|
|
756
|
+
if (cookies !== undefined) body.cookies = cookies;
|
|
757
|
+
return api.createPortHost(id, port, body, o).then((h) => h);
|
|
758
|
+
},
|
|
621
759
|
screenshot: async (port, body = {}, options) => {
|
|
622
760
|
const render = this.#need("render", client.render);
|
|
623
761
|
const { path, ...rest } = body;
|
|
@@ -652,32 +790,43 @@ export class Sandbox implements AsyncDisposable {
|
|
|
652
790
|
}
|
|
653
791
|
|
|
654
792
|
/**
|
|
655
|
-
* Run a command and wait for it. With `onStdout` or `onStderr` the output streams to them as it arrives;
|
|
656
|
-
*
|
|
793
|
+
* Run a command and wait for it. With `onStdout` or `onStderr` the output streams to them as it arrives; the
|
|
794
|
+
* result keeps up to `maxOutputBytes` of each stream (16 MiB by default). A string runs through `/bin/sh -c`, an
|
|
795
|
+
* array is executed directly. An error from a streamed call (`stream_interrupted`, `stream_ended`, `timeout`, a
|
|
796
|
+
* cut the node reports in `exec.exit`, `aborted` once the answer began) carries the call's `requestId`, as an
|
|
797
|
+
* error answer does.
|
|
657
798
|
*/
|
|
658
799
|
async exec(cmd: string | readonly string[], options: SandboxExecOptions = {}): Promise<SandboxExecOutput> {
|
|
659
800
|
const api = this.#client.sandboxes;
|
|
801
|
+
const cap = capOf(options.maxOutputBytes);
|
|
660
802
|
const body = execBody(cmd, options);
|
|
661
803
|
const o = execRequestOptions(options);
|
|
804
|
+
const stdout = new Kept(cap);
|
|
805
|
+
const stderr = new Kept(cap);
|
|
662
806
|
if (!options.onStdout && !options.onStderr) {
|
|
663
807
|
const r = await api.exec(this.id, body, o);
|
|
808
|
+
// The node keeps at most 1 MiB of each stream here; a smaller cap trims it.
|
|
809
|
+
stdout.add(r.stdout ?? "");
|
|
810
|
+
stderr.add(r.stderr ?? "");
|
|
664
811
|
return {
|
|
665
812
|
exitCode: r.exit_code,
|
|
666
813
|
signal: r.signal ?? null,
|
|
667
814
|
timedOut: r.timed_out ?? false,
|
|
668
|
-
stdout:
|
|
669
|
-
stderr:
|
|
815
|
+
stdout: stdout.text,
|
|
816
|
+
stderr: stderr.text,
|
|
670
817
|
durationMs: r.duration_ms ?? null,
|
|
818
|
+
stdoutTruncated: (r.stdout_truncated ?? false) || stdout.cut,
|
|
819
|
+
stderrTruncated: (r.stderr_truncated ?? false) || stderr.cut,
|
|
671
820
|
};
|
|
672
821
|
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
for await (const ev of
|
|
822
|
+
// Errors from the stream carry its request id (`stream.requestId`), as an error answer does.
|
|
823
|
+
const stream = api.exec(this.id, body, { ...o, stream: true });
|
|
824
|
+
for await (const ev of stream) {
|
|
676
825
|
if (ev.type === "exec.stdout" && typeof ev.data === "string") {
|
|
677
|
-
stdout
|
|
826
|
+
stdout.add(ev.data);
|
|
678
827
|
options.onStdout?.(ev.data);
|
|
679
828
|
} else if (ev.type === "exec.stderr" && typeof ev.data === "string") {
|
|
680
|
-
stderr
|
|
829
|
+
stderr.add(ev.data);
|
|
681
830
|
options.onStderr?.(ev.data);
|
|
682
831
|
} else if (ev.type === "exec.exit") {
|
|
683
832
|
const err = (ev as { error?: { code?: string; reason?: string; detail?: string } }).error;
|
|
@@ -686,15 +835,18 @@ export class Sandbox implements AsyncDisposable {
|
|
|
686
835
|
code: err.code,
|
|
687
836
|
status: null,
|
|
688
837
|
detail: err.detail ?? `the command in ${this.id} was cut (${err.reason ?? err.code})`,
|
|
838
|
+
requestId: stream.requestId,
|
|
689
839
|
});
|
|
690
840
|
}
|
|
691
841
|
return {
|
|
692
842
|
exitCode: ev.exit_code ?? null,
|
|
693
843
|
signal: ev.signal ?? null,
|
|
694
844
|
timedOut: ev.timed_out ?? false,
|
|
695
|
-
stdout,
|
|
696
|
-
stderr,
|
|
845
|
+
stdout: stdout.text,
|
|
846
|
+
stderr: stderr.text,
|
|
697
847
|
durationMs: ev.duration_ms ?? null,
|
|
848
|
+
stdoutTruncated: stdout.cut,
|
|
849
|
+
stderrTruncated: stderr.cut,
|
|
698
850
|
};
|
|
699
851
|
}
|
|
700
852
|
}
|
|
@@ -702,6 +854,7 @@ export class Sandbox implements AsyncDisposable {
|
|
|
702
854
|
code: "stream_ended",
|
|
703
855
|
status: null,
|
|
704
856
|
detail: `the exec stream of ${this.id} ended before the command exited`,
|
|
857
|
+
requestId: stream.requestId,
|
|
705
858
|
});
|
|
706
859
|
}
|
|
707
860
|
|