@harlan-zw/nuxt-cloudflare 0.0.18 → 0.1.0
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/README.md +12 -4
- package/dist/bindings.d.mts +40 -4
- package/dist/bindings.mjs +18 -2
- package/dist/chunks/binding-types.mjs +7 -13
- package/dist/cli/index.mjs +12 -2
- package/dist/module.d.mts +26 -3
- package/dist/module.json +1 -1
- package/dist/module.mjs +38 -17
- package/dist/runtime/server/plugins/runtime-config.d.ts +9 -0
- package/dist/runtime/server/plugins/runtime-config.js +5 -0
- package/dist/types.d.mts +2 -2
- package/dist/wrangler.d.mts +35 -3
- package/dist/wrangler.mjs +62 -25
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@ The module extracts production patterns already proven in Nuxt SEO and gscdump.
|
|
|
6
6
|
|
|
7
7
|
## Defaults
|
|
8
8
|
|
|
9
|
+
Every default below yields to a value you wrote. The root `wrangler.jsonc`, `wrangler.json`, or `wrangler.toml` is authored config, and so is `nitro.cloudflare.wrangler`. A module default applies only where neither names the key. Workers Caching is the one exception: the module owns that policy because it also registers the caching plugin.
|
|
10
|
+
|
|
9
11
|
- Cloudflare module preset, generated Wrangler config, and Node compatibility
|
|
10
12
|
- Static assets remain asset first by default. Blanket `assets.run_worker_first: true` warns because valid authentication and transform use cases exist
|
|
11
13
|
- Workers Logs sampled at 1%, traces at 1%, both overridable
|
|
@@ -15,7 +17,8 @@ The module extracts production patterns already proven in Nuxt SEO and gscdump.
|
|
|
15
17
|
- Smart Placement enabled unless the project chooses a placement
|
|
16
18
|
- Workers Caching enabled with version isolation
|
|
17
19
|
- Rendered HTML forced to `private, no-store`; explicit non-HTML cache policies remain intact
|
|
18
|
-
- Source-map upload when the Nitro build emits maps;
|
|
20
|
+
- Source-map upload when the Nitro build emits maps; an authored value is preserved
|
|
21
|
+
- `nodejs_compat` added unless the config chooses `nodejs_compat_v2`, which Cloudflare rejects alongside it
|
|
19
22
|
- Module-wide `secrets.required` names copied to each environment; source root secrets remain scoped to the root
|
|
20
23
|
- Version metadata is skipped when `CF_VERSION_METADATA` already names another binding
|
|
21
24
|
- Raw `cloudflare-kv-binding` on Nitro's `cache` mount upgraded to a 30-day physical expiry
|
|
@@ -184,7 +187,8 @@ D1 allows 100 bound parameters per statement, including each statement inside `d
|
|
|
184
187
|
### Bindings
|
|
185
188
|
|
|
186
189
|
```ts
|
|
187
|
-
import {
|
|
190
|
+
import type { H3Event } from 'h3'
|
|
191
|
+
import { createCloudflareBindings, useCloudflareRuntimeConfig } from '@harlan-zw/nuxt-cloudflare/bindings'
|
|
188
192
|
|
|
189
193
|
const cloudflare = createCloudflareBindings()
|
|
190
194
|
|
|
@@ -192,14 +196,18 @@ function requireDatabase(source?: unknown) {
|
|
|
192
196
|
return cloudflare.require('DB', source)
|
|
193
197
|
}
|
|
194
198
|
|
|
195
|
-
|
|
196
|
-
|
|
199
|
+
// `event` on the request path, nothing in a queue, scheduled, email, or task handler.
|
|
200
|
+
function apiToken(event?: H3Event) {
|
|
201
|
+
return useCloudflareRuntimeConfig(event).apiToken
|
|
202
|
+
}
|
|
197
203
|
```
|
|
198
204
|
|
|
199
205
|
`nuxt prepare` runs Wrangler and writes exact, compatibility-aware types to `.nuxt/types/cloudflare-bindings.d.ts`. It merges root JSON, JSONC, or TOML bindings with `nitro.cloudflare.wrangler`. Nuxt and Nitro typechecks both reference the declaration, while bindings remain server runtime values. Production builds compare it with the final generated Wrangler config and fail on drift. Set `bindingTypes: false` only when another tool owns the declaration.
|
|
200
206
|
|
|
201
207
|
The source may be an H3 event, Nitro task input, or task context. Eventless access uses Nitro's `globalThis.__env__` Cloudflare entry shim. An explicit environment always wins and never mixes with the global environment. Binding names come from the generated `CloudflareBindings` interface. Missing required bindings throw. Pass a generic only to override generated types in a focused test.
|
|
202
208
|
|
|
209
|
+
`useCloudflareRuntimeConfig` reads runtime config from both contexts. On Cloudflare, `NUXT_*` Worker vars and secrets bind onto runtime config only through an event, so a bare `useRuntimeConfig()` off the request path returns build-time defaults. Without an event this reads the Cloudflare entry environment and wraps it as the source Nitro requires. The module's Nitro plugin supplies the reader; use `runtimeConfigSource` directly only when you own the `useRuntimeConfig` call.
|
|
210
|
+
|
|
203
211
|
### Scoped secrets file
|
|
204
212
|
|
|
205
213
|
```ts
|
package/dist/bindings.d.mts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { H3Event } from 'h3';
|
|
2
|
+
import { NitroRuntimeConfig } from 'nitropack/types';
|
|
3
|
+
|
|
1
4
|
type BindingName<Environment extends object> = Extract<keyof Environment, string>;
|
|
2
5
|
declare global {
|
|
3
6
|
interface CloudflareBindings {
|
|
@@ -21,8 +24,41 @@ declare function resolveCloudflareBindings<Environment extends object = Cloudfla
|
|
|
21
24
|
declare function setCloudflareBindings<Environment extends object>(env: Environment | undefined): void;
|
|
22
25
|
/** Merges binding sources into the eventless environment. Later sources win. */
|
|
23
26
|
declare function mergeCloudflareBindings<Environment extends object = CloudflareBindings>(...sources: Array<Partial<Environment> | undefined>): Environment;
|
|
24
|
-
/**
|
|
25
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Creates the event-shaped source Nitro runtime config resolution requires.
|
|
29
|
+
*
|
|
30
|
+
* `useRuntimeConfig` writes the resolved config onto `context.nitro`, so a
|
|
31
|
+
* source without that object throws for every read that reaches it. Two
|
|
32
|
+
* production incidents came from a source that omitted it.
|
|
33
|
+
*
|
|
34
|
+
* The return type includes `H3Event` because that is the parameter
|
|
35
|
+
* `useRuntimeConfig` declares, and this value carries every field it reads.
|
|
36
|
+
* It is a config source, never a request: no other H3 helper may receive it.
|
|
37
|
+
*/
|
|
38
|
+
declare function runtimeConfigSource<Environment extends object>(env: Environment): CloudflareRuntimeConfigSource<Environment> & H3Event;
|
|
39
|
+
/** Reads Nitro runtime config for one event, or the shared config without one. */
|
|
40
|
+
type NitroRuntimeConfigReader = (event?: H3Event) => NitroRuntimeConfig;
|
|
41
|
+
/**
|
|
42
|
+
* Provides Nitro's `useRuntimeConfig` to `useCloudflareRuntimeConfig`.
|
|
43
|
+
*
|
|
44
|
+
* The module's own Nitro plugin calls this. The reader is injected rather than
|
|
45
|
+
* imported because this file is also loaded outside a Nitro bundle, where a
|
|
46
|
+
* module-top `nitropack/runtime` import resolves virtual specifiers that only
|
|
47
|
+
* exist inside the Nitro build and crashes the development server at boot.
|
|
48
|
+
*/
|
|
49
|
+
declare function provideCloudflareRuntimeConfig(read: NitroRuntimeConfigReader | undefined): void;
|
|
50
|
+
/**
|
|
51
|
+
* Reads Nitro runtime config from Cloudflare request and eventless contexts.
|
|
52
|
+
*
|
|
53
|
+
* Pass the request event on the request path. Off it, in queue consumers,
|
|
54
|
+
* scheduled handlers, email handlers, and Nitro tasks, there is no event and a
|
|
55
|
+
* bare `useRuntimeConfig()` returns build-time defaults: the deployment's
|
|
56
|
+
* `NUXT_*` Worker vars and secrets bind onto runtime config only through an
|
|
57
|
+
* event. The Cloudflare entry assigns the live environment to
|
|
58
|
+
* `globalThis.__env__` in every handler, so the eventless path reads that and
|
|
59
|
+
* wraps it as a runtime config source.
|
|
60
|
+
*/
|
|
61
|
+
declare function useCloudflareRuntimeConfig(event?: H3Event): NitroRuntimeConfig;
|
|
26
62
|
/**
|
|
27
63
|
* Creates typed binding access for the generated `CloudflareBindings` environment.
|
|
28
64
|
*
|
|
@@ -31,5 +67,5 @@ declare function runtimeConfigSource<Environment extends object>(env: Environmen
|
|
|
31
67
|
*/
|
|
32
68
|
declare function createCloudflareBindings<Environment extends object = CloudflareBindings>(): CloudflareBindingAccessor<Environment>;
|
|
33
69
|
|
|
34
|
-
export { createCloudflareBindings, mergeCloudflareBindings, resolveCloudflareBindings, runtimeConfigSource, setCloudflareBindings };
|
|
35
|
-
export type { CloudflareBindingAccessor, CloudflareRuntimeConfigSource };
|
|
70
|
+
export { createCloudflareBindings, mergeCloudflareBindings, provideCloudflareRuntimeConfig, resolveCloudflareBindings, runtimeConfigSource, setCloudflareBindings, useCloudflareRuntimeConfig };
|
|
71
|
+
export type { CloudflareBindingAccessor, CloudflareRuntimeConfigSource, NitroRuntimeConfigReader };
|
package/dist/bindings.mjs
CHANGED
|
@@ -24,12 +24,28 @@ function mergeCloudflareBindings(...sources) {
|
|
|
24
24
|
return merged;
|
|
25
25
|
}
|
|
26
26
|
function runtimeConfigSource(env) {
|
|
27
|
-
|
|
27
|
+
const source = {
|
|
28
28
|
context: {
|
|
29
29
|
nitro: {},
|
|
30
30
|
cloudflare: { env }
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
+
return source;
|
|
34
|
+
}
|
|
35
|
+
let nitroRuntimeConfigReader;
|
|
36
|
+
function provideCloudflareRuntimeConfig(read) {
|
|
37
|
+
nitroRuntimeConfigReader = read;
|
|
38
|
+
}
|
|
39
|
+
function useCloudflareRuntimeConfig(event) {
|
|
40
|
+
if (!nitroRuntimeConfigReader) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
"[nuxt-cloudflare] No Nitro runtime config reader is available. Keep the module enabled, or call provideCloudflareRuntimeConfig() at the host boundary."
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (event)
|
|
46
|
+
return nitroRuntimeConfigReader(event);
|
|
47
|
+
const env = resolveCloudflareBindings();
|
|
48
|
+
return nitroRuntimeConfigReader(env ? runtimeConfigSource(env) : void 0);
|
|
33
49
|
}
|
|
34
50
|
function createCloudflareBindings() {
|
|
35
51
|
function get(name, source) {
|
|
@@ -49,4 +65,4 @@ function createCloudflareBindings() {
|
|
|
49
65
|
};
|
|
50
66
|
}
|
|
51
67
|
|
|
52
|
-
export { createCloudflareBindings, mergeCloudflareBindings, resolveCloudflareBindings, runtimeConfigSource, setCloudflareBindings };
|
|
68
|
+
export { createCloudflareBindings, mergeCloudflareBindings, provideCloudflareRuntimeConfig, resolveCloudflareBindings, runtimeConfigSource, setCloudflareBindings, useCloudflareRuntimeConfig };
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { resolve } from 'pathe';
|
|
3
3
|
import { experimental_generateTypes } from 'wrangler';
|
|
4
|
-
import {
|
|
5
|
-
import { readWranglerConfigFile } from '../wrangler.mjs';
|
|
4
|
+
import { readAuthoredWranglerConfig } from '../wrangler.mjs';
|
|
6
5
|
import 'node:fs';
|
|
7
6
|
import 'confbox';
|
|
8
7
|
|
|
@@ -70,16 +69,11 @@ function canonicalizeBindingEnvironment(environment) {
|
|
|
70
69
|
function createBindingTypeSignature(environment, runtime) {
|
|
71
70
|
return `${canonicalizeBindingEnvironment(environment)}\0${runtime.replaceAll("\r\n", "\n")}`;
|
|
72
71
|
}
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (loaded._tag === "loaded")
|
|
79
|
-
return loaded.config;
|
|
80
|
-
if (loaded._tag === "missing")
|
|
81
|
-
return {};
|
|
82
|
-
throw new SyntaxError(`Invalid Wrangler config "${path}": ${loaded.reason}`);
|
|
72
|
+
function readAuthoredConfig(rootDir) {
|
|
73
|
+
const authored = readAuthoredWranglerConfig(rootDir);
|
|
74
|
+
if (authored._tag === "invalid")
|
|
75
|
+
throw new SyntaxError(`Invalid Wrangler config "${authored.path}": ${authored.reason}`);
|
|
76
|
+
return authored._tag === "authored" ? authored.config : {};
|
|
83
77
|
}
|
|
84
78
|
async function generateCloudflareBindingTypes(options) {
|
|
85
79
|
const directory = resolve(options.buildDir, "nuxt-cloudflare");
|
|
@@ -103,7 +97,7 @@ async function generateCloudflareBindingTypes(options) {
|
|
|
103
97
|
};
|
|
104
98
|
}
|
|
105
99
|
async function prepareCloudflareBindingTypes(options) {
|
|
106
|
-
const authored =
|
|
100
|
+
const authored = readAuthoredConfig(options.rootDir);
|
|
107
101
|
const config = mergeDefaults(options.wrangler, authored);
|
|
108
102
|
return generateCloudflareBindingTypes({ ...options, config });
|
|
109
103
|
}
|
package/dist/cli/index.mjs
CHANGED
|
@@ -5,7 +5,8 @@ import { resolve } from 'pathe';
|
|
|
5
5
|
import { d as discoverWranglerSourceConfigs, a as diagnoseWranglerSourceConfigs, p as parseWranglerAllowedWarnings, e as evaluateWranglerDiagnostics } from '../shared/nuxt-cloudflare.6X-qzF_b.mjs';
|
|
6
6
|
import { diagnoseWranglerConfig, formatWranglerDiagnostics } from '../wrangler.mjs';
|
|
7
7
|
import { readProjectWranglerConfig } from '../chunks/wrangler-reader.mjs';
|
|
8
|
-
import 'node:fs';
|
|
8
|
+
import { readFileSync } from 'node:fs';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
9
10
|
import 'node:fs/promises';
|
|
10
11
|
import 'confbox';
|
|
11
12
|
import 'wrangler';
|
|
@@ -47,6 +48,15 @@ function diagnoseWranglerProject(options) {
|
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
function readCliVersion() {
|
|
52
|
+
const path = fileURLToPath(new URL("../../package.json", import.meta.url));
|
|
53
|
+
const manifest = JSON.parse(readFileSync(path, "utf8"));
|
|
54
|
+
const version = manifest !== null && typeof manifest === "object" ? manifest.version : void 0;
|
|
55
|
+
if (typeof version !== "string")
|
|
56
|
+
throw new TypeError(`Package manifest "${path}" declares no version.`);
|
|
57
|
+
return version;
|
|
58
|
+
}
|
|
59
|
+
|
|
50
60
|
const doctor = defineCommand({
|
|
51
61
|
meta: { name: "doctor", description: "Audit the effective Wrangler configuration" },
|
|
52
62
|
args: {
|
|
@@ -104,7 +114,7 @@ const doctor = defineCommand({
|
|
|
104
114
|
const main = defineCommand({
|
|
105
115
|
meta: {
|
|
106
116
|
name: "nuxt-cloudflare",
|
|
107
|
-
version:
|
|
117
|
+
version: readCliVersion(),
|
|
108
118
|
description: "Cloudflare deployment defaults and diagnostics for Nuxt"
|
|
109
119
|
},
|
|
110
120
|
subCommands: { doctor }
|
package/dist/module.d.mts
CHANGED
|
@@ -41,9 +41,32 @@ interface NitroCloudflareShape {
|
|
|
41
41
|
storage?: Record<string, NitroStorageMount>;
|
|
42
42
|
}
|
|
43
43
|
declare function findPopulatedRuntimeSecretPaths(config: unknown, environment: Readonly<Record<string, string | undefined>>): string[];
|
|
44
|
-
|
|
44
|
+
interface NitroCloudflareContext {
|
|
45
|
+
/** The project directory Wrangler and Nitro read the authored config from. */
|
|
46
|
+
rootDir?: string;
|
|
47
|
+
/** Whether Nuxt emits server source maps. */
|
|
48
|
+
serverSourceMaps?: boolean;
|
|
49
|
+
}
|
|
50
|
+
declare function configureNitroCloudflare(nitro: NitroCloudflareShape, options: ModuleOptions, context?: NitroCloudflareContext): void;
|
|
51
|
+
type BindingTypeAudit = {
|
|
52
|
+
_tag: 'compare';
|
|
53
|
+
signature: string;
|
|
54
|
+
} | {
|
|
55
|
+
_tag: 'missing';
|
|
56
|
+
} | {
|
|
57
|
+
_tag: 'skipped';
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Decides whether the build compares generated binding types with the final
|
|
61
|
+
* Wrangler config.
|
|
62
|
+
*
|
|
63
|
+
* The signature exists only after the type template regenerates. A build that
|
|
64
|
+
* reuses a cached template must fail, not skip: a skipped comparison reports a
|
|
65
|
+
* clean build for types that no longer match the deployed config.
|
|
66
|
+
*/
|
|
67
|
+
declare function resolveBindingTypeAudit(bindingTypes: boolean | undefined, signature: string | undefined): BindingTypeAudit;
|
|
45
68
|
declare function setupCloudflareModule(options: ModuleOptions, nuxt: Nuxt): void;
|
|
46
69
|
declare const _default: any;
|
|
47
70
|
|
|
48
|
-
export { configureNitroCloudflare, _default as default, findPopulatedRuntimeSecretPaths, setupCloudflareModule };
|
|
49
|
-
export type { ModuleOptions, NitroCloudflareShape };
|
|
71
|
+
export { configureNitroCloudflare, _default as default, findPopulatedRuntimeSecretPaths, resolveBindingTypeAudit, setupCloudflareModule };
|
|
72
|
+
export type { BindingTypeAudit, ModuleOptions, NitroCloudflareContext, NitroCloudflareShape };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import process from 'node:process';
|
|
|
2
2
|
import { useLogger, defineNuxtModule, hasNuxtModule, addTypeTemplate } from '@nuxt/kit';
|
|
3
3
|
import { resolve } from 'pathe';
|
|
4
4
|
import { a as diagnoseWranglerSourceConfigs, d as discoverWranglerSourceConfigs, e as evaluateWranglerDiagnostics } from './shared/nuxt-cloudflare.6X-qzF_b.mjs';
|
|
5
|
-
import { readWranglerJsonFile, diagnoseWranglerConfig, formatWranglerDiagnostics, applyCloudflareDefaults } from './wrangler.mjs';
|
|
5
|
+
import { readWranglerJsonFile, diagnoseWranglerConfig, formatWranglerDiagnostics, readAuthoredWranglerConfig, applyCloudflareDefaults } from './wrangler.mjs';
|
|
6
6
|
import 'node:fs';
|
|
7
7
|
import 'node:fs/promises';
|
|
8
8
|
import 'confbox';
|
|
@@ -105,6 +105,10 @@ const wideEventsPluginPath = resolve(
|
|
|
105
105
|
import.meta.dirname,
|
|
106
106
|
import.meta.url.endsWith(".ts") ? "runtime/server/plugins/wide-events.ts" : "runtime/server/plugins/wide-events.js"
|
|
107
107
|
);
|
|
108
|
+
const runtimeConfigPluginPath = resolve(
|
|
109
|
+
import.meta.dirname,
|
|
110
|
+
import.meta.url.endsWith(".ts") ? "runtime/server/plugins/runtime-config.ts" : "runtime/server/plugins/runtime-config.js"
|
|
111
|
+
);
|
|
108
112
|
const WIDE_EVENT_FIELDS = [
|
|
109
113
|
"cf.colo",
|
|
110
114
|
"cf.country",
|
|
@@ -144,7 +148,15 @@ function findPopulatedRuntimeSecretPaths(config, environment) {
|
|
|
144
148
|
visit(config, []);
|
|
145
149
|
return paths;
|
|
146
150
|
}
|
|
147
|
-
function
|
|
151
|
+
function addNitroPlugin(nitro, path) {
|
|
152
|
+
nitro.plugins ??= [];
|
|
153
|
+
if (!nitro.plugins.includes(path))
|
|
154
|
+
nitro.plugins.push(path);
|
|
155
|
+
}
|
|
156
|
+
function configureNitroCloudflare(nitro, options, context = {}) {
|
|
157
|
+
const authored = readAuthoredWranglerConfig(context.rootDir);
|
|
158
|
+
if (authored._tag === "invalid")
|
|
159
|
+
throw new Error(`[nuxt-cloudflare] Wrangler config "${authored.path}" cannot be parsed: ${authored.reason}`);
|
|
148
160
|
nitro.preset ??= "cloudflare-module";
|
|
149
161
|
nitro.cloudflare ??= {};
|
|
150
162
|
nitro.cloudflare.deployConfig = true;
|
|
@@ -154,15 +166,13 @@ function configureNitroCloudflare(nitro, options, nuxtServerSourceMaps) {
|
|
|
154
166
|
logsSampleRate: options.logsSampleRate,
|
|
155
167
|
requiredSecrets: options.requiredSecrets,
|
|
156
168
|
tracesSampleRate: options.tracesSampleRate,
|
|
157
|
-
uploadSourceMaps: options.sourceMaps ?? nitro.sourceMap ??
|
|
169
|
+
uploadSourceMaps: options.sourceMaps ?? nitro.sourceMap ?? context.serverSourceMaps ?? false,
|
|
158
170
|
versionMetadataBinding: options.versionMetadataBinding,
|
|
159
171
|
workersCache: resolveModuleWorkersCachePolicy(options)
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
nitro.plugins.push(workersCachePluginPath);
|
|
165
|
-
}
|
|
172
|
+
}, authored._tag === "authored" ? authored.config : {});
|
|
173
|
+
addNitroPlugin(nitro, runtimeConfigPluginPath);
|
|
174
|
+
if (nitro.cloudflare.wrangler.cache?.enabled)
|
|
175
|
+
addNitroPlugin(nitro, workersCachePluginPath);
|
|
166
176
|
if (options.kvCache === false)
|
|
167
177
|
return;
|
|
168
178
|
const cache = nitro.storage?.cache;
|
|
@@ -177,6 +187,11 @@ function configureNitroCloudflare(nitro, options, nuxtServerSourceMaps) {
|
|
|
177
187
|
defaultTtl: configuredCache?.defaultTtl ?? 30 * 24 * 60 * 60
|
|
178
188
|
};
|
|
179
189
|
}
|
|
190
|
+
function resolveBindingTypeAudit(bindingTypes, signature) {
|
|
191
|
+
if (bindingTypes === false)
|
|
192
|
+
return { _tag: "skipped" };
|
|
193
|
+
return signature ? { _tag: "compare", signature } : { _tag: "missing" };
|
|
194
|
+
}
|
|
180
195
|
async function auditGeneratedWranglerConfig(nitro, options, buildDir, rootDir, bindingTypeSignature) {
|
|
181
196
|
const path = resolve(nitro.options.output.serverDir, "wrangler.json");
|
|
182
197
|
const result = await readWranglerJsonFile(path);
|
|
@@ -188,13 +203,19 @@ async function auditGeneratedWranglerConfig(nitro, options, buildDir, rootDir, b
|
|
|
188
203
|
const validated = readProjectWranglerConfig({ config: path, cwd: rootDir });
|
|
189
204
|
if (validated._tag === "invalid")
|
|
190
205
|
throw new Error("[nuxt-cloudflare] Generated Wrangler config fails Wrangler schema validation. Run Wrangler locally for validation details.");
|
|
191
|
-
|
|
206
|
+
const audit = resolveBindingTypeAudit(options.bindingTypes, bindingTypeSignature);
|
|
207
|
+
if (audit._tag === "missing") {
|
|
208
|
+
throw new Error(
|
|
209
|
+
"[nuxt-cloudflare] Cloudflare binding types were not generated during this build, so the drift check could not run. Run `nuxt prepare`, or set `bindingTypes: false` when another tool owns the declaration."
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
if (audit._tag === "compare") {
|
|
192
213
|
const { assertCloudflareBindingTypesCurrent } = await import('./chunks/binding-types.mjs');
|
|
193
214
|
await assertCloudflareBindingTypesCurrent({
|
|
194
215
|
buildDir,
|
|
195
216
|
compatibilityDate: result.config.compatibility_date,
|
|
196
217
|
config: result.config,
|
|
197
|
-
expectedSignature:
|
|
218
|
+
expectedSignature: audit.signature,
|
|
198
219
|
nodeCompat: Boolean(nitro.options.cloudflare?.nodeCompat)
|
|
199
220
|
});
|
|
200
221
|
}
|
|
@@ -225,17 +246,17 @@ function setupCloudflareModule(options, nuxt) {
|
|
|
225
246
|
const configure = () => configureNitroCloudflare(
|
|
226
247
|
nuxt.options.nitro,
|
|
227
248
|
options,
|
|
228
|
-
|
|
249
|
+
{
|
|
250
|
+
rootDir: nuxt.options.rootDir,
|
|
251
|
+
serverSourceMaps: Boolean(nuxt.options.sourcemap.server)
|
|
252
|
+
}
|
|
229
253
|
);
|
|
230
254
|
const registerWideEventFields = nuxt.hook;
|
|
231
255
|
registerWideEventFields("wide-events:fields", (registry) => {
|
|
232
256
|
registry.add("@harlan-zw/nuxt-cloudflare", [...WIDE_EVENT_FIELDS]);
|
|
233
257
|
});
|
|
234
258
|
if (Array.isArray(nuxt.options.modules) && hasNuxtModule("@harlan-zw/nuxt-wide-events", nuxt)) {
|
|
235
|
-
|
|
236
|
-
wideEventsNitro.plugins ??= [];
|
|
237
|
-
if (!wideEventsNitro.plugins.includes(wideEventsPluginPath))
|
|
238
|
-
wideEventsNitro.plugins.push(wideEventsPluginPath);
|
|
259
|
+
addNitroPlugin(nuxt.options.nitro, wideEventsPluginPath);
|
|
239
260
|
}
|
|
240
261
|
let bindingTypeSignature;
|
|
241
262
|
if (options.bindingTypes !== false) {
|
|
@@ -315,4 +336,4 @@ const module$1 = defineNuxtModule({
|
|
|
315
336
|
setup: setupCloudflareModule
|
|
316
337
|
});
|
|
317
338
|
|
|
318
|
-
export { configureNitroCloudflare, module$1 as default, findPopulatedRuntimeSecretPaths, setupCloudflareModule };
|
|
339
|
+
export { configureNitroCloudflare, module$1 as default, findPopulatedRuntimeSecretPaths, resolveBindingTypeAudit, setupCloudflareModule };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hands Nitro's runtime config reader to `useCloudflareRuntimeConfig`.
|
|
3
|
+
*
|
|
4
|
+
* Nitro bundles its plugins, so this file resolves `nitropack/runtime`. The
|
|
5
|
+
* bindings module cannot: applications import it outside a Nitro bundle, where
|
|
6
|
+
* that specifier fails to resolve.
|
|
7
|
+
*/
|
|
8
|
+
declare const _default: import("nitropack").NitroAppPlugin;
|
|
9
|
+
export default _default;
|
package/dist/types.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type configureNitroCloudflare, default, type findPopulatedRuntimeSecretPaths, type setupCloudflareModule } from './module.mjs'
|
|
1
|
+
export { type configureNitroCloudflare, default, type findPopulatedRuntimeSecretPaths, type resolveBindingTypeAudit, type setupCloudflareModule } from './module.mjs'
|
|
2
2
|
|
|
3
|
-
export { type ModuleOptions, type NitroCloudflareShape } from './module.mjs'
|
|
3
|
+
export { type BindingTypeAudit, type ModuleOptions, type NitroCloudflareContext, type NitroCloudflareShape } from './module.mjs'
|
package/dist/wrangler.d.mts
CHANGED
|
@@ -12,6 +12,26 @@ type WranglerConfigFileResult = {
|
|
|
12
12
|
};
|
|
13
13
|
declare function readWranglerConfigFile(path: string): WranglerConfigFileResult;
|
|
14
14
|
declare function findProjectWranglerConfig(cwd: string): string | undefined;
|
|
15
|
+
type AuthoredWranglerConfigResult = {
|
|
16
|
+
_tag: 'authored';
|
|
17
|
+
config: WranglerConfigInput;
|
|
18
|
+
path: string;
|
|
19
|
+
} | {
|
|
20
|
+
_tag: 'absent';
|
|
21
|
+
} | {
|
|
22
|
+
_tag: 'invalid';
|
|
23
|
+
path: string;
|
|
24
|
+
reason: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Reads the root Wrangler config the consumer authored.
|
|
28
|
+
*
|
|
29
|
+
* Nitro writes the deployed config as `defu(overrides, nitro.cloudflare.wrangler,
|
|
30
|
+
* thisFile, defaults)`, so every value in this file loses to a module default
|
|
31
|
+
* that names the same key. Module policy reads the file for that reason only:
|
|
32
|
+
* a value the consumer wrote here stays the value that deploys.
|
|
33
|
+
*/
|
|
34
|
+
declare function readAuthoredWranglerConfig(rootDir: string | undefined): AuthoredWranglerConfigResult;
|
|
15
35
|
|
|
16
36
|
interface WranglerAssetsInput {
|
|
17
37
|
directory?: string;
|
|
@@ -145,10 +165,22 @@ type WranglerJsonFileResult = {
|
|
|
145
165
|
path: string;
|
|
146
166
|
reason: string;
|
|
147
167
|
};
|
|
148
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Applies module policy to the Wrangler config Nitro generates.
|
|
170
|
+
*
|
|
171
|
+
* Three layers decide every value. `config` is `nitro.cloudflare.wrangler`,
|
|
172
|
+
* which the application and its modules write. `authored` is the root
|
|
173
|
+
* `wrangler.jsonc` or `wrangler.toml`. Module options are last, because a
|
|
174
|
+
* default must never overrule a value the consumer wrote in either place.
|
|
175
|
+
*
|
|
176
|
+
* Nitro merges the generated config with `defu`, where `nitro.cloudflare.wrangler`
|
|
177
|
+
* outranks the authored file. So this function resolves the effective value and
|
|
178
|
+
* writes it back: without the authored layer the module default silently wins.
|
|
179
|
+
*/
|
|
180
|
+
declare function applyCloudflareDefaults(config: WranglerConfigInput, options?: CloudflareDefaultOptions, authored?: WranglerConfigInput): WranglerConfigInput;
|
|
149
181
|
declare function diagnoseWranglerConfig(config: WranglerConfigInput, options?: WranglerDiagnosticOptions): WranglerDiagnostic[];
|
|
150
182
|
declare function formatWranglerDiagnostics(diagnostics: readonly WranglerDiagnostic[]): string;
|
|
151
183
|
declare function readWranglerJsonFile(path: string): Promise<WranglerJsonFileResult>;
|
|
152
184
|
|
|
153
|
-
export { WRANGLER_DIAGNOSTIC_CODES, applyCloudflareDefaults, diagnoseWranglerConfig, findProjectWranglerConfig, formatWranglerDiagnostics, readWranglerConfigFile, readWranglerJsonFile };
|
|
154
|
-
export type { CloudflareDefaultOptions, WorkersCachePolicy, WranglerAssetsInput, WranglerConfigFileResult, WranglerConfigInput, WranglerContainerInput, WranglerDiagnostic, WranglerDiagnosticCode, WranglerDiagnosticOptions, WranglerJsonFileResult, WranglerObservabilityInput, WranglerPlacementInput, WranglerRemoteBindingInput, WranglerWorkersCacheInput };
|
|
185
|
+
export { WRANGLER_DIAGNOSTIC_CODES, applyCloudflareDefaults, diagnoseWranglerConfig, findProjectWranglerConfig, formatWranglerDiagnostics, readAuthoredWranglerConfig, readWranglerConfigFile, readWranglerJsonFile };
|
|
186
|
+
export type { AuthoredWranglerConfigResult, CloudflareDefaultOptions, WorkersCachePolicy, WranglerAssetsInput, WranglerConfigFileResult, WranglerConfigInput, WranglerContainerInput, WranglerDiagnostic, WranglerDiagnosticCode, WranglerDiagnosticOptions, WranglerJsonFileResult, WranglerObservabilityInput, WranglerPlacementInput, WranglerRemoteBindingInput, WranglerWorkersCacheInput };
|
package/dist/wrangler.mjs
CHANGED
|
@@ -48,6 +48,17 @@ function findProjectWranglerConfig(cwd) {
|
|
|
48
48
|
directory = parent;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
+
function readAuthoredWranglerConfig(rootDir) {
|
|
52
|
+
const path = rootDir === void 0 ? void 0 : findProjectWranglerConfig(rootDir);
|
|
53
|
+
if (!path)
|
|
54
|
+
return { _tag: "absent" };
|
|
55
|
+
const loaded = readWranglerConfigFile(path);
|
|
56
|
+
if (loaded._tag === "loaded")
|
|
57
|
+
return { _tag: "authored", config: loaded.config, path };
|
|
58
|
+
if (loaded._tag === "missing")
|
|
59
|
+
return { _tag: "absent" };
|
|
60
|
+
return { _tag: "invalid", path, reason: loaded.reason };
|
|
61
|
+
}
|
|
51
62
|
|
|
52
63
|
const WRANGLER_DIAGNOSTIC_CODES = [
|
|
53
64
|
"assets-worker-first",
|
|
@@ -291,33 +302,55 @@ function configuredRemoteBindings(config) {
|
|
|
291
302
|
}
|
|
292
303
|
return bindings;
|
|
293
304
|
}
|
|
294
|
-
function applyCloudflareDefaults(config, options = {}) {
|
|
295
|
-
const root = applyEnvironmentDefaults(config, options);
|
|
305
|
+
function applyCloudflareDefaults(config, options = {}, authored = {}) {
|
|
306
|
+
const root = applyEnvironmentDefaults(config, options, { authored });
|
|
296
307
|
const environments = config.env && Object.fromEntries(
|
|
297
|
-
Object.entries(config.env).map(([name, environment]) => [name, applyEnvironmentDefaults(environment, {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
308
|
+
Object.entries(config.env).map(([name, environment]) => [name, applyEnvironmentDefaults(environment, options, {
|
|
309
|
+
// Named Wrangler environments inherit the authored root for the keys
|
|
310
|
+
// Wrangler itself inherits, and nothing else.
|
|
311
|
+
authored: resolveEnvironmentPolicy(authored, authored.env?.[name] ?? {}),
|
|
312
|
+
inherited: root
|
|
313
|
+
})])
|
|
301
314
|
);
|
|
302
315
|
return environments ? { ...root, env: environments } : root;
|
|
303
316
|
}
|
|
304
|
-
function
|
|
305
|
-
const
|
|
306
|
-
|
|
307
|
-
|
|
317
|
+
function withNodeCompatibilityFlags(flags) {
|
|
318
|
+
const resolved = new Set(flags);
|
|
319
|
+
if (resolved.has("nodejs_compat_v2") && resolved.has("no_nodejs_compat_v2"))
|
|
320
|
+
resolved.delete("nodejs_compat_v2");
|
|
321
|
+
if (!resolved.has("nodejs_compat_v2")) {
|
|
322
|
+
resolved.add("nodejs_compat");
|
|
323
|
+
resolved.add("no_nodejs_compat_v2");
|
|
324
|
+
}
|
|
325
|
+
return [...resolved];
|
|
326
|
+
}
|
|
327
|
+
function applyEnvironmentDefaults(config, options, layers = {}) {
|
|
328
|
+
const authored = layers.authored ?? {};
|
|
329
|
+
const inherited = layers.inherited ?? {};
|
|
330
|
+
const compatibilityDate = config.compatibility_date ?? authored.compatibility_date ?? inherited.compatibility_date ?? options.compatibilityDate;
|
|
331
|
+
const declaredFlags = unique([...config.compatibility_flags ?? [], ...authored.compatibility_flags ?? []]);
|
|
332
|
+
const compatibilityFlags = withNodeCompatibilityFlags(
|
|
333
|
+
declaredFlags.length > 0 ? declaredFlags : inherited.compatibility_flags ?? []
|
|
334
|
+
);
|
|
335
|
+
const authoredCache = config.cache ?? authored.cache ?? inherited.cache;
|
|
308
336
|
const cache = options.workersCache ? resolveWorkersCachePolicy(options.workersCache) : authoredCache ? { ...authoredCache, cross_version_cache: authoredCache.cross_version_cache ?? false } : resolveWorkersCachePolicy({ _tag: "disabled" });
|
|
309
|
-
const
|
|
337
|
+
const authoredSecrets = new Set(authored.secrets?.required ?? []);
|
|
338
|
+
const requiredSecrets = unique([...config.secrets?.required ?? [], ...options.requiredSecrets ?? []]).filter((name) => !authoredSecrets.has(name));
|
|
310
339
|
const logs = config.observability?.logs;
|
|
340
|
+
const authoredLogs = authored.observability?.logs;
|
|
311
341
|
const inheritedLogs = inherited.observability?.logs;
|
|
312
342
|
const traces = config.observability?.traces;
|
|
343
|
+
const authoredTraces = authored.observability?.traces;
|
|
313
344
|
const inheritedTraces = inherited.observability?.traces;
|
|
314
|
-
const uploadSourceMaps = config.upload_source_maps ?? inherited.upload_source_maps ?? options.uploadSourceMaps ?? true;
|
|
315
|
-
const previewUrls = config.preview_urls ?? inherited.preview_urls ?? false;
|
|
316
|
-
const placement = config.placement ?? inherited.placement ?? { mode: "smart" };
|
|
317
|
-
const
|
|
345
|
+
const uploadSourceMaps = config.upload_source_maps ?? authored.upload_source_maps ?? inherited.upload_source_maps ?? options.uploadSourceMaps ?? true;
|
|
346
|
+
const previewUrls = config.preview_urls ?? authored.preview_urls ?? inherited.preview_urls ?? false;
|
|
347
|
+
const placement = config.placement ?? authored.placement ?? inherited.placement ?? { mode: "smart" };
|
|
348
|
+
const route = config.route ?? authored.route ?? inherited.route;
|
|
349
|
+
const routes = config.routes ?? authored.routes ?? inherited.routes;
|
|
350
|
+
const workersDev = config.workers_dev ?? authored.workers_dev ?? inherited.workers_dev ?? (route !== void 0 || (routes?.length ?? 0) > 0 ? false : void 0);
|
|
318
351
|
const versionMetadataBinding = options.versionMetadataBinding ?? "CF_VERSION_METADATA";
|
|
319
|
-
const localBindingNames = collectBindingNames(config);
|
|
320
|
-
const versionMetadata = config.version_metadata ?? (inherited.version_metadata && !localBindingNames.has(inherited.version_metadata.binding) ? inherited.version_metadata : void 0) ?? (localBindingNames.has(versionMetadataBinding) ? void 0 : { binding: versionMetadataBinding });
|
|
352
|
+
const localBindingNames = /* @__PURE__ */ new Set([...collectBindingNames(config), ...collectBindingNames(authored)]);
|
|
353
|
+
const versionMetadata = config.version_metadata ?? authored.version_metadata ?? (inherited.version_metadata && !localBindingNames.has(inherited.version_metadata.binding) ? inherited.version_metadata : void 0) ?? (localBindingNames.has(versionMetadataBinding) ? void 0 : { binding: versionMetadataBinding });
|
|
321
354
|
return {
|
|
322
355
|
...config,
|
|
323
356
|
cache,
|
|
@@ -325,20 +358,23 @@ function applyEnvironmentDefaults(config, options, inherited = {}) {
|
|
|
325
358
|
compatibility_flags: compatibilityFlags,
|
|
326
359
|
observability: {
|
|
327
360
|
...inherited.observability,
|
|
361
|
+
...authored.observability,
|
|
328
362
|
...config.observability,
|
|
329
|
-
enabled: config.observability?.enabled ?? inherited.observability?.enabled ?? true,
|
|
363
|
+
enabled: config.observability?.enabled ?? authored.observability?.enabled ?? inherited.observability?.enabled ?? true,
|
|
330
364
|
logs: {
|
|
331
365
|
...inheritedLogs,
|
|
366
|
+
...authoredLogs,
|
|
332
367
|
...logs,
|
|
333
|
-
enabled: logs?.enabled ?? inheritedLogs?.enabled ?? true,
|
|
334
|
-
head_sampling_rate: logs?.head_sampling_rate ?? inheritedLogs?.head_sampling_rate ?? options.logsSampleRate ?? 0.01,
|
|
335
|
-
invocation_logs: logs?.invocation_logs ?? inheritedLogs?.invocation_logs ?? true
|
|
368
|
+
enabled: logs?.enabled ?? authoredLogs?.enabled ?? inheritedLogs?.enabled ?? true,
|
|
369
|
+
head_sampling_rate: logs?.head_sampling_rate ?? authoredLogs?.head_sampling_rate ?? inheritedLogs?.head_sampling_rate ?? options.logsSampleRate ?? 0.01,
|
|
370
|
+
invocation_logs: logs?.invocation_logs ?? authoredLogs?.invocation_logs ?? inheritedLogs?.invocation_logs ?? true
|
|
336
371
|
},
|
|
337
372
|
traces: {
|
|
338
373
|
...inheritedTraces,
|
|
374
|
+
...authoredTraces,
|
|
339
375
|
...traces,
|
|
340
|
-
enabled: traces?.enabled ?? inheritedTraces?.enabled ?? true,
|
|
341
|
-
head_sampling_rate: traces?.head_sampling_rate ?? inheritedTraces?.head_sampling_rate ?? options.tracesSampleRate ?? 0.01
|
|
376
|
+
enabled: traces?.enabled ?? authoredTraces?.enabled ?? inheritedTraces?.enabled ?? true,
|
|
377
|
+
head_sampling_rate: traces?.head_sampling_rate ?? authoredTraces?.head_sampling_rate ?? inheritedTraces?.head_sampling_rate ?? options.tracesSampleRate ?? 0.01
|
|
342
378
|
}
|
|
343
379
|
},
|
|
344
380
|
placement,
|
|
@@ -644,7 +680,8 @@ function diagnosePolicy(config, prefix, diagnostics, options, publicVarNames) {
|
|
|
644
680
|
});
|
|
645
681
|
}
|
|
646
682
|
}
|
|
647
|
-
|
|
683
|
+
const nodeCompatFlags = new Set(config.compatibility_flags ?? []);
|
|
684
|
+
if (options.requireNodeCompat && !nodeCompatFlags.has("nodejs_compat") && !nodeCompatFlags.has("nodejs_compat_v2")) {
|
|
648
685
|
diagnostics.push({
|
|
649
686
|
_tag: "error",
|
|
650
687
|
code: "missing-nodejs-compat",
|
|
@@ -898,4 +935,4 @@ async function readWranglerJsonFile(path) {
|
|
|
898
935
|
});
|
|
899
936
|
}
|
|
900
937
|
|
|
901
|
-
export { WRANGLER_DIAGNOSTIC_CODES, applyCloudflareDefaults, diagnoseWranglerConfig, findProjectWranglerConfig, formatWranglerDiagnostics, readWranglerConfigFile, readWranglerJsonFile };
|
|
938
|
+
export { WRANGLER_DIAGNOSTIC_CODES, applyCloudflareDefaults, diagnoseWranglerConfig, findProjectWranglerConfig, formatWranglerDiagnostics, readAuthoredWranglerConfig, readWranglerConfigFile, readWranglerJsonFile };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@harlan-zw/nuxt-cloudflare",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"description": "Opinionated Cloudflare deployment defaults and diagnostics for Nuxt.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -106,7 +106,7 @@
|
|
|
106
106
|
"unbuild": "^3.6.1",
|
|
107
107
|
"vitest": "^4.1.10",
|
|
108
108
|
"wrangler": "^4.120.1",
|
|
109
|
-
"@harlan-zw/nuxt-wide-events": "0.0
|
|
109
|
+
"@harlan-zw/nuxt-wide-events": "^0.1.0"
|
|
110
110
|
},
|
|
111
111
|
"scripts": {
|
|
112
112
|
"build": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt-module-build build",
|