@conloca/cloud-astro 0.0.0-bootstrap.0 → 0.2.1

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 (196) hide show
  1. package/README.md +327 -2
  2. package/dist/src/build-commit.d.ts +19 -0
  3. package/dist/src/build-commit.d.ts.map +1 -0
  4. package/dist/src/build-commit.js +6 -0
  5. package/dist/src/build-commit.js.map +1 -0
  6. package/dist/src/bundle.d.ts +173 -0
  7. package/dist/src/bundle.d.ts.map +1 -0
  8. package/dist/src/bundle.js +413 -0
  9. package/dist/src/bundle.js.map +1 -0
  10. package/dist/src/component-manifest.d.ts +29 -0
  11. package/dist/src/component-manifest.d.ts.map +1 -0
  12. package/dist/src/component-manifest.js +53 -0
  13. package/dist/src/component-manifest.js.map +1 -0
  14. package/dist/src/constants.d.ts +26 -0
  15. package/dist/src/constants.d.ts.map +1 -0
  16. package/dist/src/constants.js +26 -0
  17. package/dist/src/constants.js.map +1 -0
  18. package/dist/src/descriptors.d.ts +25 -0
  19. package/dist/src/descriptors.d.ts.map +1 -0
  20. package/dist/src/descriptors.js +65 -0
  21. package/dist/src/descriptors.js.map +1 -0
  22. package/dist/src/discovery.d.ts +58 -0
  23. package/dist/src/discovery.d.ts.map +1 -0
  24. package/dist/src/discovery.js +133 -0
  25. package/dist/src/discovery.js.map +1 -0
  26. package/dist/src/doctor/checks-live.d.ts +15 -0
  27. package/dist/src/doctor/checks-live.d.ts.map +1 -0
  28. package/dist/src/doctor/checks-live.js +229 -0
  29. package/dist/src/doctor/checks-live.js.map +1 -0
  30. package/dist/src/doctor/checks-offline.d.ts +36 -0
  31. package/dist/src/doctor/checks-offline.d.ts.map +1 -0
  32. package/dist/src/doctor/checks-offline.js +301 -0
  33. package/dist/src/doctor/checks-offline.js.map +1 -0
  34. package/dist/src/doctor/config-scan.d.ts +55 -0
  35. package/dist/src/doctor/config-scan.d.ts.map +1 -0
  36. package/dist/src/doctor/config-scan.js +178 -0
  37. package/dist/src/doctor/config-scan.js.map +1 -0
  38. package/dist/src/doctor/run.d.ts +4 -0
  39. package/dist/src/doctor/run.d.ts.map +1 -0
  40. package/dist/src/doctor/run.js +29 -0
  41. package/dist/src/doctor/run.js.map +1 -0
  42. package/dist/src/doctor/types.d.ts +60 -0
  43. package/dist/src/doctor/types.d.ts.map +1 -0
  44. package/dist/src/doctor/types.js +18 -0
  45. package/dist/src/doctor/types.js.map +1 -0
  46. package/dist/src/doctor.d.ts +9 -0
  47. package/dist/src/doctor.d.ts.map +1 -0
  48. package/dist/src/doctor.js +12 -0
  49. package/dist/src/doctor.js.map +1 -0
  50. package/dist/src/git.d.ts +38 -0
  51. package/dist/src/git.d.ts.map +1 -0
  52. package/dist/src/git.js +43 -0
  53. package/dist/src/git.js.map +1 -0
  54. package/dist/src/index.d.ts +3 -0
  55. package/dist/src/index.d.ts.map +1 -0
  56. package/dist/src/index.js +7 -0
  57. package/dist/src/index.js.map +1 -0
  58. package/dist/src/integration.d.ts +6 -0
  59. package/dist/src/integration.d.ts.map +1 -0
  60. package/dist/src/integration.js +532 -0
  61. package/dist/src/integration.js.map +1 -0
  62. package/dist/src/message-of.d.ts +3 -0
  63. package/dist/src/message-of.d.ts.map +1 -0
  64. package/dist/src/message-of.js +5 -0
  65. package/dist/src/message-of.js.map +1 -0
  66. package/dist/src/node.d.ts +3 -0
  67. package/dist/src/node.d.ts.map +1 -0
  68. package/dist/src/node.js +4 -0
  69. package/dist/src/node.js.map +1 -0
  70. package/dist/src/package-version.d.ts +7 -0
  71. package/dist/src/package-version.d.ts.map +1 -0
  72. package/dist/src/package-version.js +17 -0
  73. package/dist/src/package-version.js.map +1 -0
  74. package/dist/src/preview/_dispatch.d.ts +52 -0
  75. package/dist/src/preview/_dispatch.d.ts.map +1 -0
  76. package/dist/src/preview/_dispatch.js +70 -0
  77. package/dist/src/preview/_dispatch.js.map +1 -0
  78. package/dist/src/preview/_resolve-opts.d.ts +28 -0
  79. package/dist/src/preview/_resolve-opts.d.ts.map +1 -0
  80. package/dist/src/preview/_resolve-opts.js +70 -0
  81. package/dist/src/preview/_resolve-opts.js.map +1 -0
  82. package/dist/src/preview/banner.d.ts +29 -0
  83. package/dist/src/preview/banner.d.ts.map +1 -0
  84. package/dist/src/preview/banner.js +94 -0
  85. package/dist/src/preview/banner.js.map +1 -0
  86. package/dist/src/preview/content-api.d.ts +110 -0
  87. package/dist/src/preview/content-api.d.ts.map +1 -0
  88. package/dist/src/preview/content-api.js +216 -0
  89. package/dist/src/preview/content-api.js.map +1 -0
  90. package/dist/src/preview/cookie.d.ts +36 -0
  91. package/dist/src/preview/cookie.d.ts.map +1 -0
  92. package/dist/src/preview/cookie.js +59 -0
  93. package/dist/src/preview/cookie.js.map +1 -0
  94. package/dist/src/preview/headers.d.ts +25 -0
  95. package/dist/src/preview/headers.d.ts.map +1 -0
  96. package/dist/src/preview/headers.js +31 -0
  97. package/dist/src/preview/headers.js.map +1 -0
  98. package/dist/src/preview/landing.d.ts +22 -0
  99. package/dist/src/preview/landing.d.ts.map +1 -0
  100. package/dist/src/preview/landing.js +70 -0
  101. package/dist/src/preview/landing.js.map +1 -0
  102. package/dist/src/preview/middleware.d.ts +112 -0
  103. package/dist/src/preview/middleware.d.ts.map +1 -0
  104. package/dist/src/preview/middleware.js +197 -0
  105. package/dist/src/preview/middleware.js.map +1 -0
  106. package/dist/src/preview/preview-path.d.ts +21 -0
  107. package/dist/src/preview/preview-path.d.ts.map +1 -0
  108. package/dist/src/preview/preview-path.js +29 -0
  109. package/dist/src/preview/preview-path.js.map +1 -0
  110. package/dist/src/preview/satteri-workerd.d.ts +17 -0
  111. package/dist/src/preview/satteri-workerd.d.ts.map +1 -0
  112. package/dist/src/preview/satteri-workerd.js +18 -0
  113. package/dist/src/preview/satteri-workerd.js.map +1 -0
  114. package/dist/src/preview/site-middleware.d.ts +69 -0
  115. package/dist/src/preview/site-middleware.d.ts.map +1 -0
  116. package/dist/src/preview/site-middleware.js +159 -0
  117. package/dist/src/preview/site-middleware.js.map +1 -0
  118. package/dist/src/preview/workerd-shim.d.ts +19 -0
  119. package/dist/src/preview/workerd-shim.d.ts.map +1 -0
  120. package/dist/src/preview/workerd-shim.js +33 -0
  121. package/dist/src/preview/workerd-shim.js.map +1 -0
  122. package/dist/src/preview-build.d.ts +71 -0
  123. package/dist/src/preview-build.d.ts.map +1 -0
  124. package/dist/src/preview-build.js +115 -0
  125. package/dist/src/preview-build.js.map +1 -0
  126. package/dist/src/preview.d.ts +6 -0
  127. package/dist/src/preview.d.ts.map +1 -0
  128. package/dist/src/preview.js +12 -0
  129. package/dist/src/preview.js.map +1 -0
  130. package/dist/src/register.d.ts +98 -0
  131. package/dist/src/register.d.ts.map +1 -0
  132. package/dist/src/register.js +103 -0
  133. package/dist/src/register.js.map +1 -0
  134. package/dist/src/repository.d.ts +18 -0
  135. package/dist/src/repository.d.ts.map +1 -0
  136. package/dist/src/repository.js +46 -0
  137. package/dist/src/repository.js.map +1 -0
  138. package/dist/src/site-id.d.ts +18 -0
  139. package/dist/src/site-id.d.ts.map +1 -0
  140. package/dist/src/site-id.js +70 -0
  141. package/dist/src/site-id.js.map +1 -0
  142. package/dist/src/site-middleware.d.ts +18 -0
  143. package/dist/src/site-middleware.d.ts.map +1 -0
  144. package/dist/src/site-middleware.js +47 -0
  145. package/dist/src/site-middleware.js.map +1 -0
  146. package/dist/src/skill.d.ts +26 -0
  147. package/dist/src/skill.d.ts.map +1 -0
  148. package/dist/src/skill.js +67 -0
  149. package/dist/src/skill.js.map +1 -0
  150. package/dist/src/types.d.ts +125 -0
  151. package/dist/src/types.d.ts.map +1 -0
  152. package/dist/src/types.js +2 -0
  153. package/dist/src/types.js.map +1 -0
  154. package/package.json +170 -5
  155. package/src/build-commit.ts +22 -0
  156. package/src/bundle.ts +563 -0
  157. package/src/component-manifest.ts +75 -0
  158. package/src/constants.ts +28 -0
  159. package/src/descriptors.ts +92 -0
  160. package/src/discovery.ts +183 -0
  161. package/src/doctor/checks-live.ts +324 -0
  162. package/src/doctor/checks-offline.ts +325 -0
  163. package/src/doctor/config-scan.ts +205 -0
  164. package/src/doctor/run.ts +39 -0
  165. package/src/doctor/types.ts +71 -0
  166. package/src/doctor.ts +14 -0
  167. package/src/git.ts +66 -0
  168. package/src/index.ts +8 -0
  169. package/src/integration.ts +595 -0
  170. package/src/message-of.ts +4 -0
  171. package/src/node.ts +5 -0
  172. package/src/package-version.ts +16 -0
  173. package/src/preview/_dispatch.ts +101 -0
  174. package/src/preview/_resolve-opts.ts +84 -0
  175. package/src/preview/banner.ts +96 -0
  176. package/src/preview/content-api.ts +293 -0
  177. package/src/preview/cookie.ts +54 -0
  178. package/src/preview/headers.ts +30 -0
  179. package/src/preview/landing.ts +73 -0
  180. package/src/preview/middleware.ts +303 -0
  181. package/src/preview/preview-path.ts +28 -0
  182. package/src/preview/satteri-workerd.ts +24 -0
  183. package/src/preview/site-middleware.ts +209 -0
  184. package/src/preview/workerd-shim.ts +31 -0
  185. package/src/preview-build.ts +147 -0
  186. package/src/preview-handler.astro +55 -0
  187. package/src/preview-handler.d.ts +6 -0
  188. package/src/preview.ts +22 -0
  189. package/src/register.ts +180 -0
  190. package/src/repository.ts +54 -0
  191. package/src/site-id.ts +86 -0
  192. package/src/site-middleware.ts +55 -0
  193. package/src/skill/SKILL.md +69 -0
  194. package/src/skill.ts +66 -0
  195. package/src/types.ts +127 -0
  196. package/src/virtual-modules.d.ts +51 -0
@@ -0,0 +1,197 @@
1
+ import { createRemoteJWKSet, customFetch, jwtVerify } from 'jose';
2
+ import { parseCookies, serializePreviewCookie } from './cookie.js';
3
+ import { renderNoPreviewLanding } from './landing.js';
4
+ import { stripPreviewPrefix } from './preview-path.js';
5
+ /**
6
+ * Inspects a preview-route request and decides whether to validate, redirect,
7
+ * or render a landing page. See the docstring on {@link PreviewVerdict} for
8
+ * the caller contract.
9
+ *
10
+ * Order of checks:
11
+ * 1. `?conloca_preview_${secret}=<JWT>` query param → validate; on
12
+ * success issue a 302 with the cookie set and the query stripped.
13
+ * 2. `conloca_preview_${secret}=<JWT>` cookie → validate; on success
14
+ * return a `preview` verdict with the decoded context.
15
+ * 3. Neither present → no-preview landing page response.
16
+ *
17
+ * Invalid JWTs (tampered signature, expired, missing required claim, wrong
18
+ * issuer/audience/scope) fall through to the landing page. We deliberately do
19
+ * NOT return 401 — a 401 to an unauthenticated caller would confirm that
20
+ * a particular path is preview-only, which leaks information about what
21
+ * the branch contains.
22
+ */
23
+ export async function handlePreviewRequest(request, opts) {
24
+ const url = new URL(request.url);
25
+ const queryName = previewParamName(opts.secret);
26
+ const cookieName = previewParamName(opts.secret);
27
+ // 0. Exit route: the banner's "Exit Preview" target. The cookie is HttpOnly
28
+ // on purpose, so leaving preview needs a server response that expires it.
29
+ // Handled before the credential checks — an expired cookie must not strand
30
+ // the visitor in a preview they can no longer exit.
31
+ if (url.pathname === `/${previewParamName(opts.secret)}/__exit`) {
32
+ return { kind: 'response', response: makeExitRedirect(url, opts.secret) };
33
+ }
34
+ // 1. Query-param init path.
35
+ const queryToken = url.searchParams.get(queryName);
36
+ if (queryToken !== null) {
37
+ const ctx = await tryValidate(queryToken, opts);
38
+ if (ctx) {
39
+ return {
40
+ kind: 'response',
41
+ response: makeInitRedirect(url, queryName, queryToken, opts.secret, ctx.expiresInSeconds),
42
+ };
43
+ }
44
+ // Invalid query-param JWT — fall through to the landing page rather than
45
+ // issuing a 401 (see docstring).
46
+ return {
47
+ kind: 'response',
48
+ response: renderNoPreviewLanding({ pathname: stripPreviewPrefix(url.pathname), saasEndpoint: opts.saasEndpoint }),
49
+ };
50
+ }
51
+ // 2. Cookie path.
52
+ const cookies = parseCookies(request.headers.get('cookie'));
53
+ const cookieToken = cookies.get(cookieName);
54
+ if (cookieToken !== undefined) {
55
+ const ctx = await tryValidate(cookieToken, opts);
56
+ if (ctx) {
57
+ return { kind: 'preview', ctx };
58
+ }
59
+ }
60
+ // 3. No valid preview context.
61
+ if (opts.onMiss === 'pass-through') {
62
+ return { kind: 'pass-through' };
63
+ }
64
+ return {
65
+ kind: 'response',
66
+ // Stripped: the landing's "Go to production" link is the same path
67
+ // relative — rendered with the secret prefix it loops back to itself.
68
+ response: renderNoPreviewLanding({ pathname: stripPreviewPrefix(url.pathname), saasEndpoint: opts.saasEndpoint }),
69
+ };
70
+ }
71
+ /**
72
+ * Derives the query-param / cookie name from the per-branch secret. The
73
+ * name doubles as a load guard: a same-domain request-runtime wrapper
74
+ * checks for this exact string before invoking the SSR handler, so random
75
+ * `?conloca_preview_*` query strings cannot wake the preview path.
76
+ */
77
+ export function previewParamName(secret) {
78
+ return `conloca_preview_${secret}`;
79
+ }
80
+ /**
81
+ * Validates a JWT and projects its claims into a {@link PreviewContext}.
82
+ * Returns `null` on any validation failure — the failure reason is not
83
+ * surfaced because callers fall through to the same landing-page path
84
+ * regardless of which check failed.
85
+ */
86
+ async function tryValidate(token, opts) {
87
+ const getKey = opts.getKey ?? defaultJwksResolver(opts.saasEndpoint, opts.fetchFn);
88
+ try {
89
+ const { payload } = await jwtVerify(token, getKey, {
90
+ issuer: opts.saasIssuer,
91
+ audience: opts.saasAudience,
92
+ });
93
+ return projectContext(payload, token);
94
+ }
95
+ catch {
96
+ return null;
97
+ }
98
+ }
99
+ /**
100
+ * Lazily constructed `createRemoteJWKSet` for the given endpoint. We
101
+ * cache one resolver per endpoint string so jose's built-in cooldown +
102
+ * cache TTL apply across requests (constructing a fresh JWKS resolver on
103
+ * every request would defeat the cache). Tests inject their own resolver
104
+ * via `opts.getKey` and never hit this path.
105
+ */
106
+ const jwksCache = new Map();
107
+ function defaultJwksResolver(endpoint, fetchFn) {
108
+ // The transport is part of the cache key: the same endpoint reached through a
109
+ // Cloudflare binding and over the public internet are different resolvers.
110
+ const key = fetchFn ? `service:${endpoint}` : `public:${endpoint}`;
111
+ let resolver = jwksCache.get(key);
112
+ if (!resolver) {
113
+ resolver = createRemoteJWKSet(new URL('/.well-known/jwks.json', endpoint), fetchFn ? { [customFetch]: fetchFn } : undefined);
114
+ jwksCache.set(key, resolver);
115
+ }
116
+ return resolver;
117
+ }
118
+ /**
119
+ * Project a verified JWT payload into the preview context envelope. Returns
120
+ * `null` if any required claim is missing or wrong type — a JWT that
121
+ * validates cryptographically but lacks `branch` names no branch to read,
122
+ * so it is treated as invalid here.
123
+ */
124
+ function projectContext(payload, jwt) {
125
+ if (payload.scope !== 'preview')
126
+ return null;
127
+ const branch = readStringClaim(payload, 'branch');
128
+ const org = readStringClaim(payload, 'org');
129
+ const repo = readStringClaim(payload, 'repo');
130
+ if (!branch || !org || !repo)
131
+ return null;
132
+ // Remaining token lifetime, so the cookie can be bounded to the credential
133
+ // it carries rather than living for the whole browser session. `exp` is
134
+ // already verified by `jwtVerify` above, so it is present and in the future.
135
+ const expiresInSeconds = typeof payload.exp === 'number' ? Math.max(0, payload.exp - Math.floor(Date.now() / 1000)) : undefined;
136
+ return { branch, org, repo, jwt, expiresInSeconds };
137
+ }
138
+ function readStringClaim(payload, name) {
139
+ const value = payload[name];
140
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
141
+ }
142
+ /**
143
+ * Builds the 302 response that strips the preview query parameter from
144
+ * the requested URL and sets the preview cookie. Preserves every other
145
+ * query parameter so analytics / utm tags survive the redirect.
146
+ *
147
+ * The caller passes both the query-param name (so we know which key to
148
+ * strip) and the raw secret (so we can derive the cookie name without a
149
+ * roundtrip parse). The two are bound by `previewParamName(secret)` ===
150
+ * `queryName` at the call site.
151
+ */
152
+ /**
153
+ * Builds the 302 that expires the preview cookie and returns the visitor to
154
+ * the published page. `?to=` names the page the banner was on; only a
155
+ * same-origin absolute path is accepted (a full URL or `//host` would turn
156
+ * this route into an open redirect), anything else falls back to `/`.
157
+ */
158
+ function makeExitRedirect(url, secret) {
159
+ const to = url.searchParams.get('to') ?? '/';
160
+ const target = to.startsWith('/') && !to.startsWith('//') ? to : '/';
161
+ return new Response(null, {
162
+ status: 302,
163
+ headers: {
164
+ location: target,
165
+ 'set-cookie': serializePreviewCookie(previewParamName(secret), '', 0),
166
+ 'cache-control': 'private, no-store',
167
+ },
168
+ });
169
+ }
170
+ function makeInitRedirect(url, queryName, token, secret, maxAgeSeconds) {
171
+ const cleaned = new URL(url.toString());
172
+ cleaned.searchParams.delete(queryName);
173
+ // Land the browser on the REAL site path, not the secret-prefixed one.
174
+ // The prefix is an entry door: it exists so the endpoint has a URL to link to
175
+ // before any cookie exists. Once the cookie is set (Path=/), every
176
+ // same-origin request carries the credential, so the visitor belongs on the
177
+ // canonical path — otherwise the first internal link they click drops the
178
+ // prefix, escapes preview, and silently shows published content.
179
+ cleaned.pathname = stripPreviewPrefix(cleaned.pathname);
180
+ // Normalise the search string: when the only param was the preview JWT,
181
+ // URL.search becomes '' but URL.toString() still emits a trailing `?` only
182
+ // if it was already empty. Re-serialising via URL strips that automatically;
183
+ // double-check by explicitly clearing the `?` when no params remain.
184
+ let location = cleaned.toString();
185
+ if (cleaned.search === '' && location.endsWith('?')) {
186
+ location = location.slice(0, -1);
187
+ }
188
+ return new Response(null, {
189
+ status: 302,
190
+ headers: {
191
+ location,
192
+ 'set-cookie': serializePreviewCookie(previewParamName(secret), token, maxAgeSeconds),
193
+ 'cache-control': 'private, no-store',
194
+ },
195
+ });
196
+ }
197
+ //# sourceMappingURL=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../src/preview/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAyC,SAAS,EAAE,MAAM,MAAM,CAAC;AACzG,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AA0FvD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,OAAgB,EAAE,IAA8B;IACzF,MAAM,GAAG,GAA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C,CAAC;IACxC,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACvC,uEAAuE;IACvE,8EAA8E;IAC9E,mEAAmE;IACnE,4EAA4E;IAC5E,0EAA0E;IAC1E,iEAAiE;IACjE,OAAO,CAAC,QAAQ,GAAG,kBAAkB,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACxD,wEAAwE;IACxE,2EAA2E;IAC3E,6EAA6E;IAC7E,qEAAqE;IACrE,IAAI,QAAQ,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAClC,IAAI,OAAO,CAAC,MAAM,KAAK,EAAE,IAAI,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACpD,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE;QACxB,MAAM,EAAE,GAAG;QACX,OAAO,EAAE;YACP,QAAQ;YACR,YAAY,EAAE,sBAAsB,CAAC,gBAAgB,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,aAAa,CAAC;YACpF,eAAe,EAAE,mBAAmB;SACrC;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Strip a leading `/conloca_preview_<secret>` segment from a pathname.
3
+ *
4
+ * Lives in its own module because the entry-hop middleware, the site
5
+ * middleware and the page-status client all need it without depending on
6
+ * each other.
7
+ *
8
+ * The match is anchored at the FIRST segment only — a path like
9
+ * `/posts/conloca_preview_x/about` is left untouched, so a page that merely
10
+ * mentions the prefix mid-tree is never re-anchored to a different page.
11
+ */
12
+ export declare function stripPreviewPrefix(pathname: string): string;
13
+ /**
14
+ * Canonical path form for page-status lookups and production rewrites: no
15
+ * trailing slash, except the root itself. Browser-facing links carry trailing
16
+ * slashes ('/faq/') the canonical entry pathnames don't; the endpoint resolves
17
+ * pathnames by exact match. Mirrors astro-cms's `normalizePathname` — kept
18
+ * separate so this module stays dependency-free (see the note above).
19
+ */
20
+ export declare function normalizeTrailingSlash(pathname: string): string;
21
+ //# sourceMappingURL=preview-path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preview-path.d.ts","sourceRoot":"","sources":["../../../src/preview/preview-path.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAI3D;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAG/D"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Strip a leading `/conloca_preview_<secret>` segment from a pathname.
3
+ *
4
+ * Lives in its own module because the entry-hop middleware, the site
5
+ * middleware and the page-status client all need it without depending on
6
+ * each other.
7
+ *
8
+ * The match is anchored at the FIRST segment only — a path like
9
+ * `/posts/conloca_preview_x/about` is left untouched, so a page that merely
10
+ * mentions the prefix mid-tree is never re-anchored to a different page.
11
+ */
12
+ export function stripPreviewPrefix(pathname) {
13
+ const match = /^\/conloca_preview_[^/]+(\/.*)?$/.exec(pathname);
14
+ if (!match)
15
+ return pathname;
16
+ return match[1] ?? '/';
17
+ }
18
+ /**
19
+ * Canonical path form for page-status lookups and production rewrites: no
20
+ * trailing slash, except the root itself. Browser-facing links carry trailing
21
+ * slashes ('/faq/') the canonical entry pathnames don't; the endpoint resolves
22
+ * pathnames by exact match. Mirrors astro-cms's `normalizePathname` — kept
23
+ * separate so this module stays dependency-free (see the note above).
24
+ */
25
+ export function normalizeTrailingSlash(pathname) {
26
+ const stripped = pathname.replace(/\/+$/u, '');
27
+ return stripped === '' ? '/' : stripped;
28
+ }
29
+ //# sourceMappingURL=preview-path.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preview-path.js","sourceRoot":"","sources":["../../../src/preview/preview-path.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kBAAkB,CAAC,QAAgB;IACjD,MAAM,KAAK,GAAG,kCAAkC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAChE,IAAI,CAAC,KAAK;QAAE,OAAO,QAAQ,CAAC;IAC5B,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;AACzB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAAC,QAAgB;IACrD,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAC/C,OAAO,QAAQ,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC;AAC1C,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { fromHtml } from 'hast-util-from-html';
2
+ /** The options satteri's `htmlToHast` accepts. */
3
+ export interface HtmlToHastOptions {
4
+ fragment?: boolean;
5
+ space?: 'html' | 'svg';
6
+ }
7
+ /**
8
+ * Stands in for satteri's `htmlToHast` in the Cloudflare preview build:
9
+ * satteri's Cloudflare Workers binding can be neither installed nor run on workerd, so
10
+ * the preview build resolves bare `satteri` here instead. Only `htmlToHast`
11
+ * exists on purpose — any other satteri import then fails the build loudly
12
+ * rather than crashing at render time. SVG input is wrapped in `<svg>` because
13
+ * satteri parses it in SVG context (a self-closing `<path/>` stays a sibling),
14
+ * while `hast-util-from-html` would tokenize it as HTML and nest the elements.
15
+ */
16
+ export declare function htmlToHast(html: string, options?: HtmlToHastOptions): ReturnType<typeof fromHtml>;
17
+ //# sourceMappingURL=satteri-workerd.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"satteri-workerd.d.ts","sourceRoot":"","sources":["../../../src/preview/satteri-workerd.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,kDAAkD;AAClD,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CACxB;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAMrG"}
@@ -0,0 +1,18 @@
1
+ import { fromHtml } from 'hast-util-from-html';
2
+ /**
3
+ * Stands in for satteri's `htmlToHast` in the Cloudflare preview build:
4
+ * satteri's Cloudflare Workers binding can be neither installed nor run on workerd, so
5
+ * the preview build resolves bare `satteri` here instead. Only `htmlToHast`
6
+ * exists on purpose — any other satteri import then fails the build loudly
7
+ * rather than crashing at render time. SVG input is wrapped in `<svg>` because
8
+ * satteri parses it in SVG context (a self-closing `<path/>` stays a sibling),
9
+ * while `hast-util-from-html` would tokenize it as HTML and nest the elements.
10
+ */
11
+ export function htmlToHast(html, options = {}) {
12
+ if (options.space === 'svg') {
13
+ const [svg] = fromHtml(`<svg>${html}</svg>`, { fragment: true }).children;
14
+ return { type: 'root', children: svg?.type === 'element' ? svg.children : [] };
15
+ }
16
+ return fromHtml(html, { fragment: options.fragment, space: options.space });
17
+ }
18
+ //# sourceMappingURL=satteri-workerd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"satteri-workerd.js","sourceRoot":"","sources":["../../../src/preview/satteri-workerd.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAQ/C;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,OAAO,GAAsB,EAAE;IACtE,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK,EAAE,CAAC;QAC5B,MAAM,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,QAAQ,IAAI,QAAQ,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC;QAC1E,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IACjF,CAAC;IACD,OAAO,QAAQ,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Site-wide preview middleware for the recursive preview build — the SERVING
3
+ * SURFACE of draft preview.
4
+ *
5
+ * WHY THIS EXISTS — the secret path prefix is an entry door, not the serving
6
+ * surface. Without this, preview survives exactly one page view: the visitor
7
+ * lands on `/conloca_preview_<secret>/about`, clicks a link, and the
8
+ * root-absolute `/blog` the site's template emitted drops the prefix and
9
+ * silently serves the PUBLISHED page. The preview cookie is already `Path=/`,
10
+ * so the browser is sending the credential on that request — this listens.
11
+ *
12
+ * WHY THE RENDER HAPPENS HERE, VIA `next()` — a middleware rewrite onto the
13
+ * secret-prefixed handler (the previous design) forced the render through
14
+ * `Astro.rewrite`, whose route matching does not fall through prerendered
15
+ * routes: with two root catch-alls (the CMS pages route on demand, a docs
16
+ * integration's route prerendered) the rewrite matched the prerendered one
17
+ * and threw. Request-level routing — what `next()` continues into — DOES fall
18
+ * through a prerendered dynamic match to the first on-demand route, i.e. the
19
+ * CMS handler for CMS pathnames. So the middleware validates the credential,
20
+ * asks the endpoint for the page's render-graph status, installs the branch
21
+ * reader on `locals.contentApi`, and lets the site's own route render it;
22
+ * the response then gets the banner + robots/cache headers.
23
+ *
24
+ * Mounted only inside the recursive preview build; never in the site's
25
+ * production build. Deliberately fail-open: any request without a valid
26
+ * preview credential passes straight through to the ordinary page.
27
+ */
28
+ import type { ContentAPI } from '@conloca/content-api';
29
+ import { type PreviewContext, type PreviewMiddlewareOptions } from './middleware.js';
30
+ /** The subset of Astro's middleware context this middleware touches. */
31
+ export interface PreviewMiddlewareContext {
32
+ request: Request;
33
+ locals: {
34
+ conlocaPreview?: PreviewContext;
35
+ contentApi?: ContentAPI;
36
+ };
37
+ url: URL;
38
+ }
39
+ export type PreviewMiddlewareNext = () => Promise<Response>;
40
+ /**
41
+ * Render dependencies the binding entry supplies. A seam object (not module
42
+ * imports) because the collection-backed baseline needs `astro:content` and
43
+ * the routing config — both exist only inside the site's own build graph,
44
+ * which the entry module (`src/site-middleware.ts`) is compiled into.
45
+ */
46
+ export interface PreviewRenderSeams {
47
+ /** Lazily builds the collection-backed baseline reader. Called per preview render. */
48
+ baselineApi: () => Promise<ContentAPI>;
49
+ /** Site/locale from the routing config, threaded into the page-status call. */
50
+ site?: string;
51
+ locale?: string;
52
+ /**
53
+ * Transport for every endpoint call this middleware makes (JWKS,
54
+ * page-status, content reads). The binding entry supplies a binding's fetch
55
+ * when the Cloudflare Workers runtime exposes the endpoint binding (see
56
+ * `resolveEndpointFetch`), for deployments that cannot reach the endpoint
57
+ * with a plain fetch by hostname.
58
+ */
59
+ fetchFn?: typeof fetch;
60
+ }
61
+ /**
62
+ * Builds the Astro middleware `onRequest` handler.
63
+ *
64
+ * `resolveOpts` is a thunk so the (throwing) env-var resolution happens per
65
+ * request rather than at module load — a misconfigured runtime should fail the
66
+ * request with a diagnosable error, not prevent the whole site from booting.
67
+ */
68
+ export declare function createPreviewSiteMiddleware(resolveOpts: () => PreviewMiddlewareOptions, seams: PreviewRenderSeams): (context: PreviewMiddlewareContext, next: PreviewMiddlewareNext) => Promise<Response>;
69
+ //# sourceMappingURL=site-middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"site-middleware.d.ts","sourceRoot":"","sources":["../../../src/preview/site-middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAKvD,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,wBAAwB,EAE9B,MAAM,iBAAiB,CAAC;AAGzB,wEAAwE;AACxE,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE;QAAE,cAAc,CAAC,EAAE,cAAc,CAAC;QAAC,UAAU,CAAC,EAAE,UAAU,CAAA;KAAE,CAAC;IACrE,GAAG,EAAE,GAAG,CAAC;CACV;AAED,MAAM,MAAM,qBAAqB,GAAG,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAC;AAE5D;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC,sFAAsF;IACtF,WAAW,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IACvC,+EAA+E;IAC/E,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;CACxB;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAAC,WAAW,EAAE,MAAM,wBAAwB,EAAE,KAAK,EAAE,kBAAkB,aACvE,wBAAwB,QAAQ,qBAAqB,KAAG,OAAO,CAAC,QAAQ,CAAC,CA6GnH"}
@@ -0,0 +1,159 @@
1
+ /**
2
+ * Site-wide preview middleware for the recursive preview build — the SERVING
3
+ * SURFACE of draft preview.
4
+ *
5
+ * WHY THIS EXISTS — the secret path prefix is an entry door, not the serving
6
+ * surface. Without this, preview survives exactly one page view: the visitor
7
+ * lands on `/conloca_preview_<secret>/about`, clicks a link, and the
8
+ * root-absolute `/blog` the site's template emitted drops the prefix and
9
+ * silently serves the PUBLISHED page. The preview cookie is already `Path=/`,
10
+ * so the browser is sending the credential on that request — this listens.
11
+ *
12
+ * WHY THE RENDER HAPPENS HERE, VIA `next()` — a middleware rewrite onto the
13
+ * secret-prefixed handler (the previous design) forced the render through
14
+ * `Astro.rewrite`, whose route matching does not fall through prerendered
15
+ * routes: with two root catch-alls (the CMS pages route on demand, a docs
16
+ * integration's route prerendered) the rewrite matched the prerendered one
17
+ * and threw. Request-level routing — what `next()` continues into — DOES fall
18
+ * through a prerendered dynamic match to the first on-demand route, i.e. the
19
+ * CMS handler for CMS pathnames. So the middleware validates the credential,
20
+ * asks the endpoint for the page's render-graph status, installs the branch
21
+ * reader on `locals.contentApi`, and lets the site's own route render it;
22
+ * the response then gets the banner + robots/cache headers.
23
+ *
24
+ * Mounted only inside the recursive preview build; never in the site's
25
+ * production build. Deliberately fail-open: any request without a valid
26
+ * preview credential passes straight through to the ordinary page.
27
+ */
28
+ import { fetchPageStatus } from './_dispatch.js';
29
+ import { injectPreviewBanner } from './banner.js';
30
+ import { PreviewContentAPI } from './content-api.js';
31
+ import { applyPreviewHeaders } from './headers.js';
32
+ import { handlePreviewRequest, previewParamName, } from './middleware.js';
33
+ import { normalizeTrailingSlash, stripPreviewPrefix } from './preview-path.js';
34
+ /**
35
+ * Builds the Astro middleware `onRequest` handler.
36
+ *
37
+ * `resolveOpts` is a thunk so the (throwing) env-var resolution happens per
38
+ * request rather than at module load — a misconfigured runtime should fail the
39
+ * request with a diagnosable error, not prevent the whole site from booting.
40
+ */
41
+ export function createPreviewSiteMiddleware(resolveOpts, seams) {
42
+ return async function onRequest(context, next) {
43
+ // Already inside a preview render — don't re-run the credential check.
44
+ if (context.locals.conlocaPreview)
45
+ return next();
46
+ // Resolving the runtime env throws when preview was never configured — and
47
+ // ALSO at build time, where Astro runs middleware while prerendering pages
48
+ // and the deployer's runtime vars do not exist yet. Neither is a reason to
49
+ // break the site's build or the whole site over a feature it may
50
+ // not use, so pass through and warn once. The secret-prefixed entry route
51
+ // still fails loudly: reaching it means someone explicitly asked to preview.
52
+ let opts;
53
+ try {
54
+ opts = resolveOpts();
55
+ }
56
+ catch (error) {
57
+ warnUnconfiguredOnce(error);
58
+ return next();
59
+ }
60
+ const verdict = await handlePreviewRequest(context.request, {
61
+ ...opts,
62
+ onMiss: 'pass-through',
63
+ fetchFn: opts.fetchFn ?? seams.fetchFn,
64
+ });
65
+ if (verdict.kind === 'response')
66
+ return verdict.response;
67
+ if (verdict.kind !== 'preview')
68
+ return next();
69
+ // A valid credential on the LITERAL secret-prefixed URL: canonicalize to
70
+ // the clean site path instead of rendering under the prefix — links,
71
+ // assets and the page lookup all assume clean paths.
72
+ const rawPath = context.url.pathname;
73
+ const path = normalizeTrailingSlash(stripPreviewPrefix(rawPath));
74
+ if (stripPreviewPrefix(rawPath) !== rawPath) {
75
+ return new Response(null, {
76
+ status: 302,
77
+ headers: { location: path, 'cache-control': 'private, no-store' },
78
+ });
79
+ }
80
+ context.locals.conlocaPreview = verdict.ctx;
81
+ // An endpoint failure (5xx mid-deploy, auth envelope, network) must not 500
82
+ // the whole site for a visitor holding the HttpOnly cookie they cannot
83
+ // remove — degrade to published bytes, keeping the preview headers.
84
+ let status;
85
+ try {
86
+ status = await fetchPageStatus({
87
+ saasEndpoint: opts.saasEndpoint,
88
+ ctx: verdict.ctx,
89
+ path,
90
+ site: seams.site,
91
+ locale: seams.locale,
92
+ fetchFn: seams.fetchFn,
93
+ });
94
+ }
95
+ catch (error) {
96
+ console.warn(`[@conloca/cloud-astro] preview page-status failed for ${path} — serving the published page instead: ` +
97
+ `${error instanceof Error ? error.message : String(error)}`);
98
+ return applyPreviewHeaders(await next());
99
+ }
100
+ if (status.kind === 'unchanged') {
101
+ // Production-equivalent bytes: render the ordinary route against the
102
+ // build's own collections. No banner — same delegated semantics as
103
+ // before — but preview responses always carry the robots/cache headers.
104
+ return applyPreviewHeaders(await next());
105
+ }
106
+ if (status.kind === 'redirect') {
107
+ // Always 302 (fetchPageStatus already downgraded 301) so a stale preview
108
+ // redirect can never be cached by intermediaries.
109
+ return new Response(null, {
110
+ status: 302,
111
+ headers: { location: status.target, 'cache-control': 'private, no-store' },
112
+ });
113
+ }
114
+ // 'changed' and 'deleted' both render through the site's own route with
115
+ // the branch reader installed. For 'deleted' the branch listing no longer
116
+ // contains the page, so the site's own not-found flow runs (astro-cms:
117
+ // PAGE_NOT_FOUND → the configured fallback), which is the truthful render
118
+ // of a branch that removed the page.
119
+ const previewApi = new PreviewContentAPI({
120
+ saasEndpoint: opts.saasEndpoint,
121
+ ctx: verdict.ctx,
122
+ baselineApi: await seams.baselineApi(),
123
+ embeddedPayloads: status.kind === 'changed' ? status.embedded : new Map(),
124
+ fetchFn: seams.fetchFn,
125
+ });
126
+ // Prime the branch page listing before the renderer enumerates content:
127
+ // `listAllContent` is synchronous, and a page created on the branch is
128
+ // absent from the build baseline entirely.
129
+ await previewApi.loadBranchManifests();
130
+ context.locals.contentApi = previewApi;
131
+ const rendered = await next();
132
+ const contentType = rendered.headers.get('content-type') ?? '';
133
+ if (!contentType.includes('text/html')) {
134
+ // Redirects (e.g. the not-found fallback) and non-HTML responses pass
135
+ // through with the preview headers only.
136
+ return applyPreviewHeaders(rendered);
137
+ }
138
+ const html = injectPreviewBanner(await rendered.text(), verdict.ctx, opts.saasEndpoint, `/${previewParamName(opts.secret)}/__exit`);
139
+ return applyPreviewHeaders(new Response(html, { status: rendered.status, headers: rendered.headers }));
140
+ };
141
+ }
142
+ /**
143
+ * Warn once per process that the middleware is inert.
144
+ *
145
+ * Once, not per request: this runs on every request in the preview build, and
146
+ * a per-request warning would bury the site's own build/server output.
147
+ */
148
+ let warned = false;
149
+ function warnUnconfiguredOnce(error) {
150
+ if (warned)
151
+ return;
152
+ warned = true;
153
+ const detail = error instanceof Error ? error.message : String(error);
154
+ console.warn(`[@conloca/cloud-astro] draft preview is INERT — ${detail}\n` +
155
+ ' Requests are being served as the ordinary published site.\n' +
156
+ ' Draft previews on your own deployment are not available yet: keep previewBuild: false ' +
157
+ '(see the cloud-astro README "Draft previews").');
158
+ }
159
+ //# sourceMappingURL=site-middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"site-middleware.js","sourceRoot":"","sources":["../../../src/preview/site-middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAGH,OAAO,EAAE,eAAe,EAAmB,MAAM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AAgB,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAC3C,CAAC;QACF,OAAO,mBAAmB,CAAC,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IACzG,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,IAAI,MAAM,GAAG,KAAK,CAAC;AACnB,SAAS,oBAAoB,CAAC,KAAc;IAC1C,IAAI,MAAM;QAAE,OAAO;IACnB,MAAM,GAAG,IAAI,CAAC;IACd,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACtE,OAAO,CAAC,IAAI,CACV,mDAAmD,MAAM,IAAI;QAC3D,+DAA+D;QAC/D,0FAA0F;QAC1F,gDAAgD,CACnD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Strips the `[object process]` masquerade from workerd's nodejs_compat
3
+ * `process` shim.
4
+ *
5
+ * Astro picks its response-body strategy with
6
+ * `Object.prototype.toString.call(process) === '[object process]'`
7
+ * (astro/dist/runtime/server/render/util.js). Under nodejs_compat at some
8
+ * compatibility dates (observed broken at 2026-01-01, fixed by 2026-03-15)
9
+ * workerd tags its emulated process exactly like Node's, so Astro takes the
10
+ * Node branch and returns a bare async-iterable as the body — which the
11
+ * Cloudflare Workers `Response` cannot consume and coerces to the literal string
12
+ * "[object Object]" for every on-demand page.
13
+ *
14
+ * The Node branch is ALWAYS wrong on workerd, so the shim removes the tag —
15
+ * on workerd only (identified by its fixed `navigator.userAgent`), before any
16
+ * render runs. On real Node the tag is native and stays.
17
+ */
18
+ export declare function untagWorkerdProcess(proc?: unknown, userAgent?: string | undefined): boolean;
19
+ //# sourceMappingURL=workerd-shim.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerd-shim.d.ts","sourceRoot":"","sources":["../../../src/preview/workerd-shim.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,GAAE,OAAuD,EAC7D,SAAS,GAAE,MAAM,GAAG,SAAuF,GAC1G,OAAO,CAUT"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Strips the `[object process]` masquerade from workerd's nodejs_compat
3
+ * `process` shim.
4
+ *
5
+ * Astro picks its response-body strategy with
6
+ * `Object.prototype.toString.call(process) === '[object process]'`
7
+ * (astro/dist/runtime/server/render/util.js). Under nodejs_compat at some
8
+ * compatibility dates (observed broken at 2026-01-01, fixed by 2026-03-15)
9
+ * workerd tags its emulated process exactly like Node's, so Astro takes the
10
+ * Node branch and returns a bare async-iterable as the body — which the
11
+ * Cloudflare Workers `Response` cannot consume and coerces to the literal string
12
+ * "[object Object]" for every on-demand page.
13
+ *
14
+ * The Node branch is ALWAYS wrong on workerd, so the shim removes the tag —
15
+ * on workerd only (identified by its fixed `navigator.userAgent`), before any
16
+ * render runs. On real Node the tag is native and stays.
17
+ */
18
+ export function untagWorkerdProcess(proc = globalThis.process, userAgent = globalThis.navigator?.userAgent) {
19
+ if (userAgent !== 'Cloudflare-Workers')
20
+ return false;
21
+ if (typeof proc !== 'object' || proc === null)
22
+ return false;
23
+ if (Object.prototype.toString.call(proc) !== '[object process]')
24
+ return false;
25
+ try {
26
+ delete proc[Symbol.toStringTag];
27
+ }
28
+ catch {
29
+ return false;
30
+ }
31
+ return Object.prototype.toString.call(proc) !== '[object process]';
32
+ }
33
+ //# sourceMappingURL=workerd-shim.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workerd-shim.js","sourceRoot":"","sources":["../../../src/preview/workerd-shim.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,mBAAmB,CACjC,IAAI,GAAa,UAAoC,CAAC,OAAO,EAC7D,SAAS,GAAwB,UAAqD,CAAC,SAAS,EAAE,SAAS;IAE3G,IAAI,SAAS,KAAK,oBAAoB;QAAE,OAAO,KAAK,CAAC;IACrD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IAC5D,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,kBAAkB;QAAE,OAAO,KAAK,CAAC;IAC9E,IAAI,CAAC;QACH,OAAQ,IAAqC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACpE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,kBAAkB,CAAC;AACrE,CAAC"}