@prized/sdk 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/README.md +625 -2
- package/dist/cjs/commands.d.ts +168 -0
- package/dist/cjs/commands.js +567 -0
- package/dist/cjs/errors.d.ts +73 -0
- package/dist/cjs/errors.js +258 -0
- package/dist/cjs/files.d.ts +86 -0
- package/dist/cjs/files.js +366 -0
- package/dist/cjs/http.d.ts +210 -0
- package/dist/cjs/http.js +570 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/outcome.d.ts +75 -0
- package/dist/cjs/outcome.js +167 -0
- package/dist/cjs/output.d.ts +59 -0
- package/dist/cjs/output.js +288 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sandbox.d.ts +218 -0
- package/dist/cjs/sandbox.js +810 -0
- package/dist/cjs/shell.d.ts +152 -0
- package/dist/cjs/shell.js +393 -0
- package/dist/cjs/toolbox.d.ts +48 -0
- package/dist/cjs/toolbox.js +86 -0
- package/dist/cjs/types.d.ts +99 -0
- package/dist/cjs/types.js +102 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/esm/commands.d.ts +168 -0
- package/dist/esm/commands.js +560 -0
- package/dist/esm/errors.d.ts +73 -0
- package/dist/esm/errors.js +250 -0
- package/dist/esm/files.d.ts +86 -0
- package/dist/esm/files.js +360 -0
- package/dist/esm/http.d.ts +210 -0
- package/dist/esm/http.js +553 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/outcome.d.ts +75 -0
- package/dist/esm/outcome.js +152 -0
- package/dist/esm/output.d.ts +59 -0
- package/dist/esm/output.js +280 -0
- package/dist/esm/sandbox.d.ts +218 -0
- package/dist/esm/sandbox.js +806 -0
- package/dist/esm/shell.d.ts +152 -0
- package/dist/esm/shell.js +379 -0
- package/dist/esm/toolbox.d.ts +48 -0
- package/dist/esm/toolbox.js +81 -0
- package/dist/esm/types.d.ts +99 -0
- package/dist/esm/types.js +95 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/package.json +53 -4
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { PrizedError } from './errors.js';
|
|
2
|
+
import { type Toolbox } from './toolbox.js';
|
|
3
|
+
import type { ExecResult } from './types.js';
|
|
4
|
+
/** The timeout a command gets when the call names none. */
|
|
5
|
+
export declare const DEFAULT_EXEC_TIMEOUT_MS = 60000;
|
|
6
|
+
/**
|
|
7
|
+
* The longest command one blocking call carries. Longer timeouts (and
|
|
8
|
+
* `timeoutMs: 0`) run detached and are polled: Node's fetch gives up on an
|
|
9
|
+
* answer after 300 s, and load balancers drop idle connections not long after.
|
|
10
|
+
*/
|
|
11
|
+
export declare const MAX_BLOCKING_EXEC_MS = 240000;
|
|
12
|
+
/**
|
|
13
|
+
* Whether a failed poll is one an outage window rides out (sdk/retry-spec.json
|
|
14
|
+
* `pollOutage`): no answer, an answer that could not be read, a rate limit or
|
|
15
|
+
* any 5xx. Any other (a refused key, any other refusal, an answer that
|
|
16
|
+
* settles the run, an abort) ends the wait at once. The Python SDK's
|
|
17
|
+
* rides_out.
|
|
18
|
+
*/
|
|
19
|
+
export declare function ridesOut(error: PrizedError): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The failed polls of a wait that rides out an outage of up to `windowMs`
|
|
22
|
+
* (exec's pollOutageMs), from when the first poll's retries ran out: polled
|
|
23
|
+
* again with backoff, each poll and its retries ending before the last look,
|
|
24
|
+
* which goes out LAST_LOOK_MAX_MS before the window's end (half the window,
|
|
25
|
+
* for one shorter than twice that) with room for its own retries. So an
|
|
26
|
+
* outage that ends inside the window is always seen to end. The Python
|
|
27
|
+
* SDK's Outage.
|
|
28
|
+
*/
|
|
29
|
+
export declare class Outage {
|
|
30
|
+
private readonly windowMs;
|
|
31
|
+
private end;
|
|
32
|
+
private last;
|
|
33
|
+
private tries;
|
|
34
|
+
private readonly room;
|
|
35
|
+
constructor(windowMs: number);
|
|
36
|
+
/** The bound on a poll sent `now`: undefined unless polls are failing; else the last look's start, or the window's end for the last look. */
|
|
37
|
+
deadline(now: number): number | undefined;
|
|
38
|
+
/** A poll was answered: the outage, if any, is over. */
|
|
39
|
+
answered(): void;
|
|
40
|
+
/**
|
|
41
|
+
* A poll sent at `sentAt` failed `now` with `error` (`cut`: its bound cut
|
|
42
|
+
* it short). Returns the pause before polling again, or null to give up,
|
|
43
|
+
* with the failure to give up with: the last one a poll met on its own,
|
|
44
|
+
* never the cut.
|
|
45
|
+
*/
|
|
46
|
+
failed(error: PrizedError, sentAt: number, now: number, cut: boolean, backoff: (tries: number) => number): {
|
|
47
|
+
pause: number | null;
|
|
48
|
+
failure: PrizedError;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export interface ExecOptions {
|
|
52
|
+
/** Working directory. Relative paths start in /home/prized/work (the default). */
|
|
53
|
+
cwd?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Environment variables for this command, on top of the sandbox's own.
|
|
56
|
+
* Background and streamed commands also get PYTHONUNBUFFERED=1 unless
|
|
57
|
+
* this or the sandbox sets it (`''` keeps Python's buffering).
|
|
58
|
+
*/
|
|
59
|
+
env?: Record<string, string>;
|
|
60
|
+
/**
|
|
61
|
+
* Kill the command after this many ms. Default 60 000. `0` means no limit.
|
|
62
|
+
* For a background command the default is no limit.
|
|
63
|
+
*/
|
|
64
|
+
timeoutMs?: number;
|
|
65
|
+
/** Called with stdout as it arrives (the command then runs detached and is polled). */
|
|
66
|
+
onStdout?: (chunk: string) => void;
|
|
67
|
+
/** Called with stderr as it arrives. */
|
|
68
|
+
onStderr?: (chunk: string) => void;
|
|
69
|
+
/** Start the command and return a handle at once instead of waiting for it. */
|
|
70
|
+
background?: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Keep the whole output, not just the first 4 MiB of each stream: up to
|
|
73
|
+
* 100 MiB each (`truncated` past that). Still one request when the output
|
|
74
|
+
* is small; past 4 MiB the SDK reads it back from files the command wrote
|
|
75
|
+
* in the sandbox. Use it for builds, test suites and anything else whose
|
|
76
|
+
* output you need whole. Background, streamed and long commands read
|
|
77
|
+
* their whole output already.
|
|
78
|
+
*/
|
|
79
|
+
fullOutput?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Abort the call. The command stops with it, as when a blocking call is
|
|
82
|
+
* cut off: the call rejects once the SDK has tried to stop it (a round
|
|
83
|
+
* trip or two; it gives up after about 20 s). With `background: true`
|
|
84
|
+
* this covers the start only; once exec has returned the handle, its
|
|
85
|
+
* kill() stops the command.
|
|
86
|
+
*/
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
/**
|
|
89
|
+
* For a streamed or long command: how long the SDK keeps polling while
|
|
90
|
+
* its polls fail (no answer, a 5xx, a rate limit), in ms, before it gives
|
|
91
|
+
* up and stops the command. Default: the client's `pollOutageMs`
|
|
92
|
+
* (120 000). A refused key, or an answer that settles the run (the
|
|
93
|
+
* sandbox deleted or not running, the run gone), stops it at once.
|
|
94
|
+
*/
|
|
95
|
+
pollOutageMs?: number;
|
|
96
|
+
}
|
|
97
|
+
export interface WaitOptions {
|
|
98
|
+
/** Stop waiting after this many ms (the command keeps running). Default: no limit. */
|
|
99
|
+
timeoutMs?: number;
|
|
100
|
+
onStdout?: (chunk: string) => void;
|
|
101
|
+
onStderr?: (chunk: string) => void;
|
|
102
|
+
signal?: AbortSignal;
|
|
103
|
+
}
|
|
104
|
+
export interface CommandStatus {
|
|
105
|
+
running: boolean;
|
|
106
|
+
/** The exit code once it has one; -1 if its time limit stopped it or it ended without reporting one. */
|
|
107
|
+
exitCode: number | null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* A command running in the background (`sandbox.exec(cmd, { background: true })`).
|
|
111
|
+
* It runs in its own session: it keeps running after this process exits, and
|
|
112
|
+
* while it runs, kill() reaches every process it started in its process group.
|
|
113
|
+
*/
|
|
114
|
+
export declare class CommandHandle {
|
|
115
|
+
/** The run's id (`run_…`). */
|
|
116
|
+
readonly id: string;
|
|
117
|
+
/** The process group id of the command inside the sandbox. */
|
|
118
|
+
readonly pid: number;
|
|
119
|
+
readonly sandboxId: string;
|
|
120
|
+
private readonly toolbox;
|
|
121
|
+
private readonly startedAt;
|
|
122
|
+
/** @internal The time limit exec gave the command, in ms; null for none. */
|
|
123
|
+
timeoutMs: number | null;
|
|
124
|
+
/** @internal */
|
|
125
|
+
constructor(toolbox: Toolbox, id: string, pid: number, startedAt: number);
|
|
126
|
+
private poll;
|
|
127
|
+
/** Whether the command is still running, and its exit code once it is not. */
|
|
128
|
+
status(): Promise<CommandStatus>;
|
|
129
|
+
/** Everything the command has printed so far. */
|
|
130
|
+
logs(): Promise<{
|
|
131
|
+
stdout: string;
|
|
132
|
+
stderr: string;
|
|
133
|
+
}>;
|
|
134
|
+
/**
|
|
135
|
+
* Wait for the command to finish and return everything it printed.
|
|
136
|
+
* `onStdout`/`onStderr` receive the output as it arrives, from the start.
|
|
137
|
+
* A `timeoutMs` here bounds the wait only; the command keeps running. If
|
|
138
|
+
* the command ran past its own timeout (the one given to exec), it was
|
|
139
|
+
* killed and this throws CommandTimeoutError with what it printed.
|
|
140
|
+
*/
|
|
141
|
+
wait(options?: WaitOptions): Promise<ExecResult>;
|
|
142
|
+
/**
|
|
143
|
+
* @internal wait() without the timeout check: exec cleans up before it
|
|
144
|
+
* throws. `outageMs` (exec's pollOutageMs; a wait with no limit only):
|
|
145
|
+
* how long polls may fail before the wait gives up (Outage).
|
|
146
|
+
*/
|
|
147
|
+
waitRaw(options?: WaitOptions, outageMs?: number): Promise<ExecResult>;
|
|
148
|
+
private waitTimeout;
|
|
149
|
+
private result;
|
|
150
|
+
/**
|
|
151
|
+
* Send a signal (default SIGTERM) to the command and everything it
|
|
152
|
+
* started in its process group. Resolves true if it was sent, false if
|
|
153
|
+
* the run had already ended. Processes that left the group (`setsid`, a
|
|
154
|
+
* daemon) are not reached, nor anything after the run itself has ended.
|
|
155
|
+
*/
|
|
156
|
+
kill(signal?: string): Promise<boolean>;
|
|
157
|
+
/**
|
|
158
|
+
* Remove the command's files (its output under `~/.prized/runs/<id>/`)
|
|
159
|
+
* from the sandbox once it has ended: call it after wait() resolves or
|
|
160
|
+
* throws CommandTimeoutError (after a kill(), await wait() first). On a
|
|
161
|
+
* command still running it does nothing, so status(), wait() and kill()
|
|
162
|
+
* keep working. Once its files are gone, status(), logs(), wait() and
|
|
163
|
+
* kill() reject with `not_found`. Best effort: it never rejects.
|
|
164
|
+
*/
|
|
165
|
+
cleanup(): Promise<void>;
|
|
166
|
+
}
|
|
167
|
+
/** sandbox.exec's body. */
|
|
168
|
+
export declare function execCommand(toolbox: Toolbox, command: string | readonly string[], options?: ExecOptions): Promise<ExecResult | CommandHandle>;
|