@harlan-zw/nuxt-cloudflare 0.0.17 → 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.
@@ -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
- * 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;
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 { D1_MAX_BOUND_PARAMETERS, REQUEST_D1_STATS, assertD1BoundParameters, chunkD1Items, classifyD1RetryError, createD1Stats, defineD1ParameterPlan, getRecoveringRequestD1Session, getRequestD1Session, isReplayableD1Sql, isTransientD1Error, readD1Stats, recordD1Meta, recordD1Recovery, retryIdempotentD1Write, useD1Stats, withD1ResetRecovery };
153
- export type { D1Consistency, D1ParameterPlan, D1ParameterPlanInput, D1RecoveryEvent, D1RequestStats, D1ResetRecoveryOptions, D1RetryErrorKind, D1SessionLike, D1SessionSource, D1StatementLike, D1WriteSafety, RetryIdempotentD1WriteOptions };
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
- 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
- }
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, REQUEST_D1_STATS, assertD1BoundParameters, chunkD1Items, classifyD1RetryError, createD1Stats, defineD1ParameterPlan, getRecoveringRequestD1Session, getRequestD1Session, isReplayableD1Sql, isTransientD1Error, readD1Stats, recordD1Meta, recordD1Recovery, retryIdempotentD1Write, useD1Stats, withD1ResetRecovery };
388
+ export { D1_MAX_BOUND_PARAMETERS, assertD1BoundParameters, chunkD1Items, classifyD1RetryError, defineD1ParameterPlan, getRecoveringRequestD1Session, getRequestD1Session, isReplayableD1Sql, isTransientD1Error, recordD1Meta, recordD1Recovery, retryIdempotentD1Write, useD1Stats, withD1ResetRecovery };
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=4.5.0 <5.0.0"
6
6
  },
7
- "version": "0.0.17",
7
+ "version": "0.0.18",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.3",
10
10
  "unbuild": "3.6.1"
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.17",
4
+ "version": "0.0.18",
5
5
  "description": "Opinionated Cloudflare deployment defaults and diagnostics for Nuxt.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",