@harlan-zw/nuxt-cloudflare 0.0.16 → 0.0.18
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-stats.d.mts +54 -0
- package/dist/d1-stats.mjs +35 -0
- package/dist/d1.d.mts +4 -53
- package/dist/d1.mjs +3 -34
- package/dist/module.json +1 -1
- package/dist/runtime/server/plugins/wide-events.d.ts +13 -3
- package/dist/runtime/server/plugins/wide-events.js +16 -19
- package/package.json +3 -2
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request-scoped D1 counters.
|
|
3
|
+
*
|
|
4
|
+
* D1 answers every remote query with a `meta` block naming the node that served
|
|
5
|
+
* it — `served_by_primary`, `served_by_region` — and the session layer here
|
|
6
|
+
* already knows when it had to recover from a reset. Neither fact survives the
|
|
7
|
+
* request unless something records it, and both are exactly what you want when
|
|
8
|
+
* a request is slow or an isolate dies: how many queries did this request make,
|
|
9
|
+
* how many of them crossed to the primary, and did the session have to restart.
|
|
10
|
+
*
|
|
11
|
+
* Kept separate from the session machinery so it stays optional. A caller that
|
|
12
|
+
* wants no telemetry passes no stats object and pays nothing.
|
|
13
|
+
*/
|
|
14
|
+
declare const REQUEST_D1_STATS: unique symbol;
|
|
15
|
+
interface D1RequestStats {
|
|
16
|
+
/** Terminal statement executions and batches. */
|
|
17
|
+
queries: number;
|
|
18
|
+
/** Of those, served by the primary rather than a replica. */
|
|
19
|
+
primaryQueries: number;
|
|
20
|
+
/** Sessions restarted after a reset the layer could recover from. */
|
|
21
|
+
recoveries: number;
|
|
22
|
+
/** Recoveries that gave up, i.e. the caller saw the error. */
|
|
23
|
+
unrecovered: number;
|
|
24
|
+
/** Region of the last node that served a query, e.g. `OC`. */
|
|
25
|
+
region: string | null;
|
|
26
|
+
/** Total SQL execution time D1 reported, in milliseconds. */
|
|
27
|
+
durationMs: number;
|
|
28
|
+
}
|
|
29
|
+
declare function createD1Stats(): D1RequestStats;
|
|
30
|
+
/**
|
|
31
|
+
* The stats object for this request, created on first use.
|
|
32
|
+
*
|
|
33
|
+
* Takes the request context rather than an `H3Event` so the same helper works
|
|
34
|
+
* in a queue consumer or a scheduled task, which have no event but do have a
|
|
35
|
+
* per-invocation object to hang state on.
|
|
36
|
+
*/
|
|
37
|
+
declare function useD1Stats(requestContext: Record<PropertyKey, unknown>): D1RequestStats;
|
|
38
|
+
/** The stats recorded so far, or `null` when this request never touched D1. */
|
|
39
|
+
declare function readD1Stats(requestContext: Record<PropertyKey, unknown> | undefined): D1RequestStats | null;
|
|
40
|
+
/**
|
|
41
|
+
* Fold one D1 result's `meta` into the request's counters.
|
|
42
|
+
*
|
|
43
|
+
* Every field is checked before use: `meta` is absent under `wrangler dev` and
|
|
44
|
+
* on a local D1, where these keys do not exist at all, and a telemetry helper
|
|
45
|
+
* that throws in development would be worse than one that records nothing.
|
|
46
|
+
*/
|
|
47
|
+
declare function recordD1Meta(stats: D1RequestStats, result: unknown): void;
|
|
48
|
+
/** Fold a recovery event into the request's counters. */
|
|
49
|
+
declare function recordD1Recovery(stats: D1RequestStats, event: {
|
|
50
|
+
_tag: 'retrying' | 'stopped';
|
|
51
|
+
}): void;
|
|
52
|
+
|
|
53
|
+
export { REQUEST_D1_STATS, createD1Stats, readD1Stats, recordD1Meta, recordD1Recovery, useD1Stats };
|
|
54
|
+
export type { D1RequestStats };
|
|
@@ -0,0 +1,35 @@
|
|
|
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 readD1Stats(requestContext) {
|
|
14
|
+
const stats = requestContext?.[REQUEST_D1_STATS];
|
|
15
|
+
return stats && typeof stats === "object" ? stats : null;
|
|
16
|
+
}
|
|
17
|
+
function recordD1Meta(stats, result) {
|
|
18
|
+
stats.queries++;
|
|
19
|
+
const meta = result?.meta;
|
|
20
|
+
if (!meta)
|
|
21
|
+
return;
|
|
22
|
+
if (meta.served_by_primary === true)
|
|
23
|
+
stats.primaryQueries++;
|
|
24
|
+
if (typeof meta.served_by_region === "string")
|
|
25
|
+
stats.region = meta.served_by_region;
|
|
26
|
+
if (typeof meta.duration === "number")
|
|
27
|
+
stats.durationMs += meta.duration;
|
|
28
|
+
}
|
|
29
|
+
function recordD1Recovery(stats, event) {
|
|
30
|
+
stats.recoveries++;
|
|
31
|
+
if (event._tag === "stopped")
|
|
32
|
+
stats.unrecovered++;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { REQUEST_D1_STATS, createD1Stats, readD1Stats, recordD1Meta, recordD1Recovery, useD1Stats };
|
package/dist/d1.d.mts
CHANGED
|
@@ -1,54 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* D1 answers every remote query with a `meta` block naming the node that served
|
|
5
|
-
* it — `served_by_primary`, `served_by_region` — and the session layer here
|
|
6
|
-
* already knows when it had to recover from a reset. Neither fact survives the
|
|
7
|
-
* request unless something records it, and both are exactly what you want when
|
|
8
|
-
* a request is slow or an isolate dies: how many queries did this request make,
|
|
9
|
-
* how many of them crossed to the primary, and did the session have to restart.
|
|
10
|
-
*
|
|
11
|
-
* Kept separate from the session machinery so it stays optional. A caller that
|
|
12
|
-
* wants no telemetry passes no stats object and pays nothing.
|
|
13
|
-
*/
|
|
14
|
-
declare const REQUEST_D1_STATS: unique symbol;
|
|
15
|
-
interface D1RequestStats {
|
|
16
|
-
/** Terminal statement executions and batches. */
|
|
17
|
-
queries: number;
|
|
18
|
-
/** Of those, served by the primary rather than a replica. */
|
|
19
|
-
primaryQueries: number;
|
|
20
|
-
/** Sessions restarted after a reset the layer could recover from. */
|
|
21
|
-
recoveries: number;
|
|
22
|
-
/** Recoveries that gave up, i.e. the caller saw the error. */
|
|
23
|
-
unrecovered: number;
|
|
24
|
-
/** Region of the last node that served a query, e.g. `OC`. */
|
|
25
|
-
region: string | null;
|
|
26
|
-
/** Total SQL execution time D1 reported, in milliseconds. */
|
|
27
|
-
durationMs: number;
|
|
28
|
-
}
|
|
29
|
-
declare function createD1Stats(): D1RequestStats;
|
|
30
|
-
/**
|
|
31
|
-
* The stats object for this request, created on first use.
|
|
32
|
-
*
|
|
33
|
-
* Takes the request context rather than an `H3Event` so the same helper works
|
|
34
|
-
* in a queue consumer or a scheduled task, which have no event but do have a
|
|
35
|
-
* per-invocation object to hang state on.
|
|
36
|
-
*/
|
|
37
|
-
declare function useD1Stats(requestContext: Record<PropertyKey, unknown>): D1RequestStats;
|
|
38
|
-
/** The stats recorded so far, or `null` when this request never touched D1. */
|
|
39
|
-
declare function readD1Stats(requestContext: Record<PropertyKey, unknown> | undefined): D1RequestStats | null;
|
|
40
|
-
/**
|
|
41
|
-
* Fold one D1 result's `meta` into the request's counters.
|
|
42
|
-
*
|
|
43
|
-
* Every field is checked before use: `meta` is absent under `wrangler dev` and
|
|
44
|
-
* on a local D1, where these keys do not exist at all, and a telemetry helper
|
|
45
|
-
* that throws in development would be worse than one that records nothing.
|
|
46
|
-
*/
|
|
47
|
-
declare function recordD1Meta(stats: D1RequestStats, result: unknown): void;
|
|
48
|
-
/** Fold a recovery event into the request's counters. */
|
|
49
|
-
declare function recordD1Recovery(stats: D1RequestStats, event: {
|
|
50
|
-
_tag: 'retrying' | 'stopped';
|
|
51
|
-
}): void;
|
|
1
|
+
import { D1RequestStats } from './d1-stats.mjs';
|
|
2
|
+
export { REQUEST_D1_STATS, createD1Stats, readD1Stats, recordD1Meta, recordD1Recovery, useD1Stats } from './d1-stats.mjs';
|
|
52
3
|
|
|
53
4
|
type D1WriteSafety = {
|
|
54
5
|
_tag: 'lock-only';
|
|
@@ -149,5 +100,5 @@ declare function getRequestD1Session<Session>(requestContext: Record<PropertyKey
|
|
|
149
100
|
declare function getRecoveringRequestD1Session<Session extends D1SessionLike>(requestContext: Record<PropertyKey, unknown>, binding: string, database: D1SessionSource<Session>, options?: D1ResetRecoveryOptions): Session;
|
|
150
101
|
declare function retryIdempotentD1Write<T>(options: RetryIdempotentD1WriteOptions<T>): Promise<T>;
|
|
151
102
|
|
|
152
|
-
export {
|
|
153
|
-
export type { D1Consistency, D1ParameterPlan, D1ParameterPlanInput, D1RecoveryEvent,
|
|
103
|
+
export { D1RequestStats, D1_MAX_BOUND_PARAMETERS, assertD1BoundParameters, chunkD1Items, classifyD1RetryError, defineD1ParameterPlan, getRecoveringRequestD1Session, getRequestD1Session, isReplayableD1Sql, isTransientD1Error, retryIdempotentD1Write, withD1ResetRecovery };
|
|
104
|
+
export type { D1Consistency, D1ParameterPlan, D1ParameterPlanInput, D1RecoveryEvent, D1ResetRecoveryOptions, D1RetryErrorKind, D1SessionLike, D1SessionSource, D1StatementLike, D1WriteSafety, RetryIdempotentD1WriteOptions };
|
package/dist/d1.mjs
CHANGED
|
@@ -1,36 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 readD1Stats(requestContext) {
|
|
14
|
-
const stats = requestContext?.[REQUEST_D1_STATS];
|
|
15
|
-
return stats && typeof stats === "object" ? stats : null;
|
|
16
|
-
}
|
|
17
|
-
function recordD1Meta(stats, result) {
|
|
18
|
-
stats.queries++;
|
|
19
|
-
const meta = result?.meta;
|
|
20
|
-
if (!meta)
|
|
21
|
-
return;
|
|
22
|
-
if (meta.served_by_primary === true)
|
|
23
|
-
stats.primaryQueries++;
|
|
24
|
-
if (typeof meta.served_by_region === "string")
|
|
25
|
-
stats.region = meta.served_by_region;
|
|
26
|
-
if (typeof meta.duration === "number")
|
|
27
|
-
stats.durationMs += meta.duration;
|
|
28
|
-
}
|
|
29
|
-
function recordD1Recovery(stats, event) {
|
|
30
|
-
stats.recoveries++;
|
|
31
|
-
if (event._tag === "stopped")
|
|
32
|
-
stats.unrecovered++;
|
|
33
|
-
}
|
|
1
|
+
import { useD1Stats, recordD1Meta, recordD1Recovery } from './d1-stats.mjs';
|
|
2
|
+
export { REQUEST_D1_STATS, createD1Stats, readD1Stats } from './d1-stats.mjs';
|
|
34
3
|
|
|
35
4
|
const D1_MAX_BOUND_PARAMETERS = 100;
|
|
36
5
|
const D1_PARAMETER_PLAN = Symbol("@harlan-zw/nuxt-cloudflare:d1-parameter-plan");
|
|
@@ -416,4 +385,4 @@ async function retryIdempotentD1Write(options) {
|
|
|
416
385
|
throw lastError;
|
|
417
386
|
}
|
|
418
387
|
|
|
419
|
-
export { D1_MAX_BOUND_PARAMETERS,
|
|
388
|
+
export { D1_MAX_BOUND_PARAMETERS, assertD1BoundParameters, chunkD1Items, classifyD1RetryError, defineD1ParameterPlan, getRecoveringRequestD1Session, getRequestD1Session, isReplayableD1Sql, isTransientD1Error, recordD1Meta, recordD1Recovery, retryIdempotentD1Write, useD1Stats, withD1ResetRecovery };
|
package/dist/module.json
CHANGED
|
@@ -15,9 +15,19 @@ import type { NitroApp } from 'nitropack/types';
|
|
|
15
15
|
* quietly makes twenty serial primary round trips looks identical to a fast one
|
|
16
16
|
* in every other log line; these counters are what separate them.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* ── The single object literal is load-bearing ──
|
|
19
|
+
*
|
|
20
|
+
* `@harlan-zw/nuxt-wide-events` parses every server file at build time and
|
|
21
|
+
* REJECTS an `addWideEventFields` call whose argument is not an object literal:
|
|
22
|
+
* no variables, no spreads, no computed keys. That is the guarantee the whole
|
|
23
|
+
* module rests on — a reviewer can read the allowlist and know nothing else can
|
|
24
|
+
* reach an event.
|
|
25
|
+
*
|
|
26
|
+
* So this cannot build its payload conditionally. Every key is present on every
|
|
27
|
+
* call, `null` where the value is unavailable, and the whole thing is one
|
|
28
|
+
* literal. An earlier version assembled a `Record` and passed the variable; it
|
|
29
|
+
* failed the CONSUMING application's build, which is the worst place for a
|
|
30
|
+
* module's mistake to surface.
|
|
21
31
|
*/
|
|
22
32
|
declare const _default: (nitroApp: NitroApp) => void;
|
|
23
33
|
export default _default;
|
|
@@ -5,26 +5,23 @@ export default (nitroApp) => {
|
|
|
5
5
|
recordCloudflareWideEventFields(event);
|
|
6
6
|
});
|
|
7
7
|
};
|
|
8
|
+
function stringOrNull(value) {
|
|
9
|
+
return typeof value === "string" ? value : null;
|
|
10
|
+
}
|
|
8
11
|
export function recordCloudflareWideEventFields(event) {
|
|
9
|
-
const fields = {};
|
|
10
12
|
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
13
|
const d1 = readD1Stats(event.context);
|
|
20
|
-
if (d1)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
14
|
+
if (!cf && !d1)
|
|
15
|
+
return;
|
|
16
|
+
addWideEventFields(event, {
|
|
17
|
+
"cf.colo": stringOrNull(cf?.colo),
|
|
18
|
+
"cf.country": stringOrNull(cf?.country),
|
|
19
|
+
"cf.httpProtocol": stringOrNull(cf?.httpProtocol),
|
|
20
|
+
"d1.queries": d1 ? d1.queries : null,
|
|
21
|
+
"d1.primaryQueries": d1 ? d1.primaryQueries : null,
|
|
22
|
+
"d1.recoveries": d1 ? d1.recoveries : null,
|
|
23
|
+
"d1.unrecovered": d1 ? d1.unrecovered : null,
|
|
24
|
+
"d1.durationMs": d1 ? Math.round(d1.durationMs) : null,
|
|
25
|
+
"d1.region": d1 ? d1.region : null
|
|
26
|
+
});
|
|
30
27
|
}
|
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.0.18",
|
|
5
5
|
"description": "Opinionated Cloudflare deployment defaults and diagnostics for Nuxt.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -105,7 +105,8 @@
|
|
|
105
105
|
"typescript": "^5.9.3",
|
|
106
106
|
"unbuild": "^3.6.1",
|
|
107
107
|
"vitest": "^4.1.10",
|
|
108
|
-
"wrangler": "^4.120.1"
|
|
108
|
+
"wrangler": "^4.120.1",
|
|
109
|
+
"@harlan-zw/nuxt-wide-events": "0.0.3"
|
|
109
110
|
},
|
|
110
111
|
"scripts": {
|
|
111
112
|
"build": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt-module-build build",
|