@@ -12,6 +12,76 @@ const DEFAULT_TIMEOUT_MINUTES = 30
1212const POLL_INTERVAL_MS = 15_000
1313const PROGRESS_LOG_INTERVAL_MS = 60_000
1414
15+ /**
16+ * Mints a GitHub Actions OIDC token for the given audience.
17+ * Returns null outside of GitHub Actions.
18+ *
19+ * @param {string } audience
20+ * @returns {Promise<string | null> }
21+ */
22+ async function mintGitHubActionsOidcToken ( audience ) {
23+ const requestUrl = process . env . ACTIONS_ID_TOKEN_REQUEST_URL
24+ const requestToken = process . env . ACTIONS_ID_TOKEN_REQUEST_TOKEN
25+ if ( ! requestUrl || ! requestToken ) {
26+ return null
27+ }
28+
29+ const url = new URL ( requestUrl )
30+ url . searchParams . set ( 'audience' , audience )
31+ const response = await fetch ( url , {
32+ headers : { Authorization : `Bearer ${ requestToken } ` } ,
33+ } )
34+ if ( ! response . ok ) {
35+ throw new Error (
36+ `Failed to mint GitHub OIDC token: ${ response . status } ${ await response . text ( ) } `
37+ )
38+ }
39+
40+ const { value } = await response . json ( )
41+ return value
42+ }
43+
44+ /**
45+ * Reads a preview-builds credential, minting a fresh one when needed.
46+ * vercel-packages accepts GitHub Actions OIDC tokens minted for its audience.
47+ * Public preview builds need no credentials and return null. Private builds
48+ * mint a GitHub Actions OIDC token, which expires about five minutes after
49+ * issuance, so the token is cached and re-minted before it expires rather
50+ * than minted once up front.
51+ *
52+ * @returns {() => Promise<string | null> }
53+ */
54+ export function createPreviewBuildsReadTokenGetter ( ) {
55+ if ( process . env . PREVIEW_BUILDS_ACCESS !== 'private' ) {
56+ return async ( ) => null
57+ }
58+
59+ let cachedToken = null
60+ let cachedTokenExpiresAt = 0
61+
62+ return async ( ) => {
63+ if ( cachedToken !== null && cachedTokenExpiresAt - 60_000 > Date . now ( ) ) {
64+ return cachedToken
65+ }
66+ const token = await mintGitHubActionsOidcToken (
67+ 'https://vercel-packages.vercel.app'
68+ )
69+ if ( token === null ) {
70+ throw new Error (
71+ 'Preview builds are private (PREVIEW_BUILDS_ACCESS=private) ' +
72+ 'but no GitHub Actions OIDC token can be minted. ' +
73+ 'Grant the job the `id-token: write` permission.'
74+ )
75+ }
76+ const payload = JSON . parse (
77+ Buffer . from ( token . split ( '.' ) [ 1 ] , 'base64url' ) . toString ( )
78+ )
79+ cachedToken = token
80+ cachedTokenExpiresAt = payload . exp * 1000
81+ return token
82+ }
83+ }
84+
1585/**
1686 * URL of the `next` preview tarball for a commit. `vercel-packages` answers
1787 * with a redirect to Vercel Blob, which only serves the tarball once
@@ -66,40 +136,54 @@ function commitChecksUrl(commitSha) {
66136 *
67137 * @param {string } url
68138 * @param {Record<string, string> | undefined } headers
69- * @returns {Promise<{ published: boolean, lastResponse: string }> }
139+ * @returns {Promise<{ published: boolean, status: number | null, lastResponse: string, responseHeaders: string | null }> }
70140 */
71141async function probeTarball ( url , headers ) {
72142 try {
73143 const response = await fetch ( url , { method : 'HEAD' , headers } )
74144 return {
75145 published : response . ok ,
146+ status : response . status ,
76147 lastResponse :
77148 response . status === 404 ? 'not published yet' : `${ response . status } ` ,
149+ // Response headers carry request IDs that help debug failures.
150+ responseHeaders : response . ok
151+ ? null
152+ : JSON . stringify ( Object . fromEntries ( response . headers ) ) ,
78153 }
79154 } catch ( error ) {
80155 return {
81156 published : false ,
157+ status : null ,
82158 lastResponse : `request failed (${ error instanceof Error ? error . message : error } )` ,
159+ responseHeaders : null ,
83160 }
84161 }
85162}
86163
87164/**
88- * @param {string | undefined } readToken
89- * @returns {Record<string, string> | undefined }
165+ * @param {() => Promise< string | null> } getReadToken
166+ * @returns {Promise< Record<string, string> | undefined> }
90167 */
91- function requestHeaders ( readToken ) {
168+ async function requestHeaders ( getReadToken ) {
169+ const readToken = await getReadToken ( )
92170 return readToken ? { Authorization : `Bearer ${ readToken } ` } : undefined
93171}
94172
95173/**
96174 * @param {object } options
97175 * @param {string } options.commitSha
98176 * @param {string } options.lastResponse
177+ * @param {string | null } [options.responseHeaders]
99178 * @param {number } [options.timeoutMs] Omitted when nothing was waited out.
100179 * @returns {Error }
101180 */
102- function notPublishedError ( { commitSha, lastResponse, timeoutMs } ) {
181+ function notPublishedError ( {
182+ commitSha,
183+ lastResponse,
184+ responseHeaders,
185+ timeoutMs,
186+ } ) {
103187 const checksUrl = commitChecksUrl ( commitSha )
104188 return new Error (
105189 `Preview tarball for commit ${ commitSha } was not published` +
@@ -108,7 +192,8 @@ function notPublishedError({ commitSha, lastResponse, timeoutMs }) {
108192 `The tarball is published by the "upload-preview-tarballs" workflow ` +
109193 `once "build-and-deploy" has completed for this commit, so check ` +
110194 `whether that run failed or is still in progress.` +
111- ( checksUrl ? ` See ${ checksUrl } ` : '' )
195+ ( checksUrl ? ` See ${ checksUrl } ` : '' ) +
196+ ( responseHeaders ? ` Response headers: ${ responseHeaders } ` : '' )
112197 )
113198}
114199
@@ -120,22 +205,22 @@ function notPublishedError({ commitSha, lastResponse, timeoutMs }) {
120205 * @param {object } options
121206 * @param {string } options.commitSha
122207 * @param {string } [options.previewBuildsBaseUrl]
123- * @param {string } [options.readToken ]
208+ * @param {() => Promise< string | null> } [options.getReadToken ]
124209 * @returns {Promise<void> }
125210 */
126211export async function assertPreviewTarballPublished ( {
127212 commitSha,
128213 previewBuildsBaseUrl,
129- readToken ,
214+ getReadToken ,
130215} ) {
131216 const url = previewTarballUrl ( previewBuildsBaseUrl , commitSha )
132- const { published, lastResponse } = await probeTarball (
217+ const { published, lastResponse, responseHeaders } = await probeTarball (
133218 url ,
134- requestHeaders ( readToken )
219+ await requestHeaders ( getReadToken ?? ( async ( ) => null ) )
135220 )
136221
137222 if ( ! published ) {
138- throw notPublishedError ( { commitSha, lastResponse } )
223+ throw notPublishedError ( { commitSha, lastResponse, responseHeaders } )
139224 }
140225
141226 console . info ( `Preview tarball for commit ${ commitSha } is available at ${ url } ` )
@@ -151,19 +236,18 @@ export async function assertPreviewTarballPublished({
151236 * @param {string } options.commitSha
152237 * @param {string } [options.previewBuildsBaseUrl]
153238 * @param {number } options.timeoutMs
154- * @param {string } [options.readToken ]
239+ * @param {() => Promise< string | null> } [options.getReadToken ]
155240 * @param {number } [options.pollIntervalMs]
156241 * @returns {Promise<void> }
157242 */
158243export async function waitForPreviewTarball ( {
159244 commitSha,
160245 previewBuildsBaseUrl,
161246 timeoutMs,
162- readToken ,
247+ getReadToken = async ( ) => null ,
163248 pollIntervalMs = POLL_INTERVAL_MS ,
164249} ) {
165250 const url = previewTarballUrl ( previewBuildsBaseUrl , commitSha )
166- const headers = requestHeaders ( readToken )
167251 const startedAt = Date . now ( )
168252 const deadline = startedAt + timeoutMs
169253 let lastProgressLogAt = startedAt
@@ -173,7 +257,10 @@ export async function waitForPreviewTarball({
173257 )
174258
175259 for ( ; ; ) {
176- const { published, lastResponse } = await probeTarball ( url , headers )
260+ // A fresh token per probe: the OIDC token expires after five minutes,
261+ // well before the overall timeout.
262+ const { published, status, lastResponse, responseHeaders } =
263+ await probeTarball ( url , await requestHeaders ( getReadToken ) )
177264 const now = Date . now ( )
178265
179266 if ( published ) {
@@ -183,8 +270,21 @@ export async function waitForPreviewTarball({
183270 return
184271 }
185272
273+ if ( status === 401 || status === 403 ) {
274+ // Retrying won't change the authorization outcome.
275+ throw new Error (
276+ `Not authorized to access the preview tarball at ${ url } (last response: ${ lastResponse } ). ` +
277+ ( responseHeaders ? `Response headers: ${ responseHeaders } ` : '' )
278+ )
279+ }
280+
186281 if ( now >= deadline ) {
187- throw notPublishedError ( { commitSha, lastResponse, timeoutMs } )
282+ throw notPublishedError ( {
283+ commitSha,
284+ lastResponse,
285+ responseHeaders,
286+ timeoutMs,
287+ } )
188288 }
189289
190290 if ( now - lastProgressLogAt >= PROGRESS_LOG_INTERVAL_MS ) {
@@ -231,7 +331,7 @@ async function main() {
231331 commitSha,
232332 previewBuildsBaseUrl : values [ 'preview-builds-base-url' ] ,
233333 timeoutMs : timeoutMinutes * 60_000 ,
234- readToken : process . env . PREVIEW_BUILDS_READ_TOKEN ,
334+ getReadToken : createPreviewBuildsReadTokenGetter ( ) ,
235335 } )
236336}
237337
0 commit comments