@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.
Files changed (241) hide show
  1. package/README.md +378 -207
  2. package/dist/.build-inputs.json +69 -0
  3. package/dist/src/component-manifest.d.ts +5 -3
  4. package/dist/src/component-manifest.d.ts.map +1 -1
  5. package/dist/src/component-manifest.js +7 -7
  6. package/dist/src/component-manifest.js.map +1 -1
  7. package/dist/src/constants.d.ts +14 -13
  8. package/dist/src/constants.d.ts.map +1 -1
  9. package/dist/src/constants.js +14 -13
  10. package/dist/src/constants.js.map +1 -1
  11. package/dist/src/d3-build.d.ts +70 -0
  12. package/dist/src/d3-build.d.ts.map +1 -0
  13. package/dist/src/d3-build.js +272 -0
  14. package/dist/src/d3-build.js.map +1 -0
  15. package/dist/src/descriptors.d.ts +2 -2
  16. package/dist/src/descriptors.d.ts.map +1 -1
  17. package/dist/src/descriptors.js +4 -2
  18. package/dist/src/descriptors.js.map +1 -1
  19. package/dist/src/doctor/checks-address.d.ts +18 -0
  20. package/dist/src/doctor/checks-address.d.ts.map +1 -0
  21. package/dist/src/doctor/checks-address.js +163 -0
  22. package/dist/src/doctor/checks-address.js.map +1 -0
  23. package/dist/src/doctor/checks-live.d.ts +8 -5
  24. package/dist/src/doctor/checks-live.d.ts.map +1 -1
  25. package/dist/src/doctor/checks-live.js +226 -188
  26. package/dist/src/doctor/checks-live.js.map +1 -1
  27. package/dist/src/doctor/checks-offline.d.ts +6 -25
  28. package/dist/src/doctor/checks-offline.d.ts.map +1 -1
  29. package/dist/src/doctor/checks-offline.js +30 -259
  30. package/dist/src/doctor/checks-offline.js.map +1 -1
  31. package/dist/src/doctor/checks-preview-limits.d.ts +11 -0
  32. package/dist/src/doctor/checks-preview-limits.d.ts.map +1 -0
  33. package/dist/src/doctor/checks-preview-limits.js +133 -0
  34. package/dist/src/doctor/checks-preview-limits.js.map +1 -0
  35. package/dist/src/doctor/checks-preview-live.d.ts +14 -0
  36. package/dist/src/doctor/checks-preview-live.d.ts.map +1 -0
  37. package/dist/src/doctor/checks-preview-live.js +72 -0
  38. package/dist/src/doctor/checks-preview-live.js.map +1 -0
  39. package/dist/src/doctor/checks-preview.d.ts +7 -0
  40. package/dist/src/doctor/checks-preview.d.ts.map +1 -0
  41. package/dist/src/doctor/checks-preview.js +287 -0
  42. package/dist/src/doctor/checks-preview.js.map +1 -0
  43. package/dist/src/doctor/config-scan.d.ts +12 -3
  44. package/dist/src/doctor/config-scan.d.ts.map +1 -1
  45. package/dist/src/doctor/config-scan.js +38 -11
  46. package/dist/src/doctor/config-scan.js.map +1 -1
  47. package/dist/src/doctor/run.d.ts.map +1 -1
  48. package/dist/src/doctor/run.js +16 -10
  49. package/dist/src/doctor/run.js.map +1 -1
  50. package/dist/src/doctor/types.d.ts +17 -7
  51. package/dist/src/doctor/types.d.ts.map +1 -1
  52. package/dist/src/doctor/types.js +17 -7
  53. package/dist/src/doctor/types.js.map +1 -1
  54. package/dist/src/editor-page/bundle.d.ts +90 -0
  55. package/dist/src/editor-page/bundle.d.ts.map +1 -0
  56. package/dist/src/editor-page/bundle.js +373 -0
  57. package/dist/src/editor-page/bundle.js.map +1 -0
  58. package/dist/src/editor-page/data-schemas.d.ts +16 -0
  59. package/dist/src/editor-page/data-schemas.d.ts.map +1 -0
  60. package/dist/src/editor-page/data-schemas.js +39 -0
  61. package/dist/src/editor-page/data-schemas.js.map +1 -0
  62. package/dist/src/editor-page/emit.d.ts +69 -0
  63. package/dist/src/editor-page/emit.d.ts.map +1 -0
  64. package/dist/src/editor-page/emit.js +254 -0
  65. package/dist/src/editor-page/emit.js.map +1 -0
  66. package/dist/src/editor-page/framing.d.ts +23 -0
  67. package/dist/src/editor-page/framing.d.ts.map +1 -0
  68. package/dist/src/editor-page/framing.js +39 -0
  69. package/dist/src/editor-page/framing.js.map +1 -0
  70. package/dist/src/editor-page/page.d.ts +9 -0
  71. package/dist/src/editor-page/page.d.ts.map +1 -0
  72. package/dist/src/editor-page/page.js +31 -0
  73. package/dist/src/editor-page/page.js.map +1 -0
  74. package/dist/src/editor-page/site-base.d.ts +4 -0
  75. package/dist/src/editor-page/site-base.d.ts.map +1 -0
  76. package/dist/src/editor-page/site-base.js +13 -0
  77. package/dist/src/editor-page/site-base.js.map +1 -0
  78. package/dist/src/git.d.ts +11 -8
  79. package/dist/src/git.d.ts.map +1 -1
  80. package/dist/src/git.js +25 -8
  81. package/dist/src/git.js.map +1 -1
  82. package/dist/src/index.d.ts +4 -1
  83. package/dist/src/index.d.ts.map +1 -1
  84. package/dist/src/index.js +11 -6
  85. package/dist/src/index.js.map +1 -1
  86. package/dist/src/integration.d.ts +1 -1
  87. package/dist/src/integration.d.ts.map +1 -1
  88. package/dist/src/integration.js +377 -416
  89. package/dist/src/integration.js.map +1 -1
  90. package/dist/src/node.d.ts +3 -1
  91. package/dist/src/node.d.ts.map +1 -1
  92. package/dist/src/node.js +7 -2
  93. package/dist/src/node.js.map +1 -1
  94. package/dist/src/preview/_dispatch.d.ts +12 -0
  95. package/dist/src/preview/_dispatch.d.ts.map +1 -1
  96. package/dist/src/preview/_dispatch.js +31 -0
  97. package/dist/src/preview/_dispatch.js.map +1 -1
  98. package/dist/src/preview/_resolve-opts.d.ts +19 -10
  99. package/dist/src/preview/_resolve-opts.d.ts.map +1 -1
  100. package/dist/src/preview/_resolve-opts.js +24 -11
  101. package/dist/src/preview/_resolve-opts.js.map +1 -1
  102. package/dist/src/preview/banner.d.ts +12 -9
  103. package/dist/src/preview/banner.d.ts.map +1 -1
  104. package/dist/src/preview/banner.js +46 -43
  105. package/dist/src/preview/banner.js.map +1 -1
  106. package/dist/src/preview/cloudflare-adapter.d.ts +35 -0
  107. package/dist/src/preview/cloudflare-adapter.d.ts.map +1 -0
  108. package/dist/src/preview/cloudflare-adapter.js +92 -0
  109. package/dist/src/preview/cloudflare-adapter.js.map +1 -0
  110. package/dist/src/preview/cookie.d.ts +13 -14
  111. package/dist/src/preview/cookie.d.ts.map +1 -1
  112. package/dist/src/preview/cookie.js +19 -26
  113. package/dist/src/preview/cookie.js.map +1 -1
  114. package/dist/src/preview/dispatcher.d.ts +73 -0
  115. package/dist/src/preview/dispatcher.d.ts.map +1 -0
  116. package/dist/src/preview/dispatcher.js +329 -0
  117. package/dist/src/preview/dispatcher.js.map +1 -0
  118. package/dist/src/preview/entry-code.d.ts +33 -0
  119. package/dist/src/preview/entry-code.d.ts.map +1 -0
  120. package/dist/src/preview/entry-code.js +33 -0
  121. package/dist/src/preview/entry-code.js.map +1 -0
  122. package/dist/src/preview/headers.d.ts +24 -4
  123. package/dist/src/preview/headers.d.ts.map +1 -1
  124. package/dist/src/preview/headers.js +35 -7
  125. package/dist/src/preview/headers.js.map +1 -1
  126. package/dist/src/preview/label.d.ts +8 -0
  127. package/dist/src/preview/label.d.ts.map +1 -0
  128. package/dist/src/preview/label.js +14 -0
  129. package/dist/src/preview/label.js.map +1 -0
  130. package/dist/src/preview/landing.d.ts +23 -2
  131. package/dist/src/preview/landing.d.ts.map +1 -1
  132. package/dist/src/preview/landing.js +105 -37
  133. package/dist/src/preview/landing.js.map +1 -1
  134. package/dist/src/preview/middleware.d.ts +49 -22
  135. package/dist/src/preview/middleware.d.ts.map +1 -1
  136. package/dist/src/preview/middleware.js +259 -100
  137. package/dist/src/preview/middleware.js.map +1 -1
  138. package/dist/src/preview/preview-path.d.ts +47 -5
  139. package/dist/src/preview/preview-path.d.ts.map +1 -1
  140. package/dist/src/preview/preview-path.js +65 -8
  141. package/dist/src/preview/preview-path.js.map +1 -1
  142. package/dist/src/preview/redeem-url.d.ts +7 -0
  143. package/dist/src/preview/redeem-url.d.ts.map +1 -0
  144. package/dist/src/preview/redeem-url.js +14 -0
  145. package/dist/src/preview/redeem-url.js.map +1 -0
  146. package/dist/src/preview/site-middleware.d.ts +5 -5
  147. package/dist/src/preview/site-middleware.d.ts.map +1 -1
  148. package/dist/src/preview/site-middleware.js +53 -26
  149. package/dist/src/preview/site-middleware.js.map +1 -1
  150. package/dist/src/preview-build.d.ts +29 -15
  151. package/dist/src/preview-build.d.ts.map +1 -1
  152. package/dist/src/preview-build.js +66 -25
  153. package/dist/src/preview-build.js.map +1 -1
  154. package/dist/src/preview.d.ts +1 -1
  155. package/dist/src/preview.d.ts.map +1 -1
  156. package/dist/src/preview.js +1 -1
  157. package/dist/src/preview.js.map +1 -1
  158. package/dist/src/repository.d.ts +1 -1
  159. package/dist/src/repository.js +1 -1
  160. package/dist/src/resolve-options.d.ts +17 -0
  161. package/dist/src/resolve-options.d.ts.map +1 -0
  162. package/dist/src/resolve-options.js +21 -0
  163. package/dist/src/resolve-options.js.map +1 -0
  164. package/dist/src/site-id.d.ts +0 -2
  165. package/dist/src/site-id.d.ts.map +1 -1
  166. package/dist/src/site-id.js +2 -3
  167. package/dist/src/site-id.js.map +1 -1
  168. package/dist/src/skill.d.ts +11 -0
  169. package/dist/src/skill.d.ts.map +1 -1
  170. package/dist/src/skill.js +32 -9
  171. package/dist/src/skill.js.map +1 -1
  172. package/dist/src/types.d.ts +33 -78
  173. package/dist/src/types.d.ts.map +1 -1
  174. package/package.json +63 -13
  175. package/src/component-manifest.ts +11 -8
  176. package/src/constants.ts +15 -13
  177. package/src/d3-build.ts +315 -0
  178. package/src/descriptors.ts +6 -4
  179. package/src/doctor/checks-address.ts +190 -0
  180. package/src/doctor/checks-live.ts +262 -268
  181. package/src/doctor/checks-offline.ts +32 -279
  182. package/src/doctor/checks-preview-limits.ts +137 -0
  183. package/src/doctor/checks-preview-live.ts +77 -0
  184. package/src/doctor/checks-preview.ts +302 -0
  185. package/src/doctor/config-scan.ts +35 -9
  186. package/src/doctor/run.ts +18 -16
  187. package/src/doctor/types.ts +17 -7
  188. package/src/editor-page/bundle.ts +487 -0
  189. package/src/editor-page/data-schemas.ts +47 -0
  190. package/src/editor-page/emit.ts +362 -0
  191. package/src/editor-page/framing.ts +62 -0
  192. package/src/editor-page/page.ts +37 -0
  193. package/src/editor-page/site-base.ts +11 -0
  194. package/src/git.ts +29 -10
  195. package/src/index.ts +22 -6
  196. package/src/integration.ts +399 -469
  197. package/src/node.ts +18 -2
  198. package/src/preview/_dispatch.ts +36 -1
  199. package/src/preview/_resolve-opts.ts +35 -12
  200. package/src/preview/banner.ts +61 -43
  201. package/src/preview/cloudflare-adapter.ts +103 -0
  202. package/src/preview/cookie.ts +18 -24
  203. package/src/preview/dispatcher.ts +407 -0
  204. package/src/preview/entry-code.ts +51 -0
  205. package/src/preview/headers.ts +35 -7
  206. package/src/preview/label.ts +12 -0
  207. package/src/preview/landing.ts +117 -39
  208. package/src/preview/middleware.ts +302 -115
  209. package/src/preview/preview-path.ts +83 -8
  210. package/src/preview/redeem-url.ts +14 -0
  211. package/src/preview/site-middleware.ts +60 -30
  212. package/src/preview-build.ts +85 -26
  213. package/src/preview-handler.astro +12 -9
  214. package/src/preview.ts +0 -1
  215. package/src/repository.ts +1 -1
  216. package/src/resolve-options.ts +37 -0
  217. package/src/site-id.ts +2 -4
  218. package/src/skill/SKILL.md +63 -55
  219. package/src/skill.ts +36 -8
  220. package/src/types.ts +33 -79
  221. package/src/virtual-modules.d.ts +4 -1
  222. package/dist/src/build-commit.d.ts +0 -19
  223. package/dist/src/build-commit.d.ts.map +0 -1
  224. package/dist/src/build-commit.js +0 -6
  225. package/dist/src/build-commit.js.map +0 -1
  226. package/dist/src/bundle.d.ts +0 -181
  227. package/dist/src/bundle.d.ts.map +0 -1
  228. package/dist/src/bundle.js +0 -413
  229. package/dist/src/bundle.js.map +0 -1
  230. package/dist/src/discovery.d.ts +0 -58
  231. package/dist/src/discovery.d.ts.map +0 -1
  232. package/dist/src/discovery.js +0 -133
  233. package/dist/src/discovery.js.map +0 -1
  234. package/dist/src/register.d.ts +0 -98
  235. package/dist/src/register.d.ts.map +0 -1
  236. package/dist/src/register.js +0 -103
  237. package/dist/src/register.js.map +0 -1
  238. package/src/build-commit.ts +0 -22
  239. package/src/bundle.ts +0 -570
  240. package/src/discovery.ts +0 -183
  241. package/src/register.ts +0 -180
package/src/discovery.ts DELETED
@@ -1,183 +0,0 @@
1
- import { spawn } from 'node:child_process';
2
- import { readFile } from 'node:fs/promises';
3
- import type { DiscoveredWrappers } from '@conloca/astro-cms/node';
4
- import { findContentWrapper } from '@conloca/astro-cms/node';
5
-
6
- /**
7
- * Build-time style discovery. The editor at the endpoint cannot run the
8
- * discovery flow conlocaCMS runs on the site's dev server — no Vite graph to
9
- * ask for a route's CSS, and cross-origin computed styles are browser-blocked
10
- * — but every mechanism only produces plain data, and the build is the moment
11
- * this pipeline stands inside the site. So: read the discovery route's
12
- * rendered page, collect its stylesheets, run the SAME wrapper heuristic the
13
- * dev server runs (`findContentWrapper`, imported — not duplicated), and hand
14
- * the results to registration.
15
- *
16
- * Where the rendered page comes from, in order:
17
- * 1. a prerendered file in the client dir (static routes);
18
- * 2. the built server entry, booted for one request (the node adapter's
19
- * `dist/server/entry.mjs` honors HOST/PORT) — covers `output: 'server'`
20
- * sites, which is what real CMS deployments are;
21
- * 3. nothing → warn and skip; registration proceeds without the artifacts
22
- * (fail-soft, like every other optional registration input).
23
- *
24
- * ponytail: only the node adapter's entry is bootable in v1 — a
25
- * workerd-targeted build (Cloudflare adapter) skips discovery with the
26
- * warning. A site whose prose is `.conloca-prose`-scoped is already covered
27
- * by the derived tier; boot-via-wrangler is the upgrade path if a
28
- * workerd-targeted site ever needs heuristic discovery.
29
- */
30
-
31
- export interface DiscoveredStyleContext {
32
- hostWrappers: DiscoveredWrappers;
33
- /** The route's stylesheets concatenated in document order (links + inline). */
34
- routeCss: string;
35
- }
36
-
37
- export type CssRef = { kind: 'link'; href: string } | { kind: 'inline'; css: string };
38
-
39
- /**
40
- * Stylesheet references of a rendered page in document order. Regex-level on
41
- * purpose: build-output HTML is machine-generated, and the only shapes that
42
- * matter are `<link rel="stylesheet">` and `<style>` blocks.
43
- */
44
- export function extractRouteCssRefs(html: string): CssRef[] {
45
- const refs: Array<{ index: number; ref: CssRef }> = [];
46
-
47
- const linkRe = /<link\b[^>]*>/gi;
48
- for (const match of html.matchAll(linkRe)) {
49
- const tag = match[0];
50
- if (!/rel\s*=\s*["']?stylesheet["']?/i.test(tag)) continue;
51
- const href = tag.match(/href\s*=\s*["']([^"']+)["']/i)?.[1];
52
- if (href) refs.push({ index: match.index ?? 0, ref: { kind: 'link', href } });
53
- }
54
-
55
- const styleRe = /<style\b[^>]*>([\s\S]*?)<\/style>/gi;
56
- for (const match of html.matchAll(styleRe)) {
57
- const css = match[1].trim();
58
- if (css) refs.push({ index: match.index ?? 0, ref: { kind: 'inline', css } });
59
- }
60
-
61
- return refs.sort((a, b) => a.index - b.index).map((entry) => entry.ref);
62
- }
63
-
64
- interface DiscoverInput {
65
- /** The build's client output dir (where `_conloca/` also lands). */
66
- clientDir: string;
67
- /** The built SSR entry, when the adapter produces a bootable one. */
68
- serverEntry?: string;
69
- /** The representative content route (the shared `fragmentPreviewRoute`). */
70
- route: string;
71
- logger: { info(message: string): void; warn(message: string): void };
72
- }
73
-
74
- export async function discoverStyleContext(input: DiscoverInput): Promise<DiscoveredStyleContext | undefined> {
75
- const fromFile = await readPrerenderedPage(input);
76
- if (fromFile) return fromFile;
77
-
78
- if (input.serverEntry) {
79
- try {
80
- return await bootAndDiscover(input);
81
- } catch (error) {
82
- input.logger.warn(
83
- `style discovery: booting the server entry failed — skipping (${
84
- error instanceof Error ? error.message : String(error)
85
- })`,
86
- );
87
- return undefined;
88
- }
89
- }
90
-
91
- input.logger.warn(
92
- `style discovery: no built page for ${input.route} and no bootable server entry — ` +
93
- 'skipping; the editor falls back to the prose-safe tier.',
94
- );
95
- return undefined;
96
- }
97
-
98
- /** Try `<route>/index.html` then `<route>.html` under the client dir. */
99
- async function readPrerenderedPage(input: DiscoverInput): Promise<DiscoveredStyleContext | undefined> {
100
- const slug = input.route.replace(/^\/+|\/+$/g, '');
101
- for (const candidate of [`${slug}/index.html`, `${slug}.html`]) {
102
- let html: string;
103
- try {
104
- html = await readFile(join2(input.clientDir, candidate), 'utf8');
105
- } catch {
106
- continue;
107
- }
108
- const routeCss = await assembleCss(extractRouteCssRefs(html), async (href) => {
109
- try {
110
- return await readFile(join2(input.clientDir, href.replace(/^\/+/, '')), 'utf8');
111
- } catch {
112
- input.logger.warn(`style discovery: stylesheet ${href} not found in the client dir — skipped.`);
113
- return '';
114
- }
115
- });
116
- return { hostWrappers: findContentWrapper(html), routeCss };
117
- }
118
- return undefined;
119
- }
120
-
121
- /** Boot the built entry on a loopback port, fetch the route + its CSS, kill. */
122
- async function bootAndDiscover(input: DiscoverInput): Promise<DiscoveredStyleContext | undefined> {
123
- if (!input.serverEntry) return undefined;
124
- const port = 41000 + Math.floor(Math.random() * 20000);
125
- const origin = `http://127.0.0.1:${port}`;
126
- const child = spawn(process.execPath, [input.serverEntry], {
127
- env: { ...process.env, HOST: '127.0.0.1', PORT: String(port) },
128
- stdio: 'ignore',
129
- });
130
- try {
131
- await waitForServer(origin);
132
- const page = await fetch(`${origin}${input.route}`);
133
- if (!page.ok) {
134
- input.logger.warn(`style discovery: ${input.route} answered ${page.status} on the booted entry — skipping.`);
135
- return undefined;
136
- }
137
- const html = await page.text();
138
- const routeCss = await assembleCss(extractRouteCssRefs(html), async (href) => {
139
- const res = await fetch(new URL(href, origin));
140
- if (!res.ok) {
141
- input.logger.warn(`style discovery: stylesheet ${href} answered ${res.status} — skipped.`);
142
- return '';
143
- }
144
- return res.text();
145
- });
146
- return { hostWrappers: findContentWrapper(html), routeCss };
147
- } finally {
148
- child.kill('SIGTERM');
149
- }
150
- }
151
-
152
- async function assembleCss(refs: CssRef[], loadHref: (href: string) => Promise<string>): Promise<string> {
153
- const parts: string[] = [];
154
- for (const ref of refs) {
155
- if (ref.kind === 'inline') {
156
- parts.push(ref.css);
157
- continue;
158
- }
159
- const css = await loadHref(ref.href);
160
- if (css) parts.push(css);
161
- }
162
- return parts.join('\n');
163
- }
164
-
165
- async function waitForServer(origin: string): Promise<void> {
166
- // A design-time boot of the SSR entry; on a saturated CI runner the first
167
- // answer took >15s while a full parallel gate ran beside it.
168
- const deadline = Date.now() + 60_000;
169
- while (Date.now() < deadline) {
170
- try {
171
- await fetch(origin, { method: 'HEAD' });
172
- return;
173
- } catch {
174
- await new Promise((r) => setTimeout(r, 150));
175
- }
176
- }
177
- throw new Error('server entry did not answer within 60s');
178
- }
179
-
180
- /** node:path join without importing path twice under bundling edge cases. */
181
- function join2(base: string, rel: string): string {
182
- return `${base.replace(/\/+$/, '')}/${rel}`;
183
- }
package/src/register.ts DELETED
@@ -1,180 +0,0 @@
1
- import { PACKAGE_NAME } from './constants.js';
2
-
3
- /**
4
- * Registration request (`POST {endpoint}/api/v1/registrations`). Auth is the
5
- * build token (`cbt_<id>.<secret>`) the endpoint issued; the body carries build
6
- * metadata only.
7
- */
8
- export interface RegistrationInput {
9
- /** Editor endpoint, e.g. 'https://cms.example.com'. */
10
- endpoint: string;
11
- /** Build token sent as `Authorization: Bearer <token>`. */
12
- buildToken: string;
13
- /** Auto-detected `owner/repo` identity of the build checkout. */
14
- repoUri: string;
15
- gitSha: string;
16
- branch: string;
17
- /** Deployable-site id (top-level siteId in the content root's
18
- * sites.json, or the implicit 'default' for a zero-config repo). Resolved at
19
- * build via resolveBuildSiteId — registration attaches, never defines. */
20
- siteId: string;
21
- /** Absolute URL the editor fetches the bundle from. */
22
- editorBundleUrl: string;
23
- /** Full sha-256 hex of the bundle bytes; the editor refuses non-matching bytes. */
24
- bundleHash: string;
25
- /** Absolute URL of the companion canvas stylesheet; absent when the config graph has no styles. */
26
- stylesheetUrl?: string;
27
- /** Full sha-256 hex of the stylesheet bytes; the editor skips non-matching bytes (fail-soft). */
28
- stylesheetHash?: string;
29
- /**
30
- * Discovered content-wrapper shape of the fragmentPreviewRoute — inline
31
- * because it is well under 1 KB; a URL+hash would add a
32
- * fetch and a verification for nothing.
33
- */
34
- hostWrappers?: { content: { tagName: string; className: string } | null; codeBlock: unknown };
35
- /** Absolute URL of the discovery route's collected CSS; same artifact pattern as stylesheetUrl. */
36
- routeCssUrl?: string;
37
- /** Full sha-256 hex of the route-CSS bytes; the editor skips non-matching bytes (fail-soft). */
38
- routeCssHash?: string;
39
- /** Absolute URL of the build's component manifest; same artifact pattern as stylesheetUrl. */
40
- componentManifestUrl?: string;
41
- /** Full sha-256 hex of the manifest bytes; a reader refuses non-matching bytes. */
42
- componentManifestHash?: string;
43
- deploymentUrl?: string;
44
- deploymentProvider?: string;
45
- /**
46
- * The commit the artifact was actually built from; differs from gitSha only when CI hands over the branch's
47
- * latest commit.
48
- */
49
- buildId?: string;
50
- /**
51
- * The `@conloca/cloud-astro` version that produced this build, so the endpoint and the doctor can say which
52
- * package version built the editor bundle.
53
- */
54
- clientVersion?: string;
55
- }
56
-
57
- export interface RegistrationResult {
58
- /** Per-branch preview secret consumed by the recursive preview build. */
59
- previewSecret: string;
60
- }
61
-
62
- /**
63
- * The minimal fetch surface this module relies on. A narrow seam (matching
64
- * `PreviewBuildSpawn` in preview-build.ts) lets tests pass a tiny fake
65
- * without reconstructing the full `fetch` typing.
66
- */
67
- export type RegistrationFetch = (
68
- url: string,
69
- init: { method: 'POST'; headers: Record<string, string>; body: string },
70
- ) => Promise<Response>;
71
-
72
- const INITIAL_RETRY_MS = 1_000;
73
- const MAX_RETRY_MS = 30_000;
74
- const RETRY_BUDGET_MS = 180_000;
75
-
76
- /**
77
- * Backoff before each retry; length + 1 = total attempts. Starts at 1 s,
78
- * doubles to a 30 s ceiling and stops before the waits alone exceed the
79
- * budget: an endpoint that has just been set up can answer 503 for a while,
80
- * and a budget of about a second gives up long before it settles.
81
- */
82
- export function _registrationRetryDelays(): number[] {
83
- const delays: number[] = [];
84
- let next = INITIAL_RETRY_MS;
85
- let total = 0;
86
- while (total + next <= RETRY_BUDGET_MS) {
87
- delays.push(next);
88
- total += next;
89
- next = Math.min(next * 2, MAX_RETRY_MS);
90
- }
91
- return delays;
92
- }
93
-
94
- const DEFAULT_RETRY_DELAYS_MS: readonly number[] = _registrationRetryDelays();
95
-
96
- /**
97
- * POSTs the registration to the endpoint and returns the per-branch preview
98
- * secret. Network errors and 5xx responses are retried with exponential
99
- * backoff (1 s doubling to 30 s, ten attempts within a three-minute wait
100
- * budget); 4xx responses throw immediately with the response body so
101
- * the caller can surface the exact rejection reason (e.g. an invalid token).
102
- */
103
- export async function postRegistration(input: RegistrationInput): Promise<RegistrationResult> {
104
- return _postRegistrationWith((url, init) => fetch(url, init), input);
105
- }
106
-
107
- /**
108
- * Internal seam that takes the fetch function (and retry backoff) as
109
- * parameters so tests can substitute fakes. Public callers should use
110
- * {@link postRegistration}.
111
- *
112
- * Exported with a leading underscore to signal that this is an internal API
113
- * and not part of the package's public surface.
114
- */
115
- export async function _postRegistrationWith(
116
- fetchFn: RegistrationFetch,
117
- input: RegistrationInput,
118
- retryDelaysMs: readonly number[] = DEFAULT_RETRY_DELAYS_MS,
119
- ): Promise<RegistrationResult> {
120
- const url = `${input.endpoint}/api/v1/registrations`;
121
- const init = {
122
- method: 'POST' as const,
123
- headers: {
124
- 'content-type': 'application/json',
125
- authorization: `Bearer ${input.buildToken}`,
126
- },
127
- // JSON.stringify drops undefined optional fields, matching the endpoint's
128
- // "optional means absent" reading of the body.
129
- body: JSON.stringify({
130
- repoUri: input.repoUri,
131
- gitSha: input.gitSha,
132
- branch: input.branch,
133
- siteId: input.siteId,
134
- editorBundleUrl: input.editorBundleUrl,
135
- bundleHash: input.bundleHash,
136
- stylesheetUrl: input.stylesheetUrl,
137
- stylesheetHash: input.stylesheetHash,
138
- hostWrappers: input.hostWrappers,
139
- routeCssUrl: input.routeCssUrl,
140
- routeCssHash: input.routeCssHash,
141
- componentManifestUrl: input.componentManifestUrl,
142
- componentManifestHash: input.componentManifestHash,
143
- deploymentUrl: input.deploymentUrl,
144
- deploymentProvider: input.deploymentProvider,
145
- buildId: input.buildId,
146
- clientVersion: input.clientVersion,
147
- }),
148
- };
149
-
150
- const attempts = retryDelaysMs.length + 1;
151
- let lastFailure = '';
152
- for (let attempt = 0; attempt < attempts; attempt++) {
153
- if (attempt > 0 && retryDelaysMs[attempt - 1] > 0) {
154
- await new Promise((resolve) => setTimeout(resolve, retryDelaysMs[attempt - 1]));
155
- }
156
- let response: Response;
157
- try {
158
- response = await fetchFn(url, init);
159
- } catch (error) {
160
- // Network-level failure (endpoint unreachable, DNS, reset) — retry.
161
- lastFailure = error instanceof Error ? error.message : String(error);
162
- continue;
163
- }
164
- if (response.ok) {
165
- const payload = (await response.json().catch(() => null)) as { previewSecret?: unknown } | null;
166
- if (typeof payload?.previewSecret !== 'string' || payload.previewSecret === '') {
167
- throw new Error(`${PACKAGE_NAME}: registration response is missing previewSecret`);
168
- }
169
- return { previewSecret: payload.previewSecret };
170
- }
171
- const text = await response.text().catch(() => '');
172
- if (response.status < 500) {
173
- // 4xx is deterministic (bad token, repo not covered, malformed body) —
174
- // retrying cannot help, and the body carries the exact reason.
175
- throw new Error(`${PACKAGE_NAME}: registration rejected: HTTP ${response.status} ${text}`);
176
- }
177
- lastFailure = `HTTP ${response.status} ${text}`;
178
- }
179
- throw new Error(`${PACKAGE_NAME}: registration failed after ${attempts} attempts: ${lastFailure}`);
180
- }