@conloca/cloud-astro 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/README.md +155 -37
  2. package/dist/.build-inputs.json +32 -23
  3. package/dist/src/constants.d.ts +12 -3
  4. package/dist/src/constants.d.ts.map +1 -1
  5. package/dist/src/constants.js +12 -3
  6. package/dist/src/constants.js.map +1 -1
  7. package/dist/src/d3-build.d.ts +70 -0
  8. package/dist/src/d3-build.d.ts.map +1 -0
  9. package/dist/src/d3-build.js +272 -0
  10. package/dist/src/d3-build.js.map +1 -0
  11. package/dist/src/descriptors.d.ts.map +1 -1
  12. package/dist/src/descriptors.js +4 -2
  13. package/dist/src/descriptors.js.map +1 -1
  14. package/dist/src/doctor/checks-live.d.ts +2 -2
  15. package/dist/src/doctor/checks-live.d.ts.map +1 -1
  16. package/dist/src/doctor/checks-live.js +10 -4
  17. package/dist/src/doctor/checks-live.js.map +1 -1
  18. package/dist/src/doctor/checks-offline.d.ts +1 -5
  19. package/dist/src/doctor/checks-offline.d.ts.map +1 -1
  20. package/dist/src/doctor/checks-offline.js +15 -27
  21. package/dist/src/doctor/checks-offline.js.map +1 -1
  22. package/dist/src/doctor/checks-preview-limits.d.ts +11 -0
  23. package/dist/src/doctor/checks-preview-limits.d.ts.map +1 -0
  24. package/dist/src/doctor/checks-preview-limits.js +133 -0
  25. package/dist/src/doctor/checks-preview-limits.js.map +1 -0
  26. package/dist/src/doctor/checks-preview-live.d.ts +14 -0
  27. package/dist/src/doctor/checks-preview-live.d.ts.map +1 -0
  28. package/dist/src/doctor/checks-preview-live.js +72 -0
  29. package/dist/src/doctor/checks-preview-live.js.map +1 -0
  30. package/dist/src/doctor/checks-preview.d.ts +7 -0
  31. package/dist/src/doctor/checks-preview.d.ts.map +1 -0
  32. package/dist/src/doctor/checks-preview.js +287 -0
  33. package/dist/src/doctor/checks-preview.js.map +1 -0
  34. package/dist/src/doctor/config-scan.d.ts +6 -0
  35. package/dist/src/doctor/config-scan.d.ts.map +1 -1
  36. package/dist/src/doctor/config-scan.js +17 -0
  37. package/dist/src/doctor/config-scan.js.map +1 -1
  38. package/dist/src/doctor/run.d.ts.map +1 -1
  39. package/dist/src/doctor/run.js +4 -0
  40. package/dist/src/doctor/run.js.map +1 -1
  41. package/dist/src/doctor/types.d.ts +11 -1
  42. package/dist/src/doctor/types.d.ts.map +1 -1
  43. package/dist/src/doctor/types.js +11 -1
  44. package/dist/src/doctor/types.js.map +1 -1
  45. package/dist/src/editor-page/bundle.d.ts +2 -0
  46. package/dist/src/editor-page/bundle.d.ts.map +1 -1
  47. package/dist/src/editor-page/bundle.js +3 -0
  48. package/dist/src/editor-page/bundle.js.map +1 -1
  49. package/dist/src/editor-page/data-schemas.d.ts +16 -0
  50. package/dist/src/editor-page/data-schemas.d.ts.map +1 -0
  51. package/dist/src/editor-page/data-schemas.js +39 -0
  52. package/dist/src/editor-page/data-schemas.js.map +1 -0
  53. package/dist/src/editor-page/emit.d.ts +37 -1
  54. package/dist/src/editor-page/emit.d.ts.map +1 -1
  55. package/dist/src/editor-page/emit.js +75 -4
  56. package/dist/src/editor-page/emit.js.map +1 -1
  57. package/dist/src/index.d.ts +1 -0
  58. package/dist/src/index.d.ts.map +1 -1
  59. package/dist/src/index.js +2 -0
  60. package/dist/src/index.js.map +1 -1
  61. package/dist/src/integration.d.ts.map +1 -1
  62. package/dist/src/integration.js +165 -41
  63. package/dist/src/integration.js.map +1 -1
  64. package/dist/src/preview/_dispatch.d.ts +12 -0
  65. package/dist/src/preview/_dispatch.d.ts.map +1 -1
  66. package/dist/src/preview/_dispatch.js +31 -0
  67. package/dist/src/preview/_dispatch.js.map +1 -1
  68. package/dist/src/preview/_resolve-opts.d.ts +3 -0
  69. package/dist/src/preview/_resolve-opts.d.ts.map +1 -1
  70. package/dist/src/preview/_resolve-opts.js +3 -1
  71. package/dist/src/preview/_resolve-opts.js.map +1 -1
  72. package/dist/src/preview/banner.d.ts +12 -9
  73. package/dist/src/preview/banner.d.ts.map +1 -1
  74. package/dist/src/preview/banner.js +46 -41
  75. package/dist/src/preview/banner.js.map +1 -1
  76. package/dist/src/preview/cloudflare-adapter.d.ts +35 -0
  77. package/dist/src/preview/cloudflare-adapter.d.ts.map +1 -0
  78. package/dist/src/preview/cloudflare-adapter.js +92 -0
  79. package/dist/src/preview/cloudflare-adapter.js.map +1 -0
  80. package/dist/src/preview/cookie.d.ts +9 -13
  81. package/dist/src/preview/cookie.d.ts.map +1 -1
  82. package/dist/src/preview/cookie.js +15 -25
  83. package/dist/src/preview/cookie.js.map +1 -1
  84. package/dist/src/preview/dispatcher.d.ts +73 -0
  85. package/dist/src/preview/dispatcher.d.ts.map +1 -0
  86. package/dist/src/preview/dispatcher.js +329 -0
  87. package/dist/src/preview/dispatcher.js.map +1 -0
  88. package/dist/src/preview/entry-code.d.ts +33 -0
  89. package/dist/src/preview/entry-code.d.ts.map +1 -0
  90. package/dist/src/preview/entry-code.js +33 -0
  91. package/dist/src/preview/entry-code.js.map +1 -0
  92. package/dist/src/preview/headers.d.ts +24 -4
  93. package/dist/src/preview/headers.d.ts.map +1 -1
  94. package/dist/src/preview/headers.js +35 -7
  95. package/dist/src/preview/headers.js.map +1 -1
  96. package/dist/src/preview/landing.d.ts +21 -0
  97. package/dist/src/preview/landing.d.ts.map +1 -1
  98. package/dist/src/preview/landing.js +103 -35
  99. package/dist/src/preview/landing.js.map +1 -1
  100. package/dist/src/preview/middleware.d.ts +29 -7
  101. package/dist/src/preview/middleware.d.ts.map +1 -1
  102. package/dist/src/preview/middleware.js +238 -84
  103. package/dist/src/preview/middleware.js.map +1 -1
  104. package/dist/src/preview/preview-path.d.ts +29 -0
  105. package/dist/src/preview/preview-path.d.ts.map +1 -1
  106. package/dist/src/preview/preview-path.js +41 -0
  107. package/dist/src/preview/preview-path.js.map +1 -1
  108. package/dist/src/preview/redeem-url.d.ts +7 -0
  109. package/dist/src/preview/redeem-url.d.ts.map +1 -0
  110. package/dist/src/preview/redeem-url.js +14 -0
  111. package/dist/src/preview/redeem-url.js.map +1 -0
  112. package/dist/src/preview/site-middleware.d.ts.map +1 -1
  113. package/dist/src/preview/site-middleware.js +39 -12
  114. package/dist/src/preview/site-middleware.js.map +1 -1
  115. package/dist/src/preview-build.d.ts +19 -8
  116. package/dist/src/preview-build.d.ts.map +1 -1
  117. package/dist/src/preview-build.js +47 -21
  118. package/dist/src/preview-build.js.map +1 -1
  119. package/dist/src/skill.d.ts +11 -0
  120. package/dist/src/skill.d.ts.map +1 -1
  121. package/dist/src/skill.js +32 -9
  122. package/dist/src/skill.js.map +1 -1
  123. package/dist/src/types.d.ts +15 -12
  124. package/dist/src/types.d.ts.map +1 -1
  125. package/package.json +33 -7
  126. package/src/constants.ts +14 -3
  127. package/src/d3-build.ts +315 -0
  128. package/src/descriptors.ts +4 -2
  129. package/src/doctor/checks-live.ts +10 -4
  130. package/src/doctor/checks-offline.ts +16 -25
  131. package/src/doctor/checks-preview-limits.ts +137 -0
  132. package/src/doctor/checks-preview-live.ts +77 -0
  133. package/src/doctor/checks-preview.ts +302 -0
  134. package/src/doctor/config-scan.ts +14 -0
  135. package/src/doctor/run.ts +4 -0
  136. package/src/doctor/types.ts +11 -1
  137. package/src/editor-page/bundle.ts +5 -0
  138. package/src/editor-page/data-schemas.ts +47 -0
  139. package/src/editor-page/emit.ts +124 -8
  140. package/src/index.ts +2 -0
  141. package/src/integration.ts +176 -40
  142. package/src/preview/_dispatch.ts +36 -1
  143. package/src/preview/_resolve-opts.ts +6 -1
  144. package/src/preview/banner.ts +61 -41
  145. package/src/preview/cloudflare-adapter.ts +103 -0
  146. package/src/preview/cookie.ts +14 -23
  147. package/src/preview/dispatcher.ts +407 -0
  148. package/src/preview/entry-code.ts +51 -0
  149. package/src/preview/headers.ts +35 -7
  150. package/src/preview/landing.ts +115 -37
  151. package/src/preview/middleware.ts +264 -88
  152. package/src/preview/preview-path.ts +57 -0
  153. package/src/preview/redeem-url.ts +14 -0
  154. package/src/preview/site-middleware.ts +47 -12
  155. package/src/preview-build.ts +67 -24
  156. package/src/preview-handler.astro +2 -1
  157. package/src/skill/SKILL.md +14 -2
  158. package/src/skill.ts +36 -8
  159. package/src/types.ts +15 -12
@@ -13,22 +13,26 @@
13
13
  * encodes both `"` and `</script>` sequences safely, which is the
14
14
  * canonical way to embed server data in a same-origin inline script.
15
15
  */
16
- export function getBannerScriptText(ctx, endpoint, exitPath) {
16
+ export function getBannerScriptText(ctx, endpoint, exitPath, shareUrl, pagePath) {
17
17
  // Serialize then patch the `<` character that JSON.stringify does NOT
18
18
  // escape. Browsers terminate inline scripts on `</script>` regardless of
19
19
  // string-literal context, so a branch name containing `</script>` would
20
20
  // break out of the script tag. Replacing `</` with `<\/` is the standard
21
21
  // mitigation (also done by ASP.NET's Json.Encode, Rails' j helper, etc.).
22
22
  //
23
- // The endpoint rides the envelope alongside the preview ctx so the
24
- // "Share Preview" button can call `GET {endpoint}/preview/share` from the
25
- // browser with the preview JWT (the same credential the cookie carries; the
26
- // cookie is HttpOnly, so the button reads the JWT from here). Absent an
27
- // endpoint the button is inert. `endpoint: undefined` drops out of the JSON.
28
- //
29
- // `exitPath` is the server-side exit route, the fixed public
30
- // `<base>/_conloca/preview/__exit`; it reveals nothing.
31
- const ctxJson = JSON.stringify({ ...ctx, endpoint, exitPath }).replace(/</g, '\\u003c');
23
+ // Only the branch name and public addresses reach the page, never the pass:
24
+ // every script on the page, third-party tags included, could read it. A
25
+ // share viewer gets who shared the draft and when the link expires instead
26
+ // of the editor links. `undefined` values drop out of the JSON.
27
+ const embedded = ctx.grant === undefined
28
+ ? { branch: ctx.branch, endpoint, exitPath, shareUrl, path: pagePath }
29
+ : {
30
+ branch: ctx.branch,
31
+ exitPath,
32
+ path: pagePath,
33
+ viewer: { sharedBy: ctx.grant.sharedBy, expiresAt: ctx.grant.expiresAt },
34
+ };
35
+ const ctxJson = JSON.stringify(embedded).replace(/</g, '\\u003c');
32
36
  return [
33
37
  `var __conlocaPreview = ${ctxJson};`,
34
38
  "var __conlocaCollapseKey = 'conloca.preview.collapsed';",
@@ -38,34 +42,24 @@ export function getBannerScriptText(ctx, endpoint, exitPath) {
38
42
  " var host = document.createElement('div');",
39
43
  " host.id = 'conloca-preview-banner';",
40
44
  " host.style.cssText = 'position:fixed;bottom:16px;right:16px;z-index:2147483647;font-family:system-ui,sans-serif;background:#111;color:#fff;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,0.35);padding:8px 12px;font-size:13px;display:flex;gap:12px;align-items:center;';",
41
- " var label = document.createElement('span'); label.textContent = 'Preview: ' + __conlocaPreview.branch; host.appendChild(label);",
45
+ " var label = document.createElement('span');",
46
+ ' if (__conlocaPreview.viewer) {',
47
+ ' var days = Math.max(1, Math.ceil((__conlocaPreview.viewer.expiresAt * 1000 - Date.now()) / 86400000));',
48
+ " label.textContent = 'Draft: ' + __conlocaPreview.branch + (__conlocaPreview.viewer.sharedBy ? ' · Shared by ' + __conlocaPreview.viewer.sharedBy : '') + ' · Expires in ' + days + (days === 1 ? ' day' : ' days');",
49
+ " } else { label.textContent = 'Preview: ' + __conlocaPreview.branch; }",
50
+ ' host.appendChild(label);',
42
51
  // "Edit" reuses the landing page's `/login?return=` contract:
43
52
  // the endpoint's login route already knows how to send an authenticated
44
53
  // session into the editor for the requested page.
45
54
  " var edit = document.createElement('a'); edit.textContent = 'Edit'; edit.style.color = '#9cf';",
46
- " if (__conlocaPreview.endpoint) { edit.href = __conlocaPreview.endpoint + '/login?return=' + encodeURIComponent(location.pathname); host.appendChild(edit); }",
55
+ " if (__conlocaPreview.endpoint) { edit.href = __conlocaPreview.endpoint + '/login?return=' + encodeURIComponent(__conlocaPreview.path || location.pathname); host.appendChild(edit); }",
56
+ // Share opens the editor's share dialog for this page in a new tab.
57
+ " if (__conlocaPreview.shareUrl) { var share = document.createElement('a'); share.textContent = 'Share'; share.style.color = '#9cf'; share.href = __conlocaPreview.shareUrl; share.target = '_blank'; share.rel = 'noopener'; host.appendChild(share); }",
47
58
  // "Exit Preview" must round-trip the server: the cookie is HttpOnly, so
48
59
  // only a Set-Cookie response can expire it. `to` returns the visitor to
49
60
  // this page, now published.
50
61
  " var exitLink = document.createElement('a'); exitLink.textContent = 'Exit Preview'; exitLink.style.color = '#9cf';",
51
- " if (__conlocaPreview.exitPath) { exitLink.href = __conlocaPreview.exitPath + '?to=' + encodeURIComponent(location.pathname); host.appendChild(exitLink); }",
52
- " var share = document.createElement('button'); share.type = 'button'; share.textContent = 'Share Preview'; share.style.cssText = 'background:#333;color:#fff;border:1px solid #555;border-radius:4px;padding:2px 8px;cursor:pointer;'; host.appendChild(share);",
53
- // "Share Preview": mint a fresh shareable link via GET {endpoint}/preview/share
54
- // and copy it to the clipboard. Bearer-authenticated with the preview JWT
55
- // (the route reads Authorization/?token=, never the HttpOnly cookie). The
56
- // button briefly reflects the outcome, then restores.
57
- " share.addEventListener('click', function () {",
58
- ' if (!__conlocaPreview.endpoint) return;',
59
- " var shareUrl = __conlocaPreview.endpoint + '/preview/share?branch=' + encodeURIComponent(__conlocaPreview.branch);",
60
- ' var previousLabel = share.textContent;',
61
- ' share.disabled = true;',
62
- " fetch(shareUrl, { headers: { authorization: 'Bearer ' + __conlocaPreview.jwt } })",
63
- " .then(function (res) { if (!res.ok) throw new Error('share ' + res.status); return res.json(); })",
64
- " .then(function (data) { if (!data || typeof data.url !== 'string') throw new Error('missing url'); return navigator.clipboard.writeText(data.url); })",
65
- " .then(function () { share.textContent = 'Link copied'; })",
66
- " .catch(function () { share.textContent = 'Copy failed'; })",
67
- ' .then(function () { setTimeout(function () { share.textContent = previousLabel; share.disabled = false; }, 2000); });',
68
- ' });',
62
+ " if (__conlocaPreview.exitPath) { exitLink.href = __conlocaPreview.exitPath + '?to=' + encodeURIComponent(__conlocaPreview.path || location.pathname); host.appendChild(exitLink); }",
69
63
  " var collapse = document.createElement('button'); collapse.type = 'button'; collapse.textContent = __conlocaIsCollapsed() ? '+' : '-'; collapse.style.cssText = 'background:transparent;color:#fff;border:none;cursor:pointer;font-size:16px;line-height:1;'; host.appendChild(collapse);",
70
64
  " collapse.addEventListener('click', function () { var c = !__conlocaIsCollapsed(); __conlocaSetCollapsed(c); host.style.display = c ? 'none' : ''; });",
71
65
  " if (__conlocaIsCollapsed()) host.style.display = 'none';",
@@ -74,19 +68,30 @@ export function getBannerScriptText(ctx, endpoint, exitPath) {
74
68
  "if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', __conlocaRender); } else { __conlocaRender(); }",
75
69
  ].join(' ');
76
70
  }
71
+ const ROBOTS_META = '<meta name="robots" content="noindex, nofollow">';
72
+ // Any `<meta name=robots …>`, whatever the case, quoting or attribute order; `robots-extra` and `googlebot` are other names.
73
+ const SITE_ROBOTS_META_RE = /<meta\b[^>]*?\sname\s*=\s*["']?robots(?=["'\s/>])[^>]*>/giu;
77
74
  /**
78
- * Returns `html` with the preview banner script and `<meta robots noindex>`
79
- * tag injected before the closing `</body>` tag.
75
+ * Returns `html` with the preview banner script (or, on a share pass, the
76
+ * viewer banner) injected before the closing `</body>` tag, and the page's robots metas replaced by one
77
+ * `<meta robots noindex>`. The site's own robots meta (often
78
+ * `index, follow`) must not sit next to the preview's. Ours takes the first
79
+ * one's place, or goes before `</body>` when the page has none.
80
80
  *
81
- * The injection uses `String.prototype.replace` with a string argument
82
- * (not a regex), so only the FIRST `</body>` match is rewritten. That
83
- * matters because a misconfigured renderer can emit malformed HTML with
84
- * multiple `</body>` tags; we never want to inject the banner more than
85
- * once per response.
81
+ * The `</body>` injection uses `String.prototype.replace` with a string
82
+ * argument (not a regex), so only the FIRST `</body>` match is rewritten.
83
+ * That matters because a misconfigured renderer can emit malformed HTML with
84
+ * multiple `</body>` tags; we never want to inject the banner more than once
85
+ * per response.
86
86
  */
87
- export function injectPreviewBanner(html, ctx, endpoint, exitPath) {
88
- const robotsMeta = '<meta name="robots" content="noindex, nofollow">';
89
- const script = `<script>${getBannerScriptText(ctx, endpoint, exitPath)}</script>`;
90
- return html.replace('</body>', `${robotsMeta + script}</body>`);
87
+ export function injectPreviewBanner(html, ctx, endpoint, exitPath, shareUrl, pagePath) {
88
+ const script = `<script>${getBannerScriptText(ctx, endpoint, exitPath, shareUrl, pagePath)}</script>`;
89
+ let replaced = false;
90
+ const page = html.replace(SITE_ROBOTS_META_RE, () => {
91
+ const first = !replaced;
92
+ replaced = true;
93
+ return first ? ROBOTS_META : '';
94
+ });
95
+ return page.replace('</body>', `${replaced ? '' : ROBOTS_META}${script}</body>`);
91
96
  }
92
97
  //# sourceMappingURL=banner.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"banner.js","sourceRoot":"","sources":["../../../src/preview/banner.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAmB,EAAE,QAAiB,EAAE,QAAiB;IAC3F,sEAAsE;IACtE,yEAAyE;IACzE,wEAAwE;IACxE,yEAAyE;IACzE,0EAA0E;IAC1E,EAAE;IACF,mEAAmE;IACnE,0EAA0E;IAC1E,4EAA4E;IAC5E,wEAAwE;IACxE,6EAA6E;IAC7E,EAAE;IACF,6DAA6D;IAC7D,wDAAwD;IACxD,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IACxF,OAAO;QACL,0BAA0B,OAAO,GAAG;QACpC,yDAAyD;QACzD,4IAA4I;QAC5I,+HAA+H;QAC/H,qCAAqC;QACrC,6CAA6C;QAC7C,uCAAuC;QACvC,mRAAmR;QACnR,mIAAmI;QACnI,8DAA8D;QAC9D,wEAAwE;QACxE,kDAAkD;QAClD,iGAAiG;QACjG,gKAAgK;QAChK,wEAAwE;QACxE,wEAAwE;QACxE,4BAA4B;QAC5B,qHAAqH;QACrH,8JAA8J;QAC9J,kQAAkQ;QAClQ,gFAAgF;QAChF,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KAAK,CAAC;IACrB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,EAAE,GAAG,EAAE;QAClD,MAAM,KAAK,GAAG,CAAC,QAAQ,CAAC;QACxB,QAAQ,GAAG,IAAI,CAAC;QAChB,OAAO,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC;IAClC,CAAC,CAAC,CAAC;IACH,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,GAAG,MAAM,SAAS,CAAC,CAAC;AACnF,CAAC"}
@@ -0,0 +1,35 @@
1
+ import type { AstroIntegration } from 'astro';
2
+ /**
3
+ * A `wrangler.jsonc` text → its object (comments and trailing commas allowed), else undefined. The return type is
4
+ * `isRecord`'s narrowing, inferred: anti-slop's `no-unsafe-dictionary-type` refuses the type written out.
5
+ */
6
+ export declare function parseWranglerConfig(text: string): Record<string, unknown> | undefined;
7
+ /** `wrangler.jsonc`, else `wrangler.json`, in the project; undefined when it has neither. */
8
+ export declare function wranglerConfigPath(root: string): string | undefined;
9
+ /** Where `@astrojs/cloudflare` resolves from the project; throws naming the fix when it is not installed. */
10
+ export declare function resolveCloudflareAdapter(root: string): string;
11
+ /** What the preview build passes `@astrojs/cloudflare`. */
12
+ interface ChildAdapterOptions {
13
+ configPath?: string;
14
+ imageService?: {
15
+ build: 'compile';
16
+ runtime: 'passthrough';
17
+ };
18
+ }
19
+ /**
20
+ * The adapter options per mode. Under D3 the child reads its config copy, and compiles images at build time: on-demand
21
+ * draft pages then emit `/_image` URLs the passthrough endpoint answers from the static files, with no `IMAGES` binding,
22
+ * which the dispatcher's config doesn't declare (the real-build round trip serves a fixture image through it).
23
+ * Under D4 the preview deploy is a Cloudflare Worker of its own and keeps the adapter's defaults.
24
+ */
25
+ export declare function childAdapterOptions(mode: 'd3' | 'd4', configPath: string | undefined): ChildAdapterOptions;
26
+ /**
27
+ * The adapter for the preview build of a site that names none. Under D3 it reads a copy of the site's `wrangler.jsonc`
28
+ * without `main` and `assets`, at the top level and in every env block (both inherit): those belong to the dispatcher in
29
+ * 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
30
+ * block the build selects, is the preview deploy's.
31
+ * shortcut: relative paths inside the copy resolve from .conloca/; rewrite them when a site's config carries one.
32
+ */
33
+ export declare function cloudflareAdapter(rootUrl: URL, mode: 'd3' | 'd4'): Promise<AstroIntegration>;
34
+ export {};
35
+ //# sourceMappingURL=cloudflare-adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare-adapter.d.ts","sourceRoot":"","sources":["../../../src/preview/cloudflare-adapter.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAI9C;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,uCAG/C;AAED,6FAA6F;AAC7F,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEnE;AAED,6GAA6G;AAC7G,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAS7D;AAED,2DAA2D;AAC3D,UAAU,mBAAmB;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE;QAAE,KAAK,EAAE,SAAS,CAAC;QAAC,OAAO,EAAE,aAAa,CAAA;KAAE,CAAC;CAC7D;AAWD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,EAAE,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,mBAAmB,CAM1G;AAED;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,OAAO,CAAC,gBAAgB,CAAC,CA+BlG"}
@@ -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":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,2CAA2C,CAAC;AAErE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAY;IAC9C,MAAM,KAAK,GAAY,KAAK,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC,CAAC;IACrE,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AAC7C,CAAC;AAED,6FAA6F;AAC7F,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,OAAO,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;AAC9G,CAAC;AAED,6GAA6G;AAC7G,MAAM,UAAU,wBAAwB,CAAC,IAAY;IACnD,IAAI,CAAC;QACH,OA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AAC;QACvD,aAAa,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC;IACnE,CAAC;IACD,MAAM,OAAO,GAAG,OAAO,CAAC,mBAAmB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAC/D,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC;QACzB,MAAM,IAAI,KAAK,CAAC,GAAG,YAAY,2DAA2D,CAAC,CAAC;IAC9F,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -1,14 +1,10 @@
1
1
  /**
2
- * Minimal cookie-header parser. We only need to look up cookies by exact name
3
- * (the fixed `__Host-conloca_preview` cookie); a full RFC 6265 parser would be
4
- * overkill, and pulling in a dependency for this would be YAGNI.
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 parseCookies(cookieHeader: string | null | undefined): Map<string, string>;
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=/.
@@ -27,13 +23,13 @@ export declare function parseCookies(cookieHeader: string | null | undefined): M
27
23
  * cross-site POSTs and subresource loads, which is the threat that matters for
28
24
  * a read-only preview credential.
29
25
  *
30
- * `Path=/` is load-bearing: the browser sends the cookie on every same-origin
31
- * request, which is what lets the preview follow internal links instead of
32
- * surviving exactly one page view.
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.
33
29
  *
34
30
  * `maxAgeSeconds` bounds the cookie to the credential it carries — pass the
35
31
  * JWT's remaining lifetime so a stale cookie cannot outlive the token and
36
32
  * strand the visitor on the landing page.
37
33
  */
38
- 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;
39
35
  //# sourceMappingURL=cookie.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cookie.d.ts","sourceRoot":"","sources":["../../../src/preview/cookie.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAczF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,CAIlG"}
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
- * Minimal cookie-header parser. We only need to look up cookies by exact name
3
- * (the fixed `__Host-conloca_preview` cookie); a full RFC 6265 parser would be
4
- * overkill, and pulling in a dependency for this would be YAGNI.
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 parseCookies(cookieHeader) {
12
- const out = new Map();
7
+ export function parseCookieValues(cookieHeader, name) {
13
8
  if (!cookieHeader)
14
- return out;
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
- const name = segment.slice(0, eq).trim();
20
- const value = segment.slice(eq + 1).trim();
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 out;
18
+ return values;
29
19
  }
30
20
  /**
31
21
  * Serializes a `Set-Cookie` value with the security defaults the preview
@@ -45,16 +35,16 @@ export function parseCookies(cookieHeader) {
45
35
  * cross-site POSTs and subresource loads, which is the threat that matters for
46
36
  * a read-only preview credential.
47
37
  *
48
- * `Path=/` is load-bearing: the browser sends the cookie on every same-origin
49
- * request, which is what lets the preview follow internal links instead of
50
- * surviving exactly one page view.
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.
51
41
  *
52
42
  * `maxAgeSeconds` bounds the cookie to the credential it carries — pass the
53
43
  * JWT's remaining lifetime so a stale cookie cannot outlive the token and
54
44
  * strand the visitor on the landing page.
55
45
  */
56
- export function serializePreviewCookie(name, value, maxAgeSeconds) {
57
- 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}`;
58
48
  if (maxAgeSeconds === undefined)
59
49
  return base;
60
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;;;;;;;;;GASG;AACH,MAAM,UAAU,YAAY,CAAC,YAAuC;IAClE,MAAM,GAAG,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtC,IAAI,CAAC,YAAY;QAAE,OAAO,GAAG,CAAC;IAC9B,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,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QACzC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC3C,IAAI,CAAC,IAAI;YAAE,SAAS;QACpB,qEAAqE;QACrE,wDAAwD;QACxD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,sBAAsB,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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,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