@enhance-eng/sdk 0.1.0-dev.sha-d9190c779d58 → 0.1.160
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 +79 -11
- package/dist/browser/kernel.iife.js +4621 -3211
- package/dist/capture-client.client-B5R1OWuh.d.mts +78 -0
- package/dist/capture-client.client-Dztj8uko.mjs +525 -0
- package/dist/capture-context.lib-Brfs-7h6.mjs +40 -0
- package/dist/client.d.mts +2 -2
- package/dist/client.mjs +1 -1
- package/dist/{environment.config-CA-3pMfN.d.mts → environment.config-30cIR7iz.d.mts} +1 -1
- package/dist/{environment.config-DW6wFq6z.mjs → environment.config-BorTlECV.mjs} +3 -3
- package/dist/index-CDKItTQW.d.mts +656 -0
- package/dist/index.d.mts +9 -5
- package/dist/index.mjs +3 -3
- package/dist/{package-CLHe5Seu.mjs → package-Ea2ntT4R.mjs} +1 -1
- package/dist/playwright/index.d.mts +7 -1
- package/dist/playwright/index.mjs +614 -6174
- package/dist/playwright/reporter.d.mts +4 -4
- package/dist/playwright/reporter.mjs +97 -26
- package/dist/stall-watch.lib-qRMMj0yg.mjs +8494 -0
- package/package.json +11 -4
- package/dist/capture-client.client-BDphetFj.mjs +0 -226
- package/dist/capture-client.client-DsMSGK3h.d.mts +0 -34
- package/dist/capture-files.lib-BA_KBz9W.mjs +0 -406
- package/dist/capture-files.lib-BpwdV8DQ.d.mts +0 -266
- package/dist/environment.config-D898JKZB.mjs +0 -10
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { c as CaptureArtifact, f as CaptureReceipt, h as beginCaptureRunInputSchema, l as CaptureBlob, m as CaptureVerdict, n as captureReferenceSchema, p as CaptureRunDescriptor, r as updateCaptureReferenceInputSchema, s as BeginCaptureRunInput, t as CaptureReview, v as getCaptureRunOutputSchema } from "./index-CDKItTQW.mjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
//#region src/core/not-uploaded.types.d.ts
|
|
4
|
+
/** A file the SDK gave up uploading, named by its address or by the capture it belongs to. */
|
|
5
|
+
interface NotUploadedFile {
|
|
6
|
+
readonly code: 'file-not-uploaded';
|
|
7
|
+
readonly address: string;
|
|
8
|
+
/** In words that carry no status, path or storage text. */
|
|
9
|
+
readonly reason: 'it could not be read on this machine' | "Enhance's storage refused it";
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/node/capture-files.lib.d.ts
|
|
13
|
+
/** A capture and its files' bytes, as a script that captures outside Playwright adds them. */
|
|
14
|
+
interface CapturePayload {
|
|
15
|
+
readonly artifact: CaptureArtifact;
|
|
16
|
+
readonly blobs: readonly {
|
|
17
|
+
readonly description: CaptureBlob;
|
|
18
|
+
readonly bytes: Uint8Array;
|
|
19
|
+
}[];
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/node/capture-transport.client.d.ts
|
|
23
|
+
interface CaptureClientOptions {
|
|
24
|
+
readonly apiUrl: string;
|
|
25
|
+
readonly token: string;
|
|
26
|
+
readonly timeoutMs?: number;
|
|
27
|
+
readonly publicationTimeoutMs?: number;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/node/capture-publication.client.d.ts
|
|
31
|
+
/** A run's receipt, and each file the SDK gave up uploading for it. */
|
|
32
|
+
type CapturePublication = CaptureReceipt & {
|
|
33
|
+
readonly notUploaded: readonly NotUploadedFile[];
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/node/capture-output.lib.d.ts
|
|
37
|
+
/** Where the SDK's lines go; a stream's `write`, so the reporter and the CLI choose their own. */
|
|
38
|
+
type CaptureOutput = (text: string) => boolean;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/node/capture-client.client.d.ts
|
|
41
|
+
interface BeginCaptureRun {
|
|
42
|
+
readonly canvas?: string;
|
|
43
|
+
readonly destination?: BeginCaptureRunInput['destination'];
|
|
44
|
+
readonly title?: string;
|
|
45
|
+
readonly context?: z.input<typeof beginCaptureRunInputSchema>['context'];
|
|
46
|
+
readonly revision?: string;
|
|
47
|
+
readonly compareTo?: BeginCaptureRunInput['compareTo'];
|
|
48
|
+
readonly source?: string;
|
|
49
|
+
readonly runKey: string;
|
|
50
|
+
readonly participants?: readonly string[];
|
|
51
|
+
readonly directory?: string;
|
|
52
|
+
}
|
|
53
|
+
interface CaptureRun {
|
|
54
|
+
readonly descriptor: CaptureRunDescriptor;
|
|
55
|
+
readonly directory: string;
|
|
56
|
+
readonly add: (payload: CapturePayload) => Promise<void>;
|
|
57
|
+
readonly publish: () => Promise<CapturePublication>;
|
|
58
|
+
readonly cancel: () => Promise<CaptureReceipt | null>;
|
|
59
|
+
}
|
|
60
|
+
/** Where a publication prints its lines; standard error by default, so a CLI's output stays its own. */
|
|
61
|
+
interface PublishManifestOptions {
|
|
62
|
+
readonly write?: CaptureOutput;
|
|
63
|
+
}
|
|
64
|
+
interface EnhanceClient {
|
|
65
|
+
readonly captures: {
|
|
66
|
+
readonly begin: (input: BeginCaptureRun) => Promise<CaptureRun>;
|
|
67
|
+
readonly review: (canvasId: string) => Promise<CaptureReview | null>;
|
|
68
|
+
readonly decide: (canvasId: string, verdict: CaptureVerdict) => Promise<void>;
|
|
69
|
+
readonly reference: (canvasId: string, name: string) => Promise<ReturnType<typeof captureReferenceSchema.parse> | null>;
|
|
70
|
+
readonly promote: (input: ReturnType<typeof updateCaptureReferenceInputSchema.parse>) => Promise<ReturnType<typeof captureReferenceSchema.parse>>;
|
|
71
|
+
readonly publishManifests: (paths: readonly string[], options?: PublishManifestOptions) => Promise<CapturePublication>;
|
|
72
|
+
readonly publishManifest: (path: string, options?: PublishManifestOptions) => Promise<CapturePublication>;
|
|
73
|
+
readonly get: (runId: string) => Promise<ReturnType<typeof getCaptureRunOutputSchema.parse>>;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
declare const createEnhance: (options: CaptureClientOptions) => EnhanceClient;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { createEnhance as a, CapturePayload as c, PublishManifestOptions as i, NotUploadedFile as l, CaptureRun as n, CapturePublication as o, EnhanceClient as r, CaptureClientOptions as s, BeginCaptureRun as t };
|
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { A as captureSelection, C as isCaptureError, E as updateCaptureReferenceInputSchema, H as captureRunDescriptorSchema, L as beginCaptureRunInputSchema, R as beginCaptureRunOutputSchema, S as captureError, T as captureReviewSchema, U as getCaptureRunOutputSchema, W as reserveCaptureBlobsOutputSchema, _ as writeCapturePayload, a as requestOutcomeOf, d as captureDiagnostics, g as writeCaptureJson, h as writeCaptureBlob, j as mergeCaptureManifests, k as captureBlobs, m as verifiedCaptureBlobPath, n as retrying, o as retryAfterMsOf, p as readCaptureManifest, t as startStallWatch, w as captureReferenceSchema, x as captureManifestDigest } from "./stall-watch.lib-qRMMj0yg.mjs";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import { mkdir } from "node:fs/promises";
|
|
5
|
+
import nodePath from "node:path";
|
|
6
|
+
import { openAsBlob } from "node:fs";
|
|
7
|
+
//#region src/node/capture-output.lib.ts
|
|
8
|
+
const line = (text) => `Enhance: ${text}\n`;
|
|
9
|
+
/** A left-out capture by its journey and step, or by its test when it could not be read at all. */
|
|
10
|
+
const leftOutName = (leftOut, attempts) => {
|
|
11
|
+
if (leftOut.key !== void 0) return `${leftOut.key.journey} › ${leftOut.key.step}`;
|
|
12
|
+
const title = attempts.find((attempt) => "testId" in attempt && attempt.testId === leftOut.testId)?.title ?? leftOut.testId;
|
|
13
|
+
return title === void 0 ? "a capture" : `a capture of "${title}"`;
|
|
14
|
+
};
|
|
15
|
+
const notUploadedLines = (files) => files.map((file) => line(`${file.code}: ${file.address}: ${file.reason}`)).join("");
|
|
16
|
+
/** What a run lost before it could publish anything: each capture left out, each file given up. */
|
|
17
|
+
const lostLines = (lost, attempts) => (lost.leftOut ?? []).map((leftOut) => line(`capture-left-out: ${leftOutName(leftOut, attempts)}: ${leftOut.message}`)).join("") + notUploadedLines(lost.notUploaded ?? []);
|
|
18
|
+
/**
|
|
19
|
+
* The canvas address, then each capture issue on the receipt, then each file the SDK gave up.
|
|
20
|
+
* A run published through a new run, because its own had expired, names the new one first.
|
|
21
|
+
*/
|
|
22
|
+
const publicationLines = (publication, runId) => (publication.runId === runId ? "" : line(`run ${runId} had expired, so its captures were published through run ${publication.runId}.`)) + line(publication.canvasUrl) + publication.issues.map((issue) => line(`${issue.code}: ${issue.message}`)).join("") + notUploadedLines(publication.notUploaded);
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/core/not-uploaded.consts.ts
|
|
25
|
+
/** Why the SDK gave up uploading a file: it could not read it, or storage refused it for good. */
|
|
26
|
+
const NOT_UPLOADED_REASONS = {
|
|
27
|
+
unreadable: "it could not be read on this machine",
|
|
28
|
+
refused: "Enhance's storage refused it"
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/node/capture-transport.client.ts
|
|
32
|
+
const MAX_ERROR_LENGTH = 1e3;
|
|
33
|
+
const DEFAULT_TIMEOUT_MS = 3e4;
|
|
34
|
+
const DEFAULT_PUBLICATION_TIMEOUT_MS = 6e5;
|
|
35
|
+
const NODE_TIMER_MAX_MS = 2147483647;
|
|
36
|
+
const HTTP_ERRORS = /* @__PURE__ */ new Map([
|
|
37
|
+
[401, "authorization"],
|
|
38
|
+
[403, "authorization"],
|
|
39
|
+
[409, "conflict"],
|
|
40
|
+
[400, "artifact"]
|
|
41
|
+
]);
|
|
42
|
+
/** What a request observably did: a thrown error had no answer, a status that is not ok did. */
|
|
43
|
+
const failureOfResponse = (answer) => {
|
|
44
|
+
if ("error" in answer) return { kind: "no-response" };
|
|
45
|
+
if (answer.value.ok) return null;
|
|
46
|
+
return {
|
|
47
|
+
kind: "response",
|
|
48
|
+
status: answer.value.status,
|
|
49
|
+
appError: null,
|
|
50
|
+
retryAfterMs: retryAfterMsOf(answer.value)
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* One retry rule for every request the SDK makes, the product's own: what the shared request
|
|
55
|
+
* classification reads as transient is tried again, up to three tries in all, while the wait fits
|
|
56
|
+
* the deadline. Any other answer is returned at once, and the last try's answer or error stands.
|
|
57
|
+
*/
|
|
58
|
+
const sendWithRetry = async (send, retryDeadline) => retrying(send, {
|
|
59
|
+
failureOf: failureOfResponse,
|
|
60
|
+
deadline: retryDeadline,
|
|
61
|
+
wait: async (ms) => {
|
|
62
|
+
await new Promise((resolve) => {
|
|
63
|
+
setTimeout(resolve, ms);
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
release: async (response) => {
|
|
67
|
+
await response.body?.cancel();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const requestResponse = async (url, options, timeoutMs, retryDeadline) => {
|
|
71
|
+
try {
|
|
72
|
+
return await sendWithRetry(async () => fetch(url, {
|
|
73
|
+
...options,
|
|
74
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
75
|
+
}), retryDeadline);
|
|
76
|
+
} catch {
|
|
77
|
+
throw captureError("transport", "Enhance did not confirm the request. Resume the same run to recover its result.");
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const responseBody = async (response) => {
|
|
81
|
+
try {
|
|
82
|
+
return await response.json();
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const call = async (url, options, schema, request) => {
|
|
88
|
+
const { procedure, timeoutMs, publicationTimeoutMs } = request;
|
|
89
|
+
const response = await requestResponse(url, options, timeoutMs, Date.now() + publicationTimeoutMs);
|
|
90
|
+
const body = await responseBody(response);
|
|
91
|
+
if (!response.ok) {
|
|
92
|
+
const error = typeof body === "object" && body !== null ? Reflect.get(body, "error") : null;
|
|
93
|
+
const message = typeof error === "object" && error !== null ? Reflect.get(error, "message") : null;
|
|
94
|
+
throw captureError(HTTP_ERRORS.get(response.status) ?? "transport", typeof message === "string" ? message.slice(0, MAX_ERROR_LENGTH) : `Enhance rejected ${procedure} (HTTP ${String(response.status)}).`);
|
|
95
|
+
}
|
|
96
|
+
const result = typeof body === "object" && body !== null ? Reflect.get(body, "result") : null;
|
|
97
|
+
const data = typeof result === "object" && result !== null ? Reflect.get(result, "data") : void 0;
|
|
98
|
+
const parsed = schema.safeParse(data);
|
|
99
|
+
if (!parsed.success) throw captureError("transport", `Enhance returned an incompatible ${procedure} response.`);
|
|
100
|
+
return parsed.data;
|
|
101
|
+
};
|
|
102
|
+
const captureTransport = (options) => {
|
|
103
|
+
const base = URL.parse(options.apiUrl);
|
|
104
|
+
if (base === null || !["http:", "https:"].includes(base.protocol) || base.username !== "" || base.password !== "" || options.token.trim() === "") throw captureError("configuration", "Enhance needs an HTTP API URL and an explicit token.");
|
|
105
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
106
|
+
const publicationTimeoutMs = options.publicationTimeoutMs ?? DEFAULT_PUBLICATION_TIMEOUT_MS;
|
|
107
|
+
if (!Number.isSafeInteger(publicationTimeoutMs) || publicationTimeoutMs < 1) throw captureError("configuration", "Enhance publication timeout must be a positive integer in milliseconds.");
|
|
108
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > NODE_TIMER_MAX_MS) throw captureError("configuration", `Enhance timeout must fit the Node timer range of 1 to ${String(NODE_TIMER_MAX_MS)} milliseconds.`);
|
|
109
|
+
return {
|
|
110
|
+
timeoutMs,
|
|
111
|
+
publicationTimeoutMs,
|
|
112
|
+
request: async (procedure, input, schema, { isQuery = false, timeoutMs: callTimeoutMs = timeoutMs } = {}) => {
|
|
113
|
+
const url = new URL(`${base.pathname.replace(/\/$/u, "")}/trpc/${procedure}`, base.origin);
|
|
114
|
+
if (isQuery) url.searchParams.set("input", JSON.stringify(input));
|
|
115
|
+
return call(url, {
|
|
116
|
+
method: isQuery ? "GET" : "POST",
|
|
117
|
+
headers: {
|
|
118
|
+
authorization: `Bearer ${options.token}`,
|
|
119
|
+
"content-type": "application/json"
|
|
120
|
+
},
|
|
121
|
+
...!isQuery && { body: JSON.stringify(input) },
|
|
122
|
+
redirect: "error"
|
|
123
|
+
}, schema, {
|
|
124
|
+
procedure,
|
|
125
|
+
timeoutMs: Math.max(1, Math.min(callTimeoutMs, NODE_TIMER_MAX_MS)),
|
|
126
|
+
publicationTimeoutMs
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/node/capture-upload.client.ts
|
|
133
|
+
const UPLOAD_BATCH_SIZE = 400;
|
|
134
|
+
const HTTP_FORBIDDEN = 403;
|
|
135
|
+
/** S3 answers an upload to an expired signed address with this message. */
|
|
136
|
+
const EXPIRED_ADDRESS = "Request has expired";
|
|
137
|
+
/** Each file of the run named as a person would: an asset by its address, else by its capture. */
|
|
138
|
+
const fileNames = (artifacts) => {
|
|
139
|
+
const names = /* @__PURE__ */ new Map();
|
|
140
|
+
for (const artifact of artifacts) {
|
|
141
|
+
const capture = `${artifact.key.journey} › ${artifact.key.step}`;
|
|
142
|
+
for (const [blob, name] of [
|
|
143
|
+
[artifact.source.blob, `the snapshot of ${capture}`],
|
|
144
|
+
[artifact.preview.blob, `the picture of ${capture}`],
|
|
145
|
+
...artifact.assets.map((asset) => [asset.blob, asset.url])
|
|
146
|
+
]) if (!names.has(blob.sha256)) names.set(blob.sha256, name);
|
|
147
|
+
}
|
|
148
|
+
return names;
|
|
149
|
+
};
|
|
150
|
+
const answerOf = async (work) => {
|
|
151
|
+
try {
|
|
152
|
+
return { value: await work };
|
|
153
|
+
} catch (error) {
|
|
154
|
+
return { error };
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
/** One try streams the file from disk, cut off only when no bytes of it have left for the stall limit. */
|
|
158
|
+
const sentFile = async (file, upload) => {
|
|
159
|
+
const watch = startStallWatch();
|
|
160
|
+
try {
|
|
161
|
+
const onDisk = await openAsBlob(file);
|
|
162
|
+
const init = {
|
|
163
|
+
method: "PUT",
|
|
164
|
+
headers: upload.headers,
|
|
165
|
+
body: watch.watchedBody(onDisk.stream()),
|
|
166
|
+
duplex: "half",
|
|
167
|
+
signal: watch.signal,
|
|
168
|
+
redirect: "error"
|
|
169
|
+
};
|
|
170
|
+
return await fetch(upload.url, init);
|
|
171
|
+
} finally {
|
|
172
|
+
watch.stop();
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const uploadFile = async (transport, file, upload) => {
|
|
176
|
+
const answer = await answerOf(sendWithRetry(async () => sentFile(file, upload), Date.now() + transport.publicationTimeoutMs));
|
|
177
|
+
if ("error" in answer) {
|
|
178
|
+
captureDiagnostics.publish({
|
|
179
|
+
stage: "upload",
|
|
180
|
+
error: answer.error
|
|
181
|
+
});
|
|
182
|
+
return "retry";
|
|
183
|
+
}
|
|
184
|
+
const response = answer.value;
|
|
185
|
+
if (response.ok) return "stored";
|
|
186
|
+
const text = await response.text();
|
|
187
|
+
captureDiagnostics.publish({
|
|
188
|
+
stage: "upload",
|
|
189
|
+
status: response.status,
|
|
190
|
+
answer: text
|
|
191
|
+
});
|
|
192
|
+
if (response.status === HTTP_FORBIDDEN && text.includes(EXPIRED_ADDRESS)) return "expired";
|
|
193
|
+
return requestOutcomeOf({
|
|
194
|
+
kind: "response",
|
|
195
|
+
status: response.status,
|
|
196
|
+
appError: null,
|
|
197
|
+
retryAfterMs: null
|
|
198
|
+
}).kind === "transient" ? "retry" : "refused";
|
|
199
|
+
};
|
|
200
|
+
const reserve = async (transport, runId, blobs) => {
|
|
201
|
+
const reserved = await transport.request("captureRun.reserveAssets", {
|
|
202
|
+
runId,
|
|
203
|
+
blobs
|
|
204
|
+
}, reserveCaptureBlobsOutputSchema);
|
|
205
|
+
return new Map(reserved.uploads.map((item) => [item.sha256, item]));
|
|
206
|
+
};
|
|
207
|
+
/** One file's upload, reserved again once when its signed address expired. */
|
|
208
|
+
const uploadBlob = async (transport, runId, file, held) => {
|
|
209
|
+
if (held?.upload === null) return "stored";
|
|
210
|
+
if (held === void 0) return "retry";
|
|
211
|
+
const outcome = await uploadFile(transport, file.path, held.upload);
|
|
212
|
+
if (outcome !== "expired") return outcome;
|
|
213
|
+
const renewed = (await reserve(transport, runId, [file.blob])).get(file.blob.sha256);
|
|
214
|
+
if (renewed?.upload === null) return "stored";
|
|
215
|
+
if (renewed === void 0) return "retry";
|
|
216
|
+
const again = await uploadFile(transport, file.path, renewed.upload);
|
|
217
|
+
return again === "expired" ? "retry" : again;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Uploads each file not yet stored. Only a file the SDK cannot read, or that storage refuses for
|
|
221
|
+
* good, is given up and returned; one that another try could still store keeps the run open.
|
|
222
|
+
*/
|
|
223
|
+
const uploadBlobs = async (transport, manifest, directories, artifacts) => {
|
|
224
|
+
const runId = manifest.run.id;
|
|
225
|
+
const names = fileNames(artifacts);
|
|
226
|
+
const notUploaded = [];
|
|
227
|
+
const giveUp = (blob, reason) => {
|
|
228
|
+
notUploaded.push({
|
|
229
|
+
code: "file-not-uploaded",
|
|
230
|
+
address: names.get(blob.sha256) ?? blob.sha256,
|
|
231
|
+
reason
|
|
232
|
+
});
|
|
233
|
+
};
|
|
234
|
+
const available = [];
|
|
235
|
+
for (const blob of captureBlobs(artifacts)) {
|
|
236
|
+
const path = await verifiedCaptureBlobPath(directories, blob);
|
|
237
|
+
if (path === null) giveUp(blob, NOT_UPLOADED_REASONS.unreadable);
|
|
238
|
+
else available.push({
|
|
239
|
+
blob,
|
|
240
|
+
path
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
let isComplete = true;
|
|
244
|
+
for (let start = 0; start < available.length; start += UPLOAD_BATCH_SIZE) {
|
|
245
|
+
const batch = available.slice(start, start + UPLOAD_BATCH_SIZE);
|
|
246
|
+
const uploads = await reserve(transport, runId, batch.map((file) => file.blob));
|
|
247
|
+
for (const file of batch) {
|
|
248
|
+
const outcome = await uploadBlob(transport, runId, file, uploads.get(file.blob.sha256));
|
|
249
|
+
if (outcome === "refused") giveUp(file.blob, NOT_UPLOADED_REASONS.refused);
|
|
250
|
+
else if (outcome !== "stored") isComplete = false;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
notUploaded,
|
|
255
|
+
isComplete
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
//#endregion
|
|
259
|
+
//#region src/node/capture-publication.client.ts
|
|
260
|
+
const POLL_INTERVAL_MS = 250;
|
|
261
|
+
/** The API's answers to a seal, which the SDK tells apart by their words. */
|
|
262
|
+
const SEAL_RACED = "This capture run changed while it was being sealed.";
|
|
263
|
+
const NOTHING_ARRIVED = "None of this run’s screens arrived.";
|
|
264
|
+
const RESUME = "Enhance could not take every file of this run yet. Resume the same manifest from this machine to finish it.";
|
|
265
|
+
/** A run in these states has its seal, so the SDK goes straight to publishing it. */
|
|
266
|
+
const SEALED_STATES = /* @__PURE__ */ new Set([
|
|
267
|
+
"sealed",
|
|
268
|
+
"converting",
|
|
269
|
+
"committed"
|
|
270
|
+
]);
|
|
271
|
+
const resumeError = (runId) => captureError("transport", RESUME, runId);
|
|
272
|
+
const readRun = async (transport, runId) => transport.request("captureRun.get", { runId }, getCaptureRunOutputSchema, { isQuery: true });
|
|
273
|
+
/** The run as a read finds it, or the error to resume when the read itself got no answer. */
|
|
274
|
+
const runAfterSeal = async (transport, runId) => {
|
|
275
|
+
try {
|
|
276
|
+
return await readRun(transport, runId);
|
|
277
|
+
} catch {
|
|
278
|
+
throw resumeError(runId);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* Seals the run, giving the seal what is left of the publication wait. A seal whose answer was lost,
|
|
283
|
+
* or that met another try's seal still running, is decided by a read of the run.
|
|
284
|
+
*/
|
|
285
|
+
const sealRun = async (transport, manifest, lost) => {
|
|
286
|
+
const runId = manifest.run.id;
|
|
287
|
+
try {
|
|
288
|
+
await transport.request("captureRun.seal", { manifest }, getCaptureRunOutputSchema, { timeoutMs: transport.publicationTimeoutMs });
|
|
289
|
+
return;
|
|
290
|
+
} catch (error) {
|
|
291
|
+
if (!isCaptureError(error)) throw error;
|
|
292
|
+
if (error.code === "conflict" && error.message === NOTHING_ARRIVED) throw captureError("artifact", error.message, runId, lost);
|
|
293
|
+
if (!(error.code === "conflict" && error.message === SEAL_RACED) && error.code !== "transport") throw error;
|
|
294
|
+
}
|
|
295
|
+
const current = await runAfterSeal(transport, runId);
|
|
296
|
+
if (SEALED_STATES.has(current.state)) return;
|
|
297
|
+
if (current.state === "failed") throw captureError("conversion", current.failure?.message ?? "The capture run failed.", runId, lost);
|
|
298
|
+
throw resumeError(runId);
|
|
299
|
+
};
|
|
300
|
+
const committedRun = async (transport, manifest) => {
|
|
301
|
+
const runId = manifest.run.id;
|
|
302
|
+
const pending = () => captureError("pending", `Capture run ${runId} is still publishing to canvas ${manifest.run.canvasId}. It appears there when done.`, runId);
|
|
303
|
+
const publish = async () => {
|
|
304
|
+
try {
|
|
305
|
+
return await transport.request("captureRun.publish", { runId }, getCaptureRunOutputSchema);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
if (isCaptureError(error) && error.code === "transport") throw pending();
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
let status = await publish();
|
|
312
|
+
const deadline = Date.now() + transport.publicationTimeoutMs;
|
|
313
|
+
while (status.receipt === null && status.failure === null && !["cancelled", "failed"].includes(status.state)) {
|
|
314
|
+
if (Date.now() >= deadline) throw pending();
|
|
315
|
+
await new Promise((resolve) => {
|
|
316
|
+
setTimeout(resolve, POLL_INTERVAL_MS);
|
|
317
|
+
});
|
|
318
|
+
status = await publish();
|
|
319
|
+
}
|
|
320
|
+
if (status.receipt === null) throw captureError(status.failure?.code === "conflict" ? "conflict" : "conversion", status.failure?.message ?? "The capture run was cancelled.", runId);
|
|
321
|
+
return status.receipt;
|
|
322
|
+
};
|
|
323
|
+
const checkedReceipt = (receipt, manifest) => {
|
|
324
|
+
if (receipt.runId !== manifest.run.id || receipt.canvasId !== manifest.run.canvasId || receipt.manifestDigest !== captureManifestDigest(manifest)) throw captureError("conflict", "This run committed a different manifest.", manifest.run.id);
|
|
325
|
+
return receipt;
|
|
326
|
+
};
|
|
327
|
+
/** The same captures in a new run of the same canvas and source, written beside the old manifest. */
|
|
328
|
+
const renewedManifest = async (on, manifest) => {
|
|
329
|
+
const { review } = manifest.run;
|
|
330
|
+
const target = review === void 0 ? { canvasId: manifest.run.canvasId } : {
|
|
331
|
+
destination: review.destination,
|
|
332
|
+
...review.context !== void 0 && { context: review.context },
|
|
333
|
+
...review.revision !== void 0 && { revision: review.revision },
|
|
334
|
+
...review.compareTo !== void 0 && { compareTo: review.compareTo }
|
|
335
|
+
};
|
|
336
|
+
const { run } = await on.transport.request("captureRun.begin", {
|
|
337
|
+
...target,
|
|
338
|
+
source: manifest.run.source,
|
|
339
|
+
runKey: globalThis.crypto.randomUUID(),
|
|
340
|
+
participants: manifest.run.participants
|
|
341
|
+
}, beginCaptureRunOutputSchema);
|
|
342
|
+
if (run.canvasId !== manifest.run.canvasId) throw captureError("conflict", `Capture run ${manifest.run.id} expired, and a new run for its review would publish to another canvas, so nothing was published. Capture again to publish this review.`, manifest.run.id);
|
|
343
|
+
const renewed = {
|
|
344
|
+
...manifest,
|
|
345
|
+
run
|
|
346
|
+
};
|
|
347
|
+
await writeCaptureJson(nodePath.join(on.directory, `manifest-${run.id}.json`), renewed);
|
|
348
|
+
return renewed;
|
|
349
|
+
};
|
|
350
|
+
const publishManifest = async (on, manifest, mayRenew) => {
|
|
351
|
+
const { transport, directory } = on;
|
|
352
|
+
const runId = manifest.run.id;
|
|
353
|
+
const selection = captureSelection(manifest);
|
|
354
|
+
if ("refusal" in selection) throw captureError("artifact", selection.refusal, runId, {
|
|
355
|
+
leftOut: selection.leftOut,
|
|
356
|
+
notUploaded: []
|
|
357
|
+
});
|
|
358
|
+
const current = await readRun(transport, runId);
|
|
359
|
+
if (current.receipt !== null) {
|
|
360
|
+
const receipt = checkedReceipt(current.receipt, manifest);
|
|
361
|
+
await writeCaptureJson(nodePath.join(directory, "receipt.json"), receipt);
|
|
362
|
+
return {
|
|
363
|
+
...receipt,
|
|
364
|
+
notUploaded: []
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
if (current.state === "collecting" && Date.parse(current.run.expiresAt) <= Date.now()) {
|
|
368
|
+
if (!mayRenew) throw resumeError(runId);
|
|
369
|
+
return publishManifest(on, await renewedManifest(on, manifest), false);
|
|
370
|
+
}
|
|
371
|
+
let notUploaded = [];
|
|
372
|
+
if (current.state === "collecting") {
|
|
373
|
+
const uploaded = await uploadBlobs(transport, manifest, on.blobDirectories, selection.captures);
|
|
374
|
+
if (!uploaded.isComplete) throw resumeError(runId);
|
|
375
|
+
({notUploaded} = uploaded);
|
|
376
|
+
}
|
|
377
|
+
if (!SEALED_STATES.has(current.state)) await sealRun(transport, manifest, {
|
|
378
|
+
leftOut: selection.leftOut,
|
|
379
|
+
notUploaded
|
|
380
|
+
});
|
|
381
|
+
const receipt = checkedReceipt(await committedRun(transport, manifest), manifest);
|
|
382
|
+
await writeCaptureJson(nodePath.join(directory, "receipt.json"), receipt);
|
|
383
|
+
return {
|
|
384
|
+
...receipt,
|
|
385
|
+
notUploaded
|
|
386
|
+
};
|
|
387
|
+
};
|
|
388
|
+
const publishCaptureManifest = async (transport, manifest, directory, blobDirectories = [directory]) => {
|
|
389
|
+
try {
|
|
390
|
+
return await publishManifest({
|
|
391
|
+
transport,
|
|
392
|
+
directory,
|
|
393
|
+
blobDirectories
|
|
394
|
+
}, manifest, true);
|
|
395
|
+
} catch (error) {
|
|
396
|
+
if (error instanceof Error) {
|
|
397
|
+
const runId = Reflect.get(error, "runId");
|
|
398
|
+
throw Object.assign(error, { runId: typeof runId === "string" ? runId : manifest.run.id });
|
|
399
|
+
}
|
|
400
|
+
throw captureError("transport", "Capture publication was not confirmed. Resume the same manifest.", manifest.run.id);
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
//#endregion
|
|
404
|
+
//#region src/node/capture-client.client.ts
|
|
405
|
+
/** A script's run: one attempt with no test, so nothing is invented for the test it does not have. */
|
|
406
|
+
const nodeManifest = (descriptor, captured) => {
|
|
407
|
+
const participant = descriptor.participants[0];
|
|
408
|
+
if (participant === void 0) throw captureError("configuration", "No capture participant was reserved.");
|
|
409
|
+
return {
|
|
410
|
+
format: "enhance-capture-run",
|
|
411
|
+
version: 1,
|
|
412
|
+
run: descriptor,
|
|
413
|
+
ordering: "partial",
|
|
414
|
+
participants: [{
|
|
415
|
+
id: participant,
|
|
416
|
+
status: "passed"
|
|
417
|
+
}],
|
|
418
|
+
attempts: [{
|
|
419
|
+
runner: "script",
|
|
420
|
+
participant,
|
|
421
|
+
retry: 0,
|
|
422
|
+
status: "passed",
|
|
423
|
+
captures: captured
|
|
424
|
+
}]
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
/** Writes a script's capture files; one that does not match its description is left out. */
|
|
428
|
+
const addedCapture = async (directory, payload) => {
|
|
429
|
+
for (const blob of payload.blobs) await writeCaptureBlob(directory, blob.description, blob.bytes);
|
|
430
|
+
return writeCapturePayload(directory, payload.artifact);
|
|
431
|
+
};
|
|
432
|
+
const createRun = (transport, input, directory) => {
|
|
433
|
+
const descriptor = captureRunDescriptorSchema.parse(input);
|
|
434
|
+
const artifacts = [];
|
|
435
|
+
let isClosed = false;
|
|
436
|
+
return {
|
|
437
|
+
descriptor,
|
|
438
|
+
directory,
|
|
439
|
+
add: async (payload) => {
|
|
440
|
+
if (isClosed) throw captureError("configuration", "This run no longer accepts captures.");
|
|
441
|
+
const written = addedCapture(directory, payload);
|
|
442
|
+
artifacts.push(written);
|
|
443
|
+
await written;
|
|
444
|
+
},
|
|
445
|
+
publish: async () => {
|
|
446
|
+
isClosed = true;
|
|
447
|
+
const captured = await Promise.all(artifacts);
|
|
448
|
+
if (descriptor.participants.length !== 1) throw captureError("configuration", "Multi-participant runs must publish a complete merged manifest.");
|
|
449
|
+
const manifest = nodeManifest(descriptor, captured);
|
|
450
|
+
await mkdir(directory, { recursive: true });
|
|
451
|
+
await writeCaptureJson(nodePath.join(directory, "manifest.json"), manifest);
|
|
452
|
+
return publishCaptureManifest(transport, manifest, directory);
|
|
453
|
+
},
|
|
454
|
+
cancel: async () => {
|
|
455
|
+
isClosed = true;
|
|
456
|
+
return (await transport.request("captureRun.cancel", { runId: descriptor.id }, getCaptureRunOutputSchema)).receipt;
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
};
|
|
460
|
+
const beginCaptureRun = async (transport, input) => {
|
|
461
|
+
const request = beginCaptureRunInputSchema.parse({
|
|
462
|
+
canvasId: input.canvas,
|
|
463
|
+
destination: input.destination,
|
|
464
|
+
title: input.title,
|
|
465
|
+
context: input.context,
|
|
466
|
+
revision: input.revision,
|
|
467
|
+
compareTo: input.compareTo,
|
|
468
|
+
source: input.source,
|
|
469
|
+
runKey: input.runKey,
|
|
470
|
+
participants: input.participants
|
|
471
|
+
});
|
|
472
|
+
const { run } = await transport.request("captureRun.begin", request, beginCaptureRunOutputSchema);
|
|
473
|
+
const directory = input.directory ?? nodePath.join(".enhance", "captures", run.id);
|
|
474
|
+
await mkdir(directory, { recursive: true });
|
|
475
|
+
await writeCaptureJson(nodePath.join(directory, "run.json"), run);
|
|
476
|
+
return createRun(transport, run, directory);
|
|
477
|
+
};
|
|
478
|
+
/**
|
|
479
|
+
* Publishes a manifest and prints what it published and lost: on success the canvas address and
|
|
480
|
+
* every issue; before a failure, every capture left out and every file given up.
|
|
481
|
+
*/
|
|
482
|
+
const printedPublication = async (manifest, write, publish) => {
|
|
483
|
+
try {
|
|
484
|
+
const publication = await publish();
|
|
485
|
+
write(publicationLines(publication, manifest.run.id));
|
|
486
|
+
return publication;
|
|
487
|
+
} catch (error) {
|
|
488
|
+
if (isCaptureError(error)) write(lostLines(error, manifest.attempts));
|
|
489
|
+
throw error;
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
const standardError = (text) => process.stderr.write(text);
|
|
493
|
+
const createEnhance = (options) => {
|
|
494
|
+
const transport = captureTransport(options);
|
|
495
|
+
const get = async (runId) => transport.request("captureRun.get", { runId }, getCaptureRunOutputSchema, { isQuery: true });
|
|
496
|
+
return { captures: {
|
|
497
|
+
review: async (canvasId) => transport.request("captureRun.review", { canvasId }, captureReviewSchema.nullable(), { isQuery: true }),
|
|
498
|
+
decide: async (canvasId, verdict) => {
|
|
499
|
+
await transport.request("captureRun.decide", {
|
|
500
|
+
canvasId,
|
|
501
|
+
verdict
|
|
502
|
+
}, z.object({ accepted: z.literal(true) }));
|
|
503
|
+
},
|
|
504
|
+
reference: async (canvasId, name) => transport.request("captureRun.reference", {
|
|
505
|
+
canvasId,
|
|
506
|
+
name
|
|
507
|
+
}, captureReferenceSchema.nullable(), { isQuery: true }),
|
|
508
|
+
promote: async (input) => transport.request("captureRun.promote", updateCaptureReferenceInputSchema.parse(input), captureReferenceSchema),
|
|
509
|
+
publishManifests: async (paths, publishing = {}) => {
|
|
510
|
+
const manifest = mergeCaptureManifests(await Promise.all(paths.map(async (path) => readCaptureManifest(path))));
|
|
511
|
+
if (typeof manifest === "string") throw captureError("artifact", manifest);
|
|
512
|
+
const directory = nodePath.dirname(paths[0] ?? ".");
|
|
513
|
+
await writeCaptureJson(nodePath.join(directory, "merged-manifest.json"), manifest);
|
|
514
|
+
return printedPublication(manifest, publishing.write ?? standardError, async () => publishCaptureManifest(transport, manifest, directory, paths.map((path) => nodePath.dirname(path))));
|
|
515
|
+
},
|
|
516
|
+
begin: async (input) => beginCaptureRun(transport, input),
|
|
517
|
+
publishManifest: async (path, publishing = {}) => {
|
|
518
|
+
const manifest = await readCaptureManifest(path);
|
|
519
|
+
return printedPublication(manifest, publishing.write ?? standardError, async () => publishCaptureManifest(transport, manifest, nodePath.dirname(path)));
|
|
520
|
+
},
|
|
521
|
+
get
|
|
522
|
+
} };
|
|
523
|
+
};
|
|
524
|
+
//#endregion
|
|
525
|
+
export { createEnhance as t };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { H as captureRunDescriptorSchema, S as captureError } from "./stall-watch.lib-qRMMj0yg.mjs";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import nodePath from "node:path";
|
|
5
|
+
//#region src/playwright/environment.config.ts
|
|
6
|
+
const captureEnvironment = () => ({ directory: process.env["ENHANCE_CAPTURE_DIRECTORY"] ?? "" });
|
|
7
|
+
const setCaptureDirectory = (directory) => {
|
|
8
|
+
if (directory === "") throw captureError("configuration", "Capture output directory must not be empty.");
|
|
9
|
+
process.env["ENHANCE_CAPTURE_DIRECTORY"] = directory;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/playwright/capture-context.lib.ts
|
|
13
|
+
const READINESS_POLL_MS = 25;
|
|
14
|
+
/** The attachment that records a capture the step left out, in place of the capture. */
|
|
15
|
+
const LEFT_OUT_ATTACHMENT = "enhance-capture-left-out";
|
|
16
|
+
const captureContextDirectory = () => {
|
|
17
|
+
const directory = captureEnvironment().directory;
|
|
18
|
+
if (directory === "") throw captureError("configuration", "Configure the @enhance-eng/sdk/playwright/reporter before calling enhance.capture.");
|
|
19
|
+
return directory;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Waits for the reporter to start the run, for as long as it takes: the reporter writes either the
|
|
23
|
+
* run or its failure, after its own requests have been retried.
|
|
24
|
+
*/
|
|
25
|
+
const awaitCaptureRun = async (directory) => {
|
|
26
|
+
for (;;) {
|
|
27
|
+
try {
|
|
28
|
+
const raw = JSON.parse(await readFile(nodePath.join(directory, "ready.json"), "utf8"));
|
|
29
|
+
if (typeof raw === "object" && raw !== null && Reflect.get(raw, "failed") === true) throw captureError("configuration", "Enhance reporter could not reserve the capture run. Check the reporter error.");
|
|
30
|
+
return captureRunDescriptorSchema.parse(raw);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") throw error;
|
|
33
|
+
}
|
|
34
|
+
await new Promise((resolve) => {
|
|
35
|
+
setTimeout(resolve, READINESS_POLL_MS);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
export { setCaptureDirectory as i, awaitCaptureRun as n, captureContextDirectory as r, LEFT_OUT_ATTACHMENT as t };
|
package/dist/client.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { BeginCaptureRun, type CaptureClientOptions, CaptureRun, EnhanceClient, createEnhance };
|
|
1
|
+
import { a as createEnhance, i as PublishManifestOptions, n as CaptureRun, o as CapturePublication, r as EnhanceClient, s as CaptureClientOptions, t as BeginCaptureRun } from "./capture-client.client-B5R1OWuh.mjs";
|
|
2
|
+
export { BeginCaptureRun, type CaptureClientOptions, type CapturePublication, CaptureRun, EnhanceClient, PublishManifestOptions, createEnhance };
|
package/dist/client.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as createEnhance } from "./capture-client.client-
|
|
1
|
+
import { t as createEnhance } from "./capture-client.client-Dztj8uko.mjs";
|
|
2
2
|
export { createEnhance };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { r as EnhanceClient, s as CaptureClientOptions$1 } from "./capture-client.client-B5R1OWuh.mjs";
|
|
2
2
|
//#region src/node/environment.config.d.ts
|
|
3
3
|
type CaptureEnvironment = 'local' | 'development' | 'production';
|
|
4
4
|
interface CaptureConnectionOptions {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as version } from "./package-
|
|
3
|
-
import { t as createEnhance$1 } from "./capture-client.client-
|
|
1
|
+
import { S as captureError } from "./stall-watch.lib-qRMMj0yg.mjs";
|
|
2
|
+
import { t as version } from "./package-Ea2ntT4R.mjs";
|
|
3
|
+
import { t as createEnhance$1 } from "./capture-client.client-Dztj8uko.mjs";
|
|
4
4
|
import process from "node:process";
|
|
5
5
|
//#region src/node/environment.config.ts
|
|
6
6
|
const endpoints = {
|