@conloca/cloud-astro 0.2.3 → 0.2.5
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 +155 -37
- package/dist/.build-inputs.json +32 -23
- package/dist/src/constants.d.ts +12 -3
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +12 -3
- package/dist/src/constants.js.map +1 -1
- package/dist/src/d3-build.d.ts +70 -0
- package/dist/src/d3-build.d.ts.map +1 -0
- package/dist/src/d3-build.js +272 -0
- package/dist/src/d3-build.js.map +1 -0
- package/dist/src/descriptors.d.ts.map +1 -1
- package/dist/src/descriptors.js +4 -2
- package/dist/src/descriptors.js.map +1 -1
- package/dist/src/doctor/checks-live.d.ts +2 -2
- package/dist/src/doctor/checks-live.d.ts.map +1 -1
- package/dist/src/doctor/checks-live.js +10 -4
- package/dist/src/doctor/checks-live.js.map +1 -1
- package/dist/src/doctor/checks-offline.d.ts +1 -5
- package/dist/src/doctor/checks-offline.d.ts.map +1 -1
- package/dist/src/doctor/checks-offline.js +15 -27
- package/dist/src/doctor/checks-offline.js.map +1 -1
- package/dist/src/doctor/checks-preview-limits.d.ts +11 -0
- package/dist/src/doctor/checks-preview-limits.d.ts.map +1 -0
- package/dist/src/doctor/checks-preview-limits.js +133 -0
- package/dist/src/doctor/checks-preview-limits.js.map +1 -0
- package/dist/src/doctor/checks-preview-live.d.ts +14 -0
- package/dist/src/doctor/checks-preview-live.d.ts.map +1 -0
- package/dist/src/doctor/checks-preview-live.js +72 -0
- package/dist/src/doctor/checks-preview-live.js.map +1 -0
- package/dist/src/doctor/checks-preview.d.ts +7 -0
- package/dist/src/doctor/checks-preview.d.ts.map +1 -0
- package/dist/src/doctor/checks-preview.js +287 -0
- package/dist/src/doctor/checks-preview.js.map +1 -0
- package/dist/src/doctor/config-scan.d.ts +6 -0
- package/dist/src/doctor/config-scan.d.ts.map +1 -1
- package/dist/src/doctor/config-scan.js +17 -0
- package/dist/src/doctor/config-scan.js.map +1 -1
- package/dist/src/doctor/run.d.ts.map +1 -1
- package/dist/src/doctor/run.js +4 -0
- package/dist/src/doctor/run.js.map +1 -1
- package/dist/src/doctor/types.d.ts +11 -1
- package/dist/src/doctor/types.d.ts.map +1 -1
- package/dist/src/doctor/types.js +11 -1
- package/dist/src/doctor/types.js.map +1 -1
- package/dist/src/editor-page/bundle.d.ts +2 -0
- package/dist/src/editor-page/bundle.d.ts.map +1 -1
- package/dist/src/editor-page/bundle.js +3 -0
- package/dist/src/editor-page/bundle.js.map +1 -1
- package/dist/src/editor-page/data-schemas.d.ts +16 -0
- package/dist/src/editor-page/data-schemas.d.ts.map +1 -0
- package/dist/src/editor-page/data-schemas.js +39 -0
- package/dist/src/editor-page/data-schemas.js.map +1 -0
- package/dist/src/editor-page/emit.d.ts +37 -1
- package/dist/src/editor-page/emit.d.ts.map +1 -1
- package/dist/src/editor-page/emit.js +75 -4
- package/dist/src/editor-page/emit.js.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/integration.d.ts.map +1 -1
- package/dist/src/integration.js +165 -41
- package/dist/src/integration.js.map +1 -1
- package/dist/src/preview/_dispatch.d.ts +12 -0
- package/dist/src/preview/_dispatch.d.ts.map +1 -1
- package/dist/src/preview/_dispatch.js +31 -0
- package/dist/src/preview/_dispatch.js.map +1 -1
- package/dist/src/preview/_resolve-opts.d.ts +3 -0
- package/dist/src/preview/_resolve-opts.d.ts.map +1 -1
- package/dist/src/preview/_resolve-opts.js +3 -1
- package/dist/src/preview/_resolve-opts.js.map +1 -1
- package/dist/src/preview/banner.d.ts +12 -9
- package/dist/src/preview/banner.d.ts.map +1 -1
- package/dist/src/preview/banner.js +46 -41
- package/dist/src/preview/banner.js.map +1 -1
- package/dist/src/preview/cloudflare-adapter.d.ts +35 -0
- package/dist/src/preview/cloudflare-adapter.d.ts.map +1 -0
- package/dist/src/preview/cloudflare-adapter.js +92 -0
- package/dist/src/preview/cloudflare-adapter.js.map +1 -0
- package/dist/src/preview/cookie.d.ts +9 -13
- package/dist/src/preview/cookie.d.ts.map +1 -1
- package/dist/src/preview/cookie.js +15 -25
- package/dist/src/preview/cookie.js.map +1 -1
- package/dist/src/preview/dispatcher.d.ts +73 -0
- package/dist/src/preview/dispatcher.d.ts.map +1 -0
- package/dist/src/preview/dispatcher.js +329 -0
- package/dist/src/preview/dispatcher.js.map +1 -0
- package/dist/src/preview/entry-code.d.ts +33 -0
- package/dist/src/preview/entry-code.d.ts.map +1 -0
- package/dist/src/preview/entry-code.js +33 -0
- package/dist/src/preview/entry-code.js.map +1 -0
- package/dist/src/preview/headers.d.ts +24 -4
- package/dist/src/preview/headers.d.ts.map +1 -1
- package/dist/src/preview/headers.js +35 -7
- package/dist/src/preview/headers.js.map +1 -1
- package/dist/src/preview/landing.d.ts +21 -0
- package/dist/src/preview/landing.d.ts.map +1 -1
- package/dist/src/preview/landing.js +103 -35
- package/dist/src/preview/landing.js.map +1 -1
- package/dist/src/preview/middleware.d.ts +29 -7
- package/dist/src/preview/middleware.d.ts.map +1 -1
- package/dist/src/preview/middleware.js +238 -84
- package/dist/src/preview/middleware.js.map +1 -1
- package/dist/src/preview/preview-path.d.ts +29 -0
- package/dist/src/preview/preview-path.d.ts.map +1 -1
- package/dist/src/preview/preview-path.js +41 -0
- package/dist/src/preview/preview-path.js.map +1 -1
- package/dist/src/preview/redeem-url.d.ts +7 -0
- package/dist/src/preview/redeem-url.d.ts.map +1 -0
- package/dist/src/preview/redeem-url.js +14 -0
- package/dist/src/preview/redeem-url.js.map +1 -0
- package/dist/src/preview/site-middleware.d.ts.map +1 -1
- package/dist/src/preview/site-middleware.js +39 -12
- package/dist/src/preview/site-middleware.js.map +1 -1
- package/dist/src/preview-build.d.ts +19 -8
- package/dist/src/preview-build.d.ts.map +1 -1
- package/dist/src/preview-build.js +47 -21
- package/dist/src/preview-build.js.map +1 -1
- package/dist/src/skill.d.ts +11 -0
- package/dist/src/skill.d.ts.map +1 -1
- package/dist/src/skill.js +32 -9
- package/dist/src/skill.js.map +1 -1
- package/dist/src/types.d.ts +15 -12
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +33 -7
- package/src/constants.ts +14 -3
- package/src/d3-build.ts +315 -0
- package/src/descriptors.ts +4 -2
- package/src/doctor/checks-live.ts +10 -4
- package/src/doctor/checks-offline.ts +16 -25
- package/src/doctor/checks-preview-limits.ts +137 -0
- package/src/doctor/checks-preview-live.ts +77 -0
- package/src/doctor/checks-preview.ts +302 -0
- package/src/doctor/config-scan.ts +14 -0
- package/src/doctor/run.ts +4 -0
- package/src/doctor/types.ts +11 -1
- package/src/editor-page/bundle.ts +5 -0
- package/src/editor-page/data-schemas.ts +47 -0
- package/src/editor-page/emit.ts +124 -8
- package/src/index.ts +2 -0
- package/src/integration.ts +176 -40
- package/src/preview/_dispatch.ts +36 -1
- package/src/preview/_resolve-opts.ts +6 -1
- package/src/preview/banner.ts +61 -41
- package/src/preview/cloudflare-adapter.ts +103 -0
- package/src/preview/cookie.ts +14 -23
- package/src/preview/dispatcher.ts +407 -0
- package/src/preview/entry-code.ts +51 -0
- package/src/preview/headers.ts +35 -7
- package/src/preview/landing.ts +115 -37
- package/src/preview/middleware.ts +264 -88
- package/src/preview/preview-path.ts +57 -0
- package/src/preview/redeem-url.ts +14 -0
- package/src/preview/site-middleware.ts +47 -12
- package/src/preview-build.ts +67 -24
- package/src/preview-handler.astro +2 -1
- package/src/skill/SKILL.md +14 -2
- package/src/skill.ts +36 -8
- package/src/types.ts +15 -12
|
@@ -1,14 +1,23 @@
|
|
|
1
|
+
import { PREVIEW_PROTOCOL, previewBranchSlug } from '@conloca/content-api/editor-host';
|
|
1
2
|
import { createRemoteJWKSet, customFetch, type JWTPayload, type JWTVerifyGetKey, jwtVerify } from 'jose';
|
|
2
|
-
import {
|
|
3
|
+
import { parseCookieValues, serializePreviewCookie } from './cookie.js';
|
|
4
|
+
import { exchangeEntryCode } from './entry-code.js';
|
|
5
|
+
import { PREVIEW_CACHE_HEADERS } from './headers.js';
|
|
3
6
|
import { previewLabel } from './label.js';
|
|
4
|
-
import { renderNoPreviewLanding } from './landing.js';
|
|
7
|
+
import { renderNoPreviewLanding, renderPreviewUnavailable, renderShareEnded } from './landing.js';
|
|
5
8
|
import {
|
|
6
|
-
|
|
9
|
+
PREVIEW_CODE_PARAM,
|
|
7
10
|
PREVIEW_LABEL_HEADER,
|
|
11
|
+
PREVIEW_MODE_HEADER,
|
|
12
|
+
PREVIEW_PROTOCOL_HEADER,
|
|
8
13
|
PREVIEW_QUERY_PARAM,
|
|
14
|
+
type PreviewScope,
|
|
15
|
+
type PreviewServeMode,
|
|
9
16
|
previewRoutePrefix,
|
|
17
|
+
previewScope,
|
|
10
18
|
stripPreviewPrefix,
|
|
11
19
|
} from './preview-path.js';
|
|
20
|
+
import { GRANT_REF_RE, grantRef } from './redeem-url.js';
|
|
12
21
|
|
|
13
22
|
/**
|
|
14
23
|
* Options consumed by {@link handlePreviewRequest}.
|
|
@@ -28,7 +37,8 @@ import {
|
|
|
28
37
|
*/
|
|
29
38
|
export interface PreviewMiddlewareOptions {
|
|
30
39
|
saasEndpoint: string;
|
|
31
|
-
|
|
40
|
+
/** When set, every pass must name it (`iss`); unset, the signature by the endpoint's published keys is the check. */
|
|
41
|
+
saasIssuer?: string;
|
|
32
42
|
saasAudience: string;
|
|
33
43
|
/** The repository the preview build was made from, as passes name it (`owner/name`). */
|
|
34
44
|
repo: string;
|
|
@@ -46,7 +56,7 @@ export interface PreviewMiddlewareOptions {
|
|
|
46
56
|
buildCommit?: string;
|
|
47
57
|
getKey?: JWTVerifyGetKey;
|
|
48
58
|
/**
|
|
49
|
-
* Transport for the JWKS fetch. A deployment that cannot reach the endpoint
|
|
59
|
+
* Transport for the JWKS fetch and the entry-code exchange. A deployment that cannot reach the endpoint
|
|
50
60
|
* with a plain fetch by hostname supplies a Cloudflare binding's fetch here;
|
|
51
61
|
* otherwise key discovery gets another server's answer and every token
|
|
52
62
|
* looks forged.
|
|
@@ -63,6 +73,15 @@ export interface PreviewMiddlewareOptions {
|
|
|
63
73
|
* screen on every URL.
|
|
64
74
|
*/
|
|
65
75
|
onMiss?: 'landing' | 'pass-through';
|
|
76
|
+
/**
|
|
77
|
+
* How this build serves previews. `d2` (default): on the site's own deploy, where a share viewer whose pass
|
|
78
|
+
* expires opens the share link again. `d3`: behind the dispatcher next to a static site, under the route and the
|
|
79
|
+
* branch slug, with cookies scoped there. `d4`: on a separate preview deploy that has no other visitors. Under D3 and
|
|
80
|
+
* D4 a grant cookie lets an expired pass renew through the share link.
|
|
81
|
+
*/
|
|
82
|
+
mode?: PreviewServeMode;
|
|
83
|
+
/** D3: the requested branch slug, as the dispatcher names it; a pass opens only under its own branch's slug. */
|
|
84
|
+
slug?: string;
|
|
66
85
|
}
|
|
67
86
|
|
|
68
87
|
/**
|
|
@@ -80,6 +99,10 @@ export interface PreviewContext {
|
|
|
80
99
|
jwt: string;
|
|
81
100
|
/** Seconds until the JWT expires — bounds the preview cookie's Max-Age. */
|
|
82
101
|
expiresInSeconds?: number;
|
|
102
|
+
/** Set when the pass came from a share link: the link's id, its expiry (epoch seconds) and who shared it. */
|
|
103
|
+
grant?: { id: string; expiresAt: number; sharedBy: string };
|
|
104
|
+
/** The editor this branch is edited in; set only on an editor's own pass. */
|
|
105
|
+
editorUrl?: string;
|
|
83
106
|
}
|
|
84
107
|
|
|
85
108
|
// Augments Astro's `Locals` so a site's .astro files that read
|
|
@@ -121,11 +144,14 @@ export type PreviewVerdict =
|
|
|
121
144
|
* Order of checks:
|
|
122
145
|
* 0. `<base>/_conloca/preview/__exit` → expire the cookie and redirect,
|
|
123
146
|
* carrying this build's public label.
|
|
124
|
-
* 1. `?
|
|
125
|
-
* 302 with the cookie set and the
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
147
|
+
* 1. `?conloca_code=<code>` → trade the single-use code for the pass on
|
|
148
|
+
* the server; on success issue a 302 with the cookie set and the code
|
|
149
|
+
* stripped. A used code with a valid cookie redirects to the clean path.
|
|
150
|
+
* 2. `?conloca_preview=<JWT>` query param (protocol 1) → validate; on
|
|
151
|
+
* success issue a 302 with the cookie set and the query stripped.
|
|
152
|
+
* 3. The pass cookie (`__Host-conloca_preview`; under D3 `__Secure-conloca_preview_<slug>`) → every value sent,
|
|
153
|
+
* the first that verifies; on success return a `preview` verdict with the decoded context.
|
|
154
|
+
* 4. Neither present → no-preview landing page response.
|
|
129
155
|
*
|
|
130
156
|
* Invalid JWTs (tampered signature, expired, missing required claim, wrong
|
|
131
157
|
* issuer/audience/scope, or naming another repository, site or branch than
|
|
@@ -136,61 +162,134 @@ export type PreviewVerdict =
|
|
|
136
162
|
*/
|
|
137
163
|
export async function handlePreviewRequest(request: Request, opts: PreviewMiddlewareOptions): Promise<PreviewVerdict> {
|
|
138
164
|
const url = new URL(request.url);
|
|
165
|
+
const mode = opts.mode ?? 'd2';
|
|
139
166
|
|
|
140
167
|
// 0. Exit route: the banner's "Exit Preview" target. The cookie is HttpOnly
|
|
141
168
|
// on purpose, so leaving preview needs a server response that expires it.
|
|
142
169
|
// Handled before the credential checks — an expired cookie must not strand
|
|
143
170
|
// the visitor in a preview they can no longer exit. Its label lets the
|
|
144
171
|
// endpoint's probe tell which repository, site and branch this server serves.
|
|
172
|
+
// Under D3 the dispatcher answers it through this same function, without loading the preview server.
|
|
145
173
|
if (url.pathname === `${previewRoutePrefix(opts.base)}/__exit`) {
|
|
146
174
|
const label = await previewLabel(opts.repo, opts.siteId, opts.branch);
|
|
147
|
-
return { kind: 'response', response: makeExitRedirect(url, opts
|
|
175
|
+
return { kind: 'response', response: makeExitRedirect(url, opts, label) };
|
|
148
176
|
}
|
|
149
177
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// Invalid query-param JWT — fall through to the landing page rather than
|
|
161
|
-
// issuing a 401 (see docstring).
|
|
178
|
+
const landing = (): PreviewVerdict => {
|
|
179
|
+
// Stripped: the landing's "Go to production" link is the same path
|
|
180
|
+
// relative — rendered under the preview route it loops back to itself.
|
|
181
|
+
const pathname = stripPreviewPrefix(url.pathname, opts.base);
|
|
182
|
+
// Under a D3 slug the dispatcher would move that path back under the route, where the same failing cookie lands
|
|
183
|
+
// here again: the slug's exit clears it and then leaves for the published page.
|
|
184
|
+
const liveHref =
|
|
185
|
+
mode === 'd3' && opts.slug !== undefined
|
|
186
|
+
? `${previewScope('d3', opts.base, opts.slug).route}/__exit?to=${encodeURIComponent(pathname)}`
|
|
187
|
+
: undefined;
|
|
162
188
|
return {
|
|
163
189
|
kind: 'response',
|
|
164
190
|
response: renderNoPreviewLanding({
|
|
165
|
-
pathname
|
|
191
|
+
pathname,
|
|
166
192
|
saasEndpoint: opts.saasEndpoint,
|
|
193
|
+
...(liveHref === undefined ? {} : { liveHref }),
|
|
167
194
|
}),
|
|
168
195
|
};
|
|
169
|
-
}
|
|
196
|
+
};
|
|
197
|
+
const cookie = request.headers.get('cookie');
|
|
198
|
+
const code = url.searchParams.get(PREVIEW_CODE_PARAM);
|
|
199
|
+
// Under D3 only the dispatcher's slug makes a request a preview request, except an entry link without one (an older
|
|
200
|
+
// Conloca's, or a mint that couldn't read the mode): its slug comes from the pass. Anything else is the live site's.
|
|
201
|
+
const slugless = mode === 'd3' && opts.slug === undefined;
|
|
202
|
+
// A code counts only on the preview route: on the live site's own routes, trading it would let anyone make this
|
|
203
|
+
// server call Conloca once per request.
|
|
204
|
+
const entering = code !== null && stripPreviewPrefix(url.pathname, opts.base) !== url.pathname;
|
|
205
|
+
if (slugless && !entering) return opts.onMiss === 'pass-through' ? { kind: 'pass-through' } : landing();
|
|
206
|
+
// A slugless entry has no scope until the pass names its branch.
|
|
207
|
+
const scope = slugless ? null : previewScope(mode, opts.base, opts.slug);
|
|
170
208
|
|
|
171
|
-
//
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
209
|
+
// 1. Entry code (protocol 2): trade it for the pass on the server.
|
|
210
|
+
if (code !== null) {
|
|
211
|
+
const exchanged = await exchangeEntryCode({
|
|
212
|
+
saasEndpoint: opts.saasEndpoint,
|
|
213
|
+
code,
|
|
214
|
+
origin: url.origin,
|
|
215
|
+
repo: opts.repo,
|
|
216
|
+
siteId: opts.siteId,
|
|
217
|
+
fetchFn: opts.fetchFn,
|
|
218
|
+
});
|
|
219
|
+
const path = stripPreviewPrefix(url.pathname, opts.base);
|
|
220
|
+
const route = scope?.route ?? previewRoutePrefix(opts.base);
|
|
221
|
+
const exitHref = `${route}/__exit?to=${encodeURIComponent(path)}`;
|
|
222
|
+
if (exchanged.ok) {
|
|
223
|
+
const ctx = await tryValidate(exchanged.pass, opts);
|
|
224
|
+
if (ctx !== null) {
|
|
225
|
+
const entered = scope ?? previewScope('d3', opts.base, previewBranchSlug(ctx.branch));
|
|
226
|
+
return { kind: 'response', response: makeInitRedirect(url, exchanged.pass, ctx, opts, entered) };
|
|
227
|
+
}
|
|
228
|
+
} else if (exchanged.kind === 'ended') {
|
|
229
|
+
return {
|
|
230
|
+
kind: 'response',
|
|
231
|
+
response: renderShareEnded(exchanged.ended.verdict, { sharedBy: exchanged.ended.sharedBy, liveHref: exitHref }),
|
|
232
|
+
};
|
|
233
|
+
} else if (exchanged.kind === 'unavailable') {
|
|
234
|
+
// Never the held pass: it may open another branch than the link names. Under D3 the dispatcher forwarded the
|
|
235
|
+
// entry without its slug, so Retry rebuilds the link it came from; a slugless one is retried as it is.
|
|
236
|
+
const retryHref =
|
|
237
|
+
mode === 'd3' && scope !== null
|
|
238
|
+
? `${scope.route}${pageUnderBase(path, opts.base)}${url.search}`
|
|
239
|
+
: `${url.pathname}${url.search}`;
|
|
240
|
+
return { kind: 'response', response: renderPreviewUnavailable({ retryHref, exitHref }) };
|
|
241
|
+
} else if (scope !== null && (await firstValid(parseCookieValues(cookie, scope.pass), opts)) !== null) {
|
|
242
|
+
// A used code reopened from history while the cookie still holds a pass: open the page.
|
|
243
|
+
return { kind: 'response', response: makeCleanRedirect(url, opts, scope) };
|
|
178
244
|
}
|
|
245
|
+
return landing();
|
|
179
246
|
}
|
|
247
|
+
if (scope === null) return landing();
|
|
180
248
|
|
|
181
|
-
//
|
|
182
|
-
|
|
183
|
-
|
|
249
|
+
// 2. Query-param init path (protocol 1, read for one release). An invalid
|
|
250
|
+
// pass gets the landing page rather than a 401 (see docstring). D3 never had protocol-1 links, so it takes none.
|
|
251
|
+
const queryToken = mode === 'd3' ? null : url.searchParams.get(PREVIEW_QUERY_PARAM);
|
|
252
|
+
if (queryToken !== null) {
|
|
253
|
+
const ctx = await tryValidate(queryToken, opts);
|
|
254
|
+
return ctx === null
|
|
255
|
+
? landing()
|
|
256
|
+
: { kind: 'response', response: makeInitRedirect(url, queryToken, ctx, opts, scope) };
|
|
184
257
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
258
|
+
|
|
259
|
+
// 3. Cookie path.
|
|
260
|
+
const ctx = await firstValid(parseCookieValues(cookie, scope.pass), opts);
|
|
261
|
+
if (ctx !== null) return { kind: 'preview', ctx };
|
|
262
|
+
|
|
263
|
+
// 4. No valid preview context.
|
|
264
|
+
// Under D3 and D4 a share viewer's expired pass renews through the share link the grant cookie names.
|
|
265
|
+
const ref =
|
|
266
|
+
scope.grant === null ? undefined : parseCookieValues(cookie, scope.grant).find((v) => GRANT_REF_RE.test(v));
|
|
267
|
+
if (ref !== undefined) {
|
|
268
|
+
const [org, grant] = ref.split('.');
|
|
269
|
+
const to = encodeURIComponent(stripPreviewPrefix(url.pathname, opts.base));
|
|
270
|
+
return {
|
|
271
|
+
kind: 'response',
|
|
272
|
+
response: new Response(null, {
|
|
273
|
+
status: 302,
|
|
274
|
+
headers: {
|
|
275
|
+
location: `${opts.saasEndpoint}/preview/redeem/${org}/${grant}?renew=1&to=${to}`,
|
|
276
|
+
...PREVIEW_CACHE_HEADERS,
|
|
277
|
+
},
|
|
278
|
+
}),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
if (opts.onMiss === 'pass-through') return { kind: 'pass-through' };
|
|
282
|
+
return landing();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** The first token that verifies, in order; a planted cookie sent first must not lock the viewer out. */
|
|
286
|
+
async function firstValid(tokens: readonly string[], opts: PreviewMiddlewareOptions): Promise<PreviewContext | null> {
|
|
287
|
+
for (const token of tokens) {
|
|
288
|
+
// oxlint-disable-next-line no-await-in-loop -- in order: the first that verifies wins, the rest are never checked.
|
|
289
|
+
const ctx = await tryValidate(token, opts);
|
|
290
|
+
if (ctx !== null) return ctx;
|
|
291
|
+
}
|
|
292
|
+
return null;
|
|
194
293
|
}
|
|
195
294
|
|
|
196
295
|
/**
|
|
@@ -203,8 +302,8 @@ async function tryValidate(token: string, opts: PreviewMiddlewareOptions): Promi
|
|
|
203
302
|
const getKey = opts.getKey ?? defaultJwksResolver(opts.saasEndpoint, opts.fetchFn);
|
|
204
303
|
try {
|
|
205
304
|
const { payload } = await jwtVerify(token, getKey, {
|
|
206
|
-
issuer: opts.saasIssuer,
|
|
207
305
|
audience: opts.saasAudience,
|
|
306
|
+
...(opts.saasIssuer === undefined ? {} : { issuer: opts.saasIssuer }),
|
|
208
307
|
});
|
|
209
308
|
return projectContext(payload, token, opts);
|
|
210
309
|
} catch {
|
|
@@ -252,12 +351,37 @@ function projectContext(payload: JWTPayload, jwt: string, opts: PreviewMiddlewar
|
|
|
252
351
|
if (!branch || !org || !repo) return null;
|
|
253
352
|
if (repo !== opts.repo || readStringClaim(payload, 'site_id') !== opts.siteId) return null;
|
|
254
353
|
if (branch !== opts.branch && readStringClaim(payload, 'served_by') !== opts.branch) return null;
|
|
255
|
-
//
|
|
256
|
-
//
|
|
257
|
-
|
|
354
|
+
// Under D3 the cookie lives under its branch's slug: a pass for another branch never opens this one's pages. A
|
|
355
|
+
// slugless entry has no slug yet; its scope comes from this branch (handlePreviewRequest).
|
|
356
|
+
if (opts.mode === 'd3' && opts.slug !== undefined && previewBranchSlug(branch) !== opts.slug) return null;
|
|
258
357
|
const expiresInSeconds =
|
|
259
358
|
typeof payload.exp === 'number' ? Math.max(0, payload.exp - Math.floor(Date.now() / 1000)) : undefined;
|
|
260
|
-
|
|
359
|
+
// A share pass names its link and the link's expiry; a pass that names a link without one is malformed.
|
|
360
|
+
const grantId = readStringClaim(payload, 'grant');
|
|
361
|
+
const grantExp = payload.grant_exp;
|
|
362
|
+
if (grantId !== undefined && typeof grantExp !== 'number') return null;
|
|
363
|
+
const grant =
|
|
364
|
+
grantId === undefined || typeof grantExp !== 'number'
|
|
365
|
+
? undefined
|
|
366
|
+
: { id: grantId, expiresAt: grantExp, sharedBy: readStringClaim(payload, 'shared_by') ?? '' };
|
|
367
|
+
const editorUrl = grant === undefined ? editorUrlOf(readStringClaim(payload, 'editor_url')) : undefined;
|
|
368
|
+
return {
|
|
369
|
+
branch,
|
|
370
|
+
org,
|
|
371
|
+
repo,
|
|
372
|
+
jwt,
|
|
373
|
+
expiresInSeconds,
|
|
374
|
+
...(grant === undefined ? {} : { grant }),
|
|
375
|
+
...(editorUrl === undefined ? {} : { editorUrl }),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** The editor address a pass names, when it is an https URL (http only on `.localhost`). */
|
|
380
|
+
function editorUrlOf(value: string | undefined): string | undefined {
|
|
381
|
+
if (value === undefined || !URL.canParse(value)) return undefined;
|
|
382
|
+
const url = new URL(value);
|
|
383
|
+
const local = url.protocol === 'http:' && url.hostname.endsWith('.localhost');
|
|
384
|
+
return url.protocol === 'https:' || local ? url.toString() : undefined;
|
|
261
385
|
}
|
|
262
386
|
|
|
263
387
|
function readStringClaim(payload: JWTPayload, name: string): string | undefined {
|
|
@@ -266,55 +390,107 @@ function readStringClaim(payload: JWTPayload, name: string): string | undefined
|
|
|
266
390
|
}
|
|
267
391
|
|
|
268
392
|
/**
|
|
269
|
-
* Builds the 302 that expires the preview
|
|
270
|
-
* the published page
|
|
271
|
-
*
|
|
272
|
-
* this route into an open redirect), anything else falls back to the site's
|
|
273
|
-
* base. The `conloca-preview` header carries this build's public label.
|
|
393
|
+
* Builds the 302 that expires the preview cookies and returns the visitor to
|
|
394
|
+
* the published page `?to=` names (see {@link exitTarget}). The headers carry
|
|
395
|
+
* this build's public label, the preview protocol it speaks and its mode.
|
|
274
396
|
*/
|
|
275
|
-
function makeExitRedirect(url: URL,
|
|
276
|
-
const
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
},
|
|
397
|
+
function makeExitRedirect(url: URL, opts: PreviewMiddlewareOptions, label: string): Response {
|
|
398
|
+
const mode = opts.mode ?? 'd2';
|
|
399
|
+
const headers = new Headers({
|
|
400
|
+
location: exitTarget(url, opts),
|
|
401
|
+
...PREVIEW_CACHE_HEADERS,
|
|
402
|
+
[PREVIEW_LABEL_HEADER]: label,
|
|
403
|
+
// Lets Conloca refuse to link to a preview server below its protocol floor.
|
|
404
|
+
[PREVIEW_PROTOCOL_HEADER]: String(PREVIEW_PROTOCOL),
|
|
405
|
+
// Lets Conloca shape the link: under D3 it starts with the branch slug.
|
|
406
|
+
[PREVIEW_MODE_HEADER]: mode,
|
|
286
407
|
});
|
|
408
|
+
// The probe asks a D3 server without a slug: it names no cookies, so none are expired.
|
|
409
|
+
if (mode === 'd3' && opts.slug === undefined) return new Response(null, { status: 302, headers });
|
|
410
|
+
const scope = previewScope(mode, opts.base, opts.slug);
|
|
411
|
+
headers.append('set-cookie', serializePreviewCookie(scope.pass, '', 0, scope.path));
|
|
412
|
+
// Without this the renewal would see the grant and send the viewer straight back in.
|
|
413
|
+
if (scope.grant !== null) headers.append('set-cookie', serializePreviewCookie(scope.grant, '', 0, scope.path));
|
|
414
|
+
return new Response(null, { status: 302, headers });
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Where Exit sends the visitor: `?to=` resolved against this origin and kept
|
|
419
|
+
* only while it stays there, as its path and query with the preview route
|
|
420
|
+
* (under D3, the route and the slug) stripped. Anything else falls back to the
|
|
421
|
+
* site's base: another origin, `//host`, and `/\host` or `/<tab>/host`, which
|
|
422
|
+
* browsers read as `//host`. The emitted path is checked again, because the
|
|
423
|
+
* browser parses it again: `https://site//host`, `/.//host` and
|
|
424
|
+
* `<preview route>//host` all leave a same-origin parse as the path `//host`.
|
|
425
|
+
*/
|
|
426
|
+
function exitTarget(url: URL, opts: PreviewMiddlewareOptions): string {
|
|
427
|
+
const { base } = opts;
|
|
428
|
+
const to = url.searchParams.get('to');
|
|
429
|
+
if (to === null || to === '') return base;
|
|
430
|
+
let target: URL;
|
|
431
|
+
try {
|
|
432
|
+
target = new URL(to, url.origin);
|
|
433
|
+
} catch {
|
|
434
|
+
return base;
|
|
435
|
+
}
|
|
436
|
+
if (target.origin !== url.origin) return base;
|
|
437
|
+
const location = `${pageOf(target.pathname, opts)}${target.search}`;
|
|
438
|
+
return new URL(location, url.origin).origin === url.origin ? location : base;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** A pathname with the preview route stripped; under D3 the branch slug goes with it. */
|
|
442
|
+
function pageOf(pathname: string, opts: PreviewMiddlewareOptions): string {
|
|
443
|
+
if (opts.mode === 'd3' && opts.slug !== undefined) {
|
|
444
|
+
const { route } = previewScope('d3', opts.base, opts.slug);
|
|
445
|
+
if (pathname === route || pathname.startsWith(`${route}/`)) {
|
|
446
|
+
return `${opts.base.replace(/\/+$/u, '')}${pathname.slice(route.length) || '/'}`;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return stripPreviewPrefix(pathname, opts.base);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** A page path under the site's base, as it follows the D3 route: `/docs/blog` under `/docs/` is `/blog`. */
|
|
453
|
+
function pageUnderBase(path: string, base: string): string {
|
|
454
|
+
return path.slice(base.replace(/\/+$/u, '').length) || '/';
|
|
287
455
|
}
|
|
288
456
|
|
|
289
457
|
/**
|
|
290
|
-
*
|
|
291
|
-
* the
|
|
292
|
-
*
|
|
458
|
+
* The requested URL without the code or pass parameter. Under D2 and D4 the cookie is on `Path=/`, so the visitor
|
|
459
|
+
* belongs on the real path; under D3 the pass lives under the route and slug, so the page stays there. Other query
|
|
460
|
+
* parameters (analytics tags) stay.
|
|
293
461
|
*/
|
|
294
|
-
function
|
|
462
|
+
function cleanLocation(url: URL, opts: PreviewMiddlewareOptions, scope: PreviewScope): string {
|
|
295
463
|
const cleaned = new URL(url.toString());
|
|
296
464
|
cleaned.searchParams.delete(PREVIEW_QUERY_PARAM);
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
465
|
+
cleaned.searchParams.delete(PREVIEW_CODE_PARAM);
|
|
466
|
+
const page = stripPreviewPrefix(cleaned.pathname, opts.base);
|
|
467
|
+
cleaned.pathname = opts.mode === 'd3' ? `${scope.route}${pageUnderBase(page, opts.base)}` : page;
|
|
468
|
+
const location = cleaned.toString();
|
|
469
|
+
return cleaned.search === '' && location.endsWith('?') ? location.slice(0, -1) : location;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** The 302 that sets the pass cookie (and, under D3 and D4, the grant cookie) and lands on the page. */
|
|
473
|
+
function makeInitRedirect(
|
|
474
|
+
url: URL,
|
|
475
|
+
token: string,
|
|
476
|
+
ctx: PreviewContext,
|
|
477
|
+
opts: PreviewMiddlewareOptions,
|
|
478
|
+
scope: PreviewScope,
|
|
479
|
+
): Response {
|
|
480
|
+
const headers = new Headers({ location: cleanLocation(url, opts, scope), ...PREVIEW_CACHE_HEADERS });
|
|
481
|
+
headers.append('set-cookie', serializePreviewCookie(scope.pass, token, ctx.expiresInSeconds, scope.path));
|
|
482
|
+
const { org, grant } = ctx;
|
|
483
|
+
if (scope.grant !== null && grant !== undefined) {
|
|
484
|
+
const lifetime = grant.expiresAt - Math.floor(Date.now() / 1000);
|
|
485
|
+
headers.append('set-cookie', serializePreviewCookie(scope.grant, grantRef(org, grant.id), lifetime, scope.path));
|
|
311
486
|
}
|
|
487
|
+
return new Response(null, { status: 302, headers });
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** The 302 to the page for a visitor who already holds a valid pass. */
|
|
491
|
+
function makeCleanRedirect(url: URL, opts: PreviewMiddlewareOptions, scope: PreviewScope): Response {
|
|
312
492
|
return new Response(null, {
|
|
313
493
|
status: 302,
|
|
314
|
-
headers: {
|
|
315
|
-
location,
|
|
316
|
-
'set-cookie': serializePreviewCookie(PREVIEW_COOKIE_NAME, token, maxAgeSeconds),
|
|
317
|
-
'cache-control': 'private, no-store',
|
|
318
|
-
},
|
|
494
|
+
headers: { location: cleanLocation(url, opts, scope), ...PREVIEW_CACHE_HEADERS },
|
|
319
495
|
});
|
|
320
496
|
}
|
|
@@ -12,14 +12,64 @@
|
|
|
12
12
|
export const PREVIEW_ROUTE_SEGMENT = '/_conloca/preview';
|
|
13
13
|
export const PREVIEW_QUERY_PARAM = 'conloca_preview';
|
|
14
14
|
export const PREVIEW_COOKIE_NAME = '__Host-conloca_preview';
|
|
15
|
+
/** Protocol 2: the single-use code a preview link carries instead of the pass. */
|
|
16
|
+
export const PREVIEW_CODE_PARAM = 'conloca_code';
|
|
17
|
+
/** The share link a viewer's pass came from (D4 only), so an expired pass can renew through it. */
|
|
18
|
+
export const PREVIEW_GRANT_COOKIE_NAME = '__Host-conloca_grant';
|
|
15
19
|
/** Response header on the exit route's redirect that carries the public label. */
|
|
16
20
|
export const PREVIEW_LABEL_HEADER = 'conloca-preview';
|
|
17
21
|
|
|
22
|
+
/** Response header on the exit route's redirect that names the preview protocol this build speaks. */
|
|
23
|
+
export const PREVIEW_PROTOCOL_HEADER = 'conloca-preview-protocol';
|
|
24
|
+
|
|
25
|
+
/** D3: the request header the dispatcher names the requested branch slug in. It strips any the browser sent. */
|
|
26
|
+
export const PREVIEW_SLUG_HEADER = 'conloca-preview-slug';
|
|
27
|
+
|
|
28
|
+
/** Response header on the exit route's redirect that names how this build serves previews. */
|
|
29
|
+
export const PREVIEW_MODE_HEADER = 'conloca-preview-mode';
|
|
30
|
+
|
|
31
|
+
/** How one preview server is reached: D2 and D4 at the page's own path, D3 under the route and the branch slug. */
|
|
32
|
+
export type PreviewServeMode = 'd2' | 'd3' | 'd4';
|
|
33
|
+
|
|
34
|
+
/** One preview's cookie names, the path they live on, and the route its links and exit sit under. */
|
|
35
|
+
export interface PreviewScope {
|
|
36
|
+
pass: string;
|
|
37
|
+
/** The share link's cookie; D2 has none (a viewer whose pass expired opens the link again). */
|
|
38
|
+
grant: string | null;
|
|
39
|
+
path: string;
|
|
40
|
+
route: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* D3 scopes both cookies to `<base>_conloca/preview/<slug>`, so two tabs on two branches keep two passes. D2 and D4
|
|
45
|
+
* keep the `__Host-` names on `Path=/`, which is what lets the pass follow every same-origin link there.
|
|
46
|
+
*/
|
|
47
|
+
export function previewScope(mode: PreviewServeMode, base: string, slug: string | undefined): PreviewScope {
|
|
48
|
+
const prefix = previewRoutePrefix(base);
|
|
49
|
+
if (mode !== 'd3') {
|
|
50
|
+
return {
|
|
51
|
+
pass: PREVIEW_COOKIE_NAME,
|
|
52
|
+
grant: mode === 'd4' ? PREVIEW_GRANT_COOKIE_NAME : null,
|
|
53
|
+
path: '/',
|
|
54
|
+
route: prefix,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (slug === undefined) throw new Error('@conloca/cloud-astro: a d3 preview scope needs the branch slug');
|
|
58
|
+
const route = `${prefix}/${slug}`;
|
|
59
|
+
return { pass: `__Secure-conloca_preview_${slug}`, grant: `__Secure-conloca_grant_${slug}`, path: route, route };
|
|
60
|
+
}
|
|
61
|
+
|
|
18
62
|
/** The preview route under Astro's `base` (`'/'` → `/_conloca/preview`, `'/docs/'` → `/docs/_conloca/preview`). */
|
|
19
63
|
export function previewRoutePrefix(base: string): string {
|
|
20
64
|
return `${base.replace(/\/+$/u, '')}${PREVIEW_ROUTE_SEGMENT}`;
|
|
21
65
|
}
|
|
22
66
|
|
|
67
|
+
/** The `run_worker_first` entries that belong to the preview, not the site: its route, and the image endpoint drafts use. */
|
|
68
|
+
export function previewOwnEntries(base: string): string[] {
|
|
69
|
+
const prefix = previewRoutePrefix(base);
|
|
70
|
+
return [prefix, `${prefix}/*`, `${base.replace(/\/+$/u, '')}/_image`];
|
|
71
|
+
}
|
|
72
|
+
|
|
23
73
|
/**
|
|
24
74
|
* Strip the preview route from a pathname, leaving the page path under the
|
|
25
75
|
* base. Only the route at the base matches: `/blog/_conloca/preview/x` and
|
|
@@ -33,6 +83,13 @@ export function stripPreviewPrefix(pathname: string, base: string): string {
|
|
|
33
83
|
return `${base.replace(/\/+$/u, '')}${rest === '' ? '/' : rest}`;
|
|
34
84
|
}
|
|
35
85
|
|
|
86
|
+
/** A site path without the base, as the editor names pages (`/docs/guide` under `/docs/` → `/guide`). */
|
|
87
|
+
export function pathUnderBase(path: string, base: string): string {
|
|
88
|
+
const root = base.replace(/\/+$/u, '');
|
|
89
|
+
if (root === '' || (path !== root && !path.startsWith(`${root}/`))) return path;
|
|
90
|
+
return path.slice(root.length) || '/';
|
|
91
|
+
}
|
|
92
|
+
|
|
36
93
|
/**
|
|
37
94
|
* Canonical path form for page-status lookups and production rewrites: no
|
|
38
95
|
* trailing slash, except the root itself. Browser-facing links carry trailing
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The org id as unpadded base64url: the form share links, entry codes and the grant cookie carry it in. */
|
|
2
|
+
export function orgToken(org: string): string {
|
|
3
|
+
let binary = '';
|
|
4
|
+
for (const byte of new TextEncoder().encode(org)) binary += String.fromCharCode(byte);
|
|
5
|
+
return btoa(binary).replace(/\+/gu, '-').replace(/\//gu, '_').replace(/=+$/u, '');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** `<orgToken>.<grantId>`: names one share link without naming the org in clear. */
|
|
9
|
+
export function grantRef(org: string, grantId: string): string {
|
|
10
|
+
return `${orgToken(org)}.${grantId}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A grant reference as the grant cookie holds it. */
|
|
14
|
+
export const GRANT_REF_RE = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]{22}$/u;
|