@conloca/cloud-astro 0.2.2 → 0.2.4
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 +378 -207
- package/dist/.build-inputs.json +69 -0
- package/dist/src/component-manifest.d.ts +5 -3
- package/dist/src/component-manifest.d.ts.map +1 -1
- package/dist/src/component-manifest.js +7 -7
- package/dist/src/component-manifest.js.map +1 -1
- package/dist/src/constants.d.ts +14 -13
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +14 -13
- 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 +2 -2
- 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-address.d.ts +18 -0
- package/dist/src/doctor/checks-address.d.ts.map +1 -0
- package/dist/src/doctor/checks-address.js +163 -0
- package/dist/src/doctor/checks-address.js.map +1 -0
- package/dist/src/doctor/checks-live.d.ts +8 -5
- package/dist/src/doctor/checks-live.d.ts.map +1 -1
- package/dist/src/doctor/checks-live.js +226 -188
- package/dist/src/doctor/checks-live.js.map +1 -1
- package/dist/src/doctor/checks-offline.d.ts +6 -25
- package/dist/src/doctor/checks-offline.d.ts.map +1 -1
- package/dist/src/doctor/checks-offline.js +30 -259
- 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 +12 -3
- package/dist/src/doctor/config-scan.d.ts.map +1 -1
- package/dist/src/doctor/config-scan.js +38 -11
- 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 +16 -10
- package/dist/src/doctor/run.js.map +1 -1
- package/dist/src/doctor/types.d.ts +17 -7
- package/dist/src/doctor/types.d.ts.map +1 -1
- package/dist/src/doctor/types.js +17 -7
- package/dist/src/doctor/types.js.map +1 -1
- package/dist/src/editor-page/bundle.d.ts +90 -0
- package/dist/src/editor-page/bundle.d.ts.map +1 -0
- package/dist/src/editor-page/bundle.js +373 -0
- package/dist/src/editor-page/bundle.js.map +1 -0
- 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 +69 -0
- package/dist/src/editor-page/emit.d.ts.map +1 -0
- package/dist/src/editor-page/emit.js +254 -0
- package/dist/src/editor-page/emit.js.map +1 -0
- package/dist/src/editor-page/framing.d.ts +23 -0
- package/dist/src/editor-page/framing.d.ts.map +1 -0
- package/dist/src/editor-page/framing.js +39 -0
- package/dist/src/editor-page/framing.js.map +1 -0
- package/dist/src/editor-page/page.d.ts +9 -0
- package/dist/src/editor-page/page.d.ts.map +1 -0
- package/dist/src/editor-page/page.js +31 -0
- package/dist/src/editor-page/page.js.map +1 -0
- package/dist/src/editor-page/site-base.d.ts +4 -0
- package/dist/src/editor-page/site-base.d.ts.map +1 -0
- package/dist/src/editor-page/site-base.js +13 -0
- package/dist/src/editor-page/site-base.js.map +1 -0
- package/dist/src/git.d.ts +11 -8
- package/dist/src/git.d.ts.map +1 -1
- package/dist/src/git.js +25 -8
- package/dist/src/git.js.map +1 -1
- package/dist/src/index.d.ts +4 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +11 -6
- package/dist/src/index.js.map +1 -1
- package/dist/src/integration.d.ts +1 -1
- package/dist/src/integration.d.ts.map +1 -1
- package/dist/src/integration.js +377 -416
- package/dist/src/integration.js.map +1 -1
- package/dist/src/node.d.ts +3 -1
- package/dist/src/node.d.ts.map +1 -1
- package/dist/src/node.js +7 -2
- package/dist/src/node.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 +19 -10
- package/dist/src/preview/_resolve-opts.d.ts.map +1 -1
- package/dist/src/preview/_resolve-opts.js +24 -11
- 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 -43
- 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 +13 -14
- package/dist/src/preview/cookie.d.ts.map +1 -1
- package/dist/src/preview/cookie.js +19 -26
- 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/label.d.ts +8 -0
- package/dist/src/preview/label.d.ts.map +1 -0
- package/dist/src/preview/label.js +14 -0
- package/dist/src/preview/label.js.map +1 -0
- package/dist/src/preview/landing.d.ts +23 -2
- package/dist/src/preview/landing.d.ts.map +1 -1
- package/dist/src/preview/landing.js +105 -37
- package/dist/src/preview/landing.js.map +1 -1
- package/dist/src/preview/middleware.d.ts +49 -22
- package/dist/src/preview/middleware.d.ts.map +1 -1
- package/dist/src/preview/middleware.js +259 -100
- package/dist/src/preview/middleware.js.map +1 -1
- package/dist/src/preview/preview-path.d.ts +47 -5
- package/dist/src/preview/preview-path.d.ts.map +1 -1
- package/dist/src/preview/preview-path.js +65 -8
- 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 +5 -5
- package/dist/src/preview/site-middleware.d.ts.map +1 -1
- package/dist/src/preview/site-middleware.js +53 -26
- package/dist/src/preview/site-middleware.js.map +1 -1
- package/dist/src/preview-build.d.ts +29 -15
- package/dist/src/preview-build.d.ts.map +1 -1
- package/dist/src/preview-build.js +66 -25
- package/dist/src/preview-build.js.map +1 -1
- package/dist/src/preview.d.ts +1 -1
- package/dist/src/preview.d.ts.map +1 -1
- package/dist/src/preview.js +1 -1
- package/dist/src/preview.js.map +1 -1
- package/dist/src/repository.d.ts +1 -1
- package/dist/src/repository.js +1 -1
- package/dist/src/resolve-options.d.ts +17 -0
- package/dist/src/resolve-options.d.ts.map +1 -0
- package/dist/src/resolve-options.js +21 -0
- package/dist/src/resolve-options.js.map +1 -0
- package/dist/src/site-id.d.ts +0 -2
- package/dist/src/site-id.d.ts.map +1 -1
- package/dist/src/site-id.js +2 -3
- package/dist/src/site-id.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 +33 -78
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +63 -13
- package/src/component-manifest.ts +11 -8
- package/src/constants.ts +15 -13
- package/src/d3-build.ts +315 -0
- package/src/descriptors.ts +6 -4
- package/src/doctor/checks-address.ts +190 -0
- package/src/doctor/checks-live.ts +262 -268
- package/src/doctor/checks-offline.ts +32 -279
- 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 +35 -9
- package/src/doctor/run.ts +18 -16
- package/src/doctor/types.ts +17 -7
- package/src/editor-page/bundle.ts +487 -0
- package/src/editor-page/data-schemas.ts +47 -0
- package/src/editor-page/emit.ts +362 -0
- package/src/editor-page/framing.ts +62 -0
- package/src/editor-page/page.ts +37 -0
- package/src/editor-page/site-base.ts +11 -0
- package/src/git.ts +29 -10
- package/src/index.ts +22 -6
- package/src/integration.ts +399 -469
- package/src/node.ts +18 -2
- package/src/preview/_dispatch.ts +36 -1
- package/src/preview/_resolve-opts.ts +35 -12
- package/src/preview/banner.ts +61 -43
- package/src/preview/cloudflare-adapter.ts +103 -0
- package/src/preview/cookie.ts +18 -24
- 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/label.ts +12 -0
- package/src/preview/landing.ts +117 -39
- package/src/preview/middleware.ts +302 -115
- package/src/preview/preview-path.ts +83 -8
- package/src/preview/redeem-url.ts +14 -0
- package/src/preview/site-middleware.ts +60 -30
- package/src/preview-build.ts +85 -26
- package/src/preview-handler.astro +12 -9
- package/src/preview.ts +0 -1
- package/src/repository.ts +1 -1
- package/src/resolve-options.ts +37 -0
- package/src/site-id.ts +2 -4
- package/src/skill/SKILL.md +63 -55
- package/src/skill.ts +36 -8
- package/src/types.ts +33 -79
- package/src/virtual-modules.d.ts +4 -1
- package/dist/src/build-commit.d.ts +0 -19
- package/dist/src/build-commit.d.ts.map +0 -1
- package/dist/src/build-commit.js +0 -6
- package/dist/src/build-commit.js.map +0 -1
- package/dist/src/bundle.d.ts +0 -181
- package/dist/src/bundle.d.ts.map +0 -1
- package/dist/src/bundle.js +0 -413
- package/dist/src/bundle.js.map +0 -1
- package/dist/src/discovery.d.ts +0 -58
- package/dist/src/discovery.d.ts.map +0 -1
- package/dist/src/discovery.js +0 -133
- package/dist/src/discovery.js.map +0 -1
- package/dist/src/register.d.ts +0 -98
- package/dist/src/register.d.ts.map +0 -1
- package/dist/src/register.js +0 -103
- package/dist/src/register.js.map +0 -1
- package/src/build-commit.ts +0 -22
- package/src/bundle.ts +0 -570
- package/src/discovery.ts +0 -183
- package/src/register.ts +0 -180
|
@@ -1,15 +1,33 @@
|
|
|
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 {
|
|
4
|
-
import {
|
|
3
|
+
import { parseCookieValues, serializePreviewCookie } from './cookie.js';
|
|
4
|
+
import { exchangeEntryCode } from './entry-code.js';
|
|
5
|
+
import { PREVIEW_CACHE_HEADERS } from './headers.js';
|
|
6
|
+
import { previewLabel } from './label.js';
|
|
7
|
+
import { renderNoPreviewLanding, renderPreviewUnavailable, renderShareEnded } from './landing.js';
|
|
8
|
+
import {
|
|
9
|
+
PREVIEW_CODE_PARAM,
|
|
10
|
+
PREVIEW_LABEL_HEADER,
|
|
11
|
+
PREVIEW_MODE_HEADER,
|
|
12
|
+
PREVIEW_PROTOCOL_HEADER,
|
|
13
|
+
PREVIEW_QUERY_PARAM,
|
|
14
|
+
type PreviewScope,
|
|
15
|
+
type PreviewServeMode,
|
|
16
|
+
previewRoutePrefix,
|
|
17
|
+
previewScope,
|
|
18
|
+
stripPreviewPrefix,
|
|
19
|
+
} from './preview-path.js';
|
|
20
|
+
import { GRANT_REF_RE, grantRef } from './redeem-url.js';
|
|
5
21
|
|
|
6
22
|
/**
|
|
7
23
|
* Options consumed by {@link handlePreviewRequest}.
|
|
8
24
|
*
|
|
9
|
-
* `
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* a
|
|
25
|
+
* `repo`, `siteId` and `branch` name what the preview build was made from. A
|
|
26
|
+
* pass is accepted only when it names the same repository and site, and either
|
|
27
|
+
* the same branch or, in `served_by`, this build's branch (a branch with no
|
|
28
|
+
* build of its own borrows the default branch's server; a branch's own build
|
|
29
|
+
* serves its own passes). `base` is Astro's `base`; the fixed preview route
|
|
30
|
+
* sits under it.
|
|
13
31
|
*
|
|
14
32
|
* `getKey` is an optional JWKS resolver injection seam. When omitted, the
|
|
15
33
|
* middleware constructs a `createRemoteJWKSet` pointed at
|
|
@@ -18,10 +36,18 @@ import { stripPreviewPrefix } from './preview-path.js';
|
|
|
18
36
|
* deterministically without touching the network.
|
|
19
37
|
*/
|
|
20
38
|
export interface PreviewMiddlewareOptions {
|
|
21
|
-
secret: string;
|
|
22
39
|
saasEndpoint: string;
|
|
23
|
-
|
|
40
|
+
/** When set, every pass must name it (`iss`); unset, the signature by the endpoint's published keys is the check. */
|
|
41
|
+
saasIssuer?: string;
|
|
24
42
|
saasAudience: string;
|
|
43
|
+
/** The repository the preview build was made from, as passes name it (`owner/name`). */
|
|
44
|
+
repo: string;
|
|
45
|
+
/** The site the preview build serves. */
|
|
46
|
+
siteId: string;
|
|
47
|
+
/** The branch the preview build was made from. */
|
|
48
|
+
branch: string;
|
|
49
|
+
/** Astro's `base`; the preview route and its redirects stay under it. */
|
|
50
|
+
base: string;
|
|
25
51
|
/**
|
|
26
52
|
* The commit this preview artifact was built from. Page-status compares the
|
|
27
53
|
* branch's committed head with it; absent (an older artifact) ⇒ the
|
|
@@ -30,7 +56,7 @@ export interface PreviewMiddlewareOptions {
|
|
|
30
56
|
buildCommit?: string;
|
|
31
57
|
getKey?: JWTVerifyGetKey;
|
|
32
58
|
/**
|
|
33
|
-
* 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
|
|
34
60
|
* with a plain fetch by hostname supplies a Cloudflare binding's fetch here;
|
|
35
61
|
* otherwise key discovery gets another server's answer and every token
|
|
36
62
|
* looks forged.
|
|
@@ -40,13 +66,22 @@ export interface PreviewMiddlewareOptions {
|
|
|
40
66
|
* What to do when the request carries no valid preview credential.
|
|
41
67
|
*
|
|
42
68
|
* - `'landing'` (default) — the no-preview landing page. Correct for the
|
|
43
|
-
*
|
|
69
|
+
* preview entry route, which nobody reaches by accident.
|
|
44
70
|
* - `'pass-through'` — let the request continue to the ordinary page. Correct
|
|
45
71
|
* for the site-wide interceptor, which sees every request in the preview
|
|
46
72
|
* build: an uncredentialed visitor must get the normal site, not a landing
|
|
47
73
|
* screen on every URL.
|
|
48
74
|
*/
|
|
49
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;
|
|
50
85
|
}
|
|
51
86
|
|
|
52
87
|
/**
|
|
@@ -64,6 +99,10 @@ export interface PreviewContext {
|
|
|
64
99
|
jwt: string;
|
|
65
100
|
/** Seconds until the JWT expires — bounds the preview cookie's Max-Age. */
|
|
66
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;
|
|
67
106
|
}
|
|
68
107
|
|
|
69
108
|
// Augments Astro's `Locals` so a site's .astro files that read
|
|
@@ -89,8 +128,8 @@ declare global {
|
|
|
89
128
|
* - `pass-through` — the caller MAY delegate to the ordinary page or
|
|
90
129
|
* static-asset handler. Returned when `onMiss: 'pass-through'` is set —
|
|
91
130
|
* the site-wide interceptor's mode, where an uncredentialed request must
|
|
92
|
-
* get the normal site. The
|
|
93
|
-
*
|
|
131
|
+
* get the normal site. The preview entry route omits `onMiss` and never
|
|
132
|
+
* sees this verdict.
|
|
94
133
|
*/
|
|
95
134
|
export type PreviewVerdict =
|
|
96
135
|
| { kind: 'pass-through' }
|
|
@@ -103,79 +142,154 @@ export type PreviewVerdict =
|
|
|
103
142
|
* the caller contract.
|
|
104
143
|
*
|
|
105
144
|
* Order of checks:
|
|
106
|
-
*
|
|
145
|
+
* 0. `<base>/_conloca/preview/__exit` → expire the cookie and redirect,
|
|
146
|
+
* carrying this build's public label.
|
|
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
|
|
107
151
|
* success issue a 302 with the cookie set and the query stripped.
|
|
108
|
-
*
|
|
109
|
-
* return a `preview` verdict with the decoded context.
|
|
110
|
-
*
|
|
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.
|
|
111
155
|
*
|
|
112
156
|
* Invalid JWTs (tampered signature, expired, missing required claim, wrong
|
|
113
|
-
* issuer/audience/scope
|
|
157
|
+
* issuer/audience/scope, or naming another repository, site or branch than
|
|
158
|
+
* this build serves) fall through to the landing page. We deliberately do
|
|
114
159
|
* NOT return 401 — a 401 to an unauthenticated caller would confirm that
|
|
115
160
|
* a particular path is preview-only, which leaks information about what
|
|
116
161
|
* the branch contains.
|
|
117
162
|
*/
|
|
118
163
|
export async function handlePreviewRequest(request: Request, opts: PreviewMiddlewareOptions): Promise<PreviewVerdict> {
|
|
119
164
|
const url = new URL(request.url);
|
|
120
|
-
const
|
|
121
|
-
const cookieName = previewParamName(opts.secret);
|
|
165
|
+
const mode = opts.mode ?? 'd2';
|
|
122
166
|
|
|
123
167
|
// 0. Exit route: the banner's "Exit Preview" target. The cookie is HttpOnly
|
|
124
168
|
// on purpose, so leaving preview needs a server response that expires it.
|
|
125
169
|
// Handled before the credential checks — an expired cookie must not strand
|
|
126
|
-
// the visitor in a preview they can no longer exit.
|
|
127
|
-
|
|
128
|
-
|
|
170
|
+
// the visitor in a preview they can no longer exit. Its label lets the
|
|
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.
|
|
173
|
+
if (url.pathname === `${previewRoutePrefix(opts.base)}/__exit`) {
|
|
174
|
+
const label = await previewLabel(opts.repo, opts.siteId, opts.branch);
|
|
175
|
+
return { kind: 'response', response: makeExitRedirect(url, opts, label) };
|
|
129
176
|
}
|
|
130
177
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
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;
|
|
188
|
+
return {
|
|
189
|
+
kind: 'response',
|
|
190
|
+
response: renderNoPreviewLanding({
|
|
191
|
+
pathname,
|
|
192
|
+
saasEndpoint: opts.saasEndpoint,
|
|
193
|
+
...(liveHref === undefined ? {} : { liveHref }),
|
|
194
|
+
}),
|
|
195
|
+
};
|
|
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);
|
|
208
|
+
|
|
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') {
|
|
136
229
|
return {
|
|
137
230
|
kind: 'response',
|
|
138
|
-
response:
|
|
231
|
+
response: renderShareEnded(exchanged.ended.verdict, { sharedBy: exchanged.ended.sharedBy, liveHref: exitHref }),
|
|
139
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) };
|
|
140
244
|
}
|
|
141
|
-
|
|
142
|
-
// issuing a 401 (see docstring).
|
|
143
|
-
return {
|
|
144
|
-
kind: 'response',
|
|
145
|
-
response: renderNoPreviewLanding({ pathname: stripPreviewPrefix(url.pathname), saasEndpoint: opts.saasEndpoint }),
|
|
146
|
-
};
|
|
245
|
+
return landing();
|
|
147
246
|
}
|
|
247
|
+
if (scope === null) return landing();
|
|
148
248
|
|
|
149
|
-
// 2.
|
|
150
|
-
|
|
151
|
-
const
|
|
152
|
-
if (
|
|
153
|
-
const ctx = await tryValidate(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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) };
|
|
157
257
|
}
|
|
158
258
|
|
|
159
|
-
// 3.
|
|
160
|
-
|
|
161
|
-
|
|
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
|
+
};
|
|
162
280
|
}
|
|
163
|
-
return {
|
|
164
|
-
|
|
165
|
-
// Stripped: the landing's "Go to production" link is the same path
|
|
166
|
-
// relative — rendered with the secret prefix it loops back to itself.
|
|
167
|
-
response: renderNoPreviewLanding({ pathname: stripPreviewPrefix(url.pathname), saasEndpoint: opts.saasEndpoint }),
|
|
168
|
-
};
|
|
281
|
+
if (opts.onMiss === 'pass-through') return { kind: 'pass-through' };
|
|
282
|
+
return landing();
|
|
169
283
|
}
|
|
170
284
|
|
|
171
|
-
/**
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return
|
|
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;
|
|
179
293
|
}
|
|
180
294
|
|
|
181
295
|
/**
|
|
@@ -188,10 +302,10 @@ async function tryValidate(token: string, opts: PreviewMiddlewareOptions): Promi
|
|
|
188
302
|
const getKey = opts.getKey ?? defaultJwksResolver(opts.saasEndpoint, opts.fetchFn);
|
|
189
303
|
try {
|
|
190
304
|
const { payload } = await jwtVerify(token, getKey, {
|
|
191
|
-
issuer: opts.saasIssuer,
|
|
192
305
|
audience: opts.saasAudience,
|
|
306
|
+
...(opts.saasIssuer === undefined ? {} : { issuer: opts.saasIssuer }),
|
|
193
307
|
});
|
|
194
|
-
return projectContext(payload, token);
|
|
308
|
+
return projectContext(payload, token, opts);
|
|
195
309
|
} catch {
|
|
196
310
|
return null;
|
|
197
311
|
}
|
|
@@ -224,20 +338,50 @@ function defaultJwksResolver(endpoint: string, fetchFn?: typeof fetch): JWTVerif
|
|
|
224
338
|
* Project a verified JWT payload into the preview context envelope. Returns
|
|
225
339
|
* `null` if any required claim is missing or wrong type — a JWT that
|
|
226
340
|
* validates cryptographically but lacks `branch` names no branch to read,
|
|
227
|
-
* so it is treated as invalid here
|
|
341
|
+
* so it is treated as invalid here — and when the pass is for another build:
|
|
342
|
+
* its `repo` and `site_id` must be the ones this build serves, and its
|
|
343
|
+
* `branch` must be this build's branch unless `served_by` names it (only
|
|
344
|
+
* Conloca sets `served_by`, and only to the default branch).
|
|
228
345
|
*/
|
|
229
|
-
function projectContext(payload: JWTPayload, jwt: string): PreviewContext | null {
|
|
346
|
+
function projectContext(payload: JWTPayload, jwt: string, opts: PreviewMiddlewareOptions): PreviewContext | null {
|
|
230
347
|
if (payload.scope !== 'preview') return null;
|
|
231
348
|
const branch = readStringClaim(payload, 'branch');
|
|
232
349
|
const org = readStringClaim(payload, 'org');
|
|
233
350
|
const repo = readStringClaim(payload, 'repo');
|
|
234
351
|
if (!branch || !org || !repo) return null;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
//
|
|
352
|
+
if (repo !== opts.repo || readStringClaim(payload, 'site_id') !== opts.siteId) return null;
|
|
353
|
+
if (branch !== opts.branch && readStringClaim(payload, 'served_by') !== opts.branch) return null;
|
|
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;
|
|
238
357
|
const expiresInSeconds =
|
|
239
358
|
typeof payload.exp === 'number' ? Math.max(0, payload.exp - Math.floor(Date.now() / 1000)) : undefined;
|
|
240
|
-
|
|
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;
|
|
241
385
|
}
|
|
242
386
|
|
|
243
387
|
function readStringClaim(payload: JWTPayload, name: string): string | undefined {
|
|
@@ -246,64 +390,107 @@ function readStringClaim(payload: JWTPayload, name: string): string | undefined
|
|
|
246
390
|
}
|
|
247
391
|
|
|
248
392
|
/**
|
|
249
|
-
* Builds the 302
|
|
250
|
-
* the
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
* The caller passes both the query-param name (so we know which key to
|
|
254
|
-
* strip) and the raw secret (so we can derive the cookie name without a
|
|
255
|
-
* roundtrip parse). The two are bound by `previewParamName(secret)` ===
|
|
256
|
-
* `queryName` at the call site.
|
|
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.
|
|
257
396
|
*/
|
|
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,
|
|
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
|
+
|
|
258
417
|
/**
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
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`.
|
|
263
425
|
*/
|
|
264
|
-
function
|
|
265
|
-
const
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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;
|
|
275
439
|
}
|
|
276
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) || '/';
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
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.
|
|
461
|
+
*/
|
|
462
|
+
function cleanLocation(url: URL, opts: PreviewMiddlewareOptions, scope: PreviewScope): string {
|
|
463
|
+
const cleaned = new URL(url.toString());
|
|
464
|
+
cleaned.searchParams.delete(PREVIEW_QUERY_PARAM);
|
|
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. */
|
|
277
473
|
function makeInitRedirect(
|
|
278
474
|
url: URL,
|
|
279
|
-
queryName: string,
|
|
280
475
|
token: string,
|
|
281
|
-
|
|
282
|
-
|
|
476
|
+
ctx: PreviewContext,
|
|
477
|
+
opts: PreviewMiddlewareOptions,
|
|
478
|
+
scope: PreviewScope,
|
|
283
479
|
): Response {
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
// canonical path — otherwise the first internal link they click drops the
|
|
291
|
-
// prefix, escapes preview, and silently shows published content.
|
|
292
|
-
cleaned.pathname = stripPreviewPrefix(cleaned.pathname);
|
|
293
|
-
// Normalise the search string: when the only param was the preview JWT,
|
|
294
|
-
// URL.search becomes '' but URL.toString() still emits a trailing `?` only
|
|
295
|
-
// if it was already empty. Re-serialising via URL strips that automatically;
|
|
296
|
-
// double-check by explicitly clearing the `?` when no params remain.
|
|
297
|
-
let location = cleaned.toString();
|
|
298
|
-
if (cleaned.search === '' && location.endsWith('?')) {
|
|
299
|
-
location = location.slice(0, -1);
|
|
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));
|
|
300
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 {
|
|
301
492
|
return new Response(null, {
|
|
302
493
|
status: 302,
|
|
303
|
-
headers: {
|
|
304
|
-
location,
|
|
305
|
-
'set-cookie': serializePreviewCookie(previewParamName(secret), token, maxAgeSeconds),
|
|
306
|
-
'cache-control': 'private, no-store',
|
|
307
|
-
},
|
|
494
|
+
headers: { location: cleanLocation(url, opts, scope), ...PREVIEW_CACHE_HEADERS },
|
|
308
495
|
});
|
|
309
496
|
}
|
|
@@ -1,18 +1,93 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The fixed preview route and the names it uses.
|
|
3
3
|
*
|
|
4
4
|
* Lives in its own module because the entry-hop middleware, the site
|
|
5
5
|
* middleware and the page-status client all need it without depending on
|
|
6
6
|
* each other.
|
|
7
7
|
*
|
|
8
|
-
* The
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* The route is public and the same on every site: the signed pass is the
|
|
9
|
+
* lock, checked against the repository, site and branch the preview build
|
|
10
|
+
* was made from.
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
export const PREVIEW_ROUTE_SEGMENT = '/_conloca/preview';
|
|
13
|
+
export const PREVIEW_QUERY_PARAM = 'conloca_preview';
|
|
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';
|
|
19
|
+
/** Response header on the exit route's redirect that carries the public label. */
|
|
20
|
+
export const PREVIEW_LABEL_HEADER = 'conloca-preview';
|
|
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
|
+
|
|
62
|
+
/** The preview route under Astro's `base` (`'/'` → `/_conloca/preview`, `'/docs/'` → `/docs/_conloca/preview`). */
|
|
63
|
+
export function previewRoutePrefix(base: string): string {
|
|
64
|
+
return `${base.replace(/\/+$/u, '')}${PREVIEW_ROUTE_SEGMENT}`;
|
|
65
|
+
}
|
|
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
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Strip the preview route from a pathname, leaving the page path under the
|
|
75
|
+
* base. Only the route at the base matches: `/blog/_conloca/preview/x` and
|
|
76
|
+
* `/_conloca/previewer/x` are left untouched, so a page that merely mentions
|
|
77
|
+
* the route is never re-anchored to a different page.
|
|
78
|
+
*/
|
|
79
|
+
export function stripPreviewPrefix(pathname: string, base: string): string {
|
|
80
|
+
const prefix = previewRoutePrefix(base);
|
|
81
|
+
if (pathname !== prefix && !pathname.startsWith(`${prefix}/`)) return pathname;
|
|
82
|
+
const rest = pathname.slice(prefix.length);
|
|
83
|
+
return `${base.replace(/\/+$/u, '')}${rest === '' ? '/' : rest}`;
|
|
84
|
+
}
|
|
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) || '/';
|
|
16
91
|
}
|
|
17
92
|
|
|
18
93
|
/**
|