@dotcraft/sdk 0.10.0 → 0.10.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/generated/appserver/method-groups.generated.d.ts +1 -1
- package/dist/generated/appserver/method-groups.generated.js +1 -1
- package/dist/generated/appserver/models.generated.d.ts +2 -0
- package/dist/generated/appserver/models.generated.d.ts.map +1 -1
- package/dist/generated/appserver/protocol-info.generated.d.ts +2 -2
- package/dist/generated/appserver/protocol-info.generated.js +2 -2
- package/dist/hubBootstrap.d.ts +13 -10
- package/dist/hubBootstrap.d.ts.map +1 -1
- package/dist/hubBootstrap.js +68 -77
- package/dist/hubBootstrap.js.map +1 -1
- package/dist/hubBootstrap.test.d.ts +2 -0
- package/dist/hubBootstrap.test.d.ts.map +1 -0
- package/dist/hubBootstrap.test.js +278 -0
- package/dist/hubBootstrap.test.js.map +1 -0
- package/dist/hubBootstrapFlight.d.ts +4 -0
- package/dist/hubBootstrapFlight.d.ts.map +1 -0
- package/dist/hubBootstrapFlight.js +56 -0
- package/dist/hubBootstrapFlight.js.map +1 -0
- package/dist/hubClient.d.ts +33 -187
- package/dist/hubClient.d.ts.map +1 -1
- package/dist/hubClient.js +127 -99
- package/dist/hubClient.js.map +1 -1
- package/dist/hubClient.test.js +23 -31
- package/dist/hubClient.test.js.map +1 -1
- package/dist/hubStartupProcess.d.ts +16 -0
- package/dist/hubStartupProcess.d.ts.map +1 -0
- package/dist/hubStartupProcess.js +103 -0
- package/dist/hubStartupProcess.js.map +1 -0
- package/dist/hubTypes.d.ts +161 -0
- package/dist/hubTypes.d.ts.map +1 -0
- package/dist/hubTypes.js +2 -0
- package/dist/hubTypes.js.map +1 -0
- package/package.json +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.10.
|
|
1
|
+
export declare const SDK_VERSION = "0.10.2";
|
|
2
2
|
export declare const CONTRACT_FORMAT_VERSION = 1;
|
|
3
3
|
export declare const CONTRACT_VERSION = "0.1.0";
|
|
4
4
|
export declare const APPSERVER_PROTOCOL_VERSION = "1";
|
|
5
|
-
export declare const CONTRACT_SHA256 = "
|
|
5
|
+
export declare const CONTRACT_SHA256 = "8f2c0eb8ca4935daa6275c1e5bf564d81f0bbd8128f66fac0ac6f37f066ccb47";
|
|
6
6
|
//# sourceMappingURL=protocol-info.generated.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// <auto-generated />
|
|
2
|
-
export const SDK_VERSION = "0.10.
|
|
2
|
+
export const SDK_VERSION = "0.10.2";
|
|
3
3
|
export const CONTRACT_FORMAT_VERSION = 1;
|
|
4
4
|
export const CONTRACT_VERSION = "0.1.0";
|
|
5
5
|
export const APPSERVER_PROTOCOL_VERSION = "1";
|
|
6
|
-
export const CONTRACT_SHA256 = "
|
|
6
|
+
export const CONTRACT_SHA256 = "8f2c0eb8ca4935daa6275c1e5bf564d81f0bbd8128f66fac0ac6f37f066ccb47";
|
|
7
7
|
//# sourceMappingURL=protocol-info.generated.js.map
|
package/dist/hubBootstrap.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { type StartupFailure } from "./hubStartupProcess.js";
|
|
2
|
+
import type { HubClientOptions, HubLockInfo, HubStatusResponse } from "./hubTypes.js";
|
|
3
|
+
export interface HubConnection {
|
|
4
|
+
hub: HubLockInfo;
|
|
5
|
+
status: HubStatusResponse;
|
|
6
|
+
}
|
|
7
|
+
type ProbeResult = (HubConnection | {
|
|
8
|
+
hub: null;
|
|
9
|
+
status?: never;
|
|
10
|
+
}) & {
|
|
8
11
|
httpStatus?: number;
|
|
9
|
-
error?:
|
|
12
|
+
error?: StartupFailure;
|
|
10
13
|
};
|
|
11
14
|
interface BootstrapDependencies {
|
|
12
15
|
lockPath: string;
|
|
@@ -16,7 +19,7 @@ interface BootstrapDependencies {
|
|
|
16
19
|
error(code: string, message: string, details: unknown): Error;
|
|
17
20
|
}
|
|
18
21
|
export declare function isProcessAlive(pid: number): boolean;
|
|
19
|
-
export declare function probeLocalHub(info: HubLockInfo, validateBaseUrl: (value: string) => unknown, timeoutMs?: number): Promise<ProbeResult>;
|
|
20
|
-
export declare function ensureLocalHub(options: HubClientOptions, deps: BootstrapDependencies): Promise<
|
|
22
|
+
export declare function probeLocalHub(info: HubLockInfo, validateBaseUrl: (value: string) => unknown, timeoutMs?: number, cancellation?: AbortSignal): Promise<ProbeResult>;
|
|
23
|
+
export declare function ensureLocalHub(options: HubClientOptions, deps: BootstrapDependencies, signal: AbortSignal): Promise<HubConnection>;
|
|
21
24
|
export {};
|
|
22
25
|
//# sourceMappingURL=hubBootstrap.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hubBootstrap.d.ts","sourceRoot":"","sources":["../src/hubBootstrap.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hubBootstrap.d.ts","sourceRoot":"","sources":["../src/hubBootstrap.ts"],"names":[],"mappings":"AAEA,OAAO,EAA+D,KAAK,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAE1H,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAQtF,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,WAAW,CAAC;IACjB,MAAM,EAAE,iBAAiB,CAAC;CAC3B;AAED,KAAK,WAAW,GAAG,CAAC,aAAa,GAAG;IAAE,GAAG,EAAE,IAAI,CAAC;IAAC,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC,GAAG;IACnE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,cAAc,CAAC;CACxB,CAAC;AAEF,UAAU,qBAAqB;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,IAAI,WAAW,GAAG,IAAI,CAAC;IAC/B,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACxC,QAAQ,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAClE,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,KAAK,CAAC;CAC/D;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAQnD;AAED,wBAAsB,aAAa,CACjC,IAAI,EAAE,WAAW,EACjB,eAAe,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,EAC3C,SAAS,SAAmB,EAC5B,YAAY,CAAC,EAAE,WAAW,GACzB,OAAO,CAAC,WAAW,CAAC,CAuBtB;AAED,wBAAsB,cAAc,CAClC,OAAO,EAAE,gBAAgB,EACzB,IAAI,EAAE,qBAAqB,EAC3B,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,aAAa,CAAC,CAyHxB"}
|
package/dist/hubBootstrap.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import { resolve } from "node:path";
|
|
2
|
+
import { safeStartupFailure, startHubProcess } from "./hubStartupProcess.js";
|
|
3
3
|
const DEFAULT_STARTUP_TIMEOUT_MS = 15_000;
|
|
4
4
|
const DEFAULT_SHUTDOWN_TIMEOUT_MS = 30_000;
|
|
5
5
|
const PROBE_TIMEOUT_MS = 2_000;
|
|
@@ -15,36 +15,35 @@ export function isProcessAlive(pid) {
|
|
|
15
15
|
return error.code === "EPERM";
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
export async function probeLocalHub(info, validateBaseUrl, timeoutMs = PROBE_TIMEOUT_MS) {
|
|
18
|
+
export async function probeLocalHub(info, validateBaseUrl, timeoutMs = PROBE_TIMEOUT_MS, cancellation) {
|
|
19
|
+
cancellation?.throwIfAborted();
|
|
19
20
|
try {
|
|
20
21
|
validateBaseUrl(info.apiBaseUrl);
|
|
21
|
-
const
|
|
22
|
+
const timeout = AbortSignal.timeout(Math.max(1, Math.floor(timeoutMs)));
|
|
23
|
+
const signal = cancellation ? AbortSignal.any([cancellation, timeout]) : timeout;
|
|
22
24
|
const response = await fetch(`${info.apiBaseUrl}/v1/status`, { signal });
|
|
23
25
|
if (!response.ok)
|
|
24
26
|
return { hub: null, httpStatus: response.status };
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
if (signal.aborted)
|
|
31
|
-
throw error;
|
|
32
|
-
// A status response can be empty during startup races.
|
|
33
|
-
}
|
|
27
|
+
const status = await response.json();
|
|
28
|
+
if (!status)
|
|
29
|
+
return { hub: null, httpStatus: response.status };
|
|
34
30
|
return {
|
|
35
31
|
hub: {
|
|
36
32
|
...info,
|
|
37
33
|
version: typeof status?.hubVersion === "string" ? status.hubVersion : info.version,
|
|
38
34
|
binaryPath: typeof status?.binaryPath === "string" ? status.binaryPath : info.binaryPath,
|
|
39
35
|
},
|
|
36
|
+
status,
|
|
40
37
|
httpStatus: response.status,
|
|
41
38
|
};
|
|
42
39
|
}
|
|
43
40
|
catch (error) {
|
|
44
|
-
|
|
41
|
+
cancellation?.throwIfAborted();
|
|
42
|
+
return { hub: null, error: safeStartupFailure(error) };
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
|
-
export async function ensureLocalHub(options, deps) {
|
|
45
|
+
export async function ensureLocalHub(options, deps, signal) {
|
|
46
|
+
signal.throwIfAborted();
|
|
48
47
|
const startedAt = Date.now();
|
|
49
48
|
const shutdownBudgetMs = options.hubShutdownTimeoutMs ?? DEFAULT_SHUTDOWN_TIMEOUT_MS;
|
|
50
49
|
const startupBudgetMs = options.hubStartupTimeoutMs ?? DEFAULT_STARTUP_TIMEOUT_MS;
|
|
@@ -53,24 +52,33 @@ export async function ensureLocalHub(options, deps) {
|
|
|
53
52
|
let startupStartedAt = null;
|
|
54
53
|
let child = null;
|
|
55
54
|
let lastOwner = null;
|
|
55
|
+
let lastToken;
|
|
56
56
|
let lastProbe;
|
|
57
|
-
const details = (stage, mismatch) =>
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
57
|
+
const details = (stage, mismatch) => {
|
|
58
|
+
const stderr = child?.recentStderr();
|
|
59
|
+
const recentStderr = lastToken ? stderr?.replaceAll(lastToken, "[redacted]") : stderr;
|
|
60
|
+
return {
|
|
61
|
+
stage,
|
|
62
|
+
elapsedMs: Date.now() - (startupStartedAt ?? startedAt),
|
|
63
|
+
budgetMs: startupStartedAt === null ? shutdownBudgetMs : startupBudgetMs,
|
|
64
|
+
lockPath: deps.lockPath,
|
|
65
|
+
...(lastOwner ? ownerDetails(lastOwner) : {}),
|
|
66
|
+
...(options.executable ? { executable: options.executable } : {}),
|
|
67
|
+
...(options.expectedExecutable ? { expectedExecutable: options.expectedExecutable } : {}),
|
|
68
|
+
...mismatch,
|
|
69
|
+
...(lastProbe ? { lastProbe } : {}),
|
|
70
|
+
...(child?.pid !== undefined ? { spawnedPid: child.pid } : {}),
|
|
71
|
+
...(child?.spawnError ? { spawnError: child.spawnError } : {}),
|
|
72
|
+
...(child?.exitCode !== undefined ? { exitCode: child.exitCode, signal: child.signal } : {}),
|
|
73
|
+
...(recentStderr ? { recentStderr } : {}),
|
|
74
|
+
};
|
|
75
|
+
};
|
|
71
76
|
try {
|
|
72
77
|
for (;;) {
|
|
78
|
+
signal.throwIfAborted();
|
|
73
79
|
const lock = deps.readLock();
|
|
80
|
+
if (lock?.token)
|
|
81
|
+
lastToken = lock.token;
|
|
74
82
|
const owner = lock && isProcessAlive(lock.pid) ? lock : null;
|
|
75
83
|
if (owner && owner.pid === stopping?.pid) {
|
|
76
84
|
if (Date.now() >= shutdownDeadline) {
|
|
@@ -80,17 +88,17 @@ export async function ensureLocalHub(options, deps) {
|
|
|
80
88
|
else if (owner) {
|
|
81
89
|
lastOwner = owner;
|
|
82
90
|
const deadline = startupStartedAt === null ? shutdownDeadline : startupStartedAt + startupBudgetMs;
|
|
83
|
-
const probe = await probeLocalHub(owner, deps.validateBaseUrl, Math.min(PROBE_TIMEOUT_MS, deadline - Date.now()));
|
|
84
|
-
const { hub: live, ...diagnostic } = probe;
|
|
91
|
+
const probe = await probeLocalHub(owner, deps.validateBaseUrl, Math.min(PROBE_TIMEOUT_MS, deadline - Date.now()), signal);
|
|
92
|
+
const { hub: live, status, ...diagnostic } = probe;
|
|
85
93
|
lastProbe = diagnostic;
|
|
86
94
|
if (live)
|
|
87
95
|
lastOwner = live;
|
|
88
96
|
if (live && startupStartedAt !== null)
|
|
89
|
-
return live;
|
|
97
|
+
return { hub: live, status };
|
|
90
98
|
if (live) {
|
|
91
99
|
const mismatch = binaryMismatch(options, live);
|
|
92
100
|
if (!mismatch || options.binaryMatchPolicy === "ignore" || !options.binaryMatchPolicy)
|
|
93
|
-
return live;
|
|
101
|
+
return { hub: live, status };
|
|
94
102
|
if (options.binaryMatchPolicy === "errorIfMismatch") {
|
|
95
103
|
throw deps.error("hubBinaryMismatch", "Hub is running from a different executable.", mismatch);
|
|
96
104
|
}
|
|
@@ -98,15 +106,16 @@ export async function ensureLocalHub(options, deps) {
|
|
|
98
106
|
if (remainingMs <= 0) {
|
|
99
107
|
throw deps.error("hubMismatchShutdownTimeout", "Hub uses a different executable and could not be stopped before the shutdown deadline.", details("mismatch-shutdown", mismatch));
|
|
100
108
|
}
|
|
101
|
-
const
|
|
109
|
+
const timeout = AbortSignal.timeout(Math.max(1, Math.floor(remainingMs)));
|
|
102
110
|
try {
|
|
103
|
-
await deps.shutdown(live, signal);
|
|
111
|
+
await deps.shutdown(live, AbortSignal.any([signal, timeout]));
|
|
104
112
|
}
|
|
105
113
|
catch (error) {
|
|
106
|
-
|
|
114
|
+
signal.throwIfAborted();
|
|
115
|
+
const timedOut = timeout.aborted;
|
|
107
116
|
throw deps.error(timedOut ? "hubMismatchShutdownTimeout" : "hubMismatchShutdownFailed", timedOut
|
|
108
117
|
? "Hub uses a different executable and its shutdown request timed out."
|
|
109
|
-
: "Hub uses a different executable and could not be stopped.", { ...details("mismatch-shutdown", mismatch), shutdownError:
|
|
118
|
+
: "Hub uses a different executable and could not be stopped.", { ...details("mismatch-shutdown", mismatch), shutdownError: safeStartupFailure(error) });
|
|
110
119
|
}
|
|
111
120
|
stopping = { pid: live.pid, mismatch };
|
|
112
121
|
}
|
|
@@ -116,50 +125,43 @@ export async function ensureLocalHub(options, deps) {
|
|
|
116
125
|
}
|
|
117
126
|
else if (startupStartedAt === null) {
|
|
118
127
|
startupStartedAt = Date.now();
|
|
119
|
-
child =
|
|
128
|
+
child = startHubProcess(options.executable?.trim() || "dotcraft", deps.lockPath, signal);
|
|
120
129
|
}
|
|
121
130
|
if (child?.spawnError) {
|
|
122
131
|
throw deps.error("hubUnavailable", "DotCraft Hub could not be started.", details("spawn"));
|
|
123
132
|
}
|
|
133
|
+
if (child?.exitCode !== undefined && (!owner || owner.pid === child.pid)) {
|
|
134
|
+
throw deps.error("hubUnavailable", "DotCraft Hub exited before becoming ready.", {
|
|
135
|
+
...details("ready"), failureKind: "processExited",
|
|
136
|
+
});
|
|
137
|
+
}
|
|
124
138
|
if (startupStartedAt !== null && Date.now() >= startupStartedAt + startupBudgetMs) {
|
|
125
|
-
throw deps.error("hubUnavailable", "DotCraft Hub could not be started.",
|
|
139
|
+
throw deps.error("hubUnavailable", "DotCraft Hub could not be started.", {
|
|
140
|
+
...details("ready"), failureKind: "timeout",
|
|
141
|
+
});
|
|
126
142
|
}
|
|
127
143
|
const deadline = startupStartedAt === null ? shutdownDeadline : startupStartedAt + startupBudgetMs;
|
|
128
|
-
await
|
|
144
|
+
await waitForNextProbe(Math.max(1, Math.min(POLL_MS, deadline - Date.now())), signal, child?.exitCode === undefined ? child?.changed : undefined);
|
|
129
145
|
}
|
|
130
146
|
}
|
|
131
147
|
finally {
|
|
132
148
|
child?.dispose();
|
|
133
149
|
}
|
|
134
150
|
}
|
|
135
|
-
function
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
state.pid = child.pid;
|
|
145
|
-
const onError = (error) => { state.spawnError = safeFailure(error); };
|
|
146
|
-
const onExit = (code, signal) => {
|
|
147
|
-
state.exitCode = code;
|
|
148
|
-
state.signal = signal;
|
|
149
|
-
};
|
|
150
|
-
child.on("error", onError);
|
|
151
|
-
child.on("exit", onExit);
|
|
152
|
-
child.unref();
|
|
153
|
-
state.dispose = () => {
|
|
154
|
-
child.off("error", onError);
|
|
155
|
-
child.off("exit", onExit);
|
|
156
|
-
child.on("error", () => { });
|
|
151
|
+
async function waitForNextProbe(ms, signal, changed) {
|
|
152
|
+
signal.throwIfAborted();
|
|
153
|
+
await new Promise((resolve, reject) => {
|
|
154
|
+
const finish = () => { cleanup(); resolve(); };
|
|
155
|
+
const abort = () => { cleanup(); reject(signal.reason); };
|
|
156
|
+
const timer = setTimeout(finish, ms);
|
|
157
|
+
const cleanup = () => {
|
|
158
|
+
clearTimeout(timer);
|
|
159
|
+
signal.removeEventListener("abort", abort);
|
|
157
160
|
};
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return state;
|
|
161
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
162
|
+
void changed?.then(finish);
|
|
163
|
+
});
|
|
164
|
+
signal.throwIfAborted();
|
|
163
165
|
}
|
|
164
166
|
function ownerDetails(owner) {
|
|
165
167
|
return {
|
|
@@ -169,17 +171,6 @@ function ownerDetails(owner) {
|
|
|
169
171
|
...(typeof owner.binaryPath === "string" ? { binaryPath: owner.binaryPath } : {}),
|
|
170
172
|
};
|
|
171
173
|
}
|
|
172
|
-
function safeFailure(error) {
|
|
173
|
-
if (!error || typeof error !== "object")
|
|
174
|
-
return {};
|
|
175
|
-
const failure = error;
|
|
176
|
-
const name = identifier(failure.name);
|
|
177
|
-
const code = identifier(failure.code) ?? identifier(failure.cause?.code);
|
|
178
|
-
return { ...(name ? { name } : {}), ...(code ? { code } : {}) };
|
|
179
|
-
}
|
|
180
|
-
function identifier(value) {
|
|
181
|
-
return typeof value === "string" && /^[a-zA-Z][a-zA-Z0-9_]{0,63}$/.test(value) ? value : undefined;
|
|
182
|
-
}
|
|
183
174
|
function binaryMismatch(options, hub) {
|
|
184
175
|
const expected = options.expectedExecutable?.trim();
|
|
185
176
|
if (!expected)
|
package/dist/hubBootstrap.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hubBootstrap.js","sourceRoot":"","sources":["../src/hubBootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"hubBootstrap.js","sourceRoot":"","sources":["../src/hubBootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAA+C,MAAM,wBAAwB,CAAC;AAI1H,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAC1C,MAAM,2BAA2B,GAAG,MAAM,CAAC;AAC3C,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAC/B,MAAM,OAAO,GAAG,GAAG,CAAC;AAqBpB,MAAM,UAAU,cAAc,CAAC,GAAW;IACxC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IACrD,IAAI,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf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,CAAC;QAAE,OAAO,IAAI,CAAC;IACrE,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,EAAE,CAAC;AACpE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hubBootstrap.test.d.ts","sourceRoot":"","sources":["../src/hubBootstrap.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { existsSync, unlinkSync } from "node:fs";
|
|
3
|
+
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { createServer } from "node:http";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import test from "node:test";
|
|
8
|
+
import { HubClient, HubClientError, hubLockPath } from "./hubClient.js";
|
|
9
|
+
function deferred() {
|
|
10
|
+
let resolve;
|
|
11
|
+
const promise = new Promise((done) => { resolve = done; });
|
|
12
|
+
return { promise, resolve };
|
|
13
|
+
}
|
|
14
|
+
async function fixture(t) {
|
|
15
|
+
const homeDir = await mkdtemp(join(tmpdir(), "dotcraft-bootstrap-"));
|
|
16
|
+
const originalCwd = process.cwd();
|
|
17
|
+
const originalFetch = globalThis.fetch;
|
|
18
|
+
const lockPath = hubLockPath(homeDir);
|
|
19
|
+
await mkdir(join(homeDir, ".craft", "hub"), { recursive: true });
|
|
20
|
+
const cleanups = [];
|
|
21
|
+
t.after(async () => {
|
|
22
|
+
for (const cleanup of cleanups.reverse())
|
|
23
|
+
await cleanup();
|
|
24
|
+
globalThis.fetch = originalFetch;
|
|
25
|
+
process.chdir(originalCwd);
|
|
26
|
+
await rm(homeDir, { recursive: true, force: true, maxRetries: 20, retryDelay: 100 });
|
|
27
|
+
});
|
|
28
|
+
const writeOwner = async (apiBaseUrl = "http://127.0.0.1:49170") => {
|
|
29
|
+
await writeFile(lockPath, JSON.stringify({ pid: process.pid, apiBaseUrl, token: "fixture-token" }));
|
|
30
|
+
};
|
|
31
|
+
return { homeDir, lockPath, writeOwner, cleanups };
|
|
32
|
+
}
|
|
33
|
+
async function startingHub(t) {
|
|
34
|
+
const base = await fixture(t);
|
|
35
|
+
const started = deferred();
|
|
36
|
+
const probing = deferred();
|
|
37
|
+
const ready = deferred();
|
|
38
|
+
const pids = [];
|
|
39
|
+
const server = createServer((request, response) => {
|
|
40
|
+
if (request.url?.startsWith("/started?")) {
|
|
41
|
+
pids.push(Number(new URL(request.url, "http://localhost").searchParams.get("pid")));
|
|
42
|
+
response.end();
|
|
43
|
+
started.resolve();
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
probing.resolve();
|
|
47
|
+
void ready.promise.then(() => response.end(JSON.stringify({
|
|
48
|
+
hubVersion: "fixture", pid: pids[0], capabilities: {},
|
|
49
|
+
})));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
53
|
+
const address = server.address();
|
|
54
|
+
assert.ok(address && typeof address === "object");
|
|
55
|
+
const apiBaseUrl = `http://127.0.0.1:${address.port}`;
|
|
56
|
+
const spawnsPath = join(base.homeDir, "spawns");
|
|
57
|
+
await writeFile(join(base.homeDir, "hub"), `
|
|
58
|
+
const fs = require("node:fs");
|
|
59
|
+
fs.appendFileSync(${JSON.stringify(spawnsPath)}, process.pid + "\\n");
|
|
60
|
+
fs.writeFileSync(${JSON.stringify(base.lockPath)}, JSON.stringify({
|
|
61
|
+
pid: process.pid, apiBaseUrl: ${JSON.stringify(apiBaseUrl)}, token: "fixture-token"
|
|
62
|
+
}));
|
|
63
|
+
require("node:http").get(${JSON.stringify(apiBaseUrl)} + "/started?pid=" + process.pid, (response) => response.resume());
|
|
64
|
+
setInterval(() => {}, 1000);
|
|
65
|
+
`);
|
|
66
|
+
process.chdir(base.homeDir);
|
|
67
|
+
base.cleanups.push(async () => {
|
|
68
|
+
ready.resolve();
|
|
69
|
+
for (const pid of pids) {
|
|
70
|
+
try {
|
|
71
|
+
process.kill(pid);
|
|
72
|
+
}
|
|
73
|
+
catch { }
|
|
74
|
+
}
|
|
75
|
+
server.closeAllConnections();
|
|
76
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
77
|
+
});
|
|
78
|
+
const options = {
|
|
79
|
+
homeDir: base.homeDir, executable: process.execPath, hubStartupTimeoutMs: 5_000,
|
|
80
|
+
};
|
|
81
|
+
return { ...base, started: started.promise, probing: probing.promise, ready, options, spawnsPath };
|
|
82
|
+
}
|
|
83
|
+
test("concurrent clients for one runtime share startup and its readiness probe", { timeout: 10_000 }, async (t) => {
|
|
84
|
+
const hub = await startingHub(t);
|
|
85
|
+
const pending = [
|
|
86
|
+
new HubClient(hub.options).ensureHub(),
|
|
87
|
+
new HubClient(hub.options).ensureHub(),
|
|
88
|
+
new HubClient(hub.options).getStatus(),
|
|
89
|
+
];
|
|
90
|
+
await hub.started;
|
|
91
|
+
hub.ready.resolve();
|
|
92
|
+
const results = await Promise.all(pending);
|
|
93
|
+
assert.equal(results[0].pid, results[1].pid);
|
|
94
|
+
assert.equal(results[0].pid, results[2].pid);
|
|
95
|
+
assert.equal((await readFile(hub.spawnsPath, "utf8")).trim().split("\n").length, 1);
|
|
96
|
+
});
|
|
97
|
+
test("cancelling one client's lifetime does not cancel another caller's shared startup", { timeout: 10_000 }, async (t) => {
|
|
98
|
+
const hub = await startingHub(t);
|
|
99
|
+
const cancelled = new AbortController();
|
|
100
|
+
const reason = new Error("window closed");
|
|
101
|
+
const first = new HubClient({ ...hub.options, signal: cancelled.signal }).ensureHub();
|
|
102
|
+
const rejected = assert.rejects(first, (error) => error === reason);
|
|
103
|
+
const second = new HubClient(hub.options).ensureHub();
|
|
104
|
+
await hub.probing;
|
|
105
|
+
cancelled.abort(reason);
|
|
106
|
+
await rejected;
|
|
107
|
+
hub.ready.resolve();
|
|
108
|
+
assert.equal((await second).version, "fixture");
|
|
109
|
+
assert.equal((await readFile(hub.spawnsPath, "utf8")).trim().split("\n").length, 1);
|
|
110
|
+
});
|
|
111
|
+
test("an application abort before shared startup begins cannot spawn a Hub", async (t) => {
|
|
112
|
+
const hub = await fixture(t);
|
|
113
|
+
const lifetime = new AbortController();
|
|
114
|
+
const reason = new Error("updating");
|
|
115
|
+
const client = new HubClient({ homeDir: hub.homeDir, executable: process.execPath, signal: lifetime.signal });
|
|
116
|
+
const calls = [client.ensureHub(), client.listProjects()];
|
|
117
|
+
const rejected = calls.map((call) => assert.rejects(call, (error) => error === reason));
|
|
118
|
+
lifetime.abort(reason);
|
|
119
|
+
await Promise.all(rejected);
|
|
120
|
+
assert.equal(existsSync(join(hub.homeDir, ".craft", "hub", "hub-startup.stderr.log")), false);
|
|
121
|
+
});
|
|
122
|
+
test("cancellation during a failed owner probe prevents a later replacement spawn", async (t) => {
|
|
123
|
+
const hub = await fixture(t);
|
|
124
|
+
await hub.writeOwner();
|
|
125
|
+
const controller = new AbortController();
|
|
126
|
+
const reason = new Error("update started");
|
|
127
|
+
globalThis.fetch = (async () => {
|
|
128
|
+
unlinkSync(hub.lockPath);
|
|
129
|
+
controller.abort(reason);
|
|
130
|
+
return new Response("", { status: 503 });
|
|
131
|
+
});
|
|
132
|
+
await assert.rejects(new HubClient({ homeDir: hub.homeDir, executable: process.execPath }).ensureHub(controller.signal), (error) => error === reason);
|
|
133
|
+
assert.equal(existsSync(join(hub.homeDir, ".craft", "hub", "hub-startup.stderr.log")), false);
|
|
134
|
+
});
|
|
135
|
+
test("a cancelled flight cannot evict a replacement shared by new callers", async (t) => {
|
|
136
|
+
const hub = await fixture(t);
|
|
137
|
+
await hub.writeOwner();
|
|
138
|
+
const firstProbe = deferred();
|
|
139
|
+
const secondProbe = deferred();
|
|
140
|
+
const ready = deferred();
|
|
141
|
+
const controller = new AbortController();
|
|
142
|
+
let probes = 0;
|
|
143
|
+
globalThis.fetch = (async (_input, init) => {
|
|
144
|
+
if (++probes === 1) {
|
|
145
|
+
firstProbe.resolve();
|
|
146
|
+
return await new Promise((_resolve, reject) => {
|
|
147
|
+
init.signal.addEventListener("abort", () => reject(init.signal.reason), { once: true });
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
secondProbe.resolve();
|
|
151
|
+
return await ready.promise;
|
|
152
|
+
});
|
|
153
|
+
const client = new HubClient({ homeDir: hub.homeDir });
|
|
154
|
+
const cancelled = client.ensureHub(controller.signal);
|
|
155
|
+
const rejected = assert.rejects(cancelled, { name: "AbortError" });
|
|
156
|
+
await firstProbe.promise;
|
|
157
|
+
controller.abort();
|
|
158
|
+
const replacement = client.ensureHub();
|
|
159
|
+
await secondProbe.promise;
|
|
160
|
+
const joined = new HubClient({ homeDir: hub.homeDir }).ensureHub();
|
|
161
|
+
ready.resolve(new Response("{}"));
|
|
162
|
+
await Promise.all([rejected, replacement, joined]);
|
|
163
|
+
assert.equal(probes, 2);
|
|
164
|
+
});
|
|
165
|
+
test("ensureAppServer carries caller cancellation through the managed ensure request", async (t) => {
|
|
166
|
+
const hub = await fixture(t);
|
|
167
|
+
await hub.writeOwner();
|
|
168
|
+
const requested = deferred();
|
|
169
|
+
const controller = new AbortController();
|
|
170
|
+
const reason = new Error("cancel ensure");
|
|
171
|
+
globalThis.fetch = (async (input, init) => {
|
|
172
|
+
if (String(input).endsWith("/v1/status"))
|
|
173
|
+
return new Response("{}");
|
|
174
|
+
requested.resolve();
|
|
175
|
+
return await new Promise((_resolve, reject) => {
|
|
176
|
+
init.signal.addEventListener("abort", () => reject(init.signal.reason), { once: true });
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
const pending = new HubClient({ homeDir: hub.homeDir }).ensureAppServer("/workspace", {}, controller.signal);
|
|
180
|
+
await requested.promise;
|
|
181
|
+
controller.abort(reason);
|
|
182
|
+
await assert.rejects(pending, (error) => error === reason);
|
|
183
|
+
});
|
|
184
|
+
test("SSE cancellation also cancels its preceding Hub bootstrap", async (t) => {
|
|
185
|
+
const hub = await fixture(t);
|
|
186
|
+
await hub.writeOwner();
|
|
187
|
+
const probing = deferred();
|
|
188
|
+
const controller = new AbortController();
|
|
189
|
+
let eventRequests = 0;
|
|
190
|
+
globalThis.fetch = (async (input, init) => {
|
|
191
|
+
if (String(input).endsWith("/v1/events"))
|
|
192
|
+
eventRequests += 1;
|
|
193
|
+
probing.resolve();
|
|
194
|
+
return await new Promise((_resolve, reject) => {
|
|
195
|
+
init.signal.addEventListener("abort", () => reject(init.signal.reason), { once: true });
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
const pending = new HubClient({ homeDir: hub.homeDir }).subscribeEvents(() => { }, controller.signal);
|
|
199
|
+
await probing.promise;
|
|
200
|
+
controller.abort();
|
|
201
|
+
await assert.rejects(pending, { name: "AbortError" });
|
|
202
|
+
assert.equal(eventRequests, 0);
|
|
203
|
+
});
|
|
204
|
+
test("the client lifetime cancels SSE delivery even with an independent subscription signal", async (t) => {
|
|
205
|
+
const hub = await fixture(t);
|
|
206
|
+
await hub.writeOwner();
|
|
207
|
+
const lifetime = new AbortController();
|
|
208
|
+
const reason = new Error("application stopping");
|
|
209
|
+
const received = [];
|
|
210
|
+
globalThis.fetch = (async (input) => {
|
|
211
|
+
if (String(input).endsWith("/v1/status"))
|
|
212
|
+
return new Response("{}");
|
|
213
|
+
return new Response('data: {"kind":"first"}\n\ndata: {"kind":"second"}\n\n');
|
|
214
|
+
});
|
|
215
|
+
await assert.rejects(new HubClient({ homeDir: hub.homeDir, signal: lifetime.signal }).subscribeEvents((event) => {
|
|
216
|
+
received.push(event.kind);
|
|
217
|
+
lifetime.abort(reason);
|
|
218
|
+
}, new AbortController().signal), (error) => error === reason);
|
|
219
|
+
assert.deepEqual(received, ["first"]);
|
|
220
|
+
});
|
|
221
|
+
test("getStatus reuses its readiness response without retaining stale health", async (t) => {
|
|
222
|
+
const hub = await fixture(t);
|
|
223
|
+
await hub.writeOwner();
|
|
224
|
+
let probes = 0;
|
|
225
|
+
globalThis.fetch = (async () => new Response(JSON.stringify({ hubVersion: String(++probes) })));
|
|
226
|
+
const client = new HubClient({ homeDir: hub.homeDir });
|
|
227
|
+
assert.equal((await client.getStatus()).hubVersion, "1");
|
|
228
|
+
assert.equal((await client.getStatus()).hubVersion, "2");
|
|
229
|
+
assert.equal(probes, 2);
|
|
230
|
+
});
|
|
231
|
+
test("explicit binary policies do not share an incompatible bootstrap flight", async (t) => {
|
|
232
|
+
const hub = await fixture(t);
|
|
233
|
+
await hub.writeOwner();
|
|
234
|
+
globalThis.fetch = (async () => new Response(JSON.stringify({ binaryPath: "/old/dotcraft" })));
|
|
235
|
+
const options = { homeDir: hub.homeDir, expectedExecutable: "/new/dotcraft" };
|
|
236
|
+
const ignored = new HubClient({ ...options, binaryMatchPolicy: "ignore" }).ensureHub();
|
|
237
|
+
await assert.rejects(new HubClient({ ...options, binaryMatchPolicy: "errorIfMismatch" }).ensureHub(), (error) => error instanceof HubClientError && error.code === "hubBinaryMismatch");
|
|
238
|
+
assert.equal((await ignored).binaryPath, "/old/dotcraft");
|
|
239
|
+
});
|
|
240
|
+
test("early Hub exit retains a bounded redacted stderr tail instead of waiting for timeout", async (t) => {
|
|
241
|
+
const hub = await fixture(t);
|
|
242
|
+
await writeFile(join(hub.homeDir, "hub"), `
|
|
243
|
+
process.stderr.write("x".repeat(6000) + "\\nStartup failed: Bearer bearer-secret https://name:password@localhost:1234/status?token=query-secret token=field-secret\\n");
|
|
244
|
+
process.exit(23);
|
|
245
|
+
`);
|
|
246
|
+
process.chdir(hub.homeDir);
|
|
247
|
+
await assert.rejects(new HubClient({
|
|
248
|
+
homeDir: hub.homeDir, executable: process.execPath, hubStartupTimeoutMs: 5_000,
|
|
249
|
+
}).ensureHub(), (error) => {
|
|
250
|
+
assert.ok(error instanceof HubClientError);
|
|
251
|
+
const details = error.details;
|
|
252
|
+
assert.equal(error.code, "hubUnavailable");
|
|
253
|
+
assert.equal(details.stage, "ready");
|
|
254
|
+
assert.equal(details.failureKind, "processExited");
|
|
255
|
+
assert.equal(details.exitCode, 23);
|
|
256
|
+
assert.equal(typeof details.spawnedPid, "number");
|
|
257
|
+
assert.ok(Number(details.elapsedMs) < 5_000);
|
|
258
|
+
const stderr = String(details.recentStderr);
|
|
259
|
+
assert.ok(stderr.includes("Startup failed:"));
|
|
260
|
+
assert.ok(stderr.length <= 4096);
|
|
261
|
+
for (const secret of ["bearer-secret", "password", "query-secret", "field-secret"])
|
|
262
|
+
assert.ok(!stderr.includes(secret));
|
|
263
|
+
return true;
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
test("a competing start's normal exit does not hide another published live Hub", async (t) => {
|
|
267
|
+
const hub = await fixture(t);
|
|
268
|
+
await writeFile(join(hub.homeDir, "hub"), `
|
|
269
|
+
require("node:fs").writeFileSync(${JSON.stringify(hub.lockPath)}, JSON.stringify({
|
|
270
|
+
pid: ${process.pid}, apiBaseUrl: "http://127.0.0.1:49170", token: "fixture-token"
|
|
271
|
+
}));
|
|
272
|
+
process.exit(0);
|
|
273
|
+
`);
|
|
274
|
+
process.chdir(hub.homeDir);
|
|
275
|
+
globalThis.fetch = (async () => new Response("{}"));
|
|
276
|
+
assert.equal((await new HubClient({ homeDir: hub.homeDir, executable: process.execPath }).ensureHub()).pid, process.pid);
|
|
277
|
+
});
|
|
278
|
+
//# sourceMappingURL=hubBootstrap.test.js.map
|