@harlan-zw/nuxt-cloudflare 0.5.2 → 0.5.3
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.
- package/dist/module.d.mts +1 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +22 -2
- package/dist/runtime/server/plugins/request-lifetime.d.ts +1 -3
- package/dist/runtime/server/plugins/request-lifetime.js +4 -2
- package/dist/runtime/server/utils/request-lifetime.d.ts +7 -0
- package/dist/runtime/server/utils/request-lifetime.js +7 -0
- package/package.json +1 -1
package/dist/module.d.mts
CHANGED
|
@@ -50,6 +50,7 @@ interface NitroCloudflareShape {
|
|
|
50
50
|
}
|
|
51
51
|
declare function findPopulatedRuntimeSecretPaths(config: unknown, environment: Readonly<Record<string, string | undefined>>): string[];
|
|
52
52
|
interface NitroCloudflareContext {
|
|
53
|
+
nitroVersion?: number;
|
|
53
54
|
/** The project directory Wrangler and Nitro read the authored config from. */
|
|
54
55
|
rootDir?: string;
|
|
55
56
|
/** Whether Nuxt emits server source maps. */
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import process from 'node:process';
|
|
2
|
-
import { useLogger, defineNuxtModule, createResolver, resolveServerVariant, hasNuxtModule, addTypeTemplate } from '@nuxt/kit';
|
|
2
|
+
import { useLogger, defineNuxtModule, createResolver, resolveServerVariant, hasNuxtModule, addTypeTemplate, getNitroVersion } from '@nuxt/kit';
|
|
3
3
|
import { resolve } from 'pathe';
|
|
4
4
|
import { a as diagnoseWranglerSourceConfigs, d as discoverWranglerSourceConfigs, b as diagnoseStaticAssetRules, c as readStaticAssetRuleFiles, f as resolveBuildStaticAssetDirectory, e as evaluateWranglerDiagnostics } from './shared/nuxt-cloudflare.Drgx8RDh.mjs';
|
|
5
5
|
import { resolveHtmlCacheGuarantee, staleDirectivesAreDisabled } from '../dist/runtime/server/utils/workers-cache.js';
|
|
@@ -158,10 +158,21 @@ function addNitroPlugin(nitro, path) {
|
|
|
158
158
|
if (!nitro.plugins.includes(path))
|
|
159
159
|
nitro.plugins.push(path);
|
|
160
160
|
}
|
|
161
|
+
function leaveAuthoredCompatibilityFlags(config, authored) {
|
|
162
|
+
const authoredFlags = new Set(authored.compatibility_flags);
|
|
163
|
+
if (config.compatibility_flags)
|
|
164
|
+
config.compatibility_flags = config.compatibility_flags.filter((flag) => !authoredFlags.has(flag));
|
|
165
|
+
for (const [name, environment] of Object.entries(config.env ?? {})) {
|
|
166
|
+
const authoredEnvironment = authored.env?.[name];
|
|
167
|
+
if (authoredEnvironment)
|
|
168
|
+
leaveAuthoredCompatibilityFlags(environment, authoredEnvironment);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
161
171
|
function configureNitroCloudflare(nitro, options, context = {}) {
|
|
162
172
|
const authored = readAuthoredWranglerConfig(context.rootDir);
|
|
163
173
|
if (authored._tag === "invalid")
|
|
164
174
|
throw new Error(`[nuxt-cloudflare] Wrangler config "${authored.path}" cannot be parsed: ${authored.reason}`);
|
|
175
|
+
const authoredConfig = authored._tag === "authored" ? authored.config : {};
|
|
165
176
|
nitro.preset ??= "cloudflare-module";
|
|
166
177
|
nitro.cloudflare ??= {};
|
|
167
178
|
nitro.cloudflare.deployConfig = true;
|
|
@@ -175,7 +186,9 @@ function configureNitroCloudflare(nitro, options, context = {}) {
|
|
|
175
186
|
uploadSourceMaps: options.sourceMaps ?? nitro.sourceMap ?? context.serverSourceMaps ?? false,
|
|
176
187
|
versionMetadataBinding: options.versionMetadataBinding,
|
|
177
188
|
workersCache: resolveModuleWorkersCachePolicy(options)
|
|
178
|
-
},
|
|
189
|
+
}, authoredConfig);
|
|
190
|
+
if (context.nitroVersion === 3)
|
|
191
|
+
leaveAuthoredCompatibilityFlags(nitro.cloudflare.wrangler, authoredConfig);
|
|
179
192
|
addNitroPlugin(nitro, runtimeConfigPluginPath);
|
|
180
193
|
addNitroPlugin(nitro, requestLifetimePluginPath);
|
|
181
194
|
if (nitro.cloudflare.wrangler.cache?.enabled)
|
|
@@ -273,6 +286,7 @@ function setupCloudflareModule(options, nuxt) {
|
|
|
273
286
|
nuxt.options.nitro,
|
|
274
287
|
options,
|
|
275
288
|
{
|
|
289
|
+
nitroVersion: getNitroVersion(nuxt),
|
|
276
290
|
rootDir: nuxt.options.rootDir,
|
|
277
291
|
serverSourceMaps: Boolean(nuxt.options.sourcemap.server)
|
|
278
292
|
}
|
|
@@ -368,6 +382,12 @@ A chunk-retention guarantee does not cover these. A prerendered route is served
|
|
|
368
382
|
nuxt.hook("nitro:init", (nitro) => {
|
|
369
383
|
if (!nitro.options.preset?.includes("cloudflare"))
|
|
370
384
|
return;
|
|
385
|
+
nitro.hooks.hook("prerender:init", (prerenderer) => {
|
|
386
|
+
const { storage, devStorage } = prerenderer.options;
|
|
387
|
+
const mounts = devStorage?.cache ? devStorage : storage;
|
|
388
|
+
if (mounts?.cache?.driver === cacheDriverPath)
|
|
389
|
+
mounts.cache = { driver: "memory" };
|
|
390
|
+
});
|
|
371
391
|
nitro.hooks.hook("compiled", () => auditGeneratedWranglerConfig(
|
|
372
392
|
nitro,
|
|
373
393
|
options,
|
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
* Keeps each Worker request alive until its response settles, so a visitor who
|
|
3
3
|
* leaves cannot strand a promise that later requests share.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* wrapper must replace it on the app itself. An app without `localFetch`, such
|
|
7
|
-
* as Nitro 3, is left unchanged.
|
|
5
|
+
* The Cloudflare entry calls the app's `fetch` on Nitro 3 and `localFetch` on Nitro 2.
|
|
8
6
|
*/
|
|
9
7
|
declare const _default: any;
|
|
10
8
|
export default _default;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { defineNitroAppPlugin } from "#nuxt-cloudflare/nitro";
|
|
2
|
-
import { keepRequestAlive } from "../utils/request-lifetime.js";
|
|
2
|
+
import { keepNativeRequestAlive, keepRequestAlive } from "../utils/request-lifetime.js";
|
|
3
3
|
export default defineNitroAppPlugin((nitroApp) => {
|
|
4
4
|
const app = nitroApp;
|
|
5
|
-
if (typeof app.
|
|
5
|
+
if (typeof app.fetch === "function")
|
|
6
|
+
app.fetch = keepNativeRequestAlive(app.fetch.bind(app));
|
|
7
|
+
else if (typeof app.localFetch === "function")
|
|
6
8
|
app.localFetch = keepRequestAlive(app.localFetch);
|
|
7
9
|
});
|
|
@@ -6,6 +6,11 @@ export interface LocalFetchInit {
|
|
|
6
6
|
}
|
|
7
7
|
/** Nitro 2's in-process fetch, `nitroApp.localFetch`. */
|
|
8
8
|
export type LocalFetch = (input: string, init?: LocalFetchInit) => Promise<Response>;
|
|
9
|
+
interface WorkerRequest extends Request {
|
|
10
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
11
|
+
}
|
|
12
|
+
/** Nitro 3's in-process fetch. The Cloudflare entry attaches `waitUntil` to the request. */
|
|
13
|
+
export type NativeFetch = (request: WorkerRequest) => Response | Promise<Response>;
|
|
9
14
|
/**
|
|
10
15
|
* Wraps Nitro's `localFetch` so each Worker request stays alive until its own
|
|
11
16
|
* response settles.
|
|
@@ -21,3 +26,5 @@ export type LocalFetch = (input: string, init?: LocalFetchInit) => Promise<Respo
|
|
|
21
26
|
* request, passes through unchanged.
|
|
22
27
|
*/
|
|
23
28
|
export declare function keepRequestAlive(localFetch: LocalFetch): LocalFetch;
|
|
29
|
+
export declare function keepNativeRequestAlive(fetch: NativeFetch): NativeFetch;
|
|
30
|
+
export {};
|
|
@@ -5,3 +5,10 @@ export function keepRequestAlive(localFetch) {
|
|
|
5
5
|
return response;
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
|
+
export function keepNativeRequestAlive(fetch) {
|
|
9
|
+
return (request) => {
|
|
10
|
+
const response = fetch(request);
|
|
11
|
+
request.waitUntil?.(Promise.resolve(response).then(() => void 0, () => void 0));
|
|
12
|
+
return response;
|
|
13
|
+
};
|
|
14
|
+
}
|