@impz/client 2.8.0 → 2.8.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/index.d.ts +17 -17
- package/dist/index.js +271 -269
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -20844,16 +20844,6 @@ interface CheckServerOptions {
|
|
|
20844
20844
|
readonly signal?: Readonly<AbortSignal>;
|
|
20845
20845
|
}
|
|
20846
20846
|
//#endregion
|
|
20847
|
-
//#region src/build-image.d.ts
|
|
20848
|
-
type BuildContext = Blob | Uint8Array<ArrayBuffer> | ReadableStream<Uint8Array>;
|
|
20849
|
-
interface BuildImageOptions {
|
|
20850
|
-
readonly dockerfile?: string;
|
|
20851
|
-
readonly size?: number;
|
|
20852
|
-
readonly signal?: AbortSignal;
|
|
20853
|
-
readonly onProgress?: (progress: ImageBuildProgress) => void;
|
|
20854
|
-
readonly onLog?: (line: string) => void;
|
|
20855
|
-
}
|
|
20856
|
-
//#endregion
|
|
20857
20847
|
//#region ../shared/src/readonly-fields.d.ts
|
|
20858
20848
|
type ReadonlyFields<T> = { readonly [Key in keyof T]: ToReadonly<T[Key]>; };
|
|
20859
20849
|
type ToReadonly<T> = T extends readonly (infer Item)[] ? readonly Item[] : T extends object ? Readonly<T> : T;
|
|
@@ -20976,8 +20966,18 @@ interface RequireAwakeOptions {
|
|
|
20976
20966
|
readonly signal?: Readonly<AbortSignal>;
|
|
20977
20967
|
}
|
|
20978
20968
|
//#endregion
|
|
20969
|
+
//#region src/run-image-build.d.ts
|
|
20970
|
+
type BuildContext = Blob | Uint8Array<ArrayBuffer> | ReadableStream<Uint8Array>;
|
|
20971
|
+
interface BuildImageOptions {
|
|
20972
|
+
readonly dockerfile?: string;
|
|
20973
|
+
readonly size?: number;
|
|
20974
|
+
readonly signal?: AbortSignal;
|
|
20975
|
+
readonly onProgress?: (progress: ImageBuildProgress) => void;
|
|
20976
|
+
readonly onLog?: (line: string) => void;
|
|
20977
|
+
}
|
|
20978
|
+
//#endregion
|
|
20979
20979
|
//#region src/send-long-call.d.ts
|
|
20980
|
-
type StartTimer = (fire: () => void,
|
|
20980
|
+
type StartTimer = (fire: () => void, durationMs: number) => () => void;
|
|
20981
20981
|
//#endregion
|
|
20982
20982
|
//#region src/create-imp-client.d.ts
|
|
20983
20983
|
type RpcClient = ContractRouterClient<ImpContract>;
|
|
@@ -21038,6 +21038,10 @@ interface EventSource {
|
|
|
21038
21038
|
readonly stream: (input?: undefined, options?: CallOptions) => Promise<AsyncIterable<ImpEvent>>;
|
|
21039
21039
|
};
|
|
21040
21040
|
}
|
|
21041
|
+
interface WatchReconnect {
|
|
21042
|
+
readonly delayMs: number;
|
|
21043
|
+
readonly shouldRetry?: (error: unknown) => boolean;
|
|
21044
|
+
}
|
|
21041
21045
|
interface WatchEventsOptions {
|
|
21042
21046
|
readonly onEvent: (event: ImpEvent) => Promise<void> | void;
|
|
21043
21047
|
readonly name?: string;
|
|
@@ -21045,10 +21049,6 @@ interface WatchEventsOptions {
|
|
|
21045
21049
|
readonly onOpen?: (attempt: number) => Promise<void> | void;
|
|
21046
21050
|
readonly reconnect?: WatchReconnect;
|
|
21047
21051
|
}
|
|
21048
|
-
interface WatchReconnect {
|
|
21049
|
-
readonly delayMs: number;
|
|
21050
|
-
readonly shouldRetry?: (error: unknown) => boolean;
|
|
21051
|
-
}
|
|
21052
21052
|
export declare function watchEvents(client: EventSource, options: Readonly<WatchEventsOptions>): Promise<void>;
|
|
21053
21053
|
//#endregion
|
|
21054
21054
|
//#region src/require-features.d.ts
|
|
@@ -21070,7 +21070,7 @@ export declare class MissingFeatureError extends Error {
|
|
|
21070
21070
|
}
|
|
21071
21071
|
export declare function requireFeatures(client: FeatureSource, needs: FeatureNeeds, options?: CallOptions): Promise<void>;
|
|
21072
21072
|
export declare function readFeatures(client: FeatureSource, options?: CallOptions): Promise<Partial<ImpdFeatures>>;
|
|
21073
|
-
|
|
21073
|
+
declare function collectMissingFeatures(info: Pick<SystemInfo, 'features'>, wanted: readonly ImpdFeature[]): ImpdFeature[];
|
|
21074
21074
|
//#endregion
|
|
21075
21075
|
//#region src/tunnel/open-forward-tunnel.d.ts
|
|
21076
21076
|
type ForwardTunnelEnd = {
|
|
@@ -21186,4 +21186,4 @@ export declare class NoSessionError extends ExecError {
|
|
|
21186
21186
|
constructor(message: string, data: NoSessionData | undefined);
|
|
21187
21187
|
}
|
|
21188
21188
|
//#endregion
|
|
21189
|
-
export { type ApiCall, type AttachOptions, type BuildContext, type BuildImageOptions, type CheckServerOptions, type Checkpoint, type ColdBoot, type ColdBootCause, type ConsoleOptions, type DetachReason, type EventSource, type ExecClientErrorCode, type ExecExit, type ExecHandle, type ExecOptions, type ExecOutcome, type ExecRequirement, type ExecSession, type ExecSessionOptions, type ExecSocket, type ExecStarted, type FeatureNeeds, type FeatureSource, type ForwardTunnel, type ForwardTunnelEnd, type ForwardTunnelOptions, type Identity, type Image, type ImageBuildPhase, type ImageBuildProgress, type Imp, type ImpClient, type ImpClientOptions, type ImpEvent, type ImpState, type ImpdFeature, type ImpdFeatures, type InvalidResumeData, type InvalidStateData, type NoSessionData, ORPCError, type PreviousGeneration, type RequireAwakeOptions, type ResumeFrom, type ResumeResult, type ReverseForward, type ReverseForwardEnd, type ReverseForwardOptions, type ReverseGuest, type ReverseListening, type ReverseRelay, type ReverseRelayHandlers, type RunOptions, type RunResult, type Scope, type ServerCheck, type Session, type SessionLog, type SessionLogRead, type SessionOutput, type SshKey, type SystemInfo, type Token, type ToolExec, type ToolExecOptions, type WatchEventsOptions, type WatchReconnect, isDefinedError, safe };
|
|
21189
|
+
export { type ApiCall, type AttachOptions, type BuildContext, type BuildImageOptions, type CheckServerOptions, type Checkpoint, type ColdBoot, type ColdBootCause, type ConsoleOptions, type DetachReason, type EventSource, type ExecClientErrorCode, type ExecExit, type ExecHandle, type ExecOptions, type ExecOutcome, type ExecRequirement, type ExecSession, type ExecSessionOptions, type ExecSocket, type ExecStarted, type FeatureNeeds, type FeatureSource, type ForwardTunnel, type ForwardTunnelEnd, type ForwardTunnelOptions, type Identity, type Image, type ImageBuildPhase, type ImageBuildProgress, type Imp, type ImpClient, type ImpClientOptions, type ImpEvent, type ImpState, type ImpdFeature, type ImpdFeatures, type InvalidResumeData, type InvalidStateData, type NoSessionData, ORPCError, type PreviousGeneration, type RequireAwakeOptions, type ResumeFrom, type ResumeResult, type ReverseForward, type ReverseForwardEnd, type ReverseForwardOptions, type ReverseGuest, type ReverseListening, type ReverseRelay, type ReverseRelayHandlers, type RunOptions, type RunResult, type Scope, type ServerCheck, type Session, type SessionLog, type SessionLogRead, type SessionOutput, type SshKey, type SystemInfo, type Token, type ToolExec, type ToolExecOptions, type WatchEventsOptions, type WatchReconnect, collectMissingFeatures as findMissingFeatures, isDefinedError, safe };
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { P, match } from "ts-pattern";
|
|
|
5
5
|
import pRetry, { AbortError } from "p-retry";
|
|
6
6
|
//#endregion
|
|
7
7
|
//#region src/check-server.ts
|
|
8
|
-
const CLIENT_VERSION = "2.8.
|
|
8
|
+
const CLIENT_VERSION = "2.8.2";
|
|
9
9
|
async function checkServer(rpc, options = {}) {
|
|
10
10
|
const signal = options.signal;
|
|
11
11
|
const callOptions = signal === void 0 ? {} : { signal };
|
|
@@ -816,93 +816,6 @@ function buildConsoleShell(passwdPath, fallbacks) {
|
|
|
816
816
|
].join("\n");
|
|
817
817
|
}
|
|
818
818
|
//#endregion
|
|
819
|
-
//#region ../shared/src/split-lines.ts
|
|
820
|
-
function splitLines() {
|
|
821
|
-
const state = { pending: "" };
|
|
822
|
-
return new TransformStream({
|
|
823
|
-
transform: (chunk, controller) => {
|
|
824
|
-
const lines = `${state.pending}${chunk}`.split("\n");
|
|
825
|
-
state.pending = lines.pop() ?? "";
|
|
826
|
-
for (const line of lines) controller.enqueue(line);
|
|
827
|
-
},
|
|
828
|
-
flush: (controller) => {
|
|
829
|
-
if (state.pending !== "") controller.enqueue(state.pending);
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
//#endregion
|
|
834
|
-
//#region src/read-build-answer.ts
|
|
835
|
-
async function readBuildAnswer(response, onProgress, onLog) {
|
|
836
|
-
if (response.ok && isStream(response)) return readBuildEvents(response, onProgress, onLog);
|
|
837
|
-
const body = await response.json().catch(() => null);
|
|
838
|
-
if (response.ok) return ImageBuildResultSchema.parse(body);
|
|
839
|
-
const failure = ImageBuildErrorSchema.safeParse(body);
|
|
840
|
-
if (failure.success) throw new ORPCError$1(failure.data.code, {
|
|
841
|
-
status: response.status,
|
|
842
|
-
message: failure.data.message
|
|
843
|
-
});
|
|
844
|
-
const code = response.status === 401 ? "UNAUTHORIZED" : "INTERNAL_SERVER_ERROR";
|
|
845
|
-
throw new ORPCError$1(code, {
|
|
846
|
-
status: response.status,
|
|
847
|
-
message: `impd answered ${String(response.status)} to the image build`
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
function isStream(response) {
|
|
851
|
-
return response.headers.get("content-type")?.startsWith(IMAGE_BUILD_STREAM_TYPE) === true;
|
|
852
|
-
}
|
|
853
|
-
async function readBuildEvents(response, onProgress, onLog) {
|
|
854
|
-
for await (const line of readLines(response)) {
|
|
855
|
-
const parsed = ImageBuildEventSchema.safeParse(parseJson(line));
|
|
856
|
-
if (!parsed.success) continue;
|
|
857
|
-
const event = parsed.data;
|
|
858
|
-
if (event.type === "image") return event.image;
|
|
859
|
-
if (event.type === "error") throw new ORPCError$1(event.code, { message: event.message });
|
|
860
|
-
if (event.type === "log") onLog?.(event.text);
|
|
861
|
-
else onProgress?.(event);
|
|
862
|
-
}
|
|
863
|
-
throw new ORPCError$1("INTERNAL_SERVER_ERROR", { message: "the image build stream ended before impd answered the image" });
|
|
864
|
-
}
|
|
865
|
-
async function* readLines(response) {
|
|
866
|
-
if (response.body === null) return;
|
|
867
|
-
yield* response.body.pipeThrough(new TextDecoderStream()).pipeThrough(splitLines());
|
|
868
|
-
}
|
|
869
|
-
function parseJson(line) {
|
|
870
|
-
try {
|
|
871
|
-
return JSON.parse(line);
|
|
872
|
-
} catch {
|
|
873
|
-
return null;
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
//#endregion
|
|
877
|
-
//#region src/resolve-impd-url.ts
|
|
878
|
-
function resolveImpdUrl(base, path) {
|
|
879
|
-
const url = new URL(base);
|
|
880
|
-
url.pathname = `${url.pathname.replace(/\/+$/u, "")}${path}`;
|
|
881
|
-
return url;
|
|
882
|
-
}
|
|
883
|
-
//#endregion
|
|
884
|
-
//#region src/build-image.ts
|
|
885
|
-
async function buildImage(deps, name, context, options = {}) {
|
|
886
|
-
const url = resolveImpdUrl(deps.baseUrl, IMAGE_BUILD_PATH);
|
|
887
|
-
url.searchParams.set("name", name);
|
|
888
|
-
if (options.dockerfile !== void 0) url.searchParams.set("dockerfile", options.dockerfile);
|
|
889
|
-
const headers = {
|
|
890
|
-
"content-type": "application/x-tar",
|
|
891
|
-
accept: `${IMAGE_BUILD_STREAM_TYPE}, application/json`
|
|
892
|
-
};
|
|
893
|
-
if (deps.token !== null) headers["authorization"] = `Bearer ${deps.token}`;
|
|
894
|
-
if (options.size !== void 0) headers["content-length"] = String(options.size);
|
|
895
|
-
const init = {
|
|
896
|
-
method: "POST",
|
|
897
|
-
headers,
|
|
898
|
-
body: context,
|
|
899
|
-
duplex: "half",
|
|
900
|
-
...options.signal !== void 0 && { signal: options.signal }
|
|
901
|
-
};
|
|
902
|
-
const request = new Request(url.href, init);
|
|
903
|
-
return readBuildAnswer(await (deps.fetch ?? fetch)(request), options.onProgress, options.onLog);
|
|
904
|
-
}
|
|
905
|
-
//#endregion
|
|
906
819
|
//#region src/exec/exec-error.ts
|
|
907
820
|
var ExecError = class extends Error {
|
|
908
821
|
code;
|
|
@@ -916,21 +829,29 @@ var ExecError = class extends Error {
|
|
|
916
829
|
}
|
|
917
830
|
};
|
|
918
831
|
//#endregion
|
|
832
|
+
//#region src/resolve-impd-url.ts
|
|
833
|
+
function resolveImpdUrl(base, path) {
|
|
834
|
+
const url = new URL(base);
|
|
835
|
+
url.pathname = `${url.pathname.replace(/\/+$/u, "")}${path}`;
|
|
836
|
+
return url;
|
|
837
|
+
}
|
|
838
|
+
//#endregion
|
|
919
839
|
//#region src/create-rpc-client.ts
|
|
920
840
|
function createRpcClient(options) {
|
|
921
841
|
const customFetch = options.fetch;
|
|
842
|
+
const sendRequest = async (request, init) => {
|
|
843
|
+
const response = await (customFetch === void 0 ? fetch(request, init) : customFetch(request));
|
|
844
|
+
if (response.ok || isJson(response.headers.get("content-type"))) return response;
|
|
845
|
+
await response.body?.cancel();
|
|
846
|
+
throw new ORPCError$1("UNEXPECTED_ANSWER", {
|
|
847
|
+
status: response.status,
|
|
848
|
+
message: `impd answered with ${String(response.status)}`
|
|
849
|
+
});
|
|
850
|
+
};
|
|
922
851
|
return createORPCClient(new RPCLink({
|
|
923
852
|
url: resolveImpdUrl(options.url, "/rpc").href,
|
|
924
853
|
headers: options.token === null ? {} : { authorization: `Bearer ${options.token}` },
|
|
925
|
-
fetch:
|
|
926
|
-
const response = await (customFetch === void 0 ? fetch(request, init) : customFetch(request));
|
|
927
|
-
if (response.ok || isJson(response.headers.get("content-type"))) return response;
|
|
928
|
-
await response.body?.cancel();
|
|
929
|
-
throw new ORPCError$1("UNEXPECTED_ANSWER", {
|
|
930
|
-
status: response.status,
|
|
931
|
-
message: `impd answered with ${String(response.status)}`
|
|
932
|
-
});
|
|
933
|
-
}
|
|
854
|
+
fetch: sendRequest
|
|
934
855
|
}));
|
|
935
856
|
}
|
|
936
857
|
function isJson(contentType) {
|
|
@@ -1005,7 +926,7 @@ async function readFeatures(client, options = {}) {
|
|
|
1005
926
|
}
|
|
1006
927
|
return (await client.system.info(void 0, options)).features ?? {};
|
|
1007
928
|
}
|
|
1008
|
-
function
|
|
929
|
+
function collectMissingFeatures(info, wanted) {
|
|
1009
930
|
return wanted.filter((feature) => info.features?.[feature] !== true);
|
|
1010
931
|
}
|
|
1011
932
|
function isMissingProcedure(error) {
|
|
@@ -1339,11 +1260,6 @@ function toFailedError(code, message, data) {
|
|
|
1339
1260
|
}
|
|
1340
1261
|
//#endregion
|
|
1341
1262
|
//#region src/exec/open-exec.ts
|
|
1342
|
-
const DEFAULT_MAX_UNREAD_BYTES = 8388608;
|
|
1343
|
-
const TTY_SIGNAL_KEYS = {
|
|
1344
|
-
SIGINT: "",
|
|
1345
|
-
SIGQUIT: ""
|
|
1346
|
-
};
|
|
1347
1263
|
function openExec(deps, name, argv, options = {}) {
|
|
1348
1264
|
const tty = options.tty ?? false;
|
|
1349
1265
|
return openHandle(deps, options, tty, {
|
|
@@ -1372,6 +1288,7 @@ function openAttach(deps, name, session, options = {}) {
|
|
|
1372
1288
|
...options.wake !== void 0 && { wake: options.wake }
|
|
1373
1289
|
});
|
|
1374
1290
|
}
|
|
1291
|
+
const DEFAULT_MAX_UNREAD_BYTES = 8388608;
|
|
1375
1292
|
async function openHandle(deps, options, tty, start) {
|
|
1376
1293
|
const name = start.name;
|
|
1377
1294
|
const abort = options.signal;
|
|
@@ -1448,7 +1365,7 @@ async function openHandle(deps, options, tty, start) {
|
|
|
1448
1365
|
if (abort?.aborted === true) stopForAbort();
|
|
1449
1366
|
else abort?.addEventListener("abort", stopForAbort, { once: true });
|
|
1450
1367
|
waitForOutcome();
|
|
1451
|
-
return
|
|
1368
|
+
return createHandle(session, {
|
|
1452
1369
|
started: started.promise,
|
|
1453
1370
|
ended: ended.promise,
|
|
1454
1371
|
isEnded: () => state.ended,
|
|
@@ -1457,6 +1374,37 @@ async function openHandle(deps, options, tty, start) {
|
|
|
1457
1374
|
streams: [stdout.stream, stderr.stream]
|
|
1458
1375
|
});
|
|
1459
1376
|
}
|
|
1377
|
+
function buildOutputStream(channel, hooks) {
|
|
1378
|
+
const state = {
|
|
1379
|
+
controller: null,
|
|
1380
|
+
open: true
|
|
1381
|
+
};
|
|
1382
|
+
return {
|
|
1383
|
+
stream: new ReadableStream({
|
|
1384
|
+
start: (controller) => {
|
|
1385
|
+
state.controller = controller;
|
|
1386
|
+
},
|
|
1387
|
+
cancel: () => {
|
|
1388
|
+
state.open = false;
|
|
1389
|
+
hooks.onCancel();
|
|
1390
|
+
}
|
|
1391
|
+
}, {
|
|
1392
|
+
highWaterMark: hooks.maxUnreadBytes,
|
|
1393
|
+
size: (chunk) => chunk?.byteLength ?? 0
|
|
1394
|
+
}),
|
|
1395
|
+
push: (data) => {
|
|
1396
|
+
if (!state.open || state.controller === null) return;
|
|
1397
|
+
state.controller.enqueue(data);
|
|
1398
|
+
if ((state.controller.desiredSize ?? 0) < 0) hooks.onOverflow(channel);
|
|
1399
|
+
},
|
|
1400
|
+
end: () => {
|
|
1401
|
+
if (state.open) {
|
|
1402
|
+
state.open = false;
|
|
1403
|
+
state.controller?.close();
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1460
1408
|
function readAbortReason(abort) {
|
|
1461
1409
|
const reason = abort?.reason;
|
|
1462
1410
|
return reason instanceof Error ? reason : new DOMException("the exec was aborted", "AbortError");
|
|
@@ -1488,7 +1436,11 @@ const STDIN_FRAME_BYTES = 1024 ** 2;
|
|
|
1488
1436
|
function checkWriteFailure(error) {
|
|
1489
1437
|
if (!(error instanceof ExecError && error.code === "CLOSED")) throw error;
|
|
1490
1438
|
}
|
|
1491
|
-
|
|
1439
|
+
const TTY_SIGNAL_KEYS = {
|
|
1440
|
+
SIGINT: "",
|
|
1441
|
+
SIGQUIT: ""
|
|
1442
|
+
};
|
|
1443
|
+
function createHandle(session, parts) {
|
|
1492
1444
|
const encoder = new TextEncoder();
|
|
1493
1445
|
const waitForStart = async () => {
|
|
1494
1446
|
const result = await parts.started;
|
|
@@ -1554,37 +1506,6 @@ async function waitIgnoringRejection(promise) {
|
|
|
1554
1506
|
await promise;
|
|
1555
1507
|
} catch {}
|
|
1556
1508
|
}
|
|
1557
|
-
function buildOutputStream(channel, hooks) {
|
|
1558
|
-
const state = {
|
|
1559
|
-
controller: null,
|
|
1560
|
-
open: true
|
|
1561
|
-
};
|
|
1562
|
-
return {
|
|
1563
|
-
stream: new ReadableStream({
|
|
1564
|
-
start: (controller) => {
|
|
1565
|
-
state.controller = controller;
|
|
1566
|
-
},
|
|
1567
|
-
cancel: () => {
|
|
1568
|
-
state.open = false;
|
|
1569
|
-
hooks.onCancel();
|
|
1570
|
-
}
|
|
1571
|
-
}, {
|
|
1572
|
-
highWaterMark: hooks.maxUnreadBytes,
|
|
1573
|
-
size: (chunk) => chunk?.byteLength ?? 0
|
|
1574
|
-
}),
|
|
1575
|
-
push: (data) => {
|
|
1576
|
-
if (!state.open || state.controller === null) return;
|
|
1577
|
-
state.controller.enqueue(data);
|
|
1578
|
-
if ((state.controller.desiredSize ?? 0) < 0) hooks.onOverflow(channel);
|
|
1579
|
-
},
|
|
1580
|
-
end: () => {
|
|
1581
|
-
if (state.open) {
|
|
1582
|
-
state.open = false;
|
|
1583
|
-
state.controller?.close();
|
|
1584
|
-
}
|
|
1585
|
-
}
|
|
1586
|
-
};
|
|
1587
|
-
}
|
|
1588
1509
|
async function readAll(stream) {
|
|
1589
1510
|
const buffer = await new Response(stream).arrayBuffer();
|
|
1590
1511
|
return new Uint8Array(buffer);
|
|
@@ -1635,6 +1556,86 @@ function isUnavailable(error) {
|
|
|
1635
1556
|
return error instanceof TypeError;
|
|
1636
1557
|
}
|
|
1637
1558
|
//#endregion
|
|
1559
|
+
//#region ../shared/src/split-lines.ts
|
|
1560
|
+
function splitLines() {
|
|
1561
|
+
const state = { pending: "" };
|
|
1562
|
+
return new TransformStream({
|
|
1563
|
+
transform: (chunk, controller) => {
|
|
1564
|
+
const lines = `${state.pending}${chunk}`.split("\n");
|
|
1565
|
+
state.pending = lines.pop() ?? "";
|
|
1566
|
+
for (const line of lines) controller.enqueue(line);
|
|
1567
|
+
},
|
|
1568
|
+
flush: (controller) => {
|
|
1569
|
+
if (state.pending !== "") controller.enqueue(state.pending);
|
|
1570
|
+
}
|
|
1571
|
+
});
|
|
1572
|
+
}
|
|
1573
|
+
//#endregion
|
|
1574
|
+
//#region src/read-build-answer.ts
|
|
1575
|
+
async function readBuildAnswer(response, onProgress, onLog) {
|
|
1576
|
+
if (response.ok && isStream(response)) return readBuildEvents(response, onProgress, onLog);
|
|
1577
|
+
const body = await response.json().catch(() => null);
|
|
1578
|
+
if (response.ok) return ImageBuildResultSchema.parse(body);
|
|
1579
|
+
const failure = ImageBuildErrorSchema.safeParse(body);
|
|
1580
|
+
if (failure.success) throw new ORPCError$1(failure.data.code, {
|
|
1581
|
+
status: response.status,
|
|
1582
|
+
message: failure.data.message
|
|
1583
|
+
});
|
|
1584
|
+
const code = response.status === 401 ? "UNAUTHORIZED" : "INTERNAL_SERVER_ERROR";
|
|
1585
|
+
throw new ORPCError$1(code, {
|
|
1586
|
+
status: response.status,
|
|
1587
|
+
message: `impd answered ${String(response.status)} to the image build`
|
|
1588
|
+
});
|
|
1589
|
+
}
|
|
1590
|
+
function isStream(response) {
|
|
1591
|
+
return response.headers.get("content-type")?.startsWith(IMAGE_BUILD_STREAM_TYPE) === true;
|
|
1592
|
+
}
|
|
1593
|
+
async function readBuildEvents(response, onProgress, onLog) {
|
|
1594
|
+
for await (const line of readLines(response)) {
|
|
1595
|
+
const parsed = ImageBuildEventSchema.safeParse(parseJson(line));
|
|
1596
|
+
if (!parsed.success) continue;
|
|
1597
|
+
const event = parsed.data;
|
|
1598
|
+
if (event.type === "image") return event.image;
|
|
1599
|
+
if (event.type === "error") throw new ORPCError$1(event.code, { message: event.message });
|
|
1600
|
+
if (event.type === "log") onLog?.(event.text);
|
|
1601
|
+
else onProgress?.(event);
|
|
1602
|
+
}
|
|
1603
|
+
throw new ORPCError$1("INTERNAL_SERVER_ERROR", { message: "the image build stream ended before impd answered the image" });
|
|
1604
|
+
}
|
|
1605
|
+
async function* readLines(response) {
|
|
1606
|
+
if (response.body === null) return;
|
|
1607
|
+
yield* response.body.pipeThrough(new TextDecoderStream()).pipeThrough(splitLines());
|
|
1608
|
+
}
|
|
1609
|
+
function parseJson(line) {
|
|
1610
|
+
try {
|
|
1611
|
+
return JSON.parse(line);
|
|
1612
|
+
} catch {
|
|
1613
|
+
return null;
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
//#endregion
|
|
1617
|
+
//#region src/run-image-build.ts
|
|
1618
|
+
async function runImageBuild(deps, name, context, options = {}) {
|
|
1619
|
+
const url = resolveImpdUrl(deps.baseUrl, IMAGE_BUILD_PATH);
|
|
1620
|
+
url.searchParams.set("name", name);
|
|
1621
|
+
if (options.dockerfile !== void 0) url.searchParams.set("dockerfile", options.dockerfile);
|
|
1622
|
+
const headers = {
|
|
1623
|
+
"content-type": "application/x-tar",
|
|
1624
|
+
accept: `${IMAGE_BUILD_STREAM_TYPE}, application/json`
|
|
1625
|
+
};
|
|
1626
|
+
if (deps.token !== null) headers["authorization"] = `Bearer ${deps.token}`;
|
|
1627
|
+
if (options.size !== void 0) headers["content-length"] = String(options.size);
|
|
1628
|
+
const init = {
|
|
1629
|
+
method: "POST",
|
|
1630
|
+
headers,
|
|
1631
|
+
body: context,
|
|
1632
|
+
duplex: "half",
|
|
1633
|
+
...options.signal !== void 0 && { signal: options.signal }
|
|
1634
|
+
};
|
|
1635
|
+
const request = new Request(url.href, init);
|
|
1636
|
+
return readBuildAnswer(await (deps.fetch ?? fetch)(request), options.onProgress, options.onLog);
|
|
1637
|
+
}
|
|
1638
|
+
//#endregion
|
|
1638
1639
|
//#region src/send-long-call.ts
|
|
1639
1640
|
const HOUR_MS = 36e5;
|
|
1640
1641
|
const LONG_CALL_DEADLINES_MS = /* @__PURE__ */ new Map([
|
|
@@ -1674,8 +1675,8 @@ async function sendLongCall(request, init, call) {
|
|
|
1674
1675
|
request.signal.removeEventListener("abort", onCallerAbort);
|
|
1675
1676
|
}
|
|
1676
1677
|
}
|
|
1677
|
-
function formatHours(
|
|
1678
|
-
const hours =
|
|
1678
|
+
function formatHours(durationMs) {
|
|
1679
|
+
const hours = durationMs / HOUR_MS;
|
|
1679
1680
|
return hours === 1 ? "an hour" : `${String(hours)} hours`;
|
|
1680
1681
|
}
|
|
1681
1682
|
//#endregion
|
|
@@ -1685,24 +1686,25 @@ function createImpClient(options) {
|
|
|
1685
1686
|
const customFetch = options.fetch;
|
|
1686
1687
|
const startTimer = options.startTimer ?? startRealTimer;
|
|
1687
1688
|
const sendCall = (request, init) => customFetch === void 0 ? fetch(request, init) : customFetch(request);
|
|
1689
|
+
const sendLinkRequest = async (request, init, _options, path) => {
|
|
1690
|
+
const procedure = path.join(".");
|
|
1691
|
+
const deadlineMs = LONG_CALL_DEADLINES_MS.get(procedure);
|
|
1692
|
+
const response = await (deadlineMs === void 0 ? sendCall(request, init) : sendLongCall(request, init, {
|
|
1693
|
+
procedure,
|
|
1694
|
+
deadlineMs,
|
|
1695
|
+
startTimer,
|
|
1696
|
+
send: customFetch ?? null
|
|
1697
|
+
}));
|
|
1698
|
+
if (isUnknownProcedure(response.status, response.headers.get("content-type"))) {
|
|
1699
|
+
await response.body?.cancel();
|
|
1700
|
+
throw new UnknownProcedureError(procedure);
|
|
1701
|
+
}
|
|
1702
|
+
return response;
|
|
1703
|
+
};
|
|
1688
1704
|
const link = new RPCLink({
|
|
1689
1705
|
url: resolveImpdUrl(options.url, "/rpc").href,
|
|
1690
1706
|
headers: token === void 0 ? {} : { authorization: `Bearer ${token}` },
|
|
1691
|
-
fetch:
|
|
1692
|
-
const procedure = path.join(".");
|
|
1693
|
-
const deadlineMs = LONG_CALL_DEADLINES_MS.get(procedure);
|
|
1694
|
-
const response = await (deadlineMs === void 0 ? sendCall(request, init) : sendLongCall(request, init, {
|
|
1695
|
-
procedure,
|
|
1696
|
-
deadlineMs,
|
|
1697
|
-
startTimer,
|
|
1698
|
-
send: customFetch ?? null
|
|
1699
|
-
}));
|
|
1700
|
-
if (isUnknownProcedure(response.status, response.headers.get("content-type"))) {
|
|
1701
|
-
await response.body?.cancel();
|
|
1702
|
-
throw new UnknownProcedureError(procedure);
|
|
1703
|
-
}
|
|
1704
|
-
return response;
|
|
1705
|
-
}
|
|
1707
|
+
fetch: sendLinkRequest
|
|
1706
1708
|
});
|
|
1707
1709
|
const rpc = createORPCClient(link);
|
|
1708
1710
|
const namespaces = {
|
|
@@ -1740,11 +1742,11 @@ function createImpClient(options) {
|
|
|
1740
1742
|
openAttach: (name, session, attachOptions) => openAttach(execDeps, name, session, attachOptions),
|
|
1741
1743
|
requireAwake: (name, awakeOptions) => requireAwake(rpc, name, awakeOptions),
|
|
1742
1744
|
checkServer: (checkOptions) => checkServer(rpc, checkOptions),
|
|
1743
|
-
buildImage: (name, context, buildOptions) =>
|
|
1745
|
+
buildImage: (name, context, buildOptions) => runImageBuild(execDeps, name, context, buildOptions)
|
|
1744
1746
|
};
|
|
1745
1747
|
}
|
|
1746
|
-
function startRealTimer(fire,
|
|
1747
|
-
const timer = setTimeout(fire,
|
|
1748
|
+
function startRealTimer(fire, durationMs) {
|
|
1749
|
+
const timer = setTimeout(fire, durationMs);
|
|
1748
1750
|
return () => {
|
|
1749
1751
|
clearTimeout(timer);
|
|
1750
1752
|
};
|
|
@@ -1757,14 +1759,6 @@ function isUnknownProcedure(status, contentType) {
|
|
|
1757
1759
|
var ToolExecError = class extends Error {
|
|
1758
1760
|
name = "ToolExecError";
|
|
1759
1761
|
};
|
|
1760
|
-
function parseServerMessage(text) {
|
|
1761
|
-
try {
|
|
1762
|
-
const parsed = ExecServerMessageSchema.safeParse(JSON.parse(text));
|
|
1763
|
-
return parsed.success ? parsed.data : null;
|
|
1764
|
-
} catch {
|
|
1765
|
-
return null;
|
|
1766
|
-
}
|
|
1767
|
-
}
|
|
1768
1762
|
async function openToolExec(options) {
|
|
1769
1763
|
const headers = options.token === null ? {} : { authorization: `Bearer ${options.token}` };
|
|
1770
1764
|
const ws = options.connect(resolveImpdWebSocketUrl(options.baseUrl, EXEC_PATH).href, headers);
|
|
@@ -1868,6 +1862,14 @@ async function openToolExec(options) {
|
|
|
1868
1862
|
}
|
|
1869
1863
|
};
|
|
1870
1864
|
}
|
|
1865
|
+
function parseServerMessage(text) {
|
|
1866
|
+
try {
|
|
1867
|
+
const parsed = ExecServerMessageSchema.safeParse(JSON.parse(text));
|
|
1868
|
+
return parsed.success ? parsed.data : null;
|
|
1869
|
+
} catch {
|
|
1870
|
+
return null;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1871
1873
|
//#endregion
|
|
1872
1874
|
//#region src/events/watch-events.ts
|
|
1873
1875
|
async function watchEvents(client, options) {
|
|
@@ -2081,110 +2083,6 @@ function openForwardTunnel(options) {
|
|
|
2081
2083
|
}
|
|
2082
2084
|
//#endregion
|
|
2083
2085
|
//#region src/tunnel/open-reverse-forward.ts
|
|
2084
|
-
function buildListenMessage(name, guest) {
|
|
2085
|
-
return guest.network === "tcp" ? {
|
|
2086
|
-
type: "listen",
|
|
2087
|
-
name,
|
|
2088
|
-
network: "tcp",
|
|
2089
|
-
port: guest.port
|
|
2090
|
-
} : {
|
|
2091
|
-
type: "listen",
|
|
2092
|
-
name,
|
|
2093
|
-
network: "unix",
|
|
2094
|
-
path: guest.path
|
|
2095
|
-
};
|
|
2096
|
-
}
|
|
2097
|
-
function openRelay(target, accept, handlers) {
|
|
2098
|
-
const ws = target.connect(target.url, target.headers);
|
|
2099
|
-
const state = {
|
|
2100
|
-
opened: false,
|
|
2101
|
-
unacked: 0,
|
|
2102
|
-
done: false,
|
|
2103
|
-
peerEof: false,
|
|
2104
|
-
guestEof: false
|
|
2105
|
-
};
|
|
2106
|
-
const waiters = [];
|
|
2107
|
-
const pending = [];
|
|
2108
|
-
ws.binaryType = "arraybuffer";
|
|
2109
|
-
const wakeWaiters = () => {
|
|
2110
|
-
for (const wake of waiters.splice(0)) wake();
|
|
2111
|
-
};
|
|
2112
|
-
const sendText = (message) => {
|
|
2113
|
-
if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message));
|
|
2114
|
-
};
|
|
2115
|
-
const sendFrames = (data) => {
|
|
2116
|
-
for (let offset = 0; offset < data.byteLength; offset += TUNNEL_MAX_FRAME_BYTES) ws.send(Uint8Array.from(data.subarray(offset, offset + TUNNEL_MAX_FRAME_BYTES)));
|
|
2117
|
-
};
|
|
2118
|
-
const stopRelay = (lost) => {
|
|
2119
|
-
if (state.done) return;
|
|
2120
|
-
state.done = true;
|
|
2121
|
-
wakeWaiters();
|
|
2122
|
-
handlers.onClose(lost);
|
|
2123
|
-
};
|
|
2124
|
-
const sendAck = async (data) => {
|
|
2125
|
-
await handlers.onData(data);
|
|
2126
|
-
sendText({
|
|
2127
|
-
type: "ack",
|
|
2128
|
-
bytes: data.byteLength
|
|
2129
|
-
});
|
|
2130
|
-
};
|
|
2131
|
-
const handleControl = (message) => {
|
|
2132
|
-
match(message).with({ type: "opened" }, () => {
|
|
2133
|
-
state.opened = true;
|
|
2134
|
-
for (const data of pending.splice(0)) sendFrames(data);
|
|
2135
|
-
if (state.peerEof) sendText({ type: "eof" });
|
|
2136
|
-
}).with({ type: "eof" }, () => {
|
|
2137
|
-
state.guestEof = true;
|
|
2138
|
-
handlers.onEof();
|
|
2139
|
-
}).with({ type: "ack" }, (ack) => {
|
|
2140
|
-
state.unacked -= ack.bytes;
|
|
2141
|
-
if (state.unacked <= 1048576) wakeWaiters();
|
|
2142
|
-
}).with({ type: P.union("error", "listening", "connection") }, () => {
|
|
2143
|
-
ws.close(TUNNEL_CLOSE_NORMAL, "relay failed");
|
|
2144
|
-
stopRelay(true);
|
|
2145
|
-
}).exhaustive();
|
|
2146
|
-
};
|
|
2147
|
-
ws.addEventListener("open", () => {
|
|
2148
|
-
sendText(accept);
|
|
2149
|
-
});
|
|
2150
|
-
ws.addEventListener("message", (event) => {
|
|
2151
|
-
if (event.data instanceof ArrayBuffer) {
|
|
2152
|
-
sendAck(new Uint8Array(event.data));
|
|
2153
|
-
return;
|
|
2154
|
-
}
|
|
2155
|
-
const message = parseTunnelServerMessage(String(event.data));
|
|
2156
|
-
if (message === null) {
|
|
2157
|
-
ws.close(TUNNEL_CLOSE_PROTOCOL, "bad message");
|
|
2158
|
-
stopRelay(true);
|
|
2159
|
-
} else handleControl(message);
|
|
2160
|
-
});
|
|
2161
|
-
ws.addEventListener("close", (event) => {
|
|
2162
|
-
stopRelay(event.code !== TUNNEL_CLOSE_NORMAL);
|
|
2163
|
-
});
|
|
2164
|
-
ws.addEventListener("error", () => {});
|
|
2165
|
-
return {
|
|
2166
|
-
send: (data) => {
|
|
2167
|
-
if (state.done) return true;
|
|
2168
|
-
if (state.opened) sendFrames(data);
|
|
2169
|
-
else pending.push(Uint8Array.from(data));
|
|
2170
|
-
state.unacked += data.byteLength;
|
|
2171
|
-
return state.unacked <= TUNNEL_WINDOW_BYTES;
|
|
2172
|
-
},
|
|
2173
|
-
waitForRoom: async () => {
|
|
2174
|
-
while (!state.done && state.unacked > 1048576) await new Promise((resolve) => {
|
|
2175
|
-
waiters.push(resolve);
|
|
2176
|
-
});
|
|
2177
|
-
},
|
|
2178
|
-
sendEof: () => {
|
|
2179
|
-
state.peerEof = true;
|
|
2180
|
-
if (state.opened) sendText({ type: "eof" });
|
|
2181
|
-
},
|
|
2182
|
-
close: () => {
|
|
2183
|
-
if (ws.readyState === WebSocket.CONNECTING || ws.readyState === WebSocket.OPEN) ws.close(TUNNEL_CLOSE_NORMAL, "local side closed");
|
|
2184
|
-
stopRelay(false);
|
|
2185
|
-
}
|
|
2186
|
-
};
|
|
2187
|
-
}
|
|
2188
2086
|
function openReverseForward(options) {
|
|
2189
2087
|
const url = resolveImpdWebSocketUrl(options.baseUrl, TUNNEL_PATH);
|
|
2190
2088
|
const headers = options.token === null ? {} : { authorization: `Bearer ${options.token}` };
|
|
@@ -2280,5 +2178,109 @@ function openReverseForward(options) {
|
|
|
2280
2178
|
}
|
|
2281
2179
|
};
|
|
2282
2180
|
}
|
|
2181
|
+
function openRelay(target, accept, handlers) {
|
|
2182
|
+
const ws = target.connect(target.url, target.headers);
|
|
2183
|
+
const state = {
|
|
2184
|
+
opened: false,
|
|
2185
|
+
unacked: 0,
|
|
2186
|
+
done: false,
|
|
2187
|
+
peerEof: false,
|
|
2188
|
+
guestEof: false
|
|
2189
|
+
};
|
|
2190
|
+
const waiters = [];
|
|
2191
|
+
const pending = [];
|
|
2192
|
+
ws.binaryType = "arraybuffer";
|
|
2193
|
+
const wakeWaiters = () => {
|
|
2194
|
+
for (const wake of waiters.splice(0)) wake();
|
|
2195
|
+
};
|
|
2196
|
+
const sendText = (message) => {
|
|
2197
|
+
if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message));
|
|
2198
|
+
};
|
|
2199
|
+
const sendFrames = (data) => {
|
|
2200
|
+
for (let offset = 0; offset < data.byteLength; offset += TUNNEL_MAX_FRAME_BYTES) ws.send(Uint8Array.from(data.subarray(offset, offset + TUNNEL_MAX_FRAME_BYTES)));
|
|
2201
|
+
};
|
|
2202
|
+
const stopRelay = (lost) => {
|
|
2203
|
+
if (state.done) return;
|
|
2204
|
+
state.done = true;
|
|
2205
|
+
wakeWaiters();
|
|
2206
|
+
handlers.onClose(lost);
|
|
2207
|
+
};
|
|
2208
|
+
const sendAck = async (data) => {
|
|
2209
|
+
await handlers.onData(data);
|
|
2210
|
+
sendText({
|
|
2211
|
+
type: "ack",
|
|
2212
|
+
bytes: data.byteLength
|
|
2213
|
+
});
|
|
2214
|
+
};
|
|
2215
|
+
const handleControl = (message) => {
|
|
2216
|
+
match(message).with({ type: "opened" }, () => {
|
|
2217
|
+
state.opened = true;
|
|
2218
|
+
for (const data of pending.splice(0)) sendFrames(data);
|
|
2219
|
+
if (state.peerEof) sendText({ type: "eof" });
|
|
2220
|
+
}).with({ type: "eof" }, () => {
|
|
2221
|
+
state.guestEof = true;
|
|
2222
|
+
handlers.onEof();
|
|
2223
|
+
}).with({ type: "ack" }, (ack) => {
|
|
2224
|
+
state.unacked -= ack.bytes;
|
|
2225
|
+
if (state.unacked <= 1048576) wakeWaiters();
|
|
2226
|
+
}).with({ type: P.union("error", "listening", "connection") }, () => {
|
|
2227
|
+
ws.close(TUNNEL_CLOSE_NORMAL, "relay failed");
|
|
2228
|
+
stopRelay(true);
|
|
2229
|
+
}).exhaustive();
|
|
2230
|
+
};
|
|
2231
|
+
ws.addEventListener("open", () => {
|
|
2232
|
+
sendText(accept);
|
|
2233
|
+
});
|
|
2234
|
+
ws.addEventListener("message", (event) => {
|
|
2235
|
+
if (event.data instanceof ArrayBuffer) {
|
|
2236
|
+
sendAck(new Uint8Array(event.data));
|
|
2237
|
+
return;
|
|
2238
|
+
}
|
|
2239
|
+
const message = parseTunnelServerMessage(String(event.data));
|
|
2240
|
+
if (message === null) {
|
|
2241
|
+
ws.close(TUNNEL_CLOSE_PROTOCOL, "bad message");
|
|
2242
|
+
stopRelay(true);
|
|
2243
|
+
} else handleControl(message);
|
|
2244
|
+
});
|
|
2245
|
+
ws.addEventListener("close", (event) => {
|
|
2246
|
+
stopRelay(event.code !== TUNNEL_CLOSE_NORMAL);
|
|
2247
|
+
});
|
|
2248
|
+
ws.addEventListener("error", () => {});
|
|
2249
|
+
return {
|
|
2250
|
+
send: (data) => {
|
|
2251
|
+
if (state.done) return true;
|
|
2252
|
+
if (state.opened) sendFrames(data);
|
|
2253
|
+
else pending.push(Uint8Array.from(data));
|
|
2254
|
+
state.unacked += data.byteLength;
|
|
2255
|
+
return state.unacked <= TUNNEL_WINDOW_BYTES;
|
|
2256
|
+
},
|
|
2257
|
+
waitForRoom: async () => {
|
|
2258
|
+
while (!state.done && state.unacked > 1048576) await new Promise((resolve) => {
|
|
2259
|
+
waiters.push(resolve);
|
|
2260
|
+
});
|
|
2261
|
+
},
|
|
2262
|
+
sendEof: () => {
|
|
2263
|
+
state.peerEof = true;
|
|
2264
|
+
if (state.opened) sendText({ type: "eof" });
|
|
2265
|
+
},
|
|
2266
|
+
close: () => {
|
|
2267
|
+
if (ws.readyState === WebSocket.CONNECTING || ws.readyState === WebSocket.OPEN) ws.close(TUNNEL_CLOSE_NORMAL, "local side closed");
|
|
2268
|
+
stopRelay(false);
|
|
2269
|
+
}
|
|
2270
|
+
};
|
|
2271
|
+
}
|
|
2272
|
+
function buildListenMessage(name, guest) {
|
|
2273
|
+
return guest.network === "tcp" ? {
|
|
2274
|
+
type: "listen",
|
|
2275
|
+
name,
|
|
2276
|
+
network: "tcp",
|
|
2277
|
+
port: guest.port
|
|
2278
|
+
} : {
|
|
2279
|
+
type: "listen",
|
|
2280
|
+
name,
|
|
2281
|
+
network: "unix",
|
|
2282
|
+
path: guest.path
|
|
2283
|
+
};
|
|
2284
|
+
}
|
|
2283
2285
|
//#endregion
|
|
2284
|
-
export { CLIENT_VERSION, CONSOLE_SHELL, EXEC_REQUIREMENTS, ExecError, InvalidResumeError, InvalidStateError, MissingFeatureError, NoSessionError, ORPCError, UnknownProcedureError, createImpClient, findMissingFeatures, isDefinedError, openExecSession, openForwardTunnel, openReverseForward, openToolExec, readFeatures, requireFeatures, resolveImpdWebSocketUrl, safe, watchEvents };
|
|
2286
|
+
export { CLIENT_VERSION, CONSOLE_SHELL, EXEC_REQUIREMENTS, ExecError, InvalidResumeError, InvalidStateError, MissingFeatureError, NoSessionError, ORPCError, UnknownProcedureError, createImpClient, collectMissingFeatures as findMissingFeatures, isDefinedError, openExecSession, openForwardTunnel, openReverseForward, openToolExec, readFeatures, requireFeatures, resolveImpdWebSocketUrl, safe, watchEvents };
|