@firstdraft.com/cli 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +267 -0
- package/bin/firstdraft.js +19 -0
- package/package.json +49 -0
- package/src/api-authentication.js +37 -0
- package/src/api-response.js +158 -0
- package/src/application-identity.js +149 -0
- package/src/cli.js +2056 -0
- package/src/commands/compilation.js +823 -0
- package/src/commands/plan-compile.js +150 -0
- package/src/commands/plan-init.js +79 -0
- package/src/commands/plan-publish.js +994 -0
- package/src/commands/plan-push.js +302 -0
- package/src/commands/plan-status.js +485 -0
- package/src/compilation-artifact.js +717 -0
- package/src/file-system.js +12 -0
- package/src/plan-state.js +214 -0
- package/src/uuid-v7.js +55 -0
- package/src/version.js +11 -0
|
@@ -0,0 +1,823 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ARTIFACT_MEDIA_TYPE,
|
|
5
|
+
CompilationArtifactInvalidError,
|
|
6
|
+
CompilationMaterializationError,
|
|
7
|
+
CompilationOutputPathError,
|
|
8
|
+
MAX_ARTIFACT_BYTES,
|
|
9
|
+
materializeCompilationArtifact,
|
|
10
|
+
parseCompilationArtifact,
|
|
11
|
+
resolveOutputTarget,
|
|
12
|
+
} from "../compilation-artifact.js";
|
|
13
|
+
import {
|
|
14
|
+
FirstDraftNetworkError,
|
|
15
|
+
FirstDraftProtocolError,
|
|
16
|
+
isProblemBody,
|
|
17
|
+
readResponseBody,
|
|
18
|
+
readResponseBytes,
|
|
19
|
+
responseMediaType,
|
|
20
|
+
sendRequest,
|
|
21
|
+
} from "../api-response.js";
|
|
22
|
+
import { isUuidV7, readPlanState } from "../plan-state.js";
|
|
23
|
+
|
|
24
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
25
|
+
const WAIT_TIMEOUT_MS = 10 * 60_000;
|
|
26
|
+
const POLL_INTERVAL_MS = 1_000;
|
|
27
|
+
const ALL_STATUSES = new Set([
|
|
28
|
+
"queued",
|
|
29
|
+
"running",
|
|
30
|
+
"succeeded",
|
|
31
|
+
"failed",
|
|
32
|
+
"cancelled",
|
|
33
|
+
]);
|
|
34
|
+
const TERMINAL_STATUSES = new Set(["succeeded", "failed", "cancelled"]);
|
|
35
|
+
const RESPONSE_KEYS = ["project", "compilation"];
|
|
36
|
+
const PROJECT_KEYS = ["id", "graph_version"];
|
|
37
|
+
const COMPILATION_KEYS = [
|
|
38
|
+
"id",
|
|
39
|
+
"analysis_run_id",
|
|
40
|
+
"graph_version",
|
|
41
|
+
"head_source_sha256",
|
|
42
|
+
"status",
|
|
43
|
+
"compiler_release",
|
|
44
|
+
"target",
|
|
45
|
+
"status_path",
|
|
46
|
+
"cancel_path",
|
|
47
|
+
"artifact",
|
|
48
|
+
"failure",
|
|
49
|
+
"created_at",
|
|
50
|
+
"started_at",
|
|
51
|
+
"completed_at",
|
|
52
|
+
];
|
|
53
|
+
const TARGET_KEYS = ["id", "profile"];
|
|
54
|
+
const ARTIFACT_KEYS = ["path", "sha256", "media_type", "byte_size"];
|
|
55
|
+
const FAILURE_KEYS = ["phase", "code", "message"];
|
|
56
|
+
const SHA256_PATTERN = /^[0-9a-f]{64}$/;
|
|
57
|
+
const RELEASE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/;
|
|
58
|
+
const MAX_IDENTIFIER_BYTES = 256;
|
|
59
|
+
const MAX_FAILURE_MESSAGE_BYTES = 4_096;
|
|
60
|
+
|
|
61
|
+
export class CompilationNotPushedError extends Error {}
|
|
62
|
+
|
|
63
|
+
export class CompilationStatusUnavailableError extends Error {
|
|
64
|
+
/**
|
|
65
|
+
* @param {number | undefined} status
|
|
66
|
+
* @param {Record<string, unknown> | null} [response]
|
|
67
|
+
* @param {{transport?: boolean}} [options]
|
|
68
|
+
*/
|
|
69
|
+
constructor(status, response = null, options = {}) {
|
|
70
|
+
super("The compilation status is unavailable.");
|
|
71
|
+
this.status = status;
|
|
72
|
+
this.response = response;
|
|
73
|
+
this.transport = options.transport ?? false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export class CompilationStatusInvalidError extends Error {
|
|
78
|
+
/** @param {number} status */
|
|
79
|
+
constructor(status) {
|
|
80
|
+
super("The compilation status response is invalid.");
|
|
81
|
+
this.status = status;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export class CompilationChangedError extends Error {
|
|
86
|
+
/** @param {CompilationResponse} current */
|
|
87
|
+
constructor(current) {
|
|
88
|
+
super("The compilation changed while it was being polled.");
|
|
89
|
+
this.current = current;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export class CompilationTimeoutError extends Error {
|
|
94
|
+
/** @param {CompilationResponse} current */
|
|
95
|
+
constructor(current) {
|
|
96
|
+
super("The compilation did not finish before the wait deadline.");
|
|
97
|
+
this.current = current;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class CompilationNotSucceededError extends Error {
|
|
102
|
+
/** @param {CompilationResponse} current */
|
|
103
|
+
constructor(current) {
|
|
104
|
+
super("The compilation has not succeeded.");
|
|
105
|
+
this.current = current;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export class CompilationArtifactUnavailableError extends Error {
|
|
110
|
+
/**
|
|
111
|
+
* @param {number | undefined} status
|
|
112
|
+
* @param {Record<string, unknown> | null} [response]
|
|
113
|
+
*/
|
|
114
|
+
constructor(status, response = null) {
|
|
115
|
+
super("The compilation artifact is unavailable.");
|
|
116
|
+
this.status = status;
|
|
117
|
+
this.response = response;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class CompilationArtifactResponseInvalidError extends Error {
|
|
122
|
+
/** @param {number} status */
|
|
123
|
+
constructor(status) {
|
|
124
|
+
super("The compilation artifact response is invalid.");
|
|
125
|
+
this.status = status;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @typedef {object} CompilationResponse
|
|
131
|
+
* @property {{id: string, graph_version: number}} project
|
|
132
|
+
* @property {{
|
|
133
|
+
* id: string,
|
|
134
|
+
* analysis_run_id: string,
|
|
135
|
+
* graph_version: number,
|
|
136
|
+
* head_source_sha256: string,
|
|
137
|
+
* status: string,
|
|
138
|
+
* compiler_release: string,
|
|
139
|
+
* target: {id: string, profile: string},
|
|
140
|
+
* status_path: string,
|
|
141
|
+
* cancel_path: string,
|
|
142
|
+
* artifact: null | {path: string, sha256: string, media_type: string, byte_size: number},
|
|
143
|
+
* failure: null | {phase: string, code: string, message: string},
|
|
144
|
+
* created_at: string,
|
|
145
|
+
* started_at: string | null,
|
|
146
|
+
* completed_at: string | null
|
|
147
|
+
* }} compilation
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @typedef {object} CompilationStatusOptions
|
|
152
|
+
* @property {string} cwd
|
|
153
|
+
* @property {string} compilationId
|
|
154
|
+
* @property {boolean} [wait]
|
|
155
|
+
* @property {typeof globalThis.fetch} [fetchFunction]
|
|
156
|
+
* @property {import("../plan-state.js").PlanStateFileSystem} [fileSystem]
|
|
157
|
+
* @property {(timeoutMs: number) => AbortSignal} [createRequestSignal]
|
|
158
|
+
* @property {(delayMs: number) => Promise<void>} [sleep]
|
|
159
|
+
* @property {() => number} [now]
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @param {CompilationStatusOptions} options
|
|
164
|
+
* @returns {Promise<CompilationResponse>}
|
|
165
|
+
*/
|
|
166
|
+
export async function readCompilation({
|
|
167
|
+
cwd,
|
|
168
|
+
compilationId,
|
|
169
|
+
wait = false,
|
|
170
|
+
fetchFunction = globalThis.fetch,
|
|
171
|
+
fileSystem,
|
|
172
|
+
createRequestSignal = (timeoutMs) => AbortSignal.timeout(timeoutMs),
|
|
173
|
+
sleep = sleepFor,
|
|
174
|
+
now = Date.now,
|
|
175
|
+
}) {
|
|
176
|
+
const context = readContext({ cwd, compilationId, fileSystem });
|
|
177
|
+
const deadline = wait ? now() + WAIT_TIMEOUT_MS : null;
|
|
178
|
+
/** @type {CompilationResponse | null} */
|
|
179
|
+
let first = null;
|
|
180
|
+
/** @type {CompilationResponse | null} */
|
|
181
|
+
let current = null;
|
|
182
|
+
|
|
183
|
+
while (true) {
|
|
184
|
+
if (deadline !== null && current !== null && now() >= deadline) {
|
|
185
|
+
throw new CompilationTimeoutError(current);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const requestTimeout =
|
|
189
|
+
deadline === null
|
|
190
|
+
? REQUEST_TIMEOUT_MS
|
|
191
|
+
: Math.max(1, Math.min(REQUEST_TIMEOUT_MS, deadline - now()));
|
|
192
|
+
let next;
|
|
193
|
+
try {
|
|
194
|
+
next = await readCompilationStatus({
|
|
195
|
+
...context,
|
|
196
|
+
fetchFunction,
|
|
197
|
+
createRequestSignal,
|
|
198
|
+
requestTimeout,
|
|
199
|
+
});
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (
|
|
202
|
+
error instanceof CompilationStatusUnavailableError &&
|
|
203
|
+
error.transport &&
|
|
204
|
+
deadline !== null &&
|
|
205
|
+
current !== null &&
|
|
206
|
+
now() >= deadline
|
|
207
|
+
) {
|
|
208
|
+
throw new CompilationTimeoutError(current);
|
|
209
|
+
}
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
first ??= next;
|
|
214
|
+
if (!sameCompilation(first, next)) {
|
|
215
|
+
throw new CompilationChangedError(next);
|
|
216
|
+
}
|
|
217
|
+
if (current !== null && !validTransition(current, next)) {
|
|
218
|
+
throw new CompilationChangedError(next);
|
|
219
|
+
}
|
|
220
|
+
current = next;
|
|
221
|
+
|
|
222
|
+
if (!wait || TERMINAL_STATUSES.has(current.compilation.status)) {
|
|
223
|
+
return current;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const remaining = deadline === null ? 0 : deadline - now();
|
|
227
|
+
if (remaining <= 0) throw new CompilationTimeoutError(current);
|
|
228
|
+
await sleep(Math.min(POLL_INTERVAL_MS, remaining));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @typedef {object} CompilationDownloadOptions
|
|
234
|
+
* @property {string} cwd
|
|
235
|
+
* @property {string} compilationId
|
|
236
|
+
* @property {string} output
|
|
237
|
+
* @property {typeof globalThis.fetch} [fetchFunction]
|
|
238
|
+
* @property {import("../plan-state.js").PlanStateFileSystem} [fileSystem]
|
|
239
|
+
* @property {(timeoutMs: number) => AbortSignal} [createRequestSignal]
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* @param {CompilationDownloadOptions} options
|
|
244
|
+
*/
|
|
245
|
+
export async function downloadCompilation({
|
|
246
|
+
cwd,
|
|
247
|
+
compilationId,
|
|
248
|
+
output,
|
|
249
|
+
fetchFunction = globalThis.fetch,
|
|
250
|
+
fileSystem,
|
|
251
|
+
createRequestSignal = (timeoutMs) => AbortSignal.timeout(timeoutMs),
|
|
252
|
+
}) {
|
|
253
|
+
const context = readContext({ cwd, compilationId, fileSystem });
|
|
254
|
+
const outputTarget = resolveOutputTarget({ cwd, output });
|
|
255
|
+
const current = await readCompilationStatus({
|
|
256
|
+
...context,
|
|
257
|
+
fetchFunction,
|
|
258
|
+
createRequestSignal,
|
|
259
|
+
requestTimeout: REQUEST_TIMEOUT_MS,
|
|
260
|
+
});
|
|
261
|
+
if (current.compilation.status !== "succeeded") {
|
|
262
|
+
throw new CompilationNotSucceededError(current);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const metadata =
|
|
266
|
+
/** @type {{path: string, sha256: string, media_type: string, byte_size: number}} */ (
|
|
267
|
+
current.compilation.artifact
|
|
268
|
+
);
|
|
269
|
+
const source = await downloadArtifact({
|
|
270
|
+
apiUrl: context.apiUrl,
|
|
271
|
+
metadata,
|
|
272
|
+
fetchFunction,
|
|
273
|
+
createRequestSignal,
|
|
274
|
+
});
|
|
275
|
+
const artifact = parseCompilationArtifact(source, {
|
|
276
|
+
projectId: context.projectId,
|
|
277
|
+
compilationId: current.compilation.id,
|
|
278
|
+
graphVersion: current.compilation.graph_version,
|
|
279
|
+
headSourceSha256: current.compilation.head_source_sha256,
|
|
280
|
+
analysisRunId: current.compilation.analysis_run_id,
|
|
281
|
+
compilerRelease: current.compilation.compiler_release,
|
|
282
|
+
target: current.compilation.target,
|
|
283
|
+
});
|
|
284
|
+
const materialized = materializeCompilationArtifact(artifact, outputTarget);
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
project: current.project,
|
|
288
|
+
compilation: current.compilation,
|
|
289
|
+
output: materialized,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* @param {object} options
|
|
295
|
+
* @param {string} options.cwd
|
|
296
|
+
* @param {string} options.compilationId
|
|
297
|
+
* @param {import("../plan-state.js").PlanStateFileSystem | undefined} options.fileSystem
|
|
298
|
+
*/
|
|
299
|
+
function readContext({ cwd, compilationId, fileSystem }) {
|
|
300
|
+
if (!isUuidV7(compilationId)) {
|
|
301
|
+
throw new TypeError("Compilation ID must be a canonical UUIDv7.");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const state = readPlanState({ cwd, fileSystem });
|
|
305
|
+
if (state.api_url === undefined) {
|
|
306
|
+
throw new CompilationNotPushedError(
|
|
307
|
+
"The local Foundation Plan has not been pushed.",
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
apiUrl: state.api_url,
|
|
313
|
+
projectId: state.project_id,
|
|
314
|
+
compilationId,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @param {object} options
|
|
320
|
+
* @param {string} options.apiUrl
|
|
321
|
+
* @param {string} options.projectId
|
|
322
|
+
* @param {string} options.compilationId
|
|
323
|
+
* @param {typeof globalThis.fetch} options.fetchFunction
|
|
324
|
+
* @param {(timeoutMs: number) => AbortSignal} options.createRequestSignal
|
|
325
|
+
* @param {number} options.requestTimeout
|
|
326
|
+
*/
|
|
327
|
+
async function readCompilationStatus({
|
|
328
|
+
apiUrl,
|
|
329
|
+
projectId,
|
|
330
|
+
compilationId,
|
|
331
|
+
fetchFunction,
|
|
332
|
+
createRequestSignal,
|
|
333
|
+
requestTimeout,
|
|
334
|
+
}) {
|
|
335
|
+
const endpoint = new URL(
|
|
336
|
+
`/v1/projects/${projectId}/compilations/${compilationId}`,
|
|
337
|
+
apiUrl,
|
|
338
|
+
);
|
|
339
|
+
let response;
|
|
340
|
+
let body;
|
|
341
|
+
try {
|
|
342
|
+
response = await sendRequest(fetchFunction, endpoint, {
|
|
343
|
+
method: "GET",
|
|
344
|
+
headers: { Accept: "application/json, application/problem+json" },
|
|
345
|
+
redirect: "error",
|
|
346
|
+
signal: createRequestSignal(requestTimeout),
|
|
347
|
+
});
|
|
348
|
+
body = await readResponseBody(response);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
if (error instanceof FirstDraftNetworkError) {
|
|
351
|
+
throw new CompilationStatusUnavailableError(error.status, null, {
|
|
352
|
+
transport: true,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
if (error instanceof FirstDraftProtocolError) {
|
|
356
|
+
throw new CompilationStatusInvalidError(error.status);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (response.status !== 200) {
|
|
363
|
+
if (!response.ok) {
|
|
364
|
+
throw new CompilationStatusUnavailableError(
|
|
365
|
+
response.status,
|
|
366
|
+
safeProblem(response, body),
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
throw new CompilationStatusInvalidError(response.status);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const parsed = parseCompilationResponse(body, projectId, compilationId);
|
|
373
|
+
if (responseMediaType(response) !== "application/json" || parsed === null) {
|
|
374
|
+
throw new CompilationStatusInvalidError(response.status);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return parsed;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* @param {object} options
|
|
382
|
+
* @param {string} options.apiUrl
|
|
383
|
+
* @param {{path: string, sha256: string, media_type: string, byte_size: number}} options.metadata
|
|
384
|
+
* @param {typeof globalThis.fetch} options.fetchFunction
|
|
385
|
+
* @param {(timeoutMs: number) => AbortSignal} options.createRequestSignal
|
|
386
|
+
*/
|
|
387
|
+
async function downloadArtifact({
|
|
388
|
+
apiUrl,
|
|
389
|
+
metadata,
|
|
390
|
+
fetchFunction,
|
|
391
|
+
createRequestSignal,
|
|
392
|
+
}) {
|
|
393
|
+
const endpoint = new URL(metadata.path, apiUrl);
|
|
394
|
+
let response;
|
|
395
|
+
let source;
|
|
396
|
+
try {
|
|
397
|
+
response = await sendRequest(fetchFunction, endpoint, {
|
|
398
|
+
method: "GET",
|
|
399
|
+
headers: {
|
|
400
|
+
Accept: `${ARTIFACT_MEDIA_TYPE}, application/problem+json`,
|
|
401
|
+
},
|
|
402
|
+
redirect: "error",
|
|
403
|
+
signal: createRequestSignal(REQUEST_TIMEOUT_MS),
|
|
404
|
+
});
|
|
405
|
+
source = await readResponseBytes(response, MAX_ARTIFACT_BYTES);
|
|
406
|
+
} catch (error) {
|
|
407
|
+
if (error instanceof FirstDraftNetworkError) {
|
|
408
|
+
throw new CompilationArtifactUnavailableError(error.status);
|
|
409
|
+
}
|
|
410
|
+
if (error instanceof FirstDraftProtocolError) {
|
|
411
|
+
throw new CompilationArtifactResponseInvalidError(error.status);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (response.status !== 200) {
|
|
418
|
+
let body = null;
|
|
419
|
+
try {
|
|
420
|
+
body = JSON.parse(
|
|
421
|
+
new TextDecoder("utf-8", { fatal: true }).decode(source),
|
|
422
|
+
);
|
|
423
|
+
} catch (error) {
|
|
424
|
+
if (!(error instanceof SyntaxError || error instanceof TypeError)) {
|
|
425
|
+
throw error;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
throw new CompilationArtifactUnavailableError(
|
|
429
|
+
response.status,
|
|
430
|
+
safeProblem(response, body),
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const contentLength = response.headers.get("content-length");
|
|
435
|
+
if (
|
|
436
|
+
responseMediaType(response) !== ARTIFACT_MEDIA_TYPE ||
|
|
437
|
+
response.headers.get("cache-control") !== "no-store, no-transform" ||
|
|
438
|
+
response.headers.get("content-encoding") !== null ||
|
|
439
|
+
contentLength === null ||
|
|
440
|
+
contentLength !== String(metadata.byte_size) ||
|
|
441
|
+
source.byteLength !== metadata.byte_size ||
|
|
442
|
+
response.headers.get("etag") !== `"sha256:${metadata.sha256}"` ||
|
|
443
|
+
sha256(source) !== metadata.sha256
|
|
444
|
+
) {
|
|
445
|
+
throw new CompilationArtifactResponseInvalidError(response.status);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return source;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* @param {unknown} value
|
|
453
|
+
* @param {string} projectId
|
|
454
|
+
* @param {string} compilationId
|
|
455
|
+
* @returns {CompilationResponse | null}
|
|
456
|
+
*/
|
|
457
|
+
function parseCompilationResponse(value, projectId, compilationId) {
|
|
458
|
+
if (
|
|
459
|
+
!hasExactKeySet(value, RESPONSE_KEYS) ||
|
|
460
|
+
!hasExactKeySet(value.project, PROJECT_KEYS) ||
|
|
461
|
+
!hasExactKeySet(value.compilation, COMPILATION_KEYS)
|
|
462
|
+
) {
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const project = value.project;
|
|
467
|
+
const compilation = value.compilation;
|
|
468
|
+
if (
|
|
469
|
+
project.id !== projectId ||
|
|
470
|
+
!isGraphVersion(project.graph_version) ||
|
|
471
|
+
compilation.id !== compilationId ||
|
|
472
|
+
!isUuidV7(compilation.id) ||
|
|
473
|
+
!isUuidV7(compilation.analysis_run_id) ||
|
|
474
|
+
!isGraphVersion(compilation.graph_version) ||
|
|
475
|
+
compilation.graph_version !== project.graph_version ||
|
|
476
|
+
typeof compilation.head_source_sha256 !== "string" ||
|
|
477
|
+
!SHA256_PATTERN.test(compilation.head_source_sha256) ||
|
|
478
|
+
typeof compilation.status !== "string" ||
|
|
479
|
+
!ALL_STATUSES.has(compilation.status) ||
|
|
480
|
+
!isRelease(compilation.compiler_release) ||
|
|
481
|
+
!hasExactKeySet(compilation.target, TARGET_KEYS) ||
|
|
482
|
+
!isRelease(compilation.target.id) ||
|
|
483
|
+
!isRelease(compilation.target.profile) ||
|
|
484
|
+
!isCompilationPath(compilation.status_path, projectId, compilation.id) ||
|
|
485
|
+
!isCompilationPath(
|
|
486
|
+
compilation.cancel_path,
|
|
487
|
+
projectId,
|
|
488
|
+
compilation.id,
|
|
489
|
+
"/cancel",
|
|
490
|
+
) ||
|
|
491
|
+
!isNullableArtifact(compilation.artifact, projectId, compilation.id) ||
|
|
492
|
+
!isNullableFailure(compilation.failure) ||
|
|
493
|
+
!isTimestamp(compilation.created_at) ||
|
|
494
|
+
!isNullableTimestamp(compilation.started_at) ||
|
|
495
|
+
!isNullableTimestamp(compilation.completed_at) ||
|
|
496
|
+
!hasValidStatusFields(compilation)
|
|
497
|
+
) {
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const target = /** @type {{id: string, profile: string}} */ (
|
|
502
|
+
compilation.target
|
|
503
|
+
);
|
|
504
|
+
const artifact =
|
|
505
|
+
/** @type {null | {path: string, sha256: string, media_type: string, byte_size: number}} */ (
|
|
506
|
+
compilation.artifact
|
|
507
|
+
);
|
|
508
|
+
const failure =
|
|
509
|
+
/** @type {null | {phase: string, code: string, message: string}} */ (
|
|
510
|
+
compilation.failure
|
|
511
|
+
);
|
|
512
|
+
return {
|
|
513
|
+
project: { id: project.id, graph_version: project.graph_version },
|
|
514
|
+
compilation: {
|
|
515
|
+
id: compilation.id,
|
|
516
|
+
analysis_run_id: compilation.analysis_run_id,
|
|
517
|
+
graph_version: compilation.graph_version,
|
|
518
|
+
head_source_sha256: /** @type {string} */ (
|
|
519
|
+
compilation.head_source_sha256
|
|
520
|
+
),
|
|
521
|
+
status: compilation.status,
|
|
522
|
+
compiler_release: /** @type {string} */ (compilation.compiler_release),
|
|
523
|
+
target: {
|
|
524
|
+
id: target.id,
|
|
525
|
+
profile: target.profile,
|
|
526
|
+
},
|
|
527
|
+
status_path: /** @type {string} */ (compilation.status_path),
|
|
528
|
+
cancel_path: /** @type {string} */ (compilation.cancel_path),
|
|
529
|
+
artifact:
|
|
530
|
+
artifact === null
|
|
531
|
+
? null
|
|
532
|
+
: {
|
|
533
|
+
path: artifact.path,
|
|
534
|
+
sha256: artifact.sha256,
|
|
535
|
+
media_type: artifact.media_type,
|
|
536
|
+
byte_size: artifact.byte_size,
|
|
537
|
+
},
|
|
538
|
+
failure:
|
|
539
|
+
failure === null
|
|
540
|
+
? null
|
|
541
|
+
: {
|
|
542
|
+
phase: failure.phase,
|
|
543
|
+
code: failure.code,
|
|
544
|
+
message: failure.message,
|
|
545
|
+
},
|
|
546
|
+
created_at: /** @type {string} */ (compilation.created_at),
|
|
547
|
+
started_at: /** @type {string | null} */ (compilation.started_at),
|
|
548
|
+
completed_at: /** @type {string | null} */ (compilation.completed_at),
|
|
549
|
+
},
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** @param {Record<string, unknown>} compilation */
|
|
554
|
+
function hasValidStatusFields(compilation) {
|
|
555
|
+
const status = compilation.status;
|
|
556
|
+
const terminal = TERMINAL_STATUSES.has(String(status));
|
|
557
|
+
if (
|
|
558
|
+
(status === "queued" && compilation.started_at !== null) ||
|
|
559
|
+
(status === "running" && compilation.started_at === null) ||
|
|
560
|
+
(terminal && compilation.completed_at === null) ||
|
|
561
|
+
(!terminal && compilation.completed_at !== null) ||
|
|
562
|
+
(status === "succeeded" &&
|
|
563
|
+
(compilation.started_at === null ||
|
|
564
|
+
compilation.artifact === null ||
|
|
565
|
+
compilation.failure !== null)) ||
|
|
566
|
+
(status === "failed" &&
|
|
567
|
+
(compilation.started_at === null ||
|
|
568
|
+
compilation.artifact !== null ||
|
|
569
|
+
compilation.failure === null)) ||
|
|
570
|
+
(status === "cancelled" &&
|
|
571
|
+
(compilation.artifact !== null || compilation.failure !== null)) ||
|
|
572
|
+
((status === "queued" || status === "running") &&
|
|
573
|
+
(compilation.artifact !== null || compilation.failure !== null))
|
|
574
|
+
) {
|
|
575
|
+
return false;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const created = timestampMilliseconds(String(compilation.created_at));
|
|
579
|
+
const started =
|
|
580
|
+
compilation.started_at === null
|
|
581
|
+
? null
|
|
582
|
+
: timestampMilliseconds(String(compilation.started_at));
|
|
583
|
+
const completed =
|
|
584
|
+
compilation.completed_at === null
|
|
585
|
+
? null
|
|
586
|
+
: timestampMilliseconds(String(compilation.completed_at));
|
|
587
|
+
return (
|
|
588
|
+
(started === null || started >= created) &&
|
|
589
|
+
(completed === null || completed >= created) &&
|
|
590
|
+
(started === null || completed === null || completed >= started)
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* @param {unknown} value
|
|
596
|
+
* @param {string} projectId
|
|
597
|
+
* @param {string} compilationId
|
|
598
|
+
*/
|
|
599
|
+
function isNullableArtifact(value, projectId, compilationId) {
|
|
600
|
+
return (
|
|
601
|
+
value === null ||
|
|
602
|
+
(hasExactKeySet(value, ARTIFACT_KEYS) &&
|
|
603
|
+
isCompilationPath(value.path, projectId, compilationId, "/artifact") &&
|
|
604
|
+
typeof value.sha256 === "string" &&
|
|
605
|
+
SHA256_PATTERN.test(value.sha256) &&
|
|
606
|
+
value.media_type === ARTIFACT_MEDIA_TYPE &&
|
|
607
|
+
Number.isSafeInteger(value.byte_size) &&
|
|
608
|
+
Number(value.byte_size) >= 0 &&
|
|
609
|
+
Number(value.byte_size) <= MAX_ARTIFACT_BYTES)
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** @param {unknown} value */
|
|
614
|
+
function isNullableFailure(value) {
|
|
615
|
+
return (
|
|
616
|
+
value === null ||
|
|
617
|
+
(hasExactKeySet(value, FAILURE_KEYS) &&
|
|
618
|
+
isRelease(value.phase) &&
|
|
619
|
+
isRelease(value.code) &&
|
|
620
|
+
typeof value.message === "string" &&
|
|
621
|
+
Buffer.byteLength(value.message) > 0 &&
|
|
622
|
+
Buffer.byteLength(value.message) <= MAX_FAILURE_MESSAGE_BYTES)
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* @param {unknown} value
|
|
628
|
+
* @param {string} projectId
|
|
629
|
+
* @param {string} compilationId
|
|
630
|
+
* @param {string} [suffix]
|
|
631
|
+
*/
|
|
632
|
+
function isCompilationPath(value, projectId, compilationId, suffix = "") {
|
|
633
|
+
if (
|
|
634
|
+
typeof value !== "string" ||
|
|
635
|
+
!value.startsWith("/") ||
|
|
636
|
+
value.startsWith("//") ||
|
|
637
|
+
value.includes("\\")
|
|
638
|
+
) {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const parsed = new URL(value, "https://firstdraft.invalid");
|
|
643
|
+
const expected = `/v1/projects/${projectId}/compilations/${compilationId}${suffix}`;
|
|
644
|
+
return (
|
|
645
|
+
parsed.origin === "https://firstdraft.invalid" &&
|
|
646
|
+
parsed.pathname === value &&
|
|
647
|
+
parsed.search === "" &&
|
|
648
|
+
parsed.hash === "" &&
|
|
649
|
+
value === expected
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/** @param {CompilationResponse} first @param {CompilationResponse} current */
|
|
654
|
+
function sameCompilation(first, current) {
|
|
655
|
+
return (
|
|
656
|
+
current.project.id === first.project.id &&
|
|
657
|
+
current.project.graph_version === first.project.graph_version &&
|
|
658
|
+
current.compilation.id === first.compilation.id &&
|
|
659
|
+
current.compilation.analysis_run_id === first.compilation.analysis_run_id &&
|
|
660
|
+
current.compilation.graph_version === first.compilation.graph_version &&
|
|
661
|
+
current.compilation.head_source_sha256 ===
|
|
662
|
+
first.compilation.head_source_sha256 &&
|
|
663
|
+
current.compilation.compiler_release ===
|
|
664
|
+
first.compilation.compiler_release &&
|
|
665
|
+
current.compilation.target.id === first.compilation.target.id &&
|
|
666
|
+
current.compilation.target.profile === first.compilation.target.profile &&
|
|
667
|
+
current.compilation.status_path === first.compilation.status_path &&
|
|
668
|
+
current.compilation.cancel_path === first.compilation.cancel_path &&
|
|
669
|
+
current.compilation.created_at === first.compilation.created_at
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/** @param {CompilationResponse} previous @param {CompilationResponse} current */
|
|
674
|
+
function validTransition(previous, current) {
|
|
675
|
+
const from = previous.compilation.status;
|
|
676
|
+
const to = current.compilation.status;
|
|
677
|
+
if (from === to) {
|
|
678
|
+
return (
|
|
679
|
+
current.compilation.started_at === previous.compilation.started_at &&
|
|
680
|
+
current.compilation.completed_at === previous.compilation.completed_at &&
|
|
681
|
+
JSON.stringify(current.compilation.artifact) ===
|
|
682
|
+
JSON.stringify(previous.compilation.artifact) &&
|
|
683
|
+
JSON.stringify(current.compilation.failure) ===
|
|
684
|
+
JSON.stringify(previous.compilation.failure)
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (from === "queued") {
|
|
689
|
+
return (
|
|
690
|
+
to === "running" ||
|
|
691
|
+
to === "succeeded" ||
|
|
692
|
+
to === "failed" ||
|
|
693
|
+
to === "cancelled"
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
return (
|
|
697
|
+
from === "running" &&
|
|
698
|
+
(to === "succeeded" || to === "failed" || to === "cancelled") &&
|
|
699
|
+
current.compilation.started_at === previous.compilation.started_at
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/** @param {Response} response @param {unknown} body */
|
|
704
|
+
function safeProblem(response, body) {
|
|
705
|
+
if (!isRecord(body) || !isProblemBody(response, body)) return null;
|
|
706
|
+
|
|
707
|
+
return {
|
|
708
|
+
...(body.type === "about:blank" ? { type: body.type } : {}),
|
|
709
|
+
title: body.title,
|
|
710
|
+
status: body.status,
|
|
711
|
+
code: body.code,
|
|
712
|
+
detail: body.detail,
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/** @param {number} delayMs */
|
|
717
|
+
function sleepFor(delayMs) {
|
|
718
|
+
return new Promise((resolve) => {
|
|
719
|
+
setTimeout(resolve, delayMs);
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** @param {Buffer} value */
|
|
724
|
+
function sha256(value) {
|
|
725
|
+
return createHash("sha256").update(value).digest("hex");
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/** @param {unknown} value @returns {value is string} */
|
|
729
|
+
function isRelease(value) {
|
|
730
|
+
return (
|
|
731
|
+
typeof value === "string" &&
|
|
732
|
+
Buffer.byteLength(value) <= MAX_IDENTIFIER_BYTES &&
|
|
733
|
+
RELEASE_PATTERN.test(value)
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/** @param {unknown} value @returns {value is number} */
|
|
738
|
+
function isGraphVersion(value) {
|
|
739
|
+
return Number.isSafeInteger(value) && Number(value) >= 1;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** @param {unknown} value @returns {value is string | null} */
|
|
743
|
+
function isNullableTimestamp(value) {
|
|
744
|
+
return value === null || isTimestamp(value);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** @param {unknown} value @returns {value is string} */
|
|
748
|
+
function isTimestamp(value) {
|
|
749
|
+
return timestampParts(value) !== null;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/** @param {string} value */
|
|
753
|
+
function timestampMilliseconds(value) {
|
|
754
|
+
return Date.parse(value);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/** @param {unknown} value */
|
|
758
|
+
function timestampParts(value) {
|
|
759
|
+
if (typeof value !== "string") return null;
|
|
760
|
+
|
|
761
|
+
const match =
|
|
762
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.\d{6}Z$/.exec(value);
|
|
763
|
+
if (!match) return null;
|
|
764
|
+
|
|
765
|
+
const year = Number(match[1]);
|
|
766
|
+
const month = Number(match[2]);
|
|
767
|
+
const day = Number(match[3]);
|
|
768
|
+
const hour = Number(match[4]);
|
|
769
|
+
const minute = Number(match[5]);
|
|
770
|
+
const second = Number(match[6]);
|
|
771
|
+
if (
|
|
772
|
+
month < 1 ||
|
|
773
|
+
month > 12 ||
|
|
774
|
+
day < 1 ||
|
|
775
|
+
day > daysInMonth(year, month) ||
|
|
776
|
+
hour > 23 ||
|
|
777
|
+
minute > 59 ||
|
|
778
|
+
second > 59
|
|
779
|
+
) {
|
|
780
|
+
return null;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
return true;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/** @param {number} year @param {number} month */
|
|
787
|
+
function daysInMonth(year, month) {
|
|
788
|
+
if (month === 2) {
|
|
789
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
return [4, 6, 9, 11].includes(month) ? 30 : 31;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* @param {unknown} value
|
|
797
|
+
* @param {string[]} keys
|
|
798
|
+
* @returns {value is Record<string, unknown>}
|
|
799
|
+
*/
|
|
800
|
+
function hasExactKeySet(value, keys) {
|
|
801
|
+
return (
|
|
802
|
+
isRecord(value) && arraysEqual(Object.keys(value).sort(), [...keys].sort())
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
807
|
+
function isRecord(value) {
|
|
808
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/** @param {unknown[]} left @param {unknown[]} right */
|
|
812
|
+
function arraysEqual(left, right) {
|
|
813
|
+
return (
|
|
814
|
+
left.length === right.length &&
|
|
815
|
+
left.every((value, index) => value === right[index])
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
export {
|
|
820
|
+
CompilationArtifactInvalidError,
|
|
821
|
+
CompilationMaterializationError,
|
|
822
|
+
CompilationOutputPathError,
|
|
823
|
+
};
|