|
| 1 | +import type { InternalEvent, InternalResult } from "@opennextjs/aws/types/open-next"; |
| 2 | +import type { AssetResolver } from "@opennextjs/aws/types/overrides"; |
| 3 | + |
| 4 | +import { getCloudflareContext } from "../../cloudflare-context.js"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Serves assets when `run_worker_first` is set to true. |
| 8 | + * |
| 9 | + * When `run_worker_first` is `false`, the assets are served directly bypassing Next routing. |
| 10 | + * |
| 11 | + * When it is `true`, assets are served from the routing layer. It should be used when assets |
| 12 | + * should be behind the middleware or when skew protection is enabled. |
| 13 | + * |
| 14 | + * See https://developers.cloudflare.com/workers/static-assets/binding/#run_worker_first |
| 15 | + */ |
| 16 | +const resolver: AssetResolver = { |
| 17 | + name: "cloudflare-asset-resolver", |
| 18 | + async maybeGetAssetResult(event: InternalEvent) { |
| 19 | + const { ASSETS } = getCloudflareContext().env; |
| 20 | + |
| 21 | + if (!ASSETS || !isUserWorkerFirst(globalThis.__ASSETS_RUN_WORKER_FIRST__, event.rawPath)) { |
| 22 | + // Only handle assets when the user worker runs first for the path |
| 23 | + return undefined; |
| 24 | + } |
| 25 | + |
| 26 | + const { method, headers } = event; |
| 27 | + |
| 28 | + if (method !== "GET" && method != "HEAD") { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + |
| 32 | + const url = new URL(event.rawPath, "https://assets.local"); |
| 33 | + const response = await ASSETS.fetch(url, { |
| 34 | + headers, |
| 35 | + method, |
| 36 | + }); |
| 37 | + |
| 38 | + if (response.status === 404) { |
| 39 | + return undefined; |
| 40 | + } |
| 41 | + |
| 42 | + return { |
| 43 | + type: "core", |
| 44 | + statusCode: response.status, |
| 45 | + headers: Object.fromEntries(response.headers.entries()), |
| 46 | + // Workers and Node types differ. |
| 47 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 48 | + body: response.body || (new ReadableStream() as any), |
| 49 | + isBase64Encoded: false, |
| 50 | + } satisfies InternalResult; |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * @param runWorkerFirst `run_worker_first` config |
| 56 | + * @param pathname pathname of the request |
| 57 | + * @returns Whether the user worker runs first |
| 58 | + */ |
| 59 | +export function isUserWorkerFirst(runWorkerFirst: boolean | string[] | undefined, pathname: string): boolean { |
| 60 | + if (!Array.isArray(runWorkerFirst)) { |
| 61 | + return runWorkerFirst ?? false; |
| 62 | + } |
| 63 | + |
| 64 | + let hasPositiveMatch = false; |
| 65 | + |
| 66 | + for (let rule of runWorkerFirst) { |
| 67 | + let isPositiveRule = true; |
| 68 | + |
| 69 | + if (rule.startsWith("!")) { |
| 70 | + rule = rule.slice(1); |
| 71 | + isPositiveRule = false; |
| 72 | + } else if (hasPositiveMatch) { |
| 73 | + // Do not look for more positive rules once we have a match |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + // - Escapes special characters |
| 78 | + // - Replaces * with .* |
| 79 | + const match = new RegExp(`^${rule.replace(/([[\]().*+?^$|{}\\])/g, "\\$1").replace("\\*", ".*")}$`).test( |
| 80 | + pathname |
| 81 | + ); |
| 82 | + |
| 83 | + if (match) { |
| 84 | + if (isPositiveRule) { |
| 85 | + hasPositiveMatch = true; |
| 86 | + } else { |
| 87 | + // Exit early when there is a negative match |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return hasPositiveMatch; |
| 94 | +} |
| 95 | + |
| 96 | +export default resolver; |
0 commit comments