@firstdraft.com/cli 0.7.0 → 0.8.1
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 +22 -25
- package/docs/README.md +15 -13
- package/docs/commands.md +211 -52
- package/docs/errors.md +42 -6
- package/package.json +6 -4
- package/src/api-authentication.js +55 -13
- package/src/api-response.js +73 -3
- package/src/cli.js +395 -6
- package/src/commands/compilation.js +21 -9
- package/src/commands/login.js +314 -0
- package/src/commands/logout.js +107 -0
- package/src/commands/plan-publish.js +16 -13
- package/src/commands/plan-push.js +5 -5
- package/src/commands/plan-status.js +5 -5
- package/src/compilation-artifact.js +2 -2
- package/src/credentials.js +343 -0
- package/src/oauth.js +565 -0
- package/RELEASING.md +0 -114
- package/docs/release-history.md +0 -77
|
@@ -17,11 +17,13 @@ import {
|
|
|
17
17
|
import {
|
|
18
18
|
FirstDraftNetworkError,
|
|
19
19
|
FirstDraftProtocolError,
|
|
20
|
+
SERVICE_ROUTES,
|
|
20
21
|
isProblemBody,
|
|
21
22
|
readResponseBody,
|
|
22
23
|
readResponseBytes,
|
|
23
24
|
responseMediaType,
|
|
24
25
|
sendRequest,
|
|
26
|
+
serviceEndpoint,
|
|
25
27
|
} from "../api-response.js";
|
|
26
28
|
import { isUuidV7, readLocalFile, readPlanState } from "../plan-state.js";
|
|
27
29
|
|
|
@@ -332,6 +334,8 @@ export async function downloadCompilation({
|
|
|
332
334
|
);
|
|
333
335
|
const source = await downloadArtifact({
|
|
334
336
|
apiUrl: context.apiUrl,
|
|
337
|
+
projectId: context.projectId,
|
|
338
|
+
compilationId: current.compilation.id,
|
|
335
339
|
metadata,
|
|
336
340
|
fetchFunction,
|
|
337
341
|
createRequestSignal,
|
|
@@ -472,6 +476,8 @@ export async function compileAndDownload({
|
|
|
472
476
|
);
|
|
473
477
|
const source = await downloadArtifact({
|
|
474
478
|
apiUrl: context.apiUrl,
|
|
479
|
+
projectId: context.projectId,
|
|
480
|
+
compilationId: current.compilation.id,
|
|
475
481
|
metadata,
|
|
476
482
|
fetchFunction,
|
|
477
483
|
createRequestSignal,
|
|
@@ -600,12 +606,13 @@ async function startCompilation({
|
|
|
600
606
|
fetchFunction,
|
|
601
607
|
createRequestSignal,
|
|
602
608
|
}) {
|
|
603
|
-
const endpoint =
|
|
609
|
+
const endpoint = serviceEndpoint(SERVICE_ROUTES.startCompilation, apiUrl, {
|
|
610
|
+
project_id: projectId,
|
|
611
|
+
});
|
|
604
612
|
let response;
|
|
605
613
|
let body;
|
|
606
614
|
try {
|
|
607
615
|
response = await sendRequest(fetchFunction, endpoint, {
|
|
608
|
-
method: "POST",
|
|
609
616
|
headers: {
|
|
610
617
|
Accept: "application/json, application/problem+json",
|
|
611
618
|
"If-Match": etag,
|
|
@@ -667,15 +674,14 @@ async function readCompilationStatus({
|
|
|
667
674
|
createRequestSignal,
|
|
668
675
|
requestTimeout,
|
|
669
676
|
}) {
|
|
670
|
-
const endpoint =
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
);
|
|
677
|
+
const endpoint = serviceEndpoint(SERVICE_ROUTES.readCompilation, apiUrl, {
|
|
678
|
+
project_id: projectId,
|
|
679
|
+
compilation_id: compilationId,
|
|
680
|
+
});
|
|
674
681
|
let response;
|
|
675
682
|
let body;
|
|
676
683
|
try {
|
|
677
684
|
response = await sendRequest(fetchFunction, endpoint, {
|
|
678
|
-
method: "GET",
|
|
679
685
|
headers: { Accept: "application/json, application/problem+json" },
|
|
680
686
|
redirect: "error",
|
|
681
687
|
signal: createRequestSignal(requestTimeout),
|
|
@@ -749,22 +755,28 @@ function matchesExpectedCompilation(current, expected) {
|
|
|
749
755
|
/**
|
|
750
756
|
* @param {object} options
|
|
751
757
|
* @param {string} options.apiUrl
|
|
758
|
+
* @param {string} options.projectId
|
|
759
|
+
* @param {string} options.compilationId
|
|
752
760
|
* @param {{path: string, sha256: string, media_type: string, byte_size: number}} options.metadata
|
|
753
761
|
* @param {typeof globalThis.fetch} options.fetchFunction
|
|
754
762
|
* @param {(timeoutMs: number) => AbortSignal} options.createRequestSignal
|
|
755
763
|
*/
|
|
756
764
|
async function downloadArtifact({
|
|
757
765
|
apiUrl,
|
|
766
|
+
projectId,
|
|
767
|
+
compilationId,
|
|
758
768
|
metadata,
|
|
759
769
|
fetchFunction,
|
|
760
770
|
createRequestSignal,
|
|
761
771
|
}) {
|
|
762
|
-
const endpoint =
|
|
772
|
+
const endpoint = serviceEndpoint(SERVICE_ROUTES.downloadArtifact, apiUrl, {
|
|
773
|
+
project_id: projectId,
|
|
774
|
+
compilation_id: compilationId,
|
|
775
|
+
});
|
|
763
776
|
let response;
|
|
764
777
|
let source;
|
|
765
778
|
try {
|
|
766
779
|
response = await sendRequest(fetchFunction, endpoint, {
|
|
767
|
-
method: "GET",
|
|
768
780
|
headers: {
|
|
769
781
|
Accept: `${ARTIFACT_MEDIA_TYPE}, application/problem+json`,
|
|
770
782
|
},
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { hostname as osHostname } from "node:os";
|
|
2
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
STAGING_API_URL,
|
|
6
|
+
assertStagingSelection,
|
|
7
|
+
} from "../api-authentication.js";
|
|
8
|
+
import {
|
|
9
|
+
CredentialsInvalidError,
|
|
10
|
+
CredentialsLockedError,
|
|
11
|
+
credentialsPath,
|
|
12
|
+
readCredentials,
|
|
13
|
+
writeCredential,
|
|
14
|
+
} from "../credentials.js";
|
|
15
|
+
import { isFileSystemError } from "../file-system.js";
|
|
16
|
+
import {
|
|
17
|
+
LOOPBACK_TIMEOUT_MS,
|
|
18
|
+
OAuthError,
|
|
19
|
+
authorizationUrl,
|
|
20
|
+
createPkcePair,
|
|
21
|
+
createState,
|
|
22
|
+
exchangeAuthorizationCode,
|
|
23
|
+
pollDeviceToken,
|
|
24
|
+
requestDeviceAuthorization,
|
|
25
|
+
startLoopbackServer,
|
|
26
|
+
} from "../oauth.js";
|
|
27
|
+
import { normalizeApiUrl } from "../plan-state.js";
|
|
28
|
+
import { DEFAULT_API_URL } from "./plan-push.js";
|
|
29
|
+
|
|
30
|
+
const MAX_DEVICE_NAME_LENGTH = 100;
|
|
31
|
+
|
|
32
|
+
export class LoginDeniedError extends Error {}
|
|
33
|
+
export class LoginExpiredError extends Error {}
|
|
34
|
+
|
|
35
|
+
export class LoginFailedError extends Error {
|
|
36
|
+
/**
|
|
37
|
+
* @param {string} message
|
|
38
|
+
* @param {{reason?: string, status?: number, credentialsPath?: string, phase?: "read" | "write", cause?: unknown}} [options]
|
|
39
|
+
*/
|
|
40
|
+
constructor(message, options = {}) {
|
|
41
|
+
super(message, { cause: options.cause });
|
|
42
|
+
this.reason = options.reason;
|
|
43
|
+
this.status = options.status;
|
|
44
|
+
this.credentialsPath = options.credentialsPath;
|
|
45
|
+
this.phase = options.phase;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Selects the origin exactly as other API commands select their initial
|
|
51
|
+
* origin: `--staging`, else FIRSTDRAFT_API_URL, else production.
|
|
52
|
+
*
|
|
53
|
+
* @param {{apiUrl?: string, staging?: boolean}} options
|
|
54
|
+
*/
|
|
55
|
+
export function resolveLoginOrigin({ apiUrl, staging = false }) {
|
|
56
|
+
assertStagingSelection(apiUrl, staging);
|
|
57
|
+
if (staging) return STAGING_API_URL;
|
|
58
|
+
return apiUrl === undefined ? DEFAULT_API_URL : normalizeApiUrl(apiUrl);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @typedef {object} LoginOptions
|
|
63
|
+
* @property {string} origin
|
|
64
|
+
* @property {boolean} [device] use the device authorization grant
|
|
65
|
+
* @property {(text: string) => void} prompt writes user instructions (stderr)
|
|
66
|
+
* @property {typeof globalThis.fetch} [fetchFunction]
|
|
67
|
+
* @property {(timeoutMs?: number) => AbortSignal} [createRequestSignal]
|
|
68
|
+
* @property {(delayMs: number, signal?: AbortSignal) => Promise<void>} [sleep]
|
|
69
|
+
* @property {() => number} [now]
|
|
70
|
+
* @property {() => string} [hostname]
|
|
71
|
+
* @property {import("../oauth.js").StartLoopbackServer} [startLoopback]
|
|
72
|
+
* @property {import("../credentials.js").CredentialStore} [credentials]
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Runs the browser (loopback + PKCE) or device flow and stores the resulting
|
|
77
|
+
* token for `origin`. Never returns or prints the token.
|
|
78
|
+
*
|
|
79
|
+
* @param {LoginOptions} options
|
|
80
|
+
*/
|
|
81
|
+
export async function login({
|
|
82
|
+
origin,
|
|
83
|
+
device = false,
|
|
84
|
+
prompt,
|
|
85
|
+
fetchFunction,
|
|
86
|
+
createRequestSignal,
|
|
87
|
+
sleep = defaultSleep,
|
|
88
|
+
now = Date.now,
|
|
89
|
+
hostname = osHostname,
|
|
90
|
+
startLoopback = startLoopbackServer,
|
|
91
|
+
credentials = {},
|
|
92
|
+
}) {
|
|
93
|
+
// Reject an unreadable or invalid store before any network request.
|
|
94
|
+
// A later write can still fail after a token has been issued.
|
|
95
|
+
readStore(credentials);
|
|
96
|
+
const deviceName = safeDeviceName(hostname);
|
|
97
|
+
const request = { origin, fetchFunction, createRequestSignal };
|
|
98
|
+
|
|
99
|
+
/** @param {import("../oauth.js").AccessToken} token */
|
|
100
|
+
const save = (token) => {
|
|
101
|
+
try {
|
|
102
|
+
writeCredential(credentials, origin, {
|
|
103
|
+
access_token: token.access_token,
|
|
104
|
+
token_type: token.token_type,
|
|
105
|
+
created_at: new Date(now()).toISOString(),
|
|
106
|
+
});
|
|
107
|
+
} catch (error) {
|
|
108
|
+
throw credentialsError(error, credentials, "write");
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
if (device) {
|
|
114
|
+
save(await deviceLogin({ ...request, deviceName, prompt, sleep, now }));
|
|
115
|
+
} else {
|
|
116
|
+
await browserLogin({
|
|
117
|
+
...request,
|
|
118
|
+
deviceName,
|
|
119
|
+
prompt,
|
|
120
|
+
sleep,
|
|
121
|
+
startLoopback,
|
|
122
|
+
save,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
} catch (error) {
|
|
126
|
+
throw loginError(error);
|
|
127
|
+
}
|
|
128
|
+
return { origin };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @param {import("../oauth.js").RequestOptions & {
|
|
133
|
+
* deviceName: string | undefined,
|
|
134
|
+
* prompt: (text: string) => void,
|
|
135
|
+
* sleep: (delayMs: number, signal?: AbortSignal) => Promise<void>,
|
|
136
|
+
* startLoopback: import("../oauth.js").StartLoopbackServer,
|
|
137
|
+
* save: (token: import("../oauth.js").AccessToken) => void,
|
|
138
|
+
* }} options
|
|
139
|
+
*/
|
|
140
|
+
async function browserLogin({
|
|
141
|
+
deviceName,
|
|
142
|
+
prompt,
|
|
143
|
+
sleep,
|
|
144
|
+
startLoopback,
|
|
145
|
+
save,
|
|
146
|
+
...request
|
|
147
|
+
}) {
|
|
148
|
+
const state = createState();
|
|
149
|
+
const { verifier, challenge } = createPkcePair();
|
|
150
|
+
|
|
151
|
+
let listener;
|
|
152
|
+
try {
|
|
153
|
+
listener = await startLoopback({ state });
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (!(error instanceof Error)) throw error;
|
|
156
|
+
throw new LoginFailedError(
|
|
157
|
+
"Could not start the local login callback server.",
|
|
158
|
+
{ reason: "loopback_unavailable", cause: error },
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const timer = new AbortController();
|
|
163
|
+
try {
|
|
164
|
+
const url = authorizationUrl(request.origin, {
|
|
165
|
+
redirectUri: listener.redirectUri,
|
|
166
|
+
state,
|
|
167
|
+
challenge,
|
|
168
|
+
deviceName,
|
|
169
|
+
});
|
|
170
|
+
prompt(
|
|
171
|
+
`To log in to ${request.origin}, open this URL in your browser:\n\n ${url}\n\n` +
|
|
172
|
+
"Waiting up to 5 minutes for approval. On a machine without a browser, " +
|
|
173
|
+
"run 'firstdraft login --interactive' instead.\n",
|
|
174
|
+
);
|
|
175
|
+
/** @type {Promise<"timeout">} */
|
|
176
|
+
const timeout = sleep(LOOPBACK_TIMEOUT_MS, timer.signal).then(
|
|
177
|
+
() => "timeout",
|
|
178
|
+
() => new Promise(() => {}),
|
|
179
|
+
);
|
|
180
|
+
const outcome = await Promise.race([listener.result, timeout]);
|
|
181
|
+
timer.abort();
|
|
182
|
+
|
|
183
|
+
if (outcome === "timeout") {
|
|
184
|
+
throw new LoginExpiredError("No browser callback arrived in time.");
|
|
185
|
+
}
|
|
186
|
+
if ("error" in outcome) {
|
|
187
|
+
listener.respond(outcome.error === "access_denied" ? "denied" : "failed");
|
|
188
|
+
throw new OAuthError("The authorization was not granted.", {
|
|
189
|
+
code: outcome.error,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// The browser waits on the held callback until the token is exchanged and
|
|
194
|
+
// saved, so its page reports what the terminal will report.
|
|
195
|
+
save(
|
|
196
|
+
await exchangeAuthorizationCode({
|
|
197
|
+
...request,
|
|
198
|
+
code: outcome.code,
|
|
199
|
+
redirectUri: listener.redirectUri,
|
|
200
|
+
verifier,
|
|
201
|
+
}),
|
|
202
|
+
);
|
|
203
|
+
listener.respond("approved");
|
|
204
|
+
} finally {
|
|
205
|
+
timer.abort();
|
|
206
|
+
// Answers any still-held callback as failed.
|
|
207
|
+
await listener.close();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @param {import("../oauth.js").RequestOptions & {
|
|
213
|
+
* deviceName: string | undefined,
|
|
214
|
+
* prompt: (text: string) => void,
|
|
215
|
+
* sleep: (delayMs: number) => Promise<void>,
|
|
216
|
+
* now: () => number,
|
|
217
|
+
* }} options
|
|
218
|
+
*/
|
|
219
|
+
async function deviceLogin({ deviceName, prompt, sleep, now, ...request }) {
|
|
220
|
+
const authorization = await requestDeviceAuthorization({
|
|
221
|
+
...request,
|
|
222
|
+
deviceName,
|
|
223
|
+
});
|
|
224
|
+
prompt(
|
|
225
|
+
`To log in to ${request.origin}, open this URL in a browser on any device:\n\n` +
|
|
226
|
+
` ${authorization.verificationUri}\n\n` +
|
|
227
|
+
`and enter the code: ${authorization.userCode}\n\n` +
|
|
228
|
+
(authorization.verificationUriComplete === undefined
|
|
229
|
+
? ""
|
|
230
|
+
: `Or open this URL, which already includes the code:\n\n ${authorization.verificationUriComplete}\n\n`) +
|
|
231
|
+
"Waiting for approval...\n",
|
|
232
|
+
);
|
|
233
|
+
return pollDeviceToken({
|
|
234
|
+
...request,
|
|
235
|
+
deviceCode: authorization.deviceCode,
|
|
236
|
+
interval: authorization.interval,
|
|
237
|
+
expiresIn: authorization.expiresIn,
|
|
238
|
+
sleep,
|
|
239
|
+
now,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** @param {import("../credentials.js").CredentialStore} credentials */
|
|
244
|
+
function readStore(credentials) {
|
|
245
|
+
try {
|
|
246
|
+
return readCredentials(credentials);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
throw credentialsError(error, credentials, "read");
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @param {unknown} error
|
|
254
|
+
* @param {import("../credentials.js").CredentialStore} credentials
|
|
255
|
+
* @param {"read" | "write"} phase
|
|
256
|
+
*/
|
|
257
|
+
function credentialsError(error, credentials, phase) {
|
|
258
|
+
if (!(error instanceof CredentialsInvalidError) && !isFileSystemError(error))
|
|
259
|
+
throw error;
|
|
260
|
+
return new LoginFailedError("The credentials file could not be used.", {
|
|
261
|
+
reason:
|
|
262
|
+
error instanceof CredentialsLockedError
|
|
263
|
+
? "credentials_locked"
|
|
264
|
+
: error instanceof CredentialsInvalidError
|
|
265
|
+
? "credentials_invalid"
|
|
266
|
+
: "credentials_unavailable",
|
|
267
|
+
credentialsPath: credentialsPath(credentials),
|
|
268
|
+
phase,
|
|
269
|
+
cause: error,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** @param {unknown} error */
|
|
274
|
+
function loginError(error) {
|
|
275
|
+
if (
|
|
276
|
+
error instanceof LoginDeniedError ||
|
|
277
|
+
error instanceof LoginExpiredError ||
|
|
278
|
+
error instanceof LoginFailedError
|
|
279
|
+
)
|
|
280
|
+
return error;
|
|
281
|
+
if (!(error instanceof OAuthError)) throw error;
|
|
282
|
+
|
|
283
|
+
if (error.code === "access_denied") {
|
|
284
|
+
return new LoginDeniedError("The authorization was denied.");
|
|
285
|
+
}
|
|
286
|
+
if (error.code === "expired_token") {
|
|
287
|
+
return new LoginExpiredError("The authorization expired.");
|
|
288
|
+
}
|
|
289
|
+
return new LoginFailedError("The login could not be completed.", {
|
|
290
|
+
reason: error.code,
|
|
291
|
+
status: error.status,
|
|
292
|
+
cause: error,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @param {() => string} hostname */
|
|
297
|
+
function safeDeviceName(hostname) {
|
|
298
|
+
let name;
|
|
299
|
+
try {
|
|
300
|
+
name = hostname();
|
|
301
|
+
} catch {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
// eslint-disable-next-line no-control-regex
|
|
305
|
+
const cleaned = name.replace(/[\u0000-\u001f\u007f]/g, "").trim();
|
|
306
|
+
return cleaned.length === 0
|
|
307
|
+
? undefined
|
|
308
|
+
: cleaned.slice(0, MAX_DEVICE_NAME_LENGTH);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** @param {number} delayMs @param {AbortSignal} [signal] */
|
|
312
|
+
function defaultSleep(delayMs, signal) {
|
|
313
|
+
return delay(delayMs, undefined, { signal });
|
|
314
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CredentialsInvalidError,
|
|
3
|
+
CredentialsLockedError,
|
|
4
|
+
credentialsPath,
|
|
5
|
+
deleteCredential,
|
|
6
|
+
readCredentials,
|
|
7
|
+
} from "../credentials.js";
|
|
8
|
+
import { isFileSystemError } from "../file-system.js";
|
|
9
|
+
import { revokeToken } from "../oauth.js";
|
|
10
|
+
|
|
11
|
+
export class LogoutFailedError extends Error {
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} message
|
|
14
|
+
* @param {{reason: string, credentialsPath: string, phase: "read" | "write", revoked?: boolean, cause?: unknown}} options
|
|
15
|
+
*/
|
|
16
|
+
constructor(message, options) {
|
|
17
|
+
super(message, { cause: options.cause });
|
|
18
|
+
this.reason = options.reason;
|
|
19
|
+
this.credentialsPath = options.credentialsPath;
|
|
20
|
+
this.phase = options.phase;
|
|
21
|
+
this.revoked = options.revoked;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {object} LogoutOptions
|
|
27
|
+
* @property {string} origin
|
|
28
|
+
* @property {typeof globalThis.fetch} [fetchFunction]
|
|
29
|
+
* @property {(timeoutMs?: number) => AbortSignal} [createRequestSignal]
|
|
30
|
+
* @property {import("../credentials.js").CredentialStore} [credentials]
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {object} LogoutResult
|
|
35
|
+
* @property {boolean} stored whether a credential for the origin was stored
|
|
36
|
+
* @property {boolean} revoked whether First Draft confirmed revocation
|
|
37
|
+
* @property {boolean} changed whether the saved token changed during logout, so
|
|
38
|
+
* the current entry was left in place
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Revokes the stored token for `origin` (best effort) and deletes the local
|
|
43
|
+
* entry if it still holds that token. Other origins are untouched.
|
|
44
|
+
*
|
|
45
|
+
* @param {LogoutOptions} options
|
|
46
|
+
* @returns {Promise<LogoutResult>}
|
|
47
|
+
*/
|
|
48
|
+
export async function logout({
|
|
49
|
+
origin,
|
|
50
|
+
fetchFunction,
|
|
51
|
+
createRequestSignal,
|
|
52
|
+
credentials = {},
|
|
53
|
+
}) {
|
|
54
|
+
const entry = withCredentials(
|
|
55
|
+
credentials,
|
|
56
|
+
() => readCredentials(credentials).origins[origin],
|
|
57
|
+
{ phase: "read" },
|
|
58
|
+
);
|
|
59
|
+
if (entry === undefined)
|
|
60
|
+
return { stored: false, revoked: false, changed: false };
|
|
61
|
+
|
|
62
|
+
const revoked = await revokeToken({
|
|
63
|
+
origin,
|
|
64
|
+
token: entry.access_token,
|
|
65
|
+
fetchFunction,
|
|
66
|
+
createRequestSignal,
|
|
67
|
+
});
|
|
68
|
+
const removed = withCredentials(
|
|
69
|
+
credentials,
|
|
70
|
+
() =>
|
|
71
|
+
deleteCredential(credentials, origin, {
|
|
72
|
+
accessToken: entry.access_token,
|
|
73
|
+
}),
|
|
74
|
+
{ phase: "write", revoked },
|
|
75
|
+
);
|
|
76
|
+
return { stored: true, revoked, changed: !removed };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @template T
|
|
81
|
+
* @param {import("../credentials.js").CredentialStore} credentials
|
|
82
|
+
* @param {() => T} callback
|
|
83
|
+
* @param {{phase: "read" | "write", revoked?: boolean}} outcome
|
|
84
|
+
* @returns {T}
|
|
85
|
+
*/
|
|
86
|
+
function withCredentials(credentials, callback, outcome) {
|
|
87
|
+
try {
|
|
88
|
+
return callback();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (
|
|
91
|
+
!(error instanceof CredentialsInvalidError) &&
|
|
92
|
+
!isFileSystemError(error)
|
|
93
|
+
)
|
|
94
|
+
throw error;
|
|
95
|
+
throw new LogoutFailedError("The credentials file could not be used.", {
|
|
96
|
+
reason:
|
|
97
|
+
error instanceof CredentialsLockedError
|
|
98
|
+
? "credentials_locked"
|
|
99
|
+
: error instanceof CredentialsInvalidError
|
|
100
|
+
? "credentials_invalid"
|
|
101
|
+
: "credentials_unavailable",
|
|
102
|
+
credentialsPath: credentialsPath(credentials),
|
|
103
|
+
...outcome,
|
|
104
|
+
cause: error,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -6,10 +6,12 @@ import { isAuthenticationProblem } from "../api-authentication.js";
|
|
|
6
6
|
import {
|
|
7
7
|
FirstDraftNetworkError,
|
|
8
8
|
FirstDraftProtocolError,
|
|
9
|
+
SERVICE_ROUTES,
|
|
9
10
|
isProblemBody,
|
|
10
11
|
readResponseBody,
|
|
11
12
|
responseMediaType,
|
|
12
13
|
sendRequest,
|
|
14
|
+
serviceEndpoint,
|
|
13
15
|
} from "../api-response.js";
|
|
14
16
|
import { isUuidV7, readLocalFile, readPlanState } from "../plan-state.js";
|
|
15
17
|
|
|
@@ -284,10 +286,7 @@ export async function publishPlan({
|
|
|
284
286
|
);
|
|
285
287
|
}
|
|
286
288
|
|
|
287
|
-
const
|
|
288
|
-
`/v1/projects/${state.project_id}/github-publication`,
|
|
289
|
-
state.api_url,
|
|
290
|
-
);
|
|
289
|
+
const apiUrl = state.api_url;
|
|
291
290
|
const deadline = now() + WAIT_TIMEOUT_MS;
|
|
292
291
|
let initial;
|
|
293
292
|
|
|
@@ -295,7 +294,7 @@ export async function publishPlan({
|
|
|
295
294
|
|
|
296
295
|
try {
|
|
297
296
|
initial = await startPublication({
|
|
298
|
-
|
|
297
|
+
apiUrl,
|
|
299
298
|
projectId: state.project_id,
|
|
300
299
|
headSourceSha256,
|
|
301
300
|
etag: publicationEtag,
|
|
@@ -307,7 +306,7 @@ export async function publishPlan({
|
|
|
307
306
|
|
|
308
307
|
try {
|
|
309
308
|
initial = await readPublicationStatus({
|
|
310
|
-
|
|
309
|
+
apiUrl,
|
|
311
310
|
projectId: state.project_id,
|
|
312
311
|
headSourceSha256,
|
|
313
312
|
fetchFunction,
|
|
@@ -359,7 +358,7 @@ export async function publishPlan({
|
|
|
359
358
|
if (now() >= deadline) throw new PublicationTimeoutError(current);
|
|
360
359
|
|
|
361
360
|
const next = await readPublicationStatus({
|
|
362
|
-
|
|
361
|
+
apiUrl,
|
|
363
362
|
projectId: state.project_id,
|
|
364
363
|
headSourceSha256,
|
|
365
364
|
fetchFunction,
|
|
@@ -391,7 +390,7 @@ export async function publishPlan({
|
|
|
391
390
|
|
|
392
391
|
/**
|
|
393
392
|
* @param {object} options
|
|
394
|
-
* @param {
|
|
393
|
+
* @param {string} options.apiUrl
|
|
395
394
|
* @param {string} options.projectId
|
|
396
395
|
* @param {string} options.headSourceSha256
|
|
397
396
|
* @param {string} options.etag
|
|
@@ -399,18 +398,20 @@ export async function publishPlan({
|
|
|
399
398
|
* @param {(timeoutMs: number) => AbortSignal} options.createRequestSignal
|
|
400
399
|
*/
|
|
401
400
|
async function startPublication({
|
|
402
|
-
|
|
401
|
+
apiUrl,
|
|
403
402
|
projectId,
|
|
404
403
|
headSourceSha256,
|
|
405
404
|
etag,
|
|
406
405
|
fetchFunction,
|
|
407
406
|
createRequestSignal,
|
|
408
407
|
}) {
|
|
408
|
+
const endpoint = serviceEndpoint(SERVICE_ROUTES.startPublication, apiUrl, {
|
|
409
|
+
project_id: projectId,
|
|
410
|
+
});
|
|
409
411
|
let response;
|
|
410
412
|
let body;
|
|
411
413
|
try {
|
|
412
414
|
response = await sendRequest(fetchFunction, endpoint, {
|
|
413
|
-
method: "PUT",
|
|
414
415
|
headers: {
|
|
415
416
|
Accept: "application/json, application/problem+json",
|
|
416
417
|
"If-Match": etag,
|
|
@@ -452,7 +453,7 @@ async function startPublication({
|
|
|
452
453
|
|
|
453
454
|
/**
|
|
454
455
|
* @param {object} options
|
|
455
|
-
* @param {
|
|
456
|
+
* @param {string} options.apiUrl
|
|
456
457
|
* @param {string} options.projectId
|
|
457
458
|
* @param {string} options.headSourceSha256
|
|
458
459
|
* @param {typeof globalThis.fetch} options.fetchFunction
|
|
@@ -460,18 +461,20 @@ async function startPublication({
|
|
|
460
461
|
* @param {number} options.requestTimeout
|
|
461
462
|
*/
|
|
462
463
|
async function readPublicationStatus({
|
|
463
|
-
|
|
464
|
+
apiUrl,
|
|
464
465
|
projectId,
|
|
465
466
|
headSourceSha256,
|
|
466
467
|
fetchFunction,
|
|
467
468
|
createRequestSignal,
|
|
468
469
|
requestTimeout,
|
|
469
470
|
}) {
|
|
471
|
+
const endpoint = serviceEndpoint(SERVICE_ROUTES.readPublication, apiUrl, {
|
|
472
|
+
project_id: projectId,
|
|
473
|
+
});
|
|
470
474
|
let response;
|
|
471
475
|
let body;
|
|
472
476
|
try {
|
|
473
477
|
response = await sendRequest(fetchFunction, endpoint, {
|
|
474
|
-
method: "GET",
|
|
475
478
|
headers: { Accept: "application/json, application/problem+json" },
|
|
476
479
|
redirect: "error",
|
|
477
480
|
signal: createRequestSignal(requestTimeout),
|
|
@@ -4,11 +4,13 @@ import path from "node:path";
|
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
FirstDraftProtocolError,
|
|
7
|
+
SERVICE_ROUTES,
|
|
7
8
|
isDiagnostic,
|
|
8
9
|
isProblemBody,
|
|
9
10
|
readResponseBody,
|
|
10
11
|
responseMediaType,
|
|
11
12
|
sendRequest,
|
|
13
|
+
serviceEndpoint,
|
|
12
14
|
} from "../api-response.js";
|
|
13
15
|
import { isFileSystemError } from "../file-system.js";
|
|
14
16
|
import {
|
|
@@ -109,10 +111,9 @@ export async function pushPlan({
|
|
|
109
111
|
const statePath = path.join(directory, "state.json");
|
|
110
112
|
const planSource = readLocalFile(planPath, MAX_PLAN_BYTES, fileSystem);
|
|
111
113
|
const origin = resolveApiUrl(apiUrl, state.api_url);
|
|
112
|
-
const endpoint =
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
);
|
|
114
|
+
const endpoint = serviceEndpoint(SERVICE_ROUTES.pushPlan, origin, {
|
|
115
|
+
project_id: state.project_id,
|
|
116
|
+
});
|
|
116
117
|
const headers = {
|
|
117
118
|
Accept: "application/json, application/problem+json",
|
|
118
119
|
"Content-Type": FOUNDATION_PLAN_MEDIA_TYPE,
|
|
@@ -122,7 +123,6 @@ export async function pushPlan({
|
|
|
122
123
|
};
|
|
123
124
|
|
|
124
125
|
const response = await sendRequest(fetchFunction, endpoint, {
|
|
125
|
-
method: "PUT",
|
|
126
126
|
headers,
|
|
127
127
|
body: planSource,
|
|
128
128
|
redirect: "error",
|
|
@@ -4,10 +4,12 @@ import { lstatSync, readFileSync } from "node:fs";
|
|
|
4
4
|
import {
|
|
5
5
|
FirstDraftNetworkError,
|
|
6
6
|
FirstDraftProtocolError,
|
|
7
|
+
SERVICE_ROUTES,
|
|
7
8
|
isProblemBody,
|
|
8
9
|
readResponseBody,
|
|
9
10
|
responseMediaType,
|
|
10
11
|
sendRequest,
|
|
12
|
+
serviceEndpoint,
|
|
11
13
|
} from "../api-response.js";
|
|
12
14
|
import { isUuidV7, readPlanState } from "../plan-state.js";
|
|
13
15
|
|
|
@@ -166,10 +168,9 @@ export async function readPlanStatus({
|
|
|
166
168
|
);
|
|
167
169
|
}
|
|
168
170
|
|
|
169
|
-
const endpoint =
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
);
|
|
171
|
+
const endpoint = serviceEndpoint(SERVICE_ROUTES.readAnalysis, state.api_url, {
|
|
172
|
+
project_id: state.project_id,
|
|
173
|
+
});
|
|
173
174
|
const deadline = wait ? now() + WAIT_TIMEOUT_MS : null;
|
|
174
175
|
/** @type {AnalysisResponse | null} */
|
|
175
176
|
let first = null;
|
|
@@ -189,7 +190,6 @@ export async function readPlanStatus({
|
|
|
189
190
|
let body;
|
|
190
191
|
try {
|
|
191
192
|
response = await sendRequest(fetchFunction, endpoint, {
|
|
192
|
-
method: "GET",
|
|
193
193
|
headers: { Accept: "application/json, application/problem+json" },
|
|
194
194
|
redirect: "error",
|
|
195
195
|
signal: createRequestSignal(requestTimeout),
|