sandcovery 0.7.0 → 0.10.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/README.md +11 -8
- package/dist/cli/index.mjs +13025 -19868
- package/dist/cli/sandcovery-docker.mjs +8550 -3
- package/dist/cli/sandcovery-kubectl.mjs +7 -4
- package/dist/cli/sandcovery-t3.mjs +143 -61
- package/dist/index.d.mts +9072 -135
- package/dist/index.mjs +1 -1
- package/dist/{kubectl-ClFWX65x.mjs → kubectl-BZIzN5z0.mjs} +131 -97
- package/dist/saved-sandboxes-XNeQ5SEm.mjs +389 -0
- package/dist/{x-account-BSasf-z-.mjs → src-Ce-b80jJ.mjs} +19731 -8599
- package/dist/sync-KX0CReEd.mjs +4171 -0
- package/dist/third-party-licenses.md +630 -0
- package/dist/types-DotsDqXt.d.mts +1264 -0
- package/dist/types.d.mts +2 -2
- package/package.json +4 -4
- package/dist/src-gAv6vIvE.mjs +0 -566
- package/dist/tool-B8KvfvY5.mjs +0 -12890
- package/dist/types-B52jOW9v.d.mts +0 -853
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { Jt as is, Mt as Struct, Ot as Record, Rt as Union, S as KeySecret, Wt as decodeUnknownOption, _o as match, bo as some, bt as Literals, co as getOrElse, ct as update, d as Conflict, do as getOrUndefined, eo as filter, fo as isNone, go as map, ho as liftThrowable, jt as String$1, l as NotFound, m as Forbidden, no as flatMap, nt as get, ot as make, p as Unauthorized, po as isSome, qt as fromJsonString, s as Unsupported, so as fromUndefinedOr, t as Sandcovery, vo as none, yo as orElse, yt as Literal } from "./src-Ce-b80jJ.mjs";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import net from "node:net";
|
|
6
|
+
import crypto$1, { createHash } from "node:crypto";
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { once } from "node:events";
|
|
10
|
+
// ../../node_modules/.pnpm/es-toolkit@1.53.2/node_modules/es-toolkit/dist/function/noop.mjs
|
|
11
|
+
/**
|
|
12
|
+
* A no-operation function that does nothing.
|
|
13
|
+
* This can be used as a placeholder or default function.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* noop(); // Does nothing
|
|
17
|
+
*
|
|
18
|
+
* @returns This function does not return anything.
|
|
19
|
+
*/
|
|
20
|
+
function noop() {}
|
|
21
|
+
// ../../node_modules/.pnpm/es-toolkit@1.53.2/node_modules/es-toolkit/dist/math/clamp.mjs
|
|
22
|
+
/**
|
|
23
|
+
* Clamps a number within the specified bounds.
|
|
24
|
+
*
|
|
25
|
+
* This function takes a number and one or two bounds, and returns the number clamped within the specified bounds.
|
|
26
|
+
* If only one bound is provided, it returns the minimum of the value and the bound.
|
|
27
|
+
*
|
|
28
|
+
* @param value - The number to clamp.
|
|
29
|
+
* @param bound1 - The minimum bound to clamp the number, or the maximum bound if bound2 is not provided.
|
|
30
|
+
* @param [bound2] - The maximum bound to clamp the number. If not provided, the function will only consider bound1 as the upper limit.
|
|
31
|
+
* @returns The clamped number within the specified bounds.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const result1 = clamp(10, 5); // result1 will be 5, as 10 is clamped to the bound 5
|
|
35
|
+
* const result2 = clamp(10, 5, 15); // result2 will be 10, as it is within the bounds 5 and 15
|
|
36
|
+
* const result3 = clamp(2, 5, 15); // result3 will be 5, as 2 is clamped to the lower bound 5
|
|
37
|
+
* const result4 = clamp(20, 5, 15); // result4 will be 15, as 20 is clamped to the upper bound 15
|
|
38
|
+
*/
|
|
39
|
+
function clamp(value, bound1, bound2) {
|
|
40
|
+
if (bound2 == null) return Math.min(value, bound1);
|
|
41
|
+
return Math.min(Math.max(value, bound1), bound2);
|
|
42
|
+
}
|
|
43
|
+
// ../sandcovery-runner/src/start.ts
|
|
44
|
+
const RUNNER_TOKEN_ENV = "ACTIONS_RUNNER_INPUT_JITCONFIG";
|
|
45
|
+
const RUNNER_TOKEN_FILE_NAME = ".sandcovery-runner-token";
|
|
46
|
+
const RUNNER_IMAGE = "ghcr.io/starcovery/sandcovery-runner@sha256:00d2a671c98eb73162ca9a956bc0029d1ecbbbe36c19115f57cd9e87462afb86";
|
|
47
|
+
const KUBERNETES_PORT = 16443;
|
|
48
|
+
const K3S_TLS = "/var/lib/rancher/k3s/server/tls";
|
|
49
|
+
const RELAY_ENV = "SANDCOVERY_RELAY";
|
|
50
|
+
const KUBERNETES_GATEWAY_COMMAND = `s= && [ "$(id -u)" -eq 0 ] || s='sudo -n' && $s "\${${RELAY_ENV}}" kubernetes ${String(KUBERNETES_PORT)} ${K3S_TLS}/server-ca.crt`;
|
|
51
|
+
const DOCKER_SOCKET = "/var/run/docker.sock";
|
|
52
|
+
const RUNNER_HOME = "/home/runner";
|
|
53
|
+
const RUNNER_WORK = `${RUNNER_HOME}/_work`;
|
|
54
|
+
const RUNNER_UID = 1001;
|
|
55
|
+
const RUNNER_CONTAINER = "sandcovery-runner";
|
|
56
|
+
const AS_ROOT = `s= && { [ "$(id -u)" -eq 0 ] || s='sudo -n --preserve-env=${RUNNER_TOKEN_ENV}'; }`;
|
|
57
|
+
const TAKE_TOKEN = `f="$HOME/${RUNNER_TOKEN_FILE_NAME}" && { [ ! -f "$f" ] || { export ${RUNNER_TOKEN_ENV}="$(cat "$f")" && [ -n "\${${RUNNER_TOKEN_ENV}}" ] && rm -f "$f"; }; }`;
|
|
58
|
+
const RUNNER_CONTAINER_FLAGS = `--network host --user runner --group-add "$(stat -c %g ${DOCKER_SOCKET})" --volume ${DOCKER_SOCKET}:${DOCKER_SOCKET} --volume ${RUNNER_CONTAINER}:${RUNNER_HOME} --volume ${RUNNER_WORK}:${RUNNER_WORK}`;
|
|
59
|
+
const LISTEN_TIMEOUT_SECONDS = 120;
|
|
60
|
+
const LISTENING = `$s docker logs ${RUNNER_CONTAINER} 2>&1 | grep -qF 'Listening for Jobs'`;
|
|
61
|
+
const AWAIT_LISTENING = `{ [ -z "\${${RUNNER_TOKEN_ENV}-}" ] || { i=0 && while ! ${LISTENING} && [ "$($s docker inspect --format '{{.State.Running}}' ${RUNNER_CONTAINER})" = true ] && [ "$i" -lt ${String(LISTEN_TIMEOUT_SECONDS)} ]; do sleep 1 && i=$((i + 1)); done && { ${LISTENING} || { $s docker logs ${RUNNER_CONTAINER} >&2; false; }; }; }; }`;
|
|
62
|
+
`${TAKE_TOKEN}${AS_ROOT}${String(RUNNER_UID)}${String(RUNNER_UID)}${RUNNER_WORK}${RUNNER_CONTAINER_FLAGS}${AWAIT_LISTENING}`;
|
|
63
|
+
const KUBERNETES_CONTAINER = "sandcovery-kubernetes";
|
|
64
|
+
const KUBERNETES_COMMAND = `${AS_ROOT} && printf %s '${JSON.stringify({
|
|
65
|
+
name: KUBERNETES_CONTAINER,
|
|
66
|
+
services: { kubernetes: {
|
|
67
|
+
command: ["k3s", "server"],
|
|
68
|
+
container_name: KUBERNETES_CONTAINER,
|
|
69
|
+
healthcheck: {
|
|
70
|
+
start_interval: "1s",
|
|
71
|
+
start_period: `${String(300)}s`,
|
|
72
|
+
test: ["CMD-SHELL", [
|
|
73
|
+
"k3s kubectl wait --timeout=1s --for=condition=Ready node/sandcovery",
|
|
74
|
+
"k3s kubectl wait --timeout=1s --for=create --namespace default serviceaccount/default",
|
|
75
|
+
"k3s kubectl wait --timeout=1s --for=condition=Available --namespace kube-system deployment/coredns",
|
|
76
|
+
"k3s kubectl wait --timeout=1s --for=jsonpath={.data.token} --namespace kube-system secret/sandcovery-admin"
|
|
77
|
+
].join(" && ")]
|
|
78
|
+
},
|
|
79
|
+
image: RUNNER_IMAGE,
|
|
80
|
+
ports: ["127.0.0.1:6443:6443"],
|
|
81
|
+
privileged: true,
|
|
82
|
+
volumes: ["/var/lib/rancher", `${K3S_TLS}:${K3S_TLS}`]
|
|
83
|
+
} }
|
|
84
|
+
})}' | $s docker compose --file - up --detach --wait >&2 && $s docker exec ${KUBERNETES_CONTAINER} k3s kubectl get --namespace kube-system secret/sandcovery-admin --output 'go-template={{.data.token | base64decode}}'`;
|
|
85
|
+
[
|
|
86
|
+
`docker wait ${RUNNER_CONTAINER} >/dev/null`,
|
|
87
|
+
`docker logs ${RUNNER_CONTAINER} 2>&1 | grep -F 'An error occurred: Not configured'`,
|
|
88
|
+
`docker run --rm ${RUNNER_CONTAINER_FLAGS} ${RUNNER_IMAGE} sh -c 'docker run --rm registry.k8s.io/e2e-test-images/busybox:1.38.0-1@sha256:302d6221f7d600725e0f926f9f04f8fbbb2a32678b4e991da760a5062d4dc7e1 true && docker compose version && docker buildx version && sudo -n true'`
|
|
89
|
+
].join(" && ");
|
|
90
|
+
// src/cli/detached.ts
|
|
91
|
+
const READY = "ready\n";
|
|
92
|
+
const PRIVATE_DIRECTORY_MODE = 448;
|
|
93
|
+
const PRIVATE_SOCKET_MODE = 384;
|
|
94
|
+
const isTerminalError = is(Union([
|
|
95
|
+
NotFound,
|
|
96
|
+
Unauthorized,
|
|
97
|
+
Forbidden,
|
|
98
|
+
Conflict,
|
|
99
|
+
Unsupported
|
|
100
|
+
]));
|
|
101
|
+
const decodeUnserved = decodeUnknownOption(Struct({ code: Literals([
|
|
102
|
+
"ECONNREFUSED",
|
|
103
|
+
"ECONNRESET",
|
|
104
|
+
"ENOENT"
|
|
105
|
+
]) }));
|
|
106
|
+
const decodeInUse = decodeUnknownOption(Struct({ code: Literal("EADDRINUSE") }));
|
|
107
|
+
const isTerminal = (cause) => cause instanceof Error && isTerminalError(cause.cause);
|
|
108
|
+
const socketOf = ({ folder, name }) => {
|
|
109
|
+
if (process.platform !== "win32") return path.join(folder, `${name}.sock`);
|
|
110
|
+
const digest = createHash("sha256").update(folder).digest("hex");
|
|
111
|
+
return String.raw`\\.\pipe\sandcovery-${name}-${digest.slice(0, 16)}`;
|
|
112
|
+
};
|
|
113
|
+
const answers = async (socketPath) => {
|
|
114
|
+
const socket = net.connect(socketPath);
|
|
115
|
+
try {
|
|
116
|
+
await once(socket, "connect");
|
|
117
|
+
return true;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (isSome(decodeUnserved(error))) return false;
|
|
120
|
+
throw error;
|
|
121
|
+
} finally {
|
|
122
|
+
socket.destroy();
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const servedAt = async (server, socketPath) => {
|
|
126
|
+
const windows = process.platform === "win32";
|
|
127
|
+
if (!windows) {
|
|
128
|
+
const directory = path.dirname(socketPath);
|
|
129
|
+
fs.mkdirSync(directory, {
|
|
130
|
+
mode: PRIVATE_DIRECTORY_MODE,
|
|
131
|
+
recursive: true
|
|
132
|
+
});
|
|
133
|
+
fs.chmodSync(directory, PRIVATE_DIRECTORY_MODE);
|
|
134
|
+
}
|
|
135
|
+
const [listened] = await Promise.allSettled([once(server.listen(socketPath), "listening")]);
|
|
136
|
+
if (listened.status === "fulfilled") {
|
|
137
|
+
if (!windows) fs.chmodSync(socketPath, PRIVATE_SOCKET_MODE);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
if (isNone(decodeInUse(listened.reason)) || windows || await answers(socketPath)) return false;
|
|
141
|
+
fs.rmSync(socketPath, { force: true });
|
|
142
|
+
return await servedAt(server, socketPath);
|
|
143
|
+
};
|
|
144
|
+
const spawned = async ({ entry, env }) => {
|
|
145
|
+
const child = spawn(process.execPath, [entry], {
|
|
146
|
+
detached: true,
|
|
147
|
+
env: {
|
|
148
|
+
...process.env,
|
|
149
|
+
...env
|
|
150
|
+
},
|
|
151
|
+
stdio: [
|
|
152
|
+
"ignore",
|
|
153
|
+
"pipe",
|
|
154
|
+
"ignore"
|
|
155
|
+
]
|
|
156
|
+
});
|
|
157
|
+
try {
|
|
158
|
+
await Promise.race([once(child.stdout, "data"), once(child, "exit")]);
|
|
159
|
+
} finally {
|
|
160
|
+
child.stdout.destroy();
|
|
161
|
+
child.unref();
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const servedOrSpawned = async ({ attempt, entry, env }) => {
|
|
165
|
+
try {
|
|
166
|
+
return await attempt();
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (isNone(decodeUnserved(error))) throw error;
|
|
169
|
+
await spawned({
|
|
170
|
+
entry,
|
|
171
|
+
env
|
|
172
|
+
});
|
|
173
|
+
return await attempt();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
// ../../node_modules/.pnpm/uuid@14.0.3/node_modules/uuid/dist-node/stringify.js
|
|
177
|
+
const byteToHex = [];
|
|
178
|
+
for (let i = 0; i < 256; ++i) byteToHex.push((i + 256).toString(16).slice(1));
|
|
179
|
+
function unsafeStringify(arr, offset = 0) {
|
|
180
|
+
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
|
|
181
|
+
}
|
|
182
|
+
// ../../node_modules/.pnpm/uuid@14.0.3/node_modules/uuid/dist-node/rng.js
|
|
183
|
+
const rnds8 = /* @__PURE__ */ new Uint8Array(16);
|
|
184
|
+
function rng() {
|
|
185
|
+
return crypto.getRandomValues(rnds8);
|
|
186
|
+
}
|
|
187
|
+
// ../../node_modules/.pnpm/uuid@14.0.3/node_modules/uuid/dist-node/v7.js
|
|
188
|
+
const _state = {};
|
|
189
|
+
function v7(options, buf, offset) {
|
|
190
|
+
let bytes;
|
|
191
|
+
if (options) bytes = v7Bytes(options.random ?? options.rng?.() ?? rng(), options.msecs, options.seq, buf, offset);
|
|
192
|
+
else {
|
|
193
|
+
const now = Date.now();
|
|
194
|
+
const rnds = rng();
|
|
195
|
+
updateV7State(_state, now, rnds);
|
|
196
|
+
bytes = v7Bytes(rnds, _state.msecs, _state.seq, buf, offset);
|
|
197
|
+
}
|
|
198
|
+
return buf ?? unsafeStringify(bytes);
|
|
199
|
+
}
|
|
200
|
+
function updateV7State(state, now, rnds) {
|
|
201
|
+
state.msecs ??= -Infinity;
|
|
202
|
+
state.seq ??= 0;
|
|
203
|
+
if (now > state.msecs) {
|
|
204
|
+
state.seq = v7Sequence(rnds);
|
|
205
|
+
state.msecs = now;
|
|
206
|
+
} else {
|
|
207
|
+
state.seq = state.seq + 1 | 0;
|
|
208
|
+
if (state.seq === 0) state.msecs++;
|
|
209
|
+
}
|
|
210
|
+
return state;
|
|
211
|
+
}
|
|
212
|
+
function v7Bytes(rnds, msecs, seq, buf, offset = 0) {
|
|
213
|
+
if (rnds.length < 16) throw new Error("Random bytes length must be >= 16");
|
|
214
|
+
if (!buf) {
|
|
215
|
+
buf = /* @__PURE__ */ new Uint8Array(16);
|
|
216
|
+
offset = 0;
|
|
217
|
+
} else if (offset < 0 || offset + 16 > buf.length) throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
|
|
218
|
+
msecs ??= Date.now();
|
|
219
|
+
seq ??= v7Sequence(rnds);
|
|
220
|
+
buf[offset++] = msecs / 1099511627776 & 255;
|
|
221
|
+
buf[offset++] = msecs / 4294967296 & 255;
|
|
222
|
+
buf[offset++] = msecs / 16777216 & 255;
|
|
223
|
+
buf[offset++] = msecs / 65536 & 255;
|
|
224
|
+
buf[offset++] = msecs / 256 & 255;
|
|
225
|
+
buf[offset++] = msecs & 255;
|
|
226
|
+
buf[offset++] = 112 | seq >>> 28 & 15;
|
|
227
|
+
buf[offset++] = seq >>> 20 & 255;
|
|
228
|
+
buf[offset++] = 128 | seq >>> 14 & 63;
|
|
229
|
+
buf[offset++] = seq >>> 6 & 255;
|
|
230
|
+
buf[offset++] = seq << 2 & 255 | rnds[10] & 3;
|
|
231
|
+
buf[offset++] = rnds[11];
|
|
232
|
+
buf[offset++] = rnds[12];
|
|
233
|
+
buf[offset++] = rnds[13];
|
|
234
|
+
buf[offset++] = rnds[14];
|
|
235
|
+
buf[offset++] = rnds[15];
|
|
236
|
+
return buf;
|
|
237
|
+
}
|
|
238
|
+
function v7Sequence(rnds) {
|
|
239
|
+
return (rnds[6] & 127) << 24 | rnds[7] << 16 | rnds[8] << 8 | rnds[9];
|
|
240
|
+
}
|
|
241
|
+
// src/cli/tool.ts
|
|
242
|
+
const STOP_SIGNALS = [
|
|
243
|
+
"SIGHUP",
|
|
244
|
+
"SIGINT",
|
|
245
|
+
"SIGTERM"
|
|
246
|
+
];
|
|
247
|
+
const DESTROY_TIMEOUT_MS = 3e4;
|
|
248
|
+
const Credentials = fromJsonString(Struct({ api_key: KeySecret }));
|
|
249
|
+
const credentialsFileOf = (env) => path.join(fromUndefinedOr(env.XDG_CONFIG_HOME).pipe(filter((folder) => path.isAbsolute(folder)), getOrElse(() => path.join(os.homedir(), ".config"))), "sandcovery", "credentials.json");
|
|
250
|
+
const storedKey = (file) => liftThrowable(() => fs.readFileSync(file, "utf-8"))().pipe(flatMap(decodeUnknownOption(Credentials)), map(({ api_key }) => api_key));
|
|
251
|
+
const stopSignalOf = (aborted) => fromUndefinedOr(STOP_SIGNALS.find((signal) => 128 + os.constants.signals[signal] === aborted.reason));
|
|
252
|
+
const toolOf = ({ args, credentials, entry, env, folder, io, signal }) => {
|
|
253
|
+
const { origin } = new URL(env.SANDCOVERY_BASE_URL ?? "https://www.sandcovery.com");
|
|
254
|
+
const key = filter(fromUndefinedOr(env.SANDCOVERY_API_KEY), (value) => value !== "").pipe(orElse(() => storedKey(credentials)));
|
|
255
|
+
return {
|
|
256
|
+
args,
|
|
257
|
+
credentials,
|
|
258
|
+
entry,
|
|
259
|
+
folder,
|
|
260
|
+
io,
|
|
261
|
+
key,
|
|
262
|
+
origin,
|
|
263
|
+
signal,
|
|
264
|
+
client: new Sandcovery({
|
|
265
|
+
apiKey: getOrUndefined(key),
|
|
266
|
+
baseUrl: origin
|
|
267
|
+
})
|
|
268
|
+
};
|
|
269
|
+
};
|
|
270
|
+
const createdSandbox = async ({ request, tool }) => {
|
|
271
|
+
const idempotencyKey = v7();
|
|
272
|
+
try {
|
|
273
|
+
return await tool.client.sandboxes.create(request, {
|
|
274
|
+
idempotencyKey,
|
|
275
|
+
signal: tool.signal
|
|
276
|
+
});
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (tool.signal.aborted) {
|
|
279
|
+
const signal = AbortSignal.timeout(DESTROY_TIMEOUT_MS);
|
|
280
|
+
const { id } = await tool.client.sandboxes.create(request, {
|
|
281
|
+
idempotencyKey,
|
|
282
|
+
signal
|
|
283
|
+
});
|
|
284
|
+
await tool.client.sandboxes.destroy(id, { signal });
|
|
285
|
+
}
|
|
286
|
+
throw error;
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
const writePrivateFile = ({ file, text }) => {
|
|
290
|
+
const directory = path.dirname(file);
|
|
291
|
+
fs.mkdirSync(directory, {
|
|
292
|
+
mode: 448,
|
|
293
|
+
recursive: true
|
|
294
|
+
});
|
|
295
|
+
const pending = path.join(directory, `.${path.basename(file)}.${crypto$1.randomBytes(8).toString("hex")}.tmp`);
|
|
296
|
+
fs.writeFileSync(pending, text, {
|
|
297
|
+
flag: "wx",
|
|
298
|
+
mode: 384
|
|
299
|
+
});
|
|
300
|
+
fs.renameSync(pending, file);
|
|
301
|
+
};
|
|
302
|
+
const ran = async (run) => {
|
|
303
|
+
const outputs = [process.stdout, process.stderr];
|
|
304
|
+
for (const stream of outputs) stream.on("error", (error) => {
|
|
305
|
+
if (error.code === "EPIPE") process.exit(process.exitCode ?? 0);
|
|
306
|
+
throw error;
|
|
307
|
+
});
|
|
308
|
+
const controller = new AbortController();
|
|
309
|
+
for (const signal of STOP_SIGNALS) {
|
|
310
|
+
const code = 128 + os.constants.signals[signal];
|
|
311
|
+
process.on(signal, () => {
|
|
312
|
+
if (controller.signal.aborted) process.exit(code);
|
|
313
|
+
controller.abort(code);
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
const tool = toolOf({
|
|
317
|
+
args: process.argv.slice(2),
|
|
318
|
+
credentials: credentialsFileOf(process.env),
|
|
319
|
+
entry: fromUndefinedOr(process.argv[1]),
|
|
320
|
+
env: process.env,
|
|
321
|
+
folder: path.join(os.homedir(), ".config", "sandcovery"),
|
|
322
|
+
io: {
|
|
323
|
+
stderr: process.stderr,
|
|
324
|
+
stdin: process.stdin,
|
|
325
|
+
stdout: process.stdout
|
|
326
|
+
},
|
|
327
|
+
signal: controller.signal
|
|
328
|
+
});
|
|
329
|
+
try {
|
|
330
|
+
return await run(tool);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
if (!controller.signal.aborted) throw error;
|
|
333
|
+
return Number(controller.signal.reason);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const startTool = async ({ run, server }) => {
|
|
337
|
+
http.setGlobalProxyFromEnv();
|
|
338
|
+
const served = server.pipe(flatMap(({ serve, variable }) => fromUndefinedOr(process.env[variable]).pipe(map((config) => ({
|
|
339
|
+
config,
|
|
340
|
+
serve
|
|
341
|
+
})))));
|
|
342
|
+
await match(served, {
|
|
343
|
+
onNone: async () => {
|
|
344
|
+
process.exitCode = await ran(run);
|
|
345
|
+
},
|
|
346
|
+
onSome: async ({ config, serve }) => {
|
|
347
|
+
await serve(config);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
};
|
|
351
|
+
// src/cli/relay.ts
|
|
352
|
+
const RELAY_PORT = 9951;
|
|
353
|
+
const launchedRelay = async ({ command, id, tool }) => {
|
|
354
|
+
const launched = await tool.client.sandboxes.startProcess(id, {
|
|
355
|
+
command,
|
|
356
|
+
cwd: null,
|
|
357
|
+
env: null,
|
|
358
|
+
terminal: null
|
|
359
|
+
}, { signal: tool.signal });
|
|
360
|
+
const stderr = make([]);
|
|
361
|
+
const attachment = tool.client.sandboxes.attachProcess(id, launched.id, {
|
|
362
|
+
signal: tool.signal,
|
|
363
|
+
stderr: (data) => {
|
|
364
|
+
update(stderr, (chunks) => [...chunks, data]);
|
|
365
|
+
},
|
|
366
|
+
stdout: null
|
|
367
|
+
});
|
|
368
|
+
attachment.endStdin();
|
|
369
|
+
const { code } = await attachment.exit;
|
|
370
|
+
return {
|
|
371
|
+
code,
|
|
372
|
+
stderr: Buffer.concat(get(stderr)).toString()
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
// src/cli/saved-sandboxes.ts
|
|
376
|
+
const isNotFound = is(NotFound);
|
|
377
|
+
const readText = liftThrowable((file) => fs.readFileSync(file, "utf-8"));
|
|
378
|
+
const savedSandboxes = ({ file, schema }) => readText(file).pipe(flatMap(decodeUnknownOption(fromJsonString(Record(String$1, schema)))), getOrElse(() => ({})));
|
|
379
|
+
const runningSandbox = async ({ id, tool }) => {
|
|
380
|
+
if (isNone(id)) return none();
|
|
381
|
+
try {
|
|
382
|
+
const sandbox = await tool.client.sandboxes.get(id.value, { signal: tool.signal });
|
|
383
|
+
return filter(some(sandbox), (found) => found.status === "running");
|
|
384
|
+
} catch (error) {
|
|
385
|
+
if (error instanceof Error && isNotFound(error.cause)) return none();
|
|
386
|
+
throw error;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
export { clamp as C, RELAY_ENV as S, servedOrSpawned as _, Credentials as a, KUBERNETES_GATEWAY_COMMAND as b, credentialsFileOf as c, storedKey as d, writePrivateFile as f, servedAt as g, isTerminal as h, launchedRelay as i, startTool as l, READY as m, savedSandboxes as n, DESTROY_TIMEOUT_MS as o, v7 as p, RELAY_PORT as r, createdSandbox as s, runningSandbox as t, stopSignalOf as u, socketOf as v, noop as w, KUBERNETES_PORT as x, KUBERNETES_COMMAND as y };
|