@harlan-zw/nuxt-cloudflare 0.4.4 → 0.5.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.
- package/dist/bindings.mjs +3 -1
- package/dist/module.json +2 -2
- package/dist/module.mjs +11 -4
- package/dist/runtime/server/nitro-alias.d.ts +4 -0
- package/dist/runtime/server/nitro2.d.ts +3 -0
- package/dist/runtime/server/nitro2.js +57 -0
- package/dist/runtime/server/nitro3.d.ts +3 -0
- package/dist/runtime/server/nitro3.js +24 -0
- package/dist/runtime/server/plugins/headers.d.ts +23 -0
- package/dist/runtime/server/plugins/headers.js +26 -0
- package/dist/runtime/server/plugins/runtime-config.d.ts +1 -1
- package/dist/runtime/server/plugins/runtime-config.js +2 -2
- package/dist/runtime/server/plugins/wide-events.d.ts +5 -4
- package/dist/runtime/server/plugins/wide-events.js +6 -4
- package/dist/runtime/server/plugins/workers-cache.d.ts +1 -1
- package/dist/runtime/server/plugins/workers-cache.js +3 -3
- package/package.json +14 -9
package/dist/bindings.mjs
CHANGED
|
@@ -8,8 +8,10 @@ function envFromContext(value) {
|
|
|
8
8
|
function resolveCloudflareBindings(source) {
|
|
9
9
|
const sourceRecord = asRecord(source);
|
|
10
10
|
const sourceEnv = envFromContext(sourceRecord?.context) ?? envFromContext(sourceRecord);
|
|
11
|
+
const requestRuntime = asRecord(asRecord(sourceRecord?.req)?.runtime);
|
|
12
|
+
const runtimeEnv = asRecord(asRecord(requestRuntime?.cloudflare)?.env) ?? asRecord(asRecord(asRecord(sourceRecord?.runtime)?.cloudflare)?.env);
|
|
11
13
|
const globalEnv = asRecord(globalThis.__env__);
|
|
12
|
-
return sourceEnv ?? globalEnv;
|
|
14
|
+
return sourceEnv ?? runtimeEnv ?? globalEnv;
|
|
13
15
|
}
|
|
14
16
|
function setCloudflareBindings(env) {
|
|
15
17
|
const host = globalThis;
|
package/dist/module.json
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"name": "@harlan-zw/nuxt-cloudflare",
|
|
3
3
|
"configKey": "nuxtCloudflare",
|
|
4
4
|
"compatibility": {
|
|
5
|
-
"nuxt": "
|
|
5
|
+
"nuxt": "^4.6.0 || ^5.0.0"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.5.1",
|
|
8
8
|
"builder": {
|
|
9
9
|
"@nuxt/module-builder": "1.0.3",
|
|
10
10
|
"unbuild": "3.6.1"
|
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import process from 'node:process';
|
|
2
|
-
import { useLogger, defineNuxtModule, createResolver, hasNuxtModule, addTypeTemplate } from '@nuxt/kit';
|
|
2
|
+
import { useLogger, defineNuxtModule, createResolver, resolveServerVariant, hasNuxtModule, addTypeTemplate } 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';
|
|
@@ -300,8 +300,9 @@ function setupCloudflareModule(options, nuxt) {
|
|
|
300
300
|
return artifact.content;
|
|
301
301
|
}
|
|
302
302
|
}, { nitro: true });
|
|
303
|
-
nuxt.hook("prepare:types", ({ references }) => {
|
|
303
|
+
nuxt.hook("prepare:types", ({ references, sharedReferences }) => {
|
|
304
304
|
references.push({ path: bindingTypesTemplate.dst });
|
|
305
|
+
sharedReferences.push({ path: bindingTypesTemplate.dst });
|
|
305
306
|
});
|
|
306
307
|
}
|
|
307
308
|
configure();
|
|
@@ -376,7 +377,7 @@ const module$1 = defineNuxtModule({
|
|
|
376
377
|
meta: {
|
|
377
378
|
name: "@harlan-zw/nuxt-cloudflare",
|
|
378
379
|
configKey: "nuxtCloudflare",
|
|
379
|
-
compatibility: { nuxt: "
|
|
380
|
+
compatibility: { nuxt: "^4.6.0 || ^5.0.0" }
|
|
380
381
|
},
|
|
381
382
|
defaults: {
|
|
382
383
|
bindingTypes: true,
|
|
@@ -389,7 +390,13 @@ const module$1 = defineNuxtModule({
|
|
|
389
390
|
versionMetadataBinding: "CF_VERSION_METADATA",
|
|
390
391
|
workersCache: { _tag: "enabled", crossVersion: false }
|
|
391
392
|
},
|
|
392
|
-
setup
|
|
393
|
+
setup(options, nuxt) {
|
|
394
|
+
const resolver = createResolver(import.meta.url);
|
|
395
|
+
const serverRuntime = resolveServerVariant({ nitro2: resolver.resolve("./runtime/server/nitro2"), nitro3: resolver.resolve("./runtime/server/nitro3") });
|
|
396
|
+
nuxt.options.alias["#nuxt-cloudflare/nitro"] = serverRuntime;
|
|
397
|
+
(nuxt.options.nitro.alias ??= {})["#nuxt-cloudflare/nitro"] = serverRuntime;
|
|
398
|
+
setupCloudflareModule(options, nuxt);
|
|
399
|
+
}
|
|
393
400
|
});
|
|
394
401
|
|
|
395
402
|
export { configureNitroCloudflare, module$1 as default, findPopulatedRuntimeSecretPaths, resolveBindingTypeAudit, setupCloudflareModule };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { defineNitroPlugin as definePlugin } from "nitropack/runtime";
|
|
2
|
+
export { useRuntimeConfig } from "nitropack/runtime";
|
|
3
|
+
export function defineNitroPlugin(setup) {
|
|
4
|
+
return definePlugin((app) => {
|
|
5
|
+
const hooks = app.hooks;
|
|
6
|
+
const hook = hooks.hook.bind(hooks);
|
|
7
|
+
const adapted = Object.create(hooks);
|
|
8
|
+
adapted.hook = ((name, handler) => {
|
|
9
|
+
if (name === "beforeResponse") {
|
|
10
|
+
return hook("beforeResponse", async (event, result) => {
|
|
11
|
+
if (!(result.body instanceof Response)) {
|
|
12
|
+
await handler(event, result);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const response = result.body;
|
|
16
|
+
const headers = new Proxy(response.headers, {
|
|
17
|
+
get(target, key) {
|
|
18
|
+
if (key === "get") {
|
|
19
|
+
return (name2) => {
|
|
20
|
+
const value2 = event.node.res.getHeader(name2);
|
|
21
|
+
return target.get(name2) ?? (value2 === void 0 ? null : String(value2));
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (key === "set") {
|
|
25
|
+
return (name2, value2) => {
|
|
26
|
+
target.set(name2, value2);
|
|
27
|
+
event.node.res.setHeader(name2, value2);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const value = Reflect.get(target, key, target);
|
|
31
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const requestHeaders = new Headers();
|
|
35
|
+
for (const [name2, value] of Object.entries(event.node.req.headers)) {
|
|
36
|
+
if (value !== void 0)
|
|
37
|
+
requestHeaders.set(name2, Array.isArray(value) ? value.join(", ") : value);
|
|
38
|
+
}
|
|
39
|
+
await handler(new Proxy(event, {
|
|
40
|
+
has(target, key) {
|
|
41
|
+
return key === "res" || key === "req" || Reflect.has(target, key);
|
|
42
|
+
},
|
|
43
|
+
get(target, key) {
|
|
44
|
+
if (key === "res")
|
|
45
|
+
return { status: response.status, headers };
|
|
46
|
+
if (key === "req")
|
|
47
|
+
return { headers: requestHeaders };
|
|
48
|
+
return Reflect.get(target, key, target);
|
|
49
|
+
}
|
|
50
|
+
}), result);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return hook(name, handler);
|
|
54
|
+
});
|
|
55
|
+
return setup({ ...app, hooks: adapted });
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { definePlugin } from "nitro";
|
|
2
|
+
export { useRuntimeConfig } from "nuxt/server";
|
|
3
|
+
export function defineNitroPlugin(setup) {
|
|
4
|
+
return definePlugin((app) => {
|
|
5
|
+
const hooks = app.hooks;
|
|
6
|
+
const hook = hooks.hook.bind(hooks);
|
|
7
|
+
const adapted = Object.create(hooks);
|
|
8
|
+
adapted.hook = ((name, handler) => {
|
|
9
|
+
if (name === "beforeResponse") {
|
|
10
|
+
return hook("response", async (response, event) => {
|
|
11
|
+
await handler(new Proxy(event, {
|
|
12
|
+
get(target, key) {
|
|
13
|
+
if (key === "res")
|
|
14
|
+
return { status: response.status, headers: response.headers };
|
|
15
|
+
return Reflect.get(target, key, target);
|
|
16
|
+
}
|
|
17
|
+
}));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return hook(name, handler);
|
|
21
|
+
});
|
|
22
|
+
return setup({ ...app, hooks: adapted });
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RequestEvent } from 'nuxt/server';
|
|
2
|
+
interface NodeHeaderEvent {
|
|
3
|
+
path: string;
|
|
4
|
+
node: {
|
|
5
|
+
req: {
|
|
6
|
+
headers: Record<string, string | string[] | undefined>;
|
|
7
|
+
};
|
|
8
|
+
res: {
|
|
9
|
+
statusCode: number;
|
|
10
|
+
getHeader: (name: string) => string | string[] | number | undefined;
|
|
11
|
+
setHeader: (name: string, value: string) => unknown;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
type PortableHeaderEvent = Pick<RequestEvent, 'req' | 'res'> & {
|
|
16
|
+
path: string;
|
|
17
|
+
};
|
|
18
|
+
export type HeaderEvent = NodeHeaderEvent | PortableHeaderEvent;
|
|
19
|
+
export declare function getHeader(event: HeaderEvent, name: string): string | undefined;
|
|
20
|
+
export declare function getResponseHeader(event: HeaderEvent, name: string): string | undefined;
|
|
21
|
+
export declare function getResponseStatus(event: HeaderEvent): number;
|
|
22
|
+
export declare function setResponseHeader(event: HeaderEvent, name: string, value: string): void;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
function isPortableEvent(event) {
|
|
2
|
+
return "res" in event && typeof event.res.headers?.get === "function";
|
|
3
|
+
}
|
|
4
|
+
export function getHeader(event, name) {
|
|
5
|
+
if (!isPortableEvent(event)) {
|
|
6
|
+
const value = event.node.req.headers[name];
|
|
7
|
+
return Array.isArray(value) ? value.join(", ") : value;
|
|
8
|
+
}
|
|
9
|
+
return event.req.headers.get(name) ?? void 0;
|
|
10
|
+
}
|
|
11
|
+
export function getResponseHeader(event, name) {
|
|
12
|
+
if (!isPortableEvent(event)) {
|
|
13
|
+
const value = event.node.res.getHeader(name);
|
|
14
|
+
return value === void 0 ? void 0 : String(value);
|
|
15
|
+
}
|
|
16
|
+
return event.res.headers.get(name) ?? void 0;
|
|
17
|
+
}
|
|
18
|
+
export function getResponseStatus(event) {
|
|
19
|
+
return isPortableEvent(event) ? event.res.status ?? 200 : event.node.res.statusCode;
|
|
20
|
+
}
|
|
21
|
+
export function setResponseHeader(event, name, value) {
|
|
22
|
+
if (!isPortableEvent(event))
|
|
23
|
+
event.node.res.setHeader(name, value);
|
|
24
|
+
else
|
|
25
|
+
event.res.headers.set(name, value);
|
|
26
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { provideCloudflareRuntimeConfig } from "@harlan-zw/nuxt-cloudflare/bindings";
|
|
2
|
+
import { defineNitroPlugin, useRuntimeConfig } from "#nuxt-cloudflare/nitro";
|
|
3
3
|
export default defineNitroPlugin(() => {
|
|
4
4
|
provideCloudflareRuntimeConfig(useRuntimeConfig);
|
|
5
5
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { NitroApp } from 'nitropack/types';
|
|
1
|
+
import type { RequestEvent } from 'nuxt/server';
|
|
3
2
|
/**
|
|
4
3
|
* Contribute Cloudflare request context to the request's Wide Event.
|
|
5
4
|
*
|
|
@@ -28,6 +27,8 @@ import type { NitroApp } from 'nitropack/types';
|
|
|
28
27
|
* runtime. An earlier version assembled a `Record` and passed the variable; it
|
|
29
28
|
* failed the consuming application's build.
|
|
30
29
|
*/
|
|
31
|
-
declare const _default:
|
|
30
|
+
declare const _default: any;
|
|
32
31
|
export default _default;
|
|
33
|
-
export declare function recordCloudflareWideEventFields(event:
|
|
32
|
+
export declare function recordCloudflareWideEventFields(event: Pick<RequestEvent, 'context'> & {
|
|
33
|
+
req?: unknown;
|
|
34
|
+
}): void;
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import { addWideEventFields } from "
|
|
1
|
+
import { addWideEventFields } from "@harlan-zw/nuxt-wide-events/server";
|
|
2
|
+
import { defineNitroPlugin } from "#nuxt-cloudflare/nitro";
|
|
2
3
|
import { readD1Stats } from "../../../d1-stats";
|
|
3
|
-
export default (nitroApp) => {
|
|
4
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
4
5
|
nitroApp.hooks.hook("beforeResponse", (event) => {
|
|
5
6
|
recordCloudflareWideEventFields(event);
|
|
6
7
|
});
|
|
7
|
-
};
|
|
8
|
+
});
|
|
8
9
|
function stringOrUndefined(value) {
|
|
9
10
|
return typeof value === "string" ? value : void 0;
|
|
10
11
|
}
|
|
11
12
|
export function recordCloudflareWideEventFields(event) {
|
|
12
|
-
const
|
|
13
|
+
const requestCf = event.req?.cf;
|
|
14
|
+
const cf = requestCf ?? event.context.cloudflare?.request?.cf;
|
|
13
15
|
const d1 = readD1Stats(event.context);
|
|
14
16
|
if (!cf && !d1)
|
|
15
17
|
return;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default:
|
|
1
|
+
declare const _default: any;
|
|
2
2
|
export default _default;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { defineNitroPlugin, useRuntimeConfig } from "nitropack/runtime";
|
|
1
|
+
import { defineNitroPlugin, useRuntimeConfig } from "#nuxt-cloudflare/nitro";
|
|
3
2
|
import {
|
|
4
3
|
clampSharedCacheSeconds,
|
|
5
4
|
NO_STORE_BROWSER,
|
|
@@ -9,6 +8,7 @@ import {
|
|
|
9
8
|
sharedCacheSeconds,
|
|
10
9
|
statedPolicy
|
|
11
10
|
} from "../utils/workers-cache.js";
|
|
11
|
+
import { getHeader, getResponseHeader, getResponseStatus, setResponseHeader } from "./headers.js";
|
|
12
12
|
const warned = /* @__PURE__ */ new Set();
|
|
13
13
|
const WARN_LIMIT = 200;
|
|
14
14
|
function warnOnce(kind, path, message) {
|
|
@@ -41,7 +41,7 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
41
41
|
setResponseHeader(event, "cache-control", NO_STORE_BROWSER);
|
|
42
42
|
});
|
|
43
43
|
nitroApp.hooks.hook("beforeResponse", (event) => {
|
|
44
|
-
const config = useRuntimeConfig(
|
|
44
|
+
const config = useRuntimeConfig();
|
|
45
45
|
const mode = config.nuxtCloudflare?.htmlCacheMode ?? "auto";
|
|
46
46
|
const edge = getResponseHeader(event, "cloudflare-cdn-cache-control") ?? getResponseHeader(event, "cdn-cache-control");
|
|
47
47
|
const browser = getResponseHeader(event, "cache-control");
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@harlan-zw/nuxt-cloudflare",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.1",
|
|
5
5
|
"description": "Opinionated Cloudflare deployment defaults and diagnostics for Nuxt.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -99,20 +99,24 @@
|
|
|
99
99
|
"dist"
|
|
100
100
|
],
|
|
101
101
|
"engines": {
|
|
102
|
-
"node": "
|
|
102
|
+
"node": "^22.22.3 || ^24.15.0 || >=26.0.0"
|
|
103
103
|
},
|
|
104
104
|
"peerDependencies": {
|
|
105
|
-
"@harlan-zw/nuxt-checkin": "^0.
|
|
106
|
-
"
|
|
105
|
+
"@harlan-zw/nuxt-checkin": "^0.4.0",
|
|
106
|
+
"nitro": "^3.0.0-0",
|
|
107
|
+
"nuxt": "^4.6.0 || ^5.0.0",
|
|
107
108
|
"wrangler": ">=4.120.0 <5.0.0"
|
|
108
109
|
},
|
|
109
110
|
"peerDependenciesMeta": {
|
|
110
111
|
"@harlan-zw/nuxt-checkin": {
|
|
111
112
|
"optional": true
|
|
113
|
+
},
|
|
114
|
+
"nitro": {
|
|
115
|
+
"optional": true
|
|
112
116
|
}
|
|
113
117
|
},
|
|
114
118
|
"dependencies": {
|
|
115
|
-
"@nuxt/kit": "^4.
|
|
119
|
+
"@nuxt/kit": "^4.6.0",
|
|
116
120
|
"citty": "^0.2.2",
|
|
117
121
|
"confbox": "^0.2.4",
|
|
118
122
|
"consola": "^3.4.2",
|
|
@@ -126,16 +130,17 @@
|
|
|
126
130
|
"@arethetypeswrong/cli": "^0.18.5",
|
|
127
131
|
"@cloudflare/workers-types": "^5.20260811.1",
|
|
128
132
|
"@nuxt/module-builder": "^1.0.2",
|
|
129
|
-
"@nuxt/schema": "^4.
|
|
133
|
+
"@nuxt/schema": "^4.6.0",
|
|
130
134
|
"@types/node": "^26.1.1",
|
|
131
135
|
"eslint": "^10.8.1",
|
|
132
|
-
"
|
|
136
|
+
"nitro": "3.0.260903-beta",
|
|
137
|
+
"nuxt": "^4.6.0",
|
|
133
138
|
"typescript": "^5.9.3",
|
|
134
139
|
"unbuild": "^3.6.1",
|
|
135
140
|
"vitest": "^4.1.10",
|
|
136
141
|
"wrangler": "^4.120.1",
|
|
137
|
-
"@harlan-zw/nuxt-
|
|
138
|
-
"@harlan-zw/nuxt-
|
|
142
|
+
"@harlan-zw/nuxt-checkin": "0.4.1",
|
|
143
|
+
"@harlan-zw/nuxt-wide-events": "^0.2.0"
|
|
139
144
|
},
|
|
140
145
|
"scripts": {
|
|
141
146
|
"build": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt-module-build build",
|