@harlan-zw/nuxt-cloudflare 0.0.13 → 0.0.15
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/d1.d.mts +20 -0
- package/dist/d1.mjs +42 -1
- package/dist/module.json +2 -2
- package/dist/module.mjs +27 -2
- package/dist/runtime/server/plugins/wide-events.d.ts +24 -0
- package/dist/runtime/server/plugins/wide-events.js +30 -0
- package/package.json +1 -1
package/dist/d1.d.mts
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
interface D1RequestStats {
|
|
2
|
+
/** Terminal statement executions and batches. */
|
|
3
|
+
queries: number;
|
|
4
|
+
/** Of those, served by the primary rather than a replica. */
|
|
5
|
+
primaryQueries: number;
|
|
6
|
+
/** Sessions restarted after a reset the layer could recover from. */
|
|
7
|
+
recoveries: number;
|
|
8
|
+
/** Recoveries that gave up, i.e. the caller saw the error. */
|
|
9
|
+
unrecovered: number;
|
|
10
|
+
/** Region of the last node that served a query, e.g. `OC`. */
|
|
11
|
+
region: string | null;
|
|
12
|
+
/** Total SQL execution time D1 reported, in milliseconds. */
|
|
13
|
+
durationMs: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
1
16
|
type D1WriteSafety = {
|
|
2
17
|
_tag: 'lock-only';
|
|
3
18
|
} | {
|
|
@@ -78,6 +93,11 @@ interface D1ResetRecoveryOptions {
|
|
|
78
93
|
consistency?: D1Consistency;
|
|
79
94
|
maxAttempts?: number;
|
|
80
95
|
onRecovery?: (event: D1RecoveryEvent) => void;
|
|
96
|
+
/**
|
|
97
|
+
* Fold this session's query and recovery counts into a request-scoped stats
|
|
98
|
+
* object. Omit it and nothing is recorded. See `d1-stats.ts`.
|
|
99
|
+
*/
|
|
100
|
+
stats?: D1RequestStats;
|
|
81
101
|
random?: () => number;
|
|
82
102
|
sleep?: (milliseconds: number) => Promise<void>;
|
|
83
103
|
}
|
package/dist/d1.mjs
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
+
const REQUEST_D1_STATS = Symbol.for("@harlan-zw/nuxt-cloudflare:d1-request-stats");
|
|
2
|
+
function createD1Stats() {
|
|
3
|
+
return { queries: 0, primaryQueries: 0, recoveries: 0, unrecovered: 0, region: null, durationMs: 0 };
|
|
4
|
+
}
|
|
5
|
+
function useD1Stats(requestContext) {
|
|
6
|
+
const existing = requestContext[REQUEST_D1_STATS];
|
|
7
|
+
if (existing && typeof existing === "object")
|
|
8
|
+
return existing;
|
|
9
|
+
const stats = createD1Stats();
|
|
10
|
+
requestContext[REQUEST_D1_STATS] = stats;
|
|
11
|
+
return stats;
|
|
12
|
+
}
|
|
13
|
+
function recordD1Meta(stats, result) {
|
|
14
|
+
stats.queries++;
|
|
15
|
+
const meta = result?.meta;
|
|
16
|
+
if (!meta)
|
|
17
|
+
return;
|
|
18
|
+
if (meta.served_by_primary === true)
|
|
19
|
+
stats.primaryQueries++;
|
|
20
|
+
if (typeof meta.served_by_region === "string")
|
|
21
|
+
stats.region = meta.served_by_region;
|
|
22
|
+
if (typeof meta.duration === "number")
|
|
23
|
+
stats.durationMs += meta.duration;
|
|
24
|
+
}
|
|
25
|
+
function recordD1Recovery(stats, event) {
|
|
26
|
+
stats.recoveries++;
|
|
27
|
+
if (event._tag === "stopped")
|
|
28
|
+
stats.unrecovered++;
|
|
29
|
+
}
|
|
30
|
+
|
|
1
31
|
const D1_MAX_BOUND_PARAMETERS = 100;
|
|
2
32
|
const D1_PARAMETER_PLAN = Symbol("@harlan-zw/nuxt-cloudflare:d1-parameter-plan");
|
|
3
33
|
const REQUEST_D1_SESSIONS = Symbol.for("@harlan-zw/nuxt-cloudflare:d1-sessions");
|
|
@@ -234,8 +264,11 @@ function withD1ResetRecovery(database, options = {}) {
|
|
|
234
264
|
(value) => ({ _tag: "ok", value }),
|
|
235
265
|
(error) => ({ _tag: "error", error })
|
|
236
266
|
);
|
|
237
|
-
if (outcome._tag === "ok")
|
|
267
|
+
if (outcome._tag === "ok") {
|
|
268
|
+
if (options.stats)
|
|
269
|
+
recordD1Meta(options.stats, outcome.value);
|
|
238
270
|
return outcome.value;
|
|
271
|
+
}
|
|
239
272
|
const failure = classifyD1RetryError(outcome.error);
|
|
240
273
|
if (failure._tag !== "session-reset" && failure._tag !== "transient")
|
|
241
274
|
throw outcome.error;
|
|
@@ -243,6 +276,8 @@ function withD1ResetRecovery(database, options = {}) {
|
|
|
243
276
|
if (failure._tag === "session-reset")
|
|
244
277
|
renewSession();
|
|
245
278
|
if (!replayable || exhausted) {
|
|
279
|
+
if (options.stats)
|
|
280
|
+
recordD1Recovery(options.stats, { _tag: "stopped" });
|
|
246
281
|
options.onRecovery?.({
|
|
247
282
|
_tag: "stopped",
|
|
248
283
|
attempt,
|
|
@@ -252,6 +287,8 @@ function withD1ResetRecovery(database, options = {}) {
|
|
|
252
287
|
});
|
|
253
288
|
throw outcome.error;
|
|
254
289
|
}
|
|
290
|
+
if (options.stats)
|
|
291
|
+
recordD1Recovery(options.stats, { _tag: "retrying" });
|
|
255
292
|
options.onRecovery?.({ _tag: "retrying", attempt, failure, sql });
|
|
256
293
|
await sleep(retryDelay(attempt, random));
|
|
257
294
|
}
|
|
@@ -343,6 +380,10 @@ function getRecoveringRequestD1Session(requestContext, binding, database, option
|
|
|
343
380
|
const sessions = getD1SessionCache(requestContext, REQUEST_D1_RECOVERING_SESSIONS);
|
|
344
381
|
if (!sessions.has(key)) {
|
|
345
382
|
sessions.set(key, withD1ResetRecovery(database, {
|
|
383
|
+
// A request-scoped session gets request-scoped counters by default: the
|
|
384
|
+
// context to hang them on is the argument this function already takes,
|
|
385
|
+
// and a caller that wants none can pass its own `stats`.
|
|
386
|
+
stats: useD1Stats(requestContext),
|
|
346
387
|
...options,
|
|
347
388
|
consistency
|
|
348
389
|
}));
|
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": ">=4.5.0"
|
|
5
|
+
"nuxt": ">=4.5.0 <5.0.0"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.0.
|
|
7
|
+
"version": "0.0.15",
|
|
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, addTypeTemplate } from '@nuxt/kit';
|
|
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
5
|
import { readWranglerJsonFile, diagnoseWranglerConfig, formatWranglerDiagnostics, applyCloudflareDefaults } from './wrangler.mjs';
|
|
@@ -101,6 +101,21 @@ const workersCachePluginPath = resolve(
|
|
|
101
101
|
import.meta.dirname,
|
|
102
102
|
import.meta.url.endsWith(".ts") ? "runtime/server/plugins/workers-cache.ts" : "runtime/server/plugins/workers-cache.js"
|
|
103
103
|
);
|
|
104
|
+
const wideEventsPluginPath = resolve(
|
|
105
|
+
import.meta.dirname,
|
|
106
|
+
import.meta.url.endsWith(".ts") ? "runtime/server/plugins/wide-events.ts" : "runtime/server/plugins/wide-events.js"
|
|
107
|
+
);
|
|
108
|
+
const WIDE_EVENT_FIELDS = [
|
|
109
|
+
"cf.colo",
|
|
110
|
+
"cf.country",
|
|
111
|
+
"cf.httpProtocol",
|
|
112
|
+
"d1.durationMs",
|
|
113
|
+
"d1.primaryQueries",
|
|
114
|
+
"d1.queries",
|
|
115
|
+
"d1.recoveries",
|
|
116
|
+
"d1.region",
|
|
117
|
+
"d1.unrecovered"
|
|
118
|
+
];
|
|
104
119
|
const BUILD_SECRET_ENV_RE = /(?:^|_)(?:API_?KEY|AUTH_TOKEN|CLIENT_SECRET|CREDENTIALS?|ENCRYPTION_KEY|PASSWORD|PRIVATE_KEY|SECRET|SIGNING_KEY|TOKEN|SALT)(?:_|$)/i;
|
|
105
120
|
const REQUIRED_BUILD_SECRET_NAMES = /* @__PURE__ */ new Set(["NUXT_SCRIPTS_PROXY_SECRET"]);
|
|
106
121
|
function resolveModuleWorkersCachePolicy(options) {
|
|
@@ -212,6 +227,16 @@ function setupCloudflareModule(options, nuxt) {
|
|
|
212
227
|
options,
|
|
213
228
|
Boolean(nuxt.options.sourcemap.server)
|
|
214
229
|
);
|
|
230
|
+
const registerWideEventFields = nuxt.hook;
|
|
231
|
+
registerWideEventFields("wide-events:fields", (registry) => {
|
|
232
|
+
registry.add("@harlan-zw/nuxt-cloudflare", [...WIDE_EVENT_FIELDS]);
|
|
233
|
+
});
|
|
234
|
+
if (Array.isArray(nuxt.options.modules) && hasNuxtModule("@harlan-zw/nuxt-wide-events", nuxt)) {
|
|
235
|
+
const wideEventsNitro = nuxt.options.nitro;
|
|
236
|
+
wideEventsNitro.plugins ??= [];
|
|
237
|
+
if (!wideEventsNitro.plugins.includes(wideEventsPluginPath))
|
|
238
|
+
wideEventsNitro.plugins.push(wideEventsPluginPath);
|
|
239
|
+
}
|
|
215
240
|
let bindingTypeSignature;
|
|
216
241
|
if (options.bindingTypes !== false) {
|
|
217
242
|
const bindingTypesTemplate = addTypeTemplate({
|
|
@@ -275,7 +300,7 @@ const module$1 = defineNuxtModule({
|
|
|
275
300
|
meta: {
|
|
276
301
|
name: "@harlan-zw/nuxt-cloudflare",
|
|
277
302
|
configKey: "nuxtCloudflare",
|
|
278
|
-
compatibility: { nuxt: ">=4.5.0" }
|
|
303
|
+
compatibility: { nuxt: ">=4.5.0 <5.0.0" }
|
|
279
304
|
},
|
|
280
305
|
defaults: {
|
|
281
306
|
bindingTypes: true,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { H3Event } from 'h3';
|
|
2
|
+
import type { NitroApp } from 'nitropack/types';
|
|
3
|
+
/**
|
|
4
|
+
* Contribute Cloudflare request context to the request's Wide Event.
|
|
5
|
+
*
|
|
6
|
+
* Two things worth one flat record per request, neither visible from
|
|
7
|
+
* application code:
|
|
8
|
+
*
|
|
9
|
+
* `cf.*` — which colo actually ran the isolate. A Worker's latency story is
|
|
10
|
+
* mostly "where did this run relative to its data", and the colo is the only
|
|
11
|
+
* field that answers it.
|
|
12
|
+
*
|
|
13
|
+
* `d1.*` — how many queries the request made, how many crossed to the primary
|
|
14
|
+
* rather than a replica, and whether the session had to recover. A request that
|
|
15
|
+
* quietly makes twenty serial primary round trips looks identical to a fast one
|
|
16
|
+
* in every other log line; these counters are what separate them.
|
|
17
|
+
*
|
|
18
|
+
* Registered by the module only when `@harlan-zw/nuxt-wide-events` is present,
|
|
19
|
+
* and the fields are declared through `addWideEventFields` at build time, so
|
|
20
|
+
* the consuming application never hand-lists them and cannot drift from them.
|
|
21
|
+
*/
|
|
22
|
+
declare const _default: (nitroApp: NitroApp) => void;
|
|
23
|
+
export default _default;
|
|
24
|
+
export declare function recordCloudflareWideEventFields(event: H3Event): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { addWideEventFields } from "#imports";
|
|
2
|
+
import { readD1Stats } from "../../../d1-stats";
|
|
3
|
+
export default (nitroApp) => {
|
|
4
|
+
nitroApp.hooks.hook("beforeResponse", (event) => {
|
|
5
|
+
recordCloudflareWideEventFields(event);
|
|
6
|
+
});
|
|
7
|
+
};
|
|
8
|
+
export function recordCloudflareWideEventFields(event) {
|
|
9
|
+
const fields = {};
|
|
10
|
+
const cf = event.context.cloudflare?.request?.cf;
|
|
11
|
+
if (cf) {
|
|
12
|
+
if (typeof cf.colo === "string")
|
|
13
|
+
fields["cf.colo"] = cf.colo;
|
|
14
|
+
if (typeof cf.country === "string")
|
|
15
|
+
fields["cf.country"] = cf.country;
|
|
16
|
+
if (typeof cf.httpProtocol === "string")
|
|
17
|
+
fields["cf.httpProtocol"] = cf.httpProtocol;
|
|
18
|
+
}
|
|
19
|
+
const d1 = readD1Stats(event.context);
|
|
20
|
+
if (d1) {
|
|
21
|
+
fields["d1.queries"] = d1.queries;
|
|
22
|
+
fields["d1.primaryQueries"] = d1.primaryQueries;
|
|
23
|
+
fields["d1.recoveries"] = d1.recoveries;
|
|
24
|
+
fields["d1.unrecovered"] = d1.unrecovered;
|
|
25
|
+
fields["d1.durationMs"] = Math.round(d1.durationMs);
|
|
26
|
+
fields["d1.region"] = d1.region;
|
|
27
|
+
}
|
|
28
|
+
if (Object.keys(fields).length)
|
|
29
|
+
addWideEventFields(event, fields);
|
|
30
|
+
}
|
package/package.json
CHANGED