@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
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
5
|
+
import { isRecord } from '@conloca/content-api/component-vocabulary';
|
|
6
|
+
import { parse } from 'jsonc-parser';
|
|
7
|
+
import { PACKAGE_NAME } from '../constants.js';
|
|
8
|
+
/**
|
|
9
|
+
* A `wrangler.jsonc` text → its object (comments and trailing commas allowed), else undefined. The return type is
|
|
10
|
+
* `isRecord`'s narrowing, inferred: anti-slop's `no-unsafe-dictionary-type` refuses the type written out.
|
|
11
|
+
*/
|
|
12
|
+
export function parseWranglerConfig(text) {
|
|
13
|
+
const value = parse(text, [], { allowTrailingComma: true });
|
|
14
|
+
return isRecord(value) ? value : undefined;
|
|
15
|
+
}
|
|
16
|
+
/** `wrangler.jsonc`, else `wrangler.json`, in the project; undefined when it has neither. */
|
|
17
|
+
export function wranglerConfigPath(root) {
|
|
18
|
+
return ['wrangler.jsonc', 'wrangler.json'].map((name) => join(root, name)).find((path) => existsSync(path));
|
|
19
|
+
}
|
|
20
|
+
/** Where `@astrojs/cloudflare` resolves from the project; throws naming the fix when it is not installed. */
|
|
21
|
+
export function resolveCloudflareAdapter(root) {
|
|
22
|
+
try {
|
|
23
|
+
return createRequire(join(root, 'package.json')).resolve('@astrojs/cloudflare');
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new Error(`${PACKAGE_NAME}: a static site's preview server runs on Cloudflare Workers and needs the Cloudflare adapter: ` +
|
|
27
|
+
'install @astrojs/cloudflare in this project');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Returns `object`, not `unknown`: anti-slop's no-unknown-returns reads function types too; `isIntegration` narrows the result.
|
|
31
|
+
function isAdapterFactory(value) {
|
|
32
|
+
return typeof value === 'function';
|
|
33
|
+
}
|
|
34
|
+
function isIntegration(value) {
|
|
35
|
+
return isRecord(value) && typeof value.name === 'string' && isRecord(value.hooks);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The adapter options per mode. Under D3 the child reads its config copy, and compiles images at build time: on-demand
|
|
39
|
+
* draft pages then emit `/_image` URLs the passthrough endpoint answers from the static files, with no `IMAGES` binding,
|
|
40
|
+
* which the dispatcher's config doesn't declare (the real-build round trip serves a fixture image through it).
|
|
41
|
+
* Under D4 the preview deploy is a Cloudflare Worker of its own and keeps the adapter's defaults.
|
|
42
|
+
*/
|
|
43
|
+
export function childAdapterOptions(mode, configPath) {
|
|
44
|
+
if (mode === 'd4')
|
|
45
|
+
return {};
|
|
46
|
+
return {
|
|
47
|
+
...(configPath === undefined ? {} : { configPath }),
|
|
48
|
+
imageService: { build: 'compile', runtime: 'passthrough' },
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The adapter for the preview build of a site that names none. Under D3 it reads a copy of the site's `wrangler.jsonc`
|
|
53
|
+
* without `main` and `assets`, at the top level and in every env block (both inherit): those belong to the dispatcher in
|
|
54
|
+
* front of the static site, and the adapter would take `main` for its own entry. Under D4 the site's own config, with the env
|
|
55
|
+
* block the build selects, is the preview deploy's.
|
|
56
|
+
* shortcut: relative paths inside the copy resolve from .conloca/; rewrite them when a site's config carries one.
|
|
57
|
+
*/
|
|
58
|
+
export async function cloudflareAdapter(rootUrl, mode) {
|
|
59
|
+
const root = fileURLToPath(rootUrl);
|
|
60
|
+
const module = await import(pathToFileURL(resolveCloudflareAdapter(root)).href);
|
|
61
|
+
// Not isRecord: Bun gives a module namespace a prototype object, Node gives it none.
|
|
62
|
+
const factory = typeof module === 'object' && module !== null && 'default' in module ? module.default : undefined;
|
|
63
|
+
if (!isAdapterFactory(factory))
|
|
64
|
+
throw new Error(`${PACKAGE_NAME}: @astrojs/cloudflare has no default export`);
|
|
65
|
+
let configPath;
|
|
66
|
+
if (mode === 'd3') {
|
|
67
|
+
const path = wranglerConfigPath(root);
|
|
68
|
+
const child = { ...(path === undefined ? {} : (parseWranglerConfig(readFileSync(path, 'utf8')) ?? {})) };
|
|
69
|
+
delete child.main;
|
|
70
|
+
delete child.assets;
|
|
71
|
+
if (isRecord(child.env)) {
|
|
72
|
+
const blocks = { ...child.env };
|
|
73
|
+
for (const [name, block] of Object.entries(blocks)) {
|
|
74
|
+
if (!isRecord(block))
|
|
75
|
+
continue;
|
|
76
|
+
const kept = { ...block };
|
|
77
|
+
delete kept.main;
|
|
78
|
+
delete kept.assets;
|
|
79
|
+
blocks[name] = kept;
|
|
80
|
+
}
|
|
81
|
+
child.env = blocks;
|
|
82
|
+
}
|
|
83
|
+
configPath = join(root, '.conloca', 'wrangler.child.json');
|
|
84
|
+
mkdirSync(join(root, '.conloca'), { recursive: true });
|
|
85
|
+
writeFileSync(configPath, `${JSON.stringify(child, null, 2)}\n`);
|
|
86
|
+
}
|
|
87
|
+
const adapter = factory(childAdapterOptions(mode, configPath));
|
|
88
|
+
if (!isIntegration(adapter))
|
|
89
|
+
throw new Error(`${PACKAGE_NAME}: @astrojs/cloudflare did not return an Astro integration`);
|
|
90
|
+
return adapter;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=cloudflare-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloudflare-adapter.js","sourceRoot":"","sources":["../../../src/preview/cloudflare-adapter.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* The parser intentionally rejects malformed segments silently rather than
|
|
7
|
-
* throwing — a bad cookie header from a misbehaving client must not crash
|
|
8
|
-
* the SSR handler. Callers always check whether the requested cookie was
|
|
9
|
-
* actually present.
|
|
2
|
+
* Every value a cookie header carries under `name`, in header order. A browser sends one cookie per name, domain and
|
|
3
|
+
* path it holds, so a sibling subdomain can plant a second cookie with the preview's name: callers try each value,
|
|
4
|
+
* never just the first. Malformed segments are skipped, not thrown on: a bad header from a misbehaving client must
|
|
5
|
+
* not crash the handler.
|
|
10
6
|
*/
|
|
11
|
-
export declare function
|
|
7
|
+
export declare function parseCookieValues(cookieHeader: string | null | undefined, name: string): string[];
|
|
12
8
|
/**
|
|
13
9
|
* Serializes a `Set-Cookie` value with the security defaults the preview
|
|
14
10
|
* middleware requires: HttpOnly + Secure + SameSite=Lax + Path=/.
|
|
15
11
|
*
|
|
16
12
|
* Splitting this out from the middleware keeps the request handler
|
|
17
13
|
* declarative and lets the cookie defaults be reviewed in one place: the
|
|
18
|
-
*
|
|
14
|
+
* pass must never leak into a script-readable cookie.
|
|
15
|
+
*
|
|
16
|
+
* These defaults meet the `__Host-` prefix rules the cookie name relies on:
|
|
17
|
+
* Secure, `Path=/` and no `Domain`, so no other host can set or read it.
|
|
19
18
|
*
|
|
20
19
|
* `SameSite=Lax`, not `Strict`: the share link always arrives from the editor
|
|
21
20
|
* origin, and Chrome withholds a `Strict` cookie on the request that FOLLOWS a
|
|
@@ -24,13 +23,13 @@ export declare function parseCookies(cookieHeader: string | null | undefined): M
|
|
|
24
23
|
* cross-site POSTs and subresource loads, which is the threat that matters for
|
|
25
24
|
* a read-only preview credential.
|
|
26
25
|
*
|
|
27
|
-
* `Path=/` is load-bearing: the browser sends the cookie on every same-origin
|
|
28
|
-
*
|
|
29
|
-
*
|
|
26
|
+
* `Path=/` is load-bearing for D2 and D4: the browser sends the cookie on every same-origin request, which is what
|
|
27
|
+
* lets the preview follow internal links instead of surviving exactly one page view. D3 passes its route instead:
|
|
28
|
+
* there every page link is rewritten under it, and two branches keep two cookies.
|
|
30
29
|
*
|
|
31
30
|
* `maxAgeSeconds` bounds the cookie to the credential it carries — pass the
|
|
32
31
|
* JWT's remaining lifetime so a stale cookie cannot outlive the token and
|
|
33
32
|
* strand the visitor on the landing page.
|
|
34
33
|
*/
|
|
35
|
-
export declare function serializePreviewCookie(name: string, value: string, maxAgeSeconds?: number): string;
|
|
34
|
+
export declare function serializePreviewCookie(name: string, value: string, maxAgeSeconds?: number, path?: string): string;
|
|
36
35
|
//# sourceMappingURL=cookie.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/preview/cookie.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/preview/cookie.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CASjG;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,IAAI,SAAM,GAAG,MAAM,CAI9G"}
|
|
@@ -1,31 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* The parser intentionally rejects malformed segments silently rather than
|
|
7
|
-
* throwing — a bad cookie header from a misbehaving client must not crash
|
|
8
|
-
* the SSR handler. Callers always check whether the requested cookie was
|
|
9
|
-
* actually present.
|
|
2
|
+
* Every value a cookie header carries under `name`, in header order. A browser sends one cookie per name, domain and
|
|
3
|
+
* path it holds, so a sibling subdomain can plant a second cookie with the preview's name: callers try each value,
|
|
4
|
+
* never just the first. Malformed segments are skipped, not thrown on: a bad header from a misbehaving client must
|
|
5
|
+
* not crash the handler.
|
|
10
6
|
*/
|
|
11
|
-
export function
|
|
12
|
-
const out = new Map();
|
|
7
|
+
export function parseCookieValues(cookieHeader, name) {
|
|
13
8
|
if (!cookieHeader)
|
|
14
|
-
return
|
|
9
|
+
return [];
|
|
10
|
+
const values = [];
|
|
15
11
|
for (const segment of cookieHeader.split(';')) {
|
|
16
12
|
const eq = segment.indexOf('=');
|
|
17
13
|
if (eq <= 0)
|
|
18
14
|
continue;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (!name)
|
|
22
|
-
continue;
|
|
23
|
-
// First occurrence wins; matches the spec-compliant behaviour of the
|
|
24
|
-
// browsers' document.cookie reader for duplicate names.
|
|
25
|
-
if (!out.has(name))
|
|
26
|
-
out.set(name, value);
|
|
15
|
+
if (segment.slice(0, eq).trim() === name)
|
|
16
|
+
values.push(segment.slice(eq + 1).trim());
|
|
27
17
|
}
|
|
28
|
-
return
|
|
18
|
+
return values;
|
|
29
19
|
}
|
|
30
20
|
/**
|
|
31
21
|
* Serializes a `Set-Cookie` value with the security defaults the preview
|
|
@@ -33,7 +23,10 @@ export function parseCookies(cookieHeader) {
|
|
|
33
23
|
*
|
|
34
24
|
* Splitting this out from the middleware keeps the request handler
|
|
35
25
|
* declarative and lets the cookie defaults be reviewed in one place: the
|
|
36
|
-
*
|
|
26
|
+
* pass must never leak into a script-readable cookie.
|
|
27
|
+
*
|
|
28
|
+
* These defaults meet the `__Host-` prefix rules the cookie name relies on:
|
|
29
|
+
* Secure, `Path=/` and no `Domain`, so no other host can set or read it.
|
|
37
30
|
*
|
|
38
31
|
* `SameSite=Lax`, not `Strict`: the share link always arrives from the editor
|
|
39
32
|
* origin, and Chrome withholds a `Strict` cookie on the request that FOLLOWS a
|
|
@@ -42,16 +35,16 @@ export function parseCookies(cookieHeader) {
|
|
|
42
35
|
* cross-site POSTs and subresource loads, which is the threat that matters for
|
|
43
36
|
* a read-only preview credential.
|
|
44
37
|
*
|
|
45
|
-
* `Path=/` is load-bearing: the browser sends the cookie on every same-origin
|
|
46
|
-
*
|
|
47
|
-
*
|
|
38
|
+
* `Path=/` is load-bearing for D2 and D4: the browser sends the cookie on every same-origin request, which is what
|
|
39
|
+
* lets the preview follow internal links instead of surviving exactly one page view. D3 passes its route instead:
|
|
40
|
+
* there every page link is rewritten under it, and two branches keep two cookies.
|
|
48
41
|
*
|
|
49
42
|
* `maxAgeSeconds` bounds the cookie to the credential it carries — pass the
|
|
50
43
|
* JWT's remaining lifetime so a stale cookie cannot outlive the token and
|
|
51
44
|
* strand the visitor on the landing page.
|
|
52
45
|
*/
|
|
53
|
-
export function serializePreviewCookie(name, value, maxAgeSeconds) {
|
|
54
|
-
const base = `${name}=${value}; HttpOnly; Secure; SameSite=Lax; Path
|
|
46
|
+
export function serializePreviewCookie(name, value, maxAgeSeconds, path = '/') {
|
|
47
|
+
const base = `${name}=${value}; HttpOnly; Secure; SameSite=Lax; Path=${path}`;
|
|
55
48
|
if (maxAgeSeconds === undefined)
|
|
56
49
|
return base;
|
|
57
50
|
return `${base}; Max-Age=${Math.max(0, Math.floor(maxAgeSeconds))}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/preview/cookie.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"cookie.js","sourceRoot":"","sources":["../../../src/preview/cookie.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,YAAuC,EAAE,IAAY;IACrF,IAAI,CAAC,YAAY;QAAE,OAAO,EAAE,CAAC;IAC7B,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,MAAM,OAAO,IAAI,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QAC9C,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,EAAE,IAAI,CAAC;YAAE,SAAS;QACtB,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,IAAI;YAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;IACtF,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAY,EAAE,KAAa,EAAE,aAAsB,EAAE,IAAI,GAAG,GAAG;IACpG,MAAM,IAAI,GAAG,GAAG,IAAI,IAAI,KAAK,0CAA0C,IAAI,EAAE,CAAC;IAC9E,IAAI,aAAa,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IAC7C,OAAO,GAAG,IAAI,aAAa,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC;AACtE,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dispatcher a static site runs on Cloudflare Workers when draft previews are served next to it (D3). Cloudflare
|
|
3
|
+
* runs it only on the paths `wrangler.jsonc` lists in `run_worker_first`, the preview route and the site's on-demand
|
|
4
|
+
* routes, so a visitor to any other page never starts it. Under the route it answers the exit itself, counts every
|
|
5
|
+
* other request against a per-IP rate limit, refuses a request with no code and no pass before the preview server
|
|
6
|
+
* is loaded, serves published bytes to scripts and, once their pass verifies, to pages the preview build prerendered,
|
|
7
|
+
* and hands draft pages to
|
|
8
|
+
* the preview server at their own path with the branch slug in a request header only it sets.
|
|
9
|
+
*/
|
|
10
|
+
/** What the exit route needs to answer without the preview server: the build's own repository, site and branch. */
|
|
11
|
+
export interface DispatcherExit {
|
|
12
|
+
repo: string;
|
|
13
|
+
siteId: string;
|
|
14
|
+
branch: string;
|
|
15
|
+
audience: string;
|
|
16
|
+
/** The issuer every pass must name, when the integration options name one. */
|
|
17
|
+
issuer?: string;
|
|
18
|
+
}
|
|
19
|
+
/** What the build writes next to the dispatcher (`.conloca/dispatcher/dispatcher.json`). */
|
|
20
|
+
export interface DispatcherConfig {
|
|
21
|
+
/** Astro's `base`. */
|
|
22
|
+
base: string;
|
|
23
|
+
/** The editor endpoint the landing page sends a visitor to. */
|
|
24
|
+
endpoint: string;
|
|
25
|
+
/** Pages the preview build prerendered, as request paths without a trailing slash: their published bytes are the preview. */
|
|
26
|
+
prerendered: string[];
|
|
27
|
+
/** Hosts sites.json declares: links to them are links to this site. */
|
|
28
|
+
domains: string[];
|
|
29
|
+
/** The exit route's values; null when the preview build failed, so the probe reads "no preview server". */
|
|
30
|
+
exit: DispatcherExit | null;
|
|
31
|
+
}
|
|
32
|
+
/** A Cloudflare Workers rate-limit binding, as the dispatcher calls it. */
|
|
33
|
+
export interface PreviewLimiter {
|
|
34
|
+
limit(input: {
|
|
35
|
+
key: string;
|
|
36
|
+
}): Promise<{
|
|
37
|
+
success: boolean;
|
|
38
|
+
}>;
|
|
39
|
+
}
|
|
40
|
+
export interface DispatcherEnv {
|
|
41
|
+
ASSETS: {
|
|
42
|
+
fetch(request: Request): Promise<Response>;
|
|
43
|
+
};
|
|
44
|
+
/** wrangler.jsonc `ratelimits`, written by `conloca connect` on the Paid plan: 60 requests per 10 s per key. Absent on Free. */
|
|
45
|
+
conlocaPreviewLimit?: PreviewLimiter;
|
|
46
|
+
/** The same overrides the preview server reads (`resolveFromBaked`), so both check a pass alike. */
|
|
47
|
+
CONLOCA_SAAS_ENDPOINT?: string;
|
|
48
|
+
CONLOCA_SAAS_ISSUER?: string;
|
|
49
|
+
CONLOCA_SAAS_AUDIENCE?: string;
|
|
50
|
+
/** A binding that reaches the endpoint when a plain fetch by hostname can't. */
|
|
51
|
+
CONLOCA_SAAS?: {
|
|
52
|
+
fetch(input: Request | string | URL, init?: RequestInit): Promise<Response>;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** The answer over the preview route's rate limit: the limit's window is 10 s. */
|
|
56
|
+
export declare function renderTooManyPreviewRequests(): Response;
|
|
57
|
+
/** The limit's key for a client address: IPv4 whole, IPv6 by its /64, since one host holds a whole /64. */
|
|
58
|
+
export declare function limitKey(ip: string | null): string;
|
|
59
|
+
export interface PreviewServerModule {
|
|
60
|
+
default: {
|
|
61
|
+
fetch(request: Request, env: unknown, ctx: unknown): Promise<Response>;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* `value` under the preview route when it names a page of this site, else null (left as it is). It is resolved
|
|
66
|
+
* against `page`, the unprefixed URL the page was rendered for, so `../x` and `?q` keep their meaning. Files, built
|
|
67
|
+
* assets, the image endpoint, `/_conloca/` and other sites stay where they are.
|
|
68
|
+
*/
|
|
69
|
+
export declare function previewHref(value: string, page: URL, route: string, root: string, hosts: ReadonlySet<string>): string | null;
|
|
70
|
+
export declare function createDispatcher(config: DispatcherConfig, loadServer: (() => Promise<PreviewServerModule>) | null): {
|
|
71
|
+
fetch(request: Request, env: DispatcherEnv, ctx: unknown): Promise<Response>;
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=dispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../../../src/preview/dispatcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAgBH,mHAAmH;AACnH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,8EAA8E;IAC9E,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,4FAA4F;AAC5F,MAAM,WAAW,gBAAgB;IAC/B,sBAAsB;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,QAAQ,EAAE,MAAM,CAAC;IACjB,6HAA6H;IAC7H,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,uEAAuE;IACvE,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,2GAA2G;IAC3G,IAAI,EAAE,cAAc,GAAG,IAAI,CAAC;CAC7B;AAED,2EAA2E;AAC3E,MAAM,WAAW,cAAc;IAC7B,KAAK,CAAC,KAAK,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CAC9D;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE;QAAE,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;KAAE,CAAC;IACvD,gIAAgI;IAChI,mBAAmB,CAAC,EAAE,cAAc,CAAC;IACrC,oGAAoG;IACpG,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,gFAAgF;IAChF,YAAY,CAAC,EAAE;QAAE,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;KAAE,CAAC;CAChG;AAMD,kFAAkF;AAClF,wBAAgB,4BAA4B,IAAI,QAAQ,CAOvD;AAED,2GAA2G;AAC3G,wBAAgB,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAYlD;AAiBD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE;QAAE,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;KAAE,CAAC;CACrF;AAQD;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,GAAG,EACT,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,GACzB,MAAM,GAAG,IAAI,CAmBf;AAsGD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,EAAE,UAAU,EAAE,CAAC,MAAM,OAAO,CAAC,mBAAmB,CAAC,CAAC,GAAG,IAAI;IA+DxG,KAAK,UAAU,OAAO,OAAO,aAAa,OAAO,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC;EA4FrF"}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The dispatcher a static site runs on Cloudflare Workers when draft previews are served next to it (D3). Cloudflare
|
|
3
|
+
* runs it only on the paths `wrangler.jsonc` lists in `run_worker_first`, the preview route and the site's on-demand
|
|
4
|
+
* routes, so a visitor to any other page never starts it. Under the route it answers the exit itself, counts every
|
|
5
|
+
* other request against a per-IP rate limit, refuses a request with no code and no pass before the preview server
|
|
6
|
+
* is loaded, serves published bytes to scripts and, once their pass verifies, to pages the preview build prerendered,
|
|
7
|
+
* and hands draft pages to
|
|
8
|
+
* the preview server at their own path with the branch slug in a request header only it sets.
|
|
9
|
+
*/
|
|
10
|
+
import { branchOfPreviewSlug } from '@conloca/content-api/editor-host';
|
|
11
|
+
import { injectPreviewBanner } from './banner.js';
|
|
12
|
+
import { parseCookieValues } from './cookie.js';
|
|
13
|
+
import { applyPreviewHeaders, PREVIEW_CACHE_HEADERS } from './headers.js';
|
|
14
|
+
import { renderNoPreviewLanding, renderPreviewUnavailable } from './landing.js';
|
|
15
|
+
import { handlePreviewRequest } from './middleware.js';
|
|
16
|
+
import { normalizeTrailingSlash, PREVIEW_CODE_PARAM, PREVIEW_SLUG_HEADER, previewRoutePrefix, previewScope, } from './preview-path.js';
|
|
17
|
+
const TOO_MANY = '<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Too many preview requests</title></head>' +
|
|
18
|
+
'<body><p>Too many preview requests — try again in a few seconds.</p></body></html>';
|
|
19
|
+
/** The answer over the preview route's rate limit: the limit's window is 10 s. */
|
|
20
|
+
export function renderTooManyPreviewRequests() {
|
|
21
|
+
return applyPreviewHeaders(new Response(TOO_MANY, {
|
|
22
|
+
status: 429,
|
|
23
|
+
headers: { 'content-type': 'text/html; charset=utf-8', 'retry-after': '10' },
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
/** The limit's key for a client address: IPv4 whole, IPv6 by its /64, since one host holds a whole /64. */
|
|
27
|
+
export function limitKey(ip) {
|
|
28
|
+
if (ip === null || ip === '')
|
|
29
|
+
return 'unknown';
|
|
30
|
+
if (!ip.includes(':'))
|
|
31
|
+
return ip;
|
|
32
|
+
const [head = '', tail] = ip.split('::');
|
|
33
|
+
const left = head === '' ? [] : head.split(':');
|
|
34
|
+
const right = tail === undefined || tail === '' ? [] : tail.split(':');
|
|
35
|
+
const zeros = Array.from({ length: Math.max(0, 8 - left.length - right.length) }, () => '0');
|
|
36
|
+
const groups = tail === undefined ? left : [...left, ...zeros, ...right];
|
|
37
|
+
return `${groups
|
|
38
|
+
.slice(0, 4)
|
|
39
|
+
.map((group) => group.toLowerCase().replace(/^0+(?=.)/u, ''))
|
|
40
|
+
.join(':')}::/64`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Whether this request is within the preview route's limit. A D3 site on the Free plan has no binding (on Free a
|
|
44
|
+
* blocked request still counts against the daily quota, so a limiter adds nothing), and a limiter that
|
|
45
|
+
* fails at run time must not take every preview down with it, so both are served.
|
|
46
|
+
*/
|
|
47
|
+
async function withinLimit(request, env) {
|
|
48
|
+
const limiter = env.conlocaPreviewLimit;
|
|
49
|
+
if (limiter === undefined)
|
|
50
|
+
return true;
|
|
51
|
+
try {
|
|
52
|
+
return (await limiter.limit({ key: limitKey(request.headers.get('cf-connecting-ip')) })).success;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* `value` under the preview route when it names a page of this site, else null (left as it is). It is resolved
|
|
60
|
+
* against `page`, the unprefixed URL the page was rendered for, so `../x` and `?q` keep their meaning. Files, built
|
|
61
|
+
* assets, the image endpoint, `/_conloca/` and other sites stay where they are.
|
|
62
|
+
*/
|
|
63
|
+
export function previewHref(value, page, route, root, hosts) {
|
|
64
|
+
if (value === '' || value.startsWith('#'))
|
|
65
|
+
return null;
|
|
66
|
+
let target;
|
|
67
|
+
try {
|
|
68
|
+
target = new URL(value, page);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
if (target.protocol !== 'http:' && target.protocol !== 'https:')
|
|
74
|
+
return null;
|
|
75
|
+
const own = target.host === page.host;
|
|
76
|
+
if (!own && !hosts.has(target.hostname))
|
|
77
|
+
return null;
|
|
78
|
+
const { pathname } = target;
|
|
79
|
+
if (root !== '' && pathname !== root && !pathname.startsWith(`${root}/`))
|
|
80
|
+
return null;
|
|
81
|
+
const rest = pathname.slice(root.length) || '/';
|
|
82
|
+
if (rest.startsWith('/_conloca/') || rest.startsWith('/_astro/') || rest === '/_image')
|
|
83
|
+
return null;
|
|
84
|
+
const last = rest.slice(rest.lastIndexOf('/') + 1);
|
|
85
|
+
if (last.includes('.') && !last.endsWith('.html'))
|
|
86
|
+
return null;
|
|
87
|
+
const moved = `${route}${rest}${target.search}${target.hash}`;
|
|
88
|
+
return own ? moved : `${target.origin}${moved}`;
|
|
89
|
+
}
|
|
90
|
+
/** A navigation, the only request that may receive draft HTML; without Sec-Fetch-*, one that asks for HTML. */
|
|
91
|
+
function isNavigation(request) {
|
|
92
|
+
const mode = request.headers.get('sec-fetch-mode');
|
|
93
|
+
if (mode !== null)
|
|
94
|
+
return mode === 'navigate';
|
|
95
|
+
return (request.headers.get('accept') ?? '').includes('text/html');
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* `srcset` with each candidate URL that `move` answers for replaced, everything else kept byte for byte; null when none
|
|
99
|
+
* moved. A URL runs to the next whitespace, as the HTML parser reads it, so a `data:` URL's commas stay inside it.
|
|
100
|
+
*/
|
|
101
|
+
function movedSrcset(value, move) {
|
|
102
|
+
let out = '';
|
|
103
|
+
let rest = value;
|
|
104
|
+
let moved = false;
|
|
105
|
+
while (rest !== '') {
|
|
106
|
+
const gap = /^[\s,]*/u.exec(rest)?.[0] ?? '';
|
|
107
|
+
const token = /^\S*/u.exec(rest.slice(gap.length))?.[0] ?? '';
|
|
108
|
+
const commas = /,*$/u.exec(token)?.[0] ?? '';
|
|
109
|
+
const href = token.slice(0, token.length - commas.length);
|
|
110
|
+
rest = rest.slice(gap.length + token.length);
|
|
111
|
+
// Descriptors (`1x`, `400w`) run to the next comma, unless the URL already ended at one.
|
|
112
|
+
const descriptor = commas === '' ? (/^[^,]*/u.exec(rest)?.[0] ?? '') : '';
|
|
113
|
+
rest = rest.slice(descriptor.length);
|
|
114
|
+
const target = href === '' ? null : move(href);
|
|
115
|
+
if (target !== null)
|
|
116
|
+
moved = true;
|
|
117
|
+
out += `${gap}${target ?? href}${commas}${descriptor}`;
|
|
118
|
+
}
|
|
119
|
+
return moved ? out : null;
|
|
120
|
+
}
|
|
121
|
+
function linkRewriter(page, route, root, hosts) {
|
|
122
|
+
// A `<base href>` changes what relative links resolve against for every link after it.
|
|
123
|
+
let resolveAgainst = page;
|
|
124
|
+
const move = (element, name) => {
|
|
125
|
+
const value = element.getAttribute(name);
|
|
126
|
+
const moved = value === null ? null : previewHref(value, resolveAgainst, route, root, hosts);
|
|
127
|
+
if (moved !== null)
|
|
128
|
+
element.setAttribute(name, moved);
|
|
129
|
+
return moved !== null;
|
|
130
|
+
};
|
|
131
|
+
return new HTMLRewriter()
|
|
132
|
+
.on('base[href]', {
|
|
133
|
+
element(element) {
|
|
134
|
+
const value = element.getAttribute('href');
|
|
135
|
+
if (value !== null && URL.canParse(value, page.href))
|
|
136
|
+
resolveAgainst = new URL(value, page);
|
|
137
|
+
move(element, 'href');
|
|
138
|
+
},
|
|
139
|
+
})
|
|
140
|
+
.on('a[href], area[href]', {
|
|
141
|
+
element(element) {
|
|
142
|
+
// Astro's prefetch would spend the visitor's 60 requests per 10 s on links they never open.
|
|
143
|
+
if (move(element, 'href'))
|
|
144
|
+
element.setAttribute('data-astro-prefetch', 'false');
|
|
145
|
+
},
|
|
146
|
+
})
|
|
147
|
+
.on('form[action]', {
|
|
148
|
+
element(element) {
|
|
149
|
+
move(element, 'action');
|
|
150
|
+
},
|
|
151
|
+
})
|
|
152
|
+
.on('[srcset]', {
|
|
153
|
+
element(element) {
|
|
154
|
+
const value = element.getAttribute('srcset');
|
|
155
|
+
const moved = value === null ? null : movedSrcset(value, (href) => previewHref(href, resolveAgainst, route, root, hosts));
|
|
156
|
+
if (moved !== null)
|
|
157
|
+
element.setAttribute('srcset', moved);
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/** How the dispatcher checks a pass: with the values the preview build baked, and the overrides its server reads. */
|
|
162
|
+
function passOptions(config, exit, env, slug) {
|
|
163
|
+
const binding = env.CONLOCA_SAAS;
|
|
164
|
+
const issuer = env.CONLOCA_SAAS_ISSUER || exit.issuer;
|
|
165
|
+
return {
|
|
166
|
+
saasEndpoint: env.CONLOCA_SAAS_ENDPOINT || config.endpoint,
|
|
167
|
+
saasAudience: env.CONLOCA_SAAS_AUDIENCE || exit.audience,
|
|
168
|
+
...(issuer ? { saasIssuer: issuer } : {}),
|
|
169
|
+
repo: exit.repo,
|
|
170
|
+
siteId: exit.siteId,
|
|
171
|
+
branch: exit.branch,
|
|
172
|
+
base: config.base,
|
|
173
|
+
mode: 'd3',
|
|
174
|
+
slug,
|
|
175
|
+
onMiss: 'landing',
|
|
176
|
+
// `preconnect` only satisfies the type: jose calls the function alone.
|
|
177
|
+
...(binding === undefined
|
|
178
|
+
? {}
|
|
179
|
+
: {
|
|
180
|
+
fetchFn: Object.assign((input, init) => binding.fetch(input, init), {
|
|
181
|
+
preconnect: fetch.preconnect,
|
|
182
|
+
}),
|
|
183
|
+
}),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
export function createDispatcher(config, loadServer) {
|
|
187
|
+
const prefix = previewRoutePrefix(config.base);
|
|
188
|
+
const root = config.base.replace(/\/+$/u, '');
|
|
189
|
+
const prerendered = new Set(config.prerendered);
|
|
190
|
+
const hosts = new Set(config.domains);
|
|
191
|
+
/** The exit route, from the build's own values: the probe never waits for the preview server. */
|
|
192
|
+
async function exit(url, slug) {
|
|
193
|
+
if (config.exit === null) {
|
|
194
|
+
// No label: Conloca reads "no preview server" and says so, instead of minting a link to a 503 page.
|
|
195
|
+
return new Response(null, { status: 302, headers: { location: config.base, ...PREVIEW_CACHE_HEADERS } });
|
|
196
|
+
}
|
|
197
|
+
const verdict = await handlePreviewRequest(new Request(`${url.origin}${prefix}/__exit${url.search}`), {
|
|
198
|
+
saasEndpoint: config.endpoint,
|
|
199
|
+
saasAudience: config.exit.audience,
|
|
200
|
+
repo: config.exit.repo,
|
|
201
|
+
siteId: config.exit.siteId,
|
|
202
|
+
branch: config.exit.branch,
|
|
203
|
+
base: config.base,
|
|
204
|
+
mode: 'd3',
|
|
205
|
+
...(slug === undefined ? {} : { slug }),
|
|
206
|
+
});
|
|
207
|
+
// The exit branch always answers a response; anything else would be a bug in this call, so fail closed.
|
|
208
|
+
return verdict.kind === 'response'
|
|
209
|
+
? applyPreviewHeaders(verdict.response)
|
|
210
|
+
: applyPreviewHeaders(new Response(null, { status: 302, headers: { location: config.base } }));
|
|
211
|
+
}
|
|
212
|
+
/** An entry answer (a redirect or Conloca's own page): headers and `Location` only, never its HTML. */
|
|
213
|
+
function finishEntry(answer, page, route) {
|
|
214
|
+
const out = applyPreviewHeaders(answer);
|
|
215
|
+
const location = out.headers.get('location');
|
|
216
|
+
const moved = location === null ? null : previewHref(location, page, route, root, hosts);
|
|
217
|
+
if (moved !== null)
|
|
218
|
+
out.headers.set('location', moved);
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
/** The draft answer with the route put back on its location and links. */
|
|
222
|
+
async function finish(answer, page, route, published) {
|
|
223
|
+
const out = finishEntry(answer, page, route);
|
|
224
|
+
if (!(out.headers.get('content-type') ?? '').includes('text/html'))
|
|
225
|
+
return out;
|
|
226
|
+
const rewriter = linkRewriter(page, route, root, hosts);
|
|
227
|
+
if (published === null)
|
|
228
|
+
return rewriter.transform(out);
|
|
229
|
+
// A page the preview build prerendered: the preview server never saw it, so its banner goes in here.
|
|
230
|
+
out.headers.delete('content-length');
|
|
231
|
+
const html = injectPreviewBanner(await out.text(),
|
|
232
|
+
// The pass's own context: a share viewer sees who shared the draft and when the link expires.
|
|
233
|
+
published, undefined, `${route}/__exit`, undefined, page.pathname);
|
|
234
|
+
return rewriter.transform(new Response(html, out));
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
async fetch(request, env, ctx) {
|
|
238
|
+
const url = new URL(request.url);
|
|
239
|
+
const headers = new Headers(request.headers);
|
|
240
|
+
// The preview server trusts this header; only this module may set it.
|
|
241
|
+
headers.delete(PREVIEW_SLUG_HEADER);
|
|
242
|
+
const forward = async (load, target, slug) => {
|
|
243
|
+
if (slug !== undefined)
|
|
244
|
+
headers.set(PREVIEW_SLUG_HEADER, slug);
|
|
245
|
+
const body = request.method === 'GET' || request.method === 'HEAD' ? null : await request.arrayBuffer();
|
|
246
|
+
const forwarded = new Request(target.href, { method: request.method, headers, body, redirect: 'manual' });
|
|
247
|
+
return (await load()).default.fetch(forwarded, env, ctx);
|
|
248
|
+
};
|
|
249
|
+
if (url.pathname !== prefix && !url.pathname.startsWith(`${prefix}/`)) {
|
|
250
|
+
// `<base>_image` is listed for draft pages only (the live site's pages are prerendered), so it is the
|
|
251
|
+
// preview's to pay for: it counts against the preview route's limit.
|
|
252
|
+
if (url.pathname === `${root}/_image` && !(await withinLimit(request, env)))
|
|
253
|
+
return renderTooManyPreviewRequests();
|
|
254
|
+
// The site's own on-demand routes (and, with not_found_handling "none", its misses): unprefixed, no draft
|
|
255
|
+
// context, and no catch: production fails loudly here. A failed preview build ships with a null server only
|
|
256
|
+
// when the site lists nothing beyond the prefix (`finishD3Build`), so this fallback is never a live API route.
|
|
257
|
+
return loadServer === null ? env.ASSETS.fetch(request) : forward(loadServer, url, undefined);
|
|
258
|
+
}
|
|
259
|
+
const [first = '', ...rest] = url.pathname.slice(prefix.length + 1).split('/');
|
|
260
|
+
// The probe, and a page's own exit: answered here, never counted, never loading the server.
|
|
261
|
+
if (first === '__exit' && rest.length === 0)
|
|
262
|
+
return exit(url, undefined);
|
|
263
|
+
const branch = branchOfPreviewSlug(first);
|
|
264
|
+
if (branch !== null && rest.length === 1 && rest[0] === '__exit')
|
|
265
|
+
return exit(url, first);
|
|
266
|
+
// Every other request under the route counts, `__status` included, before any other work.
|
|
267
|
+
if (!(await withinLimit(request, env)))
|
|
268
|
+
return renderTooManyPreviewRequests();
|
|
269
|
+
const entering = url.searchParams.has(PREVIEW_CODE_PARAM);
|
|
270
|
+
const inner = branch === null ? `${root}/${[first, ...rest].join('/')}` : `${root}/${rest.join('/')}`;
|
|
271
|
+
const route = branch === null ? prefix : previewScope('d3', config.base, first).route;
|
|
272
|
+
const { exit: baked } = config;
|
|
273
|
+
if (loadServer === null || baked === null) {
|
|
274
|
+
const page = renderPreviewUnavailable({ retryHref: `${url.pathname}${url.search}`, exitHref: config.base });
|
|
275
|
+
return applyPreviewHeaders(new Response(page.body, { status: 503, headers: page.headers }));
|
|
276
|
+
}
|
|
277
|
+
// Under the route a preview server that throws (a Node-only import, a render error) answers
|
|
278
|
+
// "Preview couldn't load the draft", never Cloudflare's own error page.
|
|
279
|
+
const guarded = async (target, slug) => {
|
|
280
|
+
try {
|
|
281
|
+
return await forward(loadServer, target, slug);
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
console.error('@conloca/cloud-astro: the preview server failed', error);
|
|
285
|
+
const page = renderPreviewUnavailable({
|
|
286
|
+
retryHref: `${url.pathname}${url.search}`,
|
|
287
|
+
exitHref: `${route}/__exit?to=${encodeURIComponent(inner)}`,
|
|
288
|
+
});
|
|
289
|
+
return applyPreviewHeaders(new Response(page.body, { status: 503, headers: page.headers }));
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
const landing = () => applyPreviewHeaders(renderNoPreviewLanding({ pathname: inner, saasEndpoint: config.endpoint }));
|
|
293
|
+
if (branch === null) {
|
|
294
|
+
// A slugless entry link: the preview server exchanges the code and takes the slug from the pass.
|
|
295
|
+
if (!entering)
|
|
296
|
+
return landing();
|
|
297
|
+
return finishEntry(await guarded(url, undefined), url, prefix);
|
|
298
|
+
}
|
|
299
|
+
const scope = previewScope('d3', config.base, first);
|
|
300
|
+
const cookie = request.headers.get('cookie');
|
|
301
|
+
const holds = parseCookieValues(cookie, scope.pass).length > 0 ||
|
|
302
|
+
(scope.grant !== null && parseCookieValues(cookie, scope.grant).length > 0);
|
|
303
|
+
// Refused before the preview server loads: a request with no credential never pays for its start-up.
|
|
304
|
+
if (!entering && !holds)
|
|
305
|
+
return landing();
|
|
306
|
+
const page = new URL(`${inner}${url.search}`, url.origin);
|
|
307
|
+
if (entering) {
|
|
308
|
+
// The adapter serves a prerendered page from the static files before any middleware runs, so the code goes
|
|
309
|
+
// to the preview build's entry route (`<prefix>/[...path]`, always on demand), as under D2.
|
|
310
|
+
const entry = new URL(`${prefix}${inner.slice(root.length)}${url.search}`, url.origin);
|
|
311
|
+
return finishEntry(await guarded(entry, first), page, scope.route);
|
|
312
|
+
}
|
|
313
|
+
if (!isNavigation(request)) {
|
|
314
|
+
// Scripts never read drafts: a fetch under the route gets published bytes.
|
|
315
|
+
return applyPreviewHeaders(await env.ASSETS.fetch(new Request(page.href, { headers })));
|
|
316
|
+
}
|
|
317
|
+
if (prerendered.has(normalizeTrailingSlash(inner))) {
|
|
318
|
+
// The preview server never sees a prerendered page, so its pass is checked here, as the server checks every
|
|
319
|
+
// other: one that fails gets the landing page (or the share link's renewal), never the banner.
|
|
320
|
+
const verdict = await handlePreviewRequest(new Request(page.href, { headers }), passOptions(config, baked, env, first));
|
|
321
|
+
if (verdict.kind !== 'preview')
|
|
322
|
+
return finishEntry(verdict.kind === 'response' ? verdict.response : landing(), page, scope.route);
|
|
323
|
+
return finish(await env.ASSETS.fetch(new Request(page.href, { headers })), page, scope.route, verdict.ctx);
|
|
324
|
+
}
|
|
325
|
+
return finish(await guarded(page, first), page, scope.route, null);
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
//# sourceMappingURL=dispatcher.js.map
|