|
| 1 | +// node_modules/@opennextjs/cloudflare/dist/api/cloudflare-context.js |
| 2 | +var cloudflareContextSymbol = Symbol.for("__cloudflare-context__"); |
| 3 | +function getCloudflareContext(options = { async: false }) { |
| 4 | + return options.async ? getCloudflareContextAsync() : getCloudflareContextSync(); |
| 5 | +} |
| 6 | +function getCloudflareContextFromGlobalScope() { |
| 7 | + const global = globalThis; |
| 8 | + return global[cloudflareContextSymbol]; |
| 9 | +} |
| 10 | +function inSSG() { |
| 11 | + const global = globalThis; |
| 12 | + return global.__NEXT_DATA__?.nextExport === true; |
| 13 | +} |
| 14 | +function getCloudflareContextSync() { |
| 15 | + const cloudflareContext = getCloudflareContextFromGlobalScope(); |
| 16 | + if (cloudflareContext) { |
| 17 | + return cloudflareContext; |
| 18 | + } |
| 19 | + if (inSSG()) { |
| 20 | + throw new Error(` |
| 21 | +
|
| 22 | +ERROR: \`getCloudflareContext\` has been called in sync mode in either a static route or at the top level of a non-static one, both cases are not allowed but can be solved by either: |
| 23 | + - make sure that the call is not at the top level and that the route is not static |
| 24 | + - call \`getCloudflareContext({async: true})\` to use the \`async\` mode |
| 25 | + - avoid calling \`getCloudflareContext\` in the route |
| 26 | +`); |
| 27 | + } |
| 28 | + throw new Error(initOpenNextCloudflareForDevErrorMsg); |
| 29 | +} |
| 30 | +async function getCloudflareContextAsync() { |
| 31 | + const cloudflareContext = getCloudflareContextFromGlobalScope(); |
| 32 | + if (cloudflareContext) { |
| 33 | + return cloudflareContext; |
| 34 | + } |
| 35 | + const inNodejsRuntime = process.env.NEXT_RUNTIME === "nodejs"; |
| 36 | + if (inNodejsRuntime || inSSG()) { |
| 37 | + const cloudflareContext2 = await getCloudflareContextFromWrangler(); |
| 38 | + addCloudflareContextToNodejsGlobal(cloudflareContext2); |
| 39 | + return cloudflareContext2; |
| 40 | + } |
| 41 | + throw new Error(initOpenNextCloudflareForDevErrorMsg); |
| 42 | +} |
| 43 | +function addCloudflareContextToNodejsGlobal(cloudflareContext) { |
| 44 | + const global = globalThis; |
| 45 | + global[cloudflareContextSymbol] = cloudflareContext; |
| 46 | +} |
| 47 | +async function getCloudflareContextFromWrangler(options) { |
| 48 | + const { getPlatformProxy } = await import( |
| 49 | + /* webpackIgnore: true */ |
| 50 | + `${"__wrangler".replaceAll("_", "")}` |
| 51 | + ); |
| 52 | + const environment = options?.environment ?? process.env.NEXT_DEV_WRANGLER_ENV; |
| 53 | + const { env, cf, ctx } = await getPlatformProxy({ |
| 54 | + ...options, |
| 55 | + environment |
| 56 | + }); |
| 57 | + return { |
| 58 | + env, |
| 59 | + cf, |
| 60 | + ctx |
| 61 | + }; |
| 62 | +} |
| 63 | +var initOpenNextCloudflareForDevErrorMsg = ` |
| 64 | +
|
| 65 | +ERROR: \`getCloudflareContext\` has been called without having called \`initOpenNextCloudflareForDev\` from the Next.js config file. |
| 66 | +You should update your Next.js config file as shown below: |
| 67 | +
|
| 68 | + \`\`\` |
| 69 | + // next.config.mjs |
| 70 | +
|
| 71 | + import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare"; |
| 72 | +
|
| 73 | + initOpenNextCloudflareForDev(); |
| 74 | +
|
| 75 | + const nextConfig = { ... }; |
| 76 | + export default nextConfig; |
| 77 | + \`\`\` |
| 78 | +
|
| 79 | +`; |
| 80 | + |
| 81 | +// node_modules/@opennextjs/cloudflare/dist/api/overrides/asset-resolver/index.js |
| 82 | +var resolver = { |
| 83 | + name: "cloudflare-asset-resolver", |
| 84 | + async maybeGetAssetResult(event) { |
| 85 | + const { ASSETS } = getCloudflareContext().env; |
| 86 | + if (!ASSETS || !isUserWorkerFirst(globalThis.__ASSETS_RUN_WORKER_FIRST__, event.rawPath)) { |
| 87 | + return void 0; |
| 88 | + } |
| 89 | + const { method, headers } = event; |
| 90 | + if (method !== "GET" && method != "HEAD") { |
| 91 | + return void 0; |
| 92 | + } |
| 93 | + const url = new URL(event.rawPath, "https://assets.local"); |
| 94 | + const response = await ASSETS.fetch(url, { |
| 95 | + headers, |
| 96 | + method |
| 97 | + }); |
| 98 | + if (response.status === 404) { |
| 99 | + await response.body?.cancel(); |
| 100 | + return void 0; |
| 101 | + } |
| 102 | + return { |
| 103 | + type: "core", |
| 104 | + statusCode: response.status, |
| 105 | + headers: Object.fromEntries(response.headers.entries()), |
| 106 | + // Workers and Node types differ. |
| 107 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 108 | + body: response.body || new ReadableStream(), |
| 109 | + isBase64Encoded: false |
| 110 | + }; |
| 111 | + } |
| 112 | +}; |
| 113 | +function isUserWorkerFirst(runWorkerFirst, pathname) { |
| 114 | + if (!Array.isArray(runWorkerFirst)) { |
| 115 | + return runWorkerFirst ?? false; |
| 116 | + } |
| 117 | + let hasPositiveMatch = false; |
| 118 | + for (let rule of runWorkerFirst) { |
| 119 | + let isPositiveRule = true; |
| 120 | + if (rule.startsWith("!")) { |
| 121 | + rule = rule.slice(1); |
| 122 | + isPositiveRule = false; |
| 123 | + } else if (hasPositiveMatch) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + const match = new RegExp(`^${rule.replace(/([[\]().*+?^$|{}\\])/g, "\\$1").replace("\\*", ".*")}$`).test(pathname); |
| 127 | + if (match) { |
| 128 | + if (isPositiveRule) { |
| 129 | + hasPositiveMatch = true; |
| 130 | + } else { |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return hasPositiveMatch; |
| 136 | +} |
| 137 | +var asset_resolver_default = resolver; |
| 138 | + |
| 139 | +// node_modules/@opennextjs/cloudflare/dist/api/config.js |
| 140 | +function defineCloudflareConfig(config = {}) { |
| 141 | + const { incrementalCache, tagCache, queue, cachePurge, enableCacheInterception = false, routePreloadingBehavior = "none" } = config; |
| 142 | + return { |
| 143 | + default: { |
| 144 | + override: { |
| 145 | + wrapper: "cloudflare-node", |
| 146 | + converter: "edge", |
| 147 | + proxyExternalRequest: "fetch", |
| 148 | + incrementalCache: resolveIncrementalCache(incrementalCache), |
| 149 | + tagCache: resolveTagCache(tagCache), |
| 150 | + queue: resolveQueue(queue), |
| 151 | + cdnInvalidation: resolveCdnInvalidation(cachePurge) |
| 152 | + }, |
| 153 | + routePreloadingBehavior |
| 154 | + }, |
| 155 | + // node:crypto is used to compute cache keys |
| 156 | + edgeExternals: ["node:crypto"], |
| 157 | + cloudflare: { |
| 158 | + useWorkerdCondition: true |
| 159 | + }, |
| 160 | + dangerous: { |
| 161 | + enableCacheInterception |
| 162 | + }, |
| 163 | + middleware: { |
| 164 | + external: true, |
| 165 | + override: { |
| 166 | + wrapper: "cloudflare-edge", |
| 167 | + converter: "edge", |
| 168 | + proxyExternalRequest: "fetch", |
| 169 | + incrementalCache: resolveIncrementalCache(incrementalCache), |
| 170 | + tagCache: resolveTagCache(tagCache), |
| 171 | + queue: resolveQueue(queue) |
| 172 | + }, |
| 173 | + assetResolver: () => asset_resolver_default |
| 174 | + } |
| 175 | + }; |
| 176 | +} |
| 177 | +function resolveIncrementalCache(value = "dummy") { |
| 178 | + if (typeof value === "string") { |
| 179 | + return value; |
| 180 | + } |
| 181 | + return typeof value === "function" ? value : () => value; |
| 182 | +} |
| 183 | +function resolveTagCache(value = "dummy") { |
| 184 | + if (typeof value === "string") { |
| 185 | + return value; |
| 186 | + } |
| 187 | + return typeof value === "function" ? value : () => value; |
| 188 | +} |
| 189 | +function resolveQueue(value = "dummy") { |
| 190 | + if (typeof value === "string") { |
| 191 | + return value; |
| 192 | + } |
| 193 | + return typeof value === "function" ? value : () => value; |
| 194 | +} |
| 195 | +function resolveCdnInvalidation(value = "dummy") { |
| 196 | + if (typeof value === "string") { |
| 197 | + return value; |
| 198 | + } |
| 199 | + return typeof value === "function" ? value : () => value; |
| 200 | +} |
| 201 | + |
| 202 | +// open-next.config.ts |
| 203 | +var open_next_config_default = defineCloudflareConfig(); |
| 204 | +export { |
| 205 | + open_next_config_default as default |
| 206 | +}; |
0 commit comments