@getenergy/gke-sandboxes 0.4.1 → 0.6.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.
@@ -14,6 +14,11 @@ export type ExecOptions = {
14
14
  maxOutputBytes?: number;
15
15
  };
16
16
  export declare const DEFAULT_MAX_OUTPUT_BYTES: number;
17
+ /** Gathers a command's output as it arrives, for the result every way of running one returns. */
18
+ export declare const collectOutput: ({ onOutput, maxOutputBytes, }: ExecOptions) => {
19
+ add(payload: Uint8Array, stream: 'stdout' | 'stderr'): void;
20
+ result: (exitCode: number) => KubernetesExecResult;
21
+ };
17
22
  /** The few Kubernetes API calls sandboxes need: JSON resources, watches and exec. */
18
23
  export declare class KubernetesApi {
19
24
  private readonly baseUrl;
@@ -38,5 +43,5 @@ export declare class KubernetesApi {
38
43
  /** One watch's events, until the server ends it, the connection breaks or `ended` aborts. */
39
44
  private events;
40
45
  /** Runs a command in a pod's container, feeding it `stdin` and collecting its output. */
41
- exec(podPath: string, command: string[], stdin: Uint8Array, timeoutSeconds: number, { onOutput, maxOutputBytes }?: ExecOptions): Promise<KubernetesExecResult>;
46
+ exec(podPath: string, command: string[], stdin: Uint8Array, timeoutSeconds: number, options?: ExecOptions): Promise<KubernetesExecResult>;
42
47
  }
@@ -33,6 +33,27 @@ export class KubernetesApiError extends Error {
33
33
  }
34
34
  // A command that prints without end must not exhaust the caller's memory.
35
35
  export const DEFAULT_MAX_OUTPUT_BYTES = 32 * 2 ** 20;
36
+ /** Gathers a command's output as it arrives, for the result every way of running one returns. */
37
+ export const collectOutput = ({ onOutput, maxOutputBytes = DEFAULT_MAX_OUTPUT_BYTES, }) => {
38
+ const output = [];
39
+ let kept = 0;
40
+ let truncated = false;
41
+ return {
42
+ add(payload, stream) {
43
+ onOutput?.(payload, stream);
44
+ const fits = payload.subarray(0, Math.max(0, maxOutputBytes - kept));
45
+ if (fits.byteLength)
46
+ output.push(fits);
47
+ kept += fits.byteLength;
48
+ truncated ||= fits.byteLength < payload.byteLength;
49
+ },
50
+ result: (exitCode) => ({
51
+ exitCode,
52
+ output: Buffer.concat(output).toString('utf8'),
53
+ truncated,
54
+ }),
55
+ };
56
+ };
36
57
  /** The few Kubernetes API calls sandboxes need: JSON resources, watches and exec. */
37
58
  export class KubernetesApi {
38
59
  baseUrl;
@@ -154,7 +175,7 @@ export class KubernetesApi {
154
175
  }
155
176
  }
156
177
  /** Runs a command in a pod's container, feeding it `stdin` and collecting its output. */
157
- async exec(podPath, command, stdin, timeoutSeconds, { onOutput, maxOutputBytes = DEFAULT_MAX_OUTPUT_BYTES } = {}) {
178
+ async exec(podPath, command, stdin, timeoutSeconds, options = {}) {
158
179
  const url = new URL(`${podPath}/exec`, this.baseUrl);
159
180
  url.protocol = 'wss:';
160
181
  for (const stream of ['stdin', 'stdout', 'stderr'])
@@ -169,9 +190,7 @@ export class KubernetesApi {
169
190
  });
170
191
  socket.binaryType = 'arraybuffer';
171
192
  const finished = Promise.withResolvers();
172
- const output = [];
173
- let kept = 0;
174
- let truncated = false;
193
+ const collected = collectOutput(options);
175
194
  const timer = setTimeout(() => {
176
195
  finished.reject(new Error(`Sandbox command timed out after ${timeoutSeconds} seconds.`));
177
196
  socket.close();
@@ -189,14 +208,8 @@ export class KubernetesApi {
189
208
  socket.onmessage = ({ data }) => {
190
209
  const frame = new Uint8Array(data);
191
210
  const payload = frame.subarray(1);
192
- if (frame[0] === STDOUT || frame[0] === STDERR) {
193
- onOutput?.(payload, frame[0] === STDOUT ? 'stdout' : 'stderr');
194
- const fits = payload.subarray(0, Math.max(0, maxOutputBytes - kept));
195
- if (fits.byteLength)
196
- output.push(fits);
197
- kept += fits.byteLength;
198
- truncated ||= fits.byteLength < payload.byteLength;
199
- }
211
+ if (frame[0] === STDOUT || frame[0] === STDERR)
212
+ collected.add(payload, frame[0] === STDOUT ? 'stdout' : 'stderr');
200
213
  if (frame[0] !== STATUS)
201
214
  return;
202
215
  // GKE sends the final status after stdout and stderr. Node can keep the socket
@@ -208,11 +221,7 @@ export class KubernetesApi {
208
221
  finished.reject(new Error(`Sandbox command failed: ${status.message}`));
209
222
  }
210
223
  else {
211
- finished.resolve({
212
- exitCode: exitCode ? Number(exitCode.message) : 0,
213
- output: Buffer.concat(output).toString('utf8'),
214
- truncated,
215
- });
224
+ finished.resolve(collected.result(exitCode ? Number(exitCode.message) : 0));
216
225
  }
217
226
  }
218
227
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getenergy/gke-sandboxes",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {