@b3os/sdk 0.2.0 → 0.2.1
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/errors.d.ts +1 -1
- package/dist/cjs/generated/errors.js +20 -0
- package/dist/cjs/generated/errors.js.map +1 -1
- package/dist/cjs/generated/ns/actions.d.ts +66 -1
- package/dist/cjs/generated/ns/actions.js +101 -1
- package/dist/cjs/generated/ns/actions.js.map +1 -1
- package/dist/cjs/generated/types.d.ts +218 -12
- package/dist/cjs/helpers/sandboxes.d.ts +26 -2
- package/dist/cjs/helpers/sandboxes.js +114 -13
- 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/errors.d.ts +1 -1
- package/dist/esm/generated/errors.js +20 -0
- package/dist/esm/generated/errors.js.map +1 -1
- package/dist/esm/generated/ns/actions.d.ts +66 -1
- package/dist/esm/generated/ns/actions.js +101 -1
- package/dist/esm/generated/ns/actions.js.map +1 -1
- package/dist/esm/generated/types.d.ts +218 -12
- package/dist/esm/helpers/sandboxes.d.ts +26 -2
- package/dist/esm/helpers/sandboxes.js +113 -12
- 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/errors.ts +40 -0
- package/src/generated/ns/actions.ts +110 -1
- package/src/generated/types.ts +232 -11
- package/src/helpers/sandboxes.ts +136 -12
- package/src/index.ts +1 -0
package/src/helpers/sandboxes.ts
CHANGED
|
@@ -471,8 +471,23 @@ export interface SandboxExecOptions extends RequestOptions {
|
|
|
471
471
|
/** Kill the command after this long (default 30 s; at most 60 s unless streamed). */
|
|
472
472
|
commandTimeoutMs?: number;
|
|
473
473
|
user?: "user" | "root";
|
|
474
|
+
/**
|
|
475
|
+
* What the result keeps of each stream, in UTF-8 bytes, never part of a character (default
|
|
476
|
+
* {@link DEFAULT_MAX_OUTPUT_BYTES}, 16 MiB; `Infinity` keeps everything, and `null` is refused). Past it a streamed
|
|
477
|
+
* call stops keeping output as it arrives while the command runs on and `onStdout`/`onStderr` still get every
|
|
478
|
+
* chunk; a buffered call gets at most the node's 1 MiB, and a smaller cap trims it here. Either way
|
|
479
|
+
* `stdoutTruncated` / `stderrTruncated` say so.
|
|
480
|
+
*/
|
|
481
|
+
maxOutputBytes?: number;
|
|
474
482
|
}
|
|
475
483
|
|
|
484
|
+
/**
|
|
485
|
+
* What `Sandbox.exec` keeps of each output stream by default: 16 MiB of stdout and 16 MiB of stderr (UTF-8 bytes).
|
|
486
|
+
* A streamed command may run for an hour, and keeping all of a chatty one (`yes`, a verbose build) would fill the
|
|
487
|
+
* caller's memory; `maxOutputBytes: Infinity` keeps everything.
|
|
488
|
+
*/
|
|
489
|
+
export const DEFAULT_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
|
|
490
|
+
|
|
476
491
|
/** A finished command. */
|
|
477
492
|
export interface SandboxExecOutput {
|
|
478
493
|
/** The exit status; `null` when a signal killed it. */
|
|
@@ -482,6 +497,13 @@ export interface SandboxExecOutput {
|
|
|
482
497
|
stdout: string;
|
|
483
498
|
stderr: string;
|
|
484
499
|
durationMs: number | null;
|
|
500
|
+
/**
|
|
501
|
+
* True when `stdout` is not all the command wrote: the node keeps at most 1 MiB of each stream on a buffered call,
|
|
502
|
+
* and `maxOutputBytes` caps what the SDK keeps on either path (a streamed call's callbacks still get every chunk).
|
|
503
|
+
*/
|
|
504
|
+
stdoutTruncated: boolean;
|
|
505
|
+
/** The same for stderr. */
|
|
506
|
+
stderrTruncated: boolean;
|
|
485
507
|
}
|
|
486
508
|
|
|
487
509
|
/** Files inside the sandbox (`/v1/sandboxes/{id}/files`, `files/stat`, `files/move`, `files/mkdir`). */
|
|
@@ -548,10 +570,97 @@ function execBody(cmd: string | readonly string[], o: SandboxExecOptions): T.Exe
|
|
|
548
570
|
}
|
|
549
571
|
|
|
550
572
|
function execRequestOptions(o: SandboxExecOptions): RequestOptions {
|
|
551
|
-
const {
|
|
573
|
+
const {
|
|
574
|
+
onStdout: _o,
|
|
575
|
+
onStderr: _e,
|
|
576
|
+
cwd: _c,
|
|
577
|
+
env: _v,
|
|
578
|
+
stdin: _s,
|
|
579
|
+
commandTimeoutMs: _t,
|
|
580
|
+
user: _u,
|
|
581
|
+
maxOutputBytes: _m,
|
|
582
|
+
...rest
|
|
583
|
+
} = o;
|
|
552
584
|
return rest;
|
|
553
585
|
}
|
|
554
586
|
|
|
587
|
+
function capOf(maxOutputBytes: number | undefined): number {
|
|
588
|
+
// Only undefined takes the default: null (Python's None, which keeps everything) is refused, naming Infinity.
|
|
589
|
+
const cap = maxOutputBytes === undefined ? DEFAULT_MAX_OUTPUT_BYTES : maxOutputBytes;
|
|
590
|
+
if (cap !== Number.POSITIVE_INFINITY && !(Number.isInteger(cap) && cap >= 0)) {
|
|
591
|
+
throw new B3OSError({
|
|
592
|
+
code: "invalid_request",
|
|
593
|
+
status: null,
|
|
594
|
+
detail: `maxOutputBytes must be a non-negative integer or Infinity, got ${String(maxOutputBytes)}`,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
return cap;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/** UTF-8 bytes of one UTF-16 code unit at `i` of `s` (4 for a surrogate pair, 0 for its second half). */
|
|
601
|
+
function utf8Bytes(s: string, i: number): number {
|
|
602
|
+
const c = s.charCodeAt(i);
|
|
603
|
+
if (c < 0x80) return 1;
|
|
604
|
+
if (c < 0x800) return 2;
|
|
605
|
+
if (c >= 0xd800 && c <= 0xdbff) {
|
|
606
|
+
const d = s.charCodeAt(i + 1);
|
|
607
|
+
if (d >= 0xdc00 && d <= 0xdfff) return 4;
|
|
608
|
+
} else if (c >= 0xdc00 && c <= 0xdfff) {
|
|
609
|
+
const p = s.charCodeAt(i - 1);
|
|
610
|
+
if (i > 0 && p >= 0xd800 && p <= 0xdbff) return 0;
|
|
611
|
+
}
|
|
612
|
+
return 3; // the rest of the BMP; a lone surrogate is sent as U+FFFD
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** One output stream as it arrives, up to `cap` UTF-8 bytes; past the cap nothing more is kept and `cut` is set. */
|
|
616
|
+
class Kept {
|
|
617
|
+
text = "";
|
|
618
|
+
size = 0;
|
|
619
|
+
cut = false;
|
|
620
|
+
readonly cap: number;
|
|
621
|
+
constructor(cap: number) {
|
|
622
|
+
this.cap = cap;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
add(data: string): void {
|
|
626
|
+
if (this.cut || data.length === 0) return;
|
|
627
|
+
if (this.cap === Number.POSITIVE_INFINITY) {
|
|
628
|
+
this.text += data;
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
// At most 3 bytes a code unit: a chunk that fits even then needs no counting.
|
|
632
|
+
const room = this.cap - this.size;
|
|
633
|
+
if (data.length * 3 <= room) {
|
|
634
|
+
this.text += data;
|
|
635
|
+
this.size += utf8Length(data);
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
let n = 0;
|
|
639
|
+
for (let i = 0; i < data.length; i++) {
|
|
640
|
+
const b = utf8Bytes(data, i);
|
|
641
|
+
if (n + b > room) {
|
|
642
|
+
// Never half a surrogate pair: `b` was 4 at its first half, so the cut falls before it. A pair sent in two
|
|
643
|
+
// events, cut between its halves, leaves its first half at the end: drop it.
|
|
644
|
+
this.text += data.slice(0, i);
|
|
645
|
+
const last = this.text.charCodeAt(this.text.length - 1);
|
|
646
|
+
if (last >= 0xd800 && last <= 0xdbff) this.text = this.text.slice(0, -1);
|
|
647
|
+
this.size = this.cap;
|
|
648
|
+
this.cut = true;
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
n += b;
|
|
652
|
+
}
|
|
653
|
+
this.text += data;
|
|
654
|
+
this.size += n;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function utf8Length(s: string): number {
|
|
659
|
+
let n = 0;
|
|
660
|
+
for (let i = 0; i < s.length; i++) n += utf8Bytes(s, i);
|
|
661
|
+
return n;
|
|
662
|
+
}
|
|
663
|
+
|
|
555
664
|
/**
|
|
556
665
|
* A sandbox you hold: every operation of `b3os.sandboxes` bound to one id, plus `files`, `ports` and disposal. The
|
|
557
666
|
* API's sandbox object (what `createSandbox` and `getSandbox` return) is the type `SandboxInfo`, in `sbx.info`.
|
|
@@ -652,32 +761,43 @@ export class Sandbox implements AsyncDisposable {
|
|
|
652
761
|
}
|
|
653
762
|
|
|
654
763
|
/**
|
|
655
|
-
* Run a command and wait for it. With `onStdout` or `onStderr` the output streams to them as it arrives;
|
|
656
|
-
*
|
|
764
|
+
* Run a command and wait for it. With `onStdout` or `onStderr` the output streams to them as it arrives; the
|
|
765
|
+
* result keeps up to `maxOutputBytes` of each stream (16 MiB by default). A string runs through `/bin/sh -c`, an
|
|
766
|
+
* array is executed directly. An error from a streamed call (`stream_interrupted`, `stream_ended`, `timeout`, a
|
|
767
|
+
* cut the node reports in `exec.exit`, `aborted` once the answer began) carries the call's `requestId`, as an
|
|
768
|
+
* error answer does.
|
|
657
769
|
*/
|
|
658
770
|
async exec(cmd: string | readonly string[], options: SandboxExecOptions = {}): Promise<SandboxExecOutput> {
|
|
659
771
|
const api = this.#client.sandboxes;
|
|
772
|
+
const cap = capOf(options.maxOutputBytes);
|
|
660
773
|
const body = execBody(cmd, options);
|
|
661
774
|
const o = execRequestOptions(options);
|
|
775
|
+
const stdout = new Kept(cap);
|
|
776
|
+
const stderr = new Kept(cap);
|
|
662
777
|
if (!options.onStdout && !options.onStderr) {
|
|
663
778
|
const r = await api.exec(this.id, body, o);
|
|
779
|
+
// The node keeps at most 1 MiB of each stream here; a smaller cap trims it.
|
|
780
|
+
stdout.add(r.stdout ?? "");
|
|
781
|
+
stderr.add(r.stderr ?? "");
|
|
664
782
|
return {
|
|
665
783
|
exitCode: r.exit_code,
|
|
666
784
|
signal: r.signal ?? null,
|
|
667
785
|
timedOut: r.timed_out ?? false,
|
|
668
|
-
stdout:
|
|
669
|
-
stderr:
|
|
786
|
+
stdout: stdout.text,
|
|
787
|
+
stderr: stderr.text,
|
|
670
788
|
durationMs: r.duration_ms ?? null,
|
|
789
|
+
stdoutTruncated: (r.stdout_truncated ?? false) || stdout.cut,
|
|
790
|
+
stderrTruncated: (r.stderr_truncated ?? false) || stderr.cut,
|
|
671
791
|
};
|
|
672
792
|
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
for await (const ev of
|
|
793
|
+
// Errors from the stream carry its request id (`stream.requestId`), as an error answer does.
|
|
794
|
+
const stream = api.exec(this.id, body, { ...o, stream: true });
|
|
795
|
+
for await (const ev of stream) {
|
|
676
796
|
if (ev.type === "exec.stdout" && typeof ev.data === "string") {
|
|
677
|
-
stdout
|
|
797
|
+
stdout.add(ev.data);
|
|
678
798
|
options.onStdout?.(ev.data);
|
|
679
799
|
} else if (ev.type === "exec.stderr" && typeof ev.data === "string") {
|
|
680
|
-
stderr
|
|
800
|
+
stderr.add(ev.data);
|
|
681
801
|
options.onStderr?.(ev.data);
|
|
682
802
|
} else if (ev.type === "exec.exit") {
|
|
683
803
|
const err = (ev as { error?: { code?: string; reason?: string; detail?: string } }).error;
|
|
@@ -686,15 +806,18 @@ export class Sandbox implements AsyncDisposable {
|
|
|
686
806
|
code: err.code,
|
|
687
807
|
status: null,
|
|
688
808
|
detail: err.detail ?? `the command in ${this.id} was cut (${err.reason ?? err.code})`,
|
|
809
|
+
requestId: stream.requestId,
|
|
689
810
|
});
|
|
690
811
|
}
|
|
691
812
|
return {
|
|
692
813
|
exitCode: ev.exit_code ?? null,
|
|
693
814
|
signal: ev.signal ?? null,
|
|
694
815
|
timedOut: ev.timed_out ?? false,
|
|
695
|
-
stdout,
|
|
696
|
-
stderr,
|
|
816
|
+
stdout: stdout.text,
|
|
817
|
+
stderr: stderr.text,
|
|
697
818
|
durationMs: ev.duration_ms ?? null,
|
|
819
|
+
stdoutTruncated: stdout.cut,
|
|
820
|
+
stderrTruncated: stderr.cut,
|
|
698
821
|
};
|
|
699
822
|
}
|
|
700
823
|
}
|
|
@@ -702,6 +825,7 @@ export class Sandbox implements AsyncDisposable {
|
|
|
702
825
|
code: "stream_ended",
|
|
703
826
|
status: null,
|
|
704
827
|
detail: `the exec stream of ${this.id} ended before the command exited`,
|
|
828
|
+
requestId: stream.requestId,
|
|
705
829
|
});
|
|
706
830
|
}
|
|
707
831
|
|