@astrale-os/adapter-cloudflare 0.5.0-beta.12 → 0.5.0-beta.13

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.
@@ -259,20 +259,35 @@ async function frontendAsset<S extends schema.DomainSchema>(
259
259
  ): Promise<Response | undefined> {
260
260
  const frontend = definition.frontend
261
261
  if (frontend === undefined || frontend.source.kind === 'external') return undefined
262
+ if (isReservedServicePath(url.pathname)) return undefined
263
+ if (request.method !== 'GET' && request.method !== 'HEAD') return undefined
264
+ const dev = environment.FRONTEND_DEV_URL?.replace(/\/+$/u, '')
265
+ if (dev !== undefined && dev.length > 0) {
266
+ return fetch(new Request(new URL(`${url.pathname}${url.search}`, `${dev}/`), request))
267
+ }
262
268
  const routes = frontend.routes as Readonly<Record<string, FrontendRoute>>
263
- const route = Object.values(routes).find((candidate) => candidate.path === url.pathname)
269
+ const spa = frontend.source.kind === 'vite' && frontend.source.spa
270
+ const route = Object.values(routes)
271
+ .filter(
272
+ (candidate) =>
273
+ candidate.path === url.pathname ||
274
+ (spa && (candidate.path === '/' || url.pathname.startsWith(`${candidate.path}/`))),
275
+ )
276
+ .sort((left, right) => right.path.length - left.path.length)[0]
264
277
  const staticAsset = url.pathname.startsWith('/assets/')
265
278
  if (route === undefined && !staticAsset) return undefined
266
- if (request.method !== 'GET' && request.method !== 'HEAD') return undefined
267
279
  const binding = environment.ASSETS
268
280
  if (binding === undefined) return undefined
269
- const dev = environment.FRONTEND_DEV_URL?.replace(/\/+$/u, '')
270
- if (dev !== undefined && dev.length > 0) {
271
- return fetch(new Request(`${dev}${url.pathname}${url.search}`, request))
272
- }
273
- const path =
274
- route === undefined ? `${url.pathname}${url.search}` : `/${route.document}${url.search}`
275
- return binding.fetch(new Request(new URL(path, url.origin), request))
281
+ const asset = await binding.fetch(request)
282
+ if (asset.status !== 404 || route === undefined) return asset
283
+ await asset.body?.cancel()
284
+ return binding.fetch(new Request(new URL(`/${route.document}${url.search}`, url.origin), request))
285
+ }
286
+
287
+ function isReservedServicePath(path: string): boolean {
288
+ return ['/invoke', '/install', '/health', '/meta', '/.well-known'].some(
289
+ (reserved) => path === reserved || path.startsWith(`${reserved}/`),
290
+ )
276
291
  }
277
292
 
278
293
  function requestOrigin(url: URL, request: Request): string {