emdash 0.10.0 → 0.11.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/dist/{apply-UsrFuO7l.mjs → apply-Ded_1vng.mjs} +36 -25
- package/dist/{apply-UsrFuO7l.mjs.map → apply-Ded_1vng.mjs.map} +1 -1
- package/dist/astro/index.d.mts +5 -5
- package/dist/astro/index.mjs +1 -1
- package/dist/astro/middleware/auth.d.mts +5 -5
- package/dist/astro/middleware/redirect.mjs +2 -2
- package/dist/astro/middleware.d.mts.map +1 -1
- package/dist/astro/middleware.mjs +83 -33
- package/dist/astro/middleware.mjs.map +1 -1
- package/dist/astro/types.d.mts +9 -7
- package/dist/astro/types.d.mts.map +1 -1
- package/dist/{byline-C3vnhIpU.mjs → byline-gFn1r0vA.mjs} +2 -2
- package/dist/{byline-C3vnhIpU.mjs.map → byline-gFn1r0vA.mjs.map} +1 -1
- package/dist/{bylines-esI7ioa9.mjs → bylines-DTFI8nDM.mjs} +4 -4
- package/dist/{bylines-esI7ioa9.mjs.map → bylines-DTFI8nDM.mjs.map} +1 -1
- package/dist/{cache-fTzxgMFJ.mjs → cache-BAJbeoZ8.mjs} +2 -2
- package/dist/{cache-fTzxgMFJ.mjs.map → cache-BAJbeoZ8.mjs.map} +1 -1
- package/dist/{chunks-Da2-b-oA.mjs → chunks-BK1oZS-l.mjs} +2 -2
- package/dist/{chunks-Da2-b-oA.mjs.map → chunks-BK1oZS-l.mjs.map} +1 -1
- package/dist/cli/index.mjs +102 -27
- package/dist/cli/index.mjs.map +1 -1
- package/dist/{content-C7G4QXkK.mjs → content-CERxPUN0.mjs} +2 -2
- package/dist/{content-C7G4QXkK.mjs.map → content-CERxPUN0.mjs.map} +1 -1
- package/dist/database/instrumentation.d.mts +6 -4
- package/dist/database/instrumentation.d.mts.map +1 -1
- package/dist/database/instrumentation.mjs +19 -7
- package/dist/database/instrumentation.mjs.map +1 -1
- package/dist/db/index.d.mts +2 -2
- package/dist/db/index.mjs +1 -1
- package/dist/{index-DjPMOfO0.d.mts → index-Cg-rC4Gj.d.mts} +32 -24
- package/dist/index-Cg-rC4Gj.d.mts.map +1 -0
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +19 -19
- package/dist/{load-sXRuM7Us.mjs → load-DR1VwFXR.mjs} +2 -2
- package/dist/{load-sXRuM7Us.mjs.map → load-DR1VwFXR.mjs.map} +1 -1
- package/dist/{loader-Bx2_9-5e.mjs → loader-ou_PXAjg.mjs} +2 -2
- package/dist/{loader-Bx2_9-5e.mjs.map → loader-ou_PXAjg.mjs.map} +1 -1
- package/dist/media/local-runtime.d.mts +5 -5
- package/dist/media/local-runtime.mjs +1 -1
- package/dist/{media-D8FbNsl0.mjs → media-1fFhub9c.mjs} +21 -9
- package/dist/media-1fFhub9c.mjs.map +1 -0
- package/dist/page/index.d.mts +2 -2
- package/dist/plugins/adapt-sandbox-entry.d.mts +5 -5
- package/dist/plugins/adapt-sandbox-entry.mjs +1 -1
- package/dist/{query-Bo-msrmu.mjs → query-8c_meo_K.mjs} +10 -10
- package/dist/{query-Bo-msrmu.mjs.map → query-8c_meo_K.mjs.map} +1 -1
- package/dist/{registry-Beb7wxFc.mjs → registry-Do34mz_P.mjs} +6 -5
- package/dist/registry-Do34mz_P.mjs.map +1 -0
- package/dist/{request-cache-C-tIpYIw.mjs → request-cache-D4I69LeL.mjs} +6 -2
- package/dist/request-cache-D4I69LeL.mjs.map +1 -0
- package/dist/request-context.d.mts +27 -1
- package/dist/request-context.d.mts.map +1 -1
- package/dist/request-context.mjs +16 -3
- package/dist/request-context.mjs.map +1 -1
- package/dist/{runner-DMnlIkh4.mjs → runner-DIcU2UCC.mjs} +174 -152
- package/dist/runner-DIcU2UCC.mjs.map +1 -0
- package/dist/{runner-Clwe4Mme.d.mts → runner-Iu3IZSDM.d.mts} +2 -2
- package/dist/{runner-Clwe4Mme.d.mts.map → runner-Iu3IZSDM.d.mts.map} +1 -1
- package/dist/runtime.d.mts +5 -5
- package/dist/runtime.mjs +1 -1
- package/dist/{search-DkN-BqsS.mjs → search-DuWhx4NG.mjs} +172 -30
- package/dist/search-DuWhx4NG.mjs.map +1 -0
- package/dist/seed/index.d.mts +2 -2
- package/dist/seed/index.mjs +10 -10
- package/dist/{taxonomies-CTtewrSQ.mjs → taxonomies-Bw76xAxo.mjs} +6 -6
- package/dist/{taxonomies-CTtewrSQ.mjs.map → taxonomies-Bw76xAxo.mjs.map} +1 -1
- package/dist/{taxonomy-DSxx2K2L.mjs → taxonomy-D6NvlKo8.mjs} +3 -3
- package/dist/{taxonomy-DSxx2K2L.mjs.map → taxonomy-D6NvlKo8.mjs.map} +1 -1
- package/dist/{types-Eg829jj9.mjs → types-56BKbld_.mjs} +1 -1
- package/dist/types-56BKbld_.mjs.map +1 -0
- package/dist/{types-Dtx1mSMX.d.mts → types-BQx6ZXpR.d.mts} +2 -1
- package/dist/types-BQx6ZXpR.d.mts.map +1 -0
- package/dist/types-DiI8NOG_.mjs +16 -0
- package/dist/types-DiI8NOG_.mjs.map +1 -0
- package/dist/{types-D19uBYWn.d.mts → types-IN5z_S3P.d.mts} +19 -98
- package/dist/types-IN5z_S3P.d.mts.map +1 -0
- package/dist/{types-Dl1fgFjn.d.mts → types-IZSZfEwv.d.mts} +4 -3
- package/dist/types-IZSZfEwv.d.mts.map +1 -0
- package/dist/{validate-DHGwADqO.d.mts → validate-CO3JjFV5.d.mts} +7 -3
- package/dist/validate-CO3JjFV5.d.mts.map +1 -0
- package/dist/{validate-CBIbxM3L.mjs → validate-UK4Ja1uo.mjs} +3 -3
- package/dist/{validate-CBIbxM3L.mjs.map → validate-UK4Ja1uo.mjs.map} +1 -1
- package/dist/{validation-B1NYiEos.mjs → validation-Vc5DQkJa.mjs} +4 -4
- package/dist/{validation-B1NYiEos.mjs.map → validation-Vc5DQkJa.mjs.map} +1 -1
- package/dist/version-Bg31I_Ff.mjs +7 -0
- package/dist/{version-CMD42IRC.mjs.map → version-Bg31I_Ff.mjs.map} +1 -1
- package/dist/{zod-generator-BNJDQBSZ.mjs → zod-generator-CHnJUP2l.mjs} +1 -1
- package/dist/{zod-generator-BNJDQBSZ.mjs.map → zod-generator-CHnJUP2l.mjs.map} +1 -1
- package/package.json +9 -8
- package/src/api/errors.ts +5 -0
- package/src/api/handlers/content.ts +9 -0
- package/src/api/handlers/media-allowlist.ts +40 -0
- package/src/api/handlers/media.ts +1 -1
- package/src/api/handlers/menus.ts +158 -28
- package/src/api/handlers/validate-media-fields.ts +125 -0
- package/src/api/schemas/media.ts +23 -3
- package/src/api/schemas/schema.ts +11 -2
- package/src/astro/middleware.ts +46 -11
- package/src/astro/routes/api/media/upload-url.ts +10 -4
- package/src/astro/routes/api/media.ts +12 -4
- package/src/astro/types.ts +5 -1
- package/src/auth/rate-limit.ts +3 -3
- package/src/cli/commands/bundle-utils.ts +81 -6
- package/src/cli/commands/bundle.ts +18 -15
- package/src/cli/commands/export-seed.ts +57 -3
- package/src/database/instrumentation.ts +22 -8
- package/src/database/migrations/016_api_tokens.ts +18 -3
- package/src/database/migrations/037_credential_algorithm.ts +18 -0
- package/src/database/migrations/runner.ts +2 -0
- package/src/database/repositories/media.ts +40 -10
- package/src/database/types.ts +2 -1
- package/src/emdash-runtime.ts +16 -3
- package/src/fields/file.ts +7 -6
- package/src/fields/image.ts +12 -11
- package/src/fields/types.ts +3 -0
- package/src/index.ts +1 -1
- package/src/mcp/server.ts +37 -8
- package/src/media/mime.ts +75 -0
- package/src/plugins/types.ts +81 -191
- package/src/request-cache.ts +6 -2
- package/src/request-context.ts +42 -2
- package/src/schema/registry.ts +5 -5
- package/src/schema/types.ts +3 -2
- package/src/seed/apply.ts +25 -8
- package/src/seed/types.ts +4 -0
- package/dist/index-DjPMOfO0.d.mts.map +0 -1
- package/dist/media-D8FbNsl0.mjs.map +0 -1
- package/dist/registry-Beb7wxFc.mjs.map +0 -1
- package/dist/request-cache-C-tIpYIw.mjs.map +0 -1
- package/dist/runner-DMnlIkh4.mjs.map +0 -1
- package/dist/search-DkN-BqsS.mjs.map +0 -1
- package/dist/types-CoO6mpV3.mjs +0 -68
- package/dist/types-CoO6mpV3.mjs.map +0 -1
- package/dist/types-D19uBYWn.d.mts.map +0 -1
- package/dist/types-Dl1fgFjn.d.mts.map +0 -1
- package/dist/types-Dtx1mSMX.d.mts.map +0 -1
- package/dist/types-Eg829jj9.mjs.map +0 -1
- package/dist/validate-DHGwADqO.d.mts.map +0 -1
- package/dist/version-CMD42IRC.mjs +0 -7
|
@@ -24,7 +24,11 @@ function requestCached(key, fn) {
|
|
|
24
24
|
store.set(ctx, cache);
|
|
25
25
|
}
|
|
26
26
|
const existing = cache.get(key);
|
|
27
|
-
if (existing)
|
|
27
|
+
if (existing) {
|
|
28
|
+
if (ctx.metrics) ctx.metrics.cacheHits += 1;
|
|
29
|
+
return existing;
|
|
30
|
+
}
|
|
31
|
+
if (ctx.metrics) ctx.metrics.cacheMisses += 1;
|
|
28
32
|
const promise = Promise.resolve().then(fn).catch((error) => {
|
|
29
33
|
cache.delete(key);
|
|
30
34
|
throw error;
|
|
@@ -76,4 +80,4 @@ function setRequestCacheEntry(key, value) {
|
|
|
76
80
|
|
|
77
81
|
//#endregion
|
|
78
82
|
export { requestCached as n, setRequestCacheEntry as r, peekRequestCache as t };
|
|
79
|
-
//# sourceMappingURL=request-cache-
|
|
83
|
+
//# sourceMappingURL=request-cache-D4I69LeL.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-cache-D4I69LeL.mjs","names":[],"sources":["../src/request-cache.ts"],"sourcesContent":["/**\n * Per-request query cache\n *\n * Deduplicates identical database queries within a single page render.\n * Uses the ALS request context as a WeakMap key so the cache is\n * automatically GC'd when the request completes.\n *\n * When no request context is available (e.g. local dev without D1\n * replicas), queries bypass the cache — local SQLite is fast enough\n * that deduplication doesn't matter.\n *\n * The WeakMap is stored on globalThis with a Symbol key to guarantee\n * a singleton even when bundlers duplicate this module across chunks\n * (same pattern as request-context.ts).\n */\n\nimport type { EmDashRequestContext } from \"./request-context.js\";\nimport { getRequestContext } from \"./request-context.js\";\n\ntype CacheStore = WeakMap<EmDashRequestContext, Map<string, Promise<unknown>>>;\n\nconst STORE_KEY = Symbol.for(\"emdash:request-cache\");\nconst g = globalThis as Record<symbol, unknown>;\nconst store: CacheStore =\n\t// eslint-disable-next-line typescript-eslint(no-unsafe-type-assertion) -- globalThis singleton pattern (see request-context.ts)\n\t(g[STORE_KEY] as CacheStore | undefined) ??\n\t(() => {\n\t\tconst wm: CacheStore = new WeakMap();\n\t\tg[STORE_KEY] = wm;\n\t\treturn wm;\n\t})();\n\n/**\n * Return a cached result for `key` if one exists in the current\n * request scope, otherwise call `fn`, cache its promise, and return it.\n *\n * Caches the *promise*, not the resolved value, so concurrent calls\n * with the same key share a single in-flight query.\n */\nexport function requestCached<T>(key: string, fn: () => Promise<T>): Promise<T> {\n\tconst ctx = getRequestContext();\n\tif (!ctx) return fn();\n\n\tlet cache = store.get(ctx);\n\tif (!cache) {\n\t\tcache = new Map();\n\t\tstore.set(ctx, cache);\n\t}\n\n\tconst existing = cache.get(key);\n\tif (existing) {\n\t\tif (ctx.metrics) ctx.metrics.cacheHits += 1;\n\t\t// eslint-disable-next-line typescript-eslint(no-unsafe-type-assertion) -- heterogeneous cache; key namespacing guarantees the stored promise resolves to T\n\t\treturn existing as Promise<T>;\n\t}\n\tif (ctx.metrics) ctx.metrics.cacheMisses += 1;\n\n\tconst promise = Promise.resolve()\n\t\t.then(fn)\n\t\t.catch((error) => {\n\t\t\tcache.delete(key);\n\t\t\tthrow error;\n\t\t});\n\tcache.set(key, promise);\n\treturn promise;\n}\n\n/**\n * Look up an entry in the request-scoped cache without inserting one.\n *\n * Returns the in-flight or resolved promise if the key exists in the\n * current request, otherwise `undefined`. Callers can use this to\n * opportunistically satisfy a narrower query (e.g. `getSiteSetting(\"seo\")`)\n * from a broader one (`getSiteSettings()`) that's already been loaded\n * by a parent template — avoiding a redundant round-trip.\n *\n * No-ops outside a request context.\n */\nexport function peekRequestCache<T>(key: string): Promise<T> | undefined {\n\tconst ctx = getRequestContext();\n\tif (!ctx) return undefined;\n\tconst cache = store.get(ctx);\n\t// eslint-disable-next-line typescript-eslint(no-unsafe-type-assertion) -- heterogeneous cache; caller is responsible for using a T-compatible key\n\treturn cache?.get(key) as Promise<T> | undefined;\n}\n\n/**\n * Pre-populate the request-scoped cache with a resolved value.\n *\n * Internal helper shared between hydration paths (taxonomy terms,\n * bylines, etc.) that already have the data in hand and want downstream\n * callers using `requestCached(key, ...)` to skip the database entirely.\n * Not exported from the package entrypoint — keep it internal until we\n * have a documented plugin/extension surface for hydration.\n *\n * No-ops outside a request context (local dev without ALS).\n *\n * Does not overwrite an existing entry — if a query for this key is already\n * in flight, its promise wins.\n */\nexport function setRequestCacheEntry<T>(key: string, value: T): void {\n\tconst ctx = getRequestContext();\n\tif (!ctx) return;\n\n\tlet cache = store.get(ctx);\n\tif (!cache) {\n\t\tcache = new Map();\n\t\tstore.set(ctx, cache);\n\t}\n\n\tif (cache.has(key)) return;\n\tcache.set(key, Promise.resolve(value));\n}\n"],"mappings":";;;AAqBA,MAAM,YAAY,OAAO,IAAI,uBAAuB;AACpD,MAAM,IAAI;AACV,MAAM,QAEJ,EAAE,qBACI;CACN,MAAM,qBAAiB,IAAI,SAAS;AACpC,GAAE,aAAa;AACf,QAAO;IACJ;;;;;;;;AASL,SAAgB,cAAiB,KAAa,IAAkC;CAC/E,MAAM,MAAM,mBAAmB;AAC/B,KAAI,CAAC,IAAK,QAAO,IAAI;CAErB,IAAI,QAAQ,MAAM,IAAI,IAAI;AAC1B,KAAI,CAAC,OAAO;AACX,0BAAQ,IAAI,KAAK;AACjB,QAAM,IAAI,KAAK,MAAM;;CAGtB,MAAM,WAAW,MAAM,IAAI,IAAI;AAC/B,KAAI,UAAU;AACb,MAAI,IAAI,QAAS,KAAI,QAAQ,aAAa;AAE1C,SAAO;;AAER,KAAI,IAAI,QAAS,KAAI,QAAQ,eAAe;CAE5C,MAAM,UAAU,QAAQ,SAAS,CAC/B,KAAK,GAAG,CACR,OAAO,UAAU;AACjB,QAAM,OAAO,IAAI;AACjB,QAAM;GACL;AACH,OAAM,IAAI,KAAK,QAAQ;AACvB,QAAO;;;;;;;;;;;;;AAcR,SAAgB,iBAAoB,KAAqC;CACxE,MAAM,MAAM,mBAAmB;AAC/B,KAAI,CAAC,IAAK,QAAO;AAGjB,QAFc,MAAM,IAAI,IAAI,EAEd,IAAI,IAAI;;;;;;;;;;;;;;;;AAiBvB,SAAgB,qBAAwB,KAAa,OAAgB;CACpE,MAAM,MAAM,mBAAmB;AAC/B,KAAI,CAAC,IAAK;CAEV,IAAI,QAAQ,MAAM,IAAI,IAAI;AAC1B,KAAI,CAAC,OAAO;AACX,0BAAQ,IAAI,KAAK;AACjB,QAAM,IAAI,KAAK,MAAM;;AAGtB,KAAI,MAAM,IAAI,IAAI,CAAE;AACpB,OAAM,IAAI,KAAK,QAAQ,QAAQ,MAAM,CAAC"}
|
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import { QueryRecorder } from "./database/instrumentation.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/request-context.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Lightweight always-on counters surfaced in Server-Timing.
|
|
6
|
+
*
|
|
7
|
+
* Bumped by the Kysely log hook (db queries) and by `requestCached`
|
|
8
|
+
* (cache hits/misses). Read by middleware after the response is
|
|
9
|
+
* generated to emit `db.*` and `cache.*` Server-Timing fields.
|
|
10
|
+
*
|
|
11
|
+
* Offsets are milliseconds from `start` (the request's entry into
|
|
12
|
+
* middleware), captured via `performance.now()`.
|
|
13
|
+
*/
|
|
14
|
+
interface RequestMetrics {
|
|
15
|
+
start: number;
|
|
16
|
+
dbCount: number;
|
|
17
|
+
dbTotalMs: number;
|
|
18
|
+
dbFirstOffset: number | null;
|
|
19
|
+
dbLastOffset: number | null;
|
|
20
|
+
cacheHits: number;
|
|
21
|
+
cacheMisses: number;
|
|
22
|
+
}
|
|
23
|
+
declare function createRequestMetrics(start: number): RequestMetrics;
|
|
4
24
|
interface EmDashRequestContext {
|
|
5
25
|
/** Whether the current request is in visual editing mode */
|
|
6
26
|
editMode: boolean;
|
|
@@ -36,6 +56,12 @@ interface EmDashRequestContext {
|
|
|
36
56
|
* to NDJSON after the response.
|
|
37
57
|
*/
|
|
38
58
|
queryRecorder?: QueryRecorder;
|
|
59
|
+
/**
|
|
60
|
+
* Per-request metrics for Server-Timing. Always attached by middleware
|
|
61
|
+
* for requests that emit timing headers; bumped by the Kysely log hook
|
|
62
|
+
* and `requestCached`.
|
|
63
|
+
*/
|
|
64
|
+
metrics?: RequestMetrics;
|
|
39
65
|
}
|
|
40
66
|
/**
|
|
41
67
|
* Run a function within an EmDash request context.
|
|
@@ -48,5 +74,5 @@ declare function runWithContext<T>(ctx: EmDashRequestContext, fn: () => T): T;
|
|
|
48
74
|
*/
|
|
49
75
|
declare function getRequestContext(): EmDashRequestContext | undefined;
|
|
50
76
|
//#endregion
|
|
51
|
-
export { EmDashRequestContext, getRequestContext, runWithContext };
|
|
77
|
+
export { EmDashRequestContext, RequestMetrics, createRequestMetrics, getRequestContext, runWithContext };
|
|
52
78
|
//# sourceMappingURL=request-context.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.d.mts","names":[],"sources":["../src/request-context.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-context.d.mts","names":[],"sources":["../src/request-context.ts"],"mappings":";;;;;AAuDA;;;;;;;;UAtBiB,cAAA;EAChB,KAAA;EACA,OAAA;EACA,SAAA;EACA,aAAA;EACA,YAAA;EACA,SAAA;EACA,WAAA;AAAA;AAAA,iBAGe,oBAAA,CAAqB,KAAA,WAAgB,cAAA;AAAA,UAYpC,oBAAA;EA4Da;EA1D7B,QAAA;EA0DsE;EAxDtE,OAAA;IACC,UAAA;IACA,EAAA;EAAA;EAsDqC;EAnDtC,MAAA;EAmDsE;;;;;AAQvE;;EAnDC,EAAA;EAmDoC;;;;;;;;;;EAxCpC,YAAA;;;;;;EAMA,aAAA,GAAgB,aAAA;;;;;;EAMhB,OAAA,GAAU,cAAA;AAAA;;;;;iBAoBK,cAAA,GAAA,CAAkB,GAAA,EAAK,oBAAA,EAAsB,EAAA,QAAU,CAAA,GAAI,CAAA;;;;;iBAQ3D,iBAAA,CAAA,GAAqB,oBAAA"}
|
package/dist/request-context.mjs
CHANGED
|
@@ -8,8 +8,10 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
8
8
|
* without requiring explicit parameter passing. The middleware wraps next()
|
|
9
9
|
* in als.run(), making the context available to all code during rendering.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
11
|
+
* Middleware always wraps each request in a context so per-request
|
|
12
|
+
* metrics (db.*, cache.*) can be surfaced via Server-Timing. The cost is
|
|
13
|
+
* one ALS frame per request — sub-microsecond, negligible compared to
|
|
14
|
+
* any real work.
|
|
13
15
|
*
|
|
14
16
|
* The AsyncLocalStorage instance is stored on globalThis with a Symbol key
|
|
15
17
|
* to guarantee a singleton even when bundlers duplicate this module across
|
|
@@ -17,6 +19,17 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
17
19
|
* multiple chunks (e.g. middleware and page components), each with its own
|
|
18
20
|
* ALS instance — breaking request-scoped state propagation.
|
|
19
21
|
*/
|
|
22
|
+
function createRequestMetrics(start) {
|
|
23
|
+
return {
|
|
24
|
+
start,
|
|
25
|
+
dbCount: 0,
|
|
26
|
+
dbTotalMs: 0,
|
|
27
|
+
dbFirstOffset: null,
|
|
28
|
+
dbLastOffset: null,
|
|
29
|
+
cacheHits: 0,
|
|
30
|
+
cacheMisses: 0
|
|
31
|
+
};
|
|
32
|
+
}
|
|
20
33
|
const ALS_KEY = Symbol.for("emdash:request-context");
|
|
21
34
|
const storage = globalThis[ALS_KEY] ?? (() => {
|
|
22
35
|
const als = new AsyncLocalStorage();
|
|
@@ -39,5 +52,5 @@ function getRequestContext() {
|
|
|
39
52
|
}
|
|
40
53
|
|
|
41
54
|
//#endregion
|
|
42
|
-
export { getRequestContext, runWithContext };
|
|
55
|
+
export { createRequestMetrics, getRequestContext, runWithContext };
|
|
43
56
|
//# sourceMappingURL=request-context.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-context.mjs","names":[],"sources":["../src/request-context.ts"],"sourcesContent":["/**\n * EmDash Request Context\n *\n * Uses AsyncLocalStorage to provide request-scoped state to query functions\n * without requiring explicit parameter passing. The middleware wraps next()\n * in als.run(), making the context available to all code during rendering.\n *\n *
|
|
1
|
+
{"version":3,"file":"request-context.mjs","names":[],"sources":["../src/request-context.ts"],"sourcesContent":["/**\n * EmDash Request Context\n *\n * Uses AsyncLocalStorage to provide request-scoped state to query functions\n * without requiring explicit parameter passing. The middleware wraps next()\n * in als.run(), making the context available to all code during rendering.\n *\n * Middleware always wraps each request in a context so per-request\n * metrics (db.*, cache.*) can be surfaced via Server-Timing. The cost is\n * one ALS frame per request — sub-microsecond, negligible compared to\n * any real work.\n *\n * The AsyncLocalStorage instance is stored on globalThis with a Symbol key\n * to guarantee a singleton even when bundlers duplicate this module across\n * code-split chunks. Without this, Rollup/Vite may inline the module into\n * multiple chunks (e.g. middleware and page components), each with its own\n * ALS instance — breaking request-scoped state propagation.\n */\n\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nimport type { QueryRecorder } from \"./database/instrumentation.js\";\n\n/**\n * Lightweight always-on counters surfaced in Server-Timing.\n *\n * Bumped by the Kysely log hook (db queries) and by `requestCached`\n * (cache hits/misses). Read by middleware after the response is\n * generated to emit `db.*` and `cache.*` Server-Timing fields.\n *\n * Offsets are milliseconds from `start` (the request's entry into\n * middleware), captured via `performance.now()`.\n */\nexport interface RequestMetrics {\n\tstart: number;\n\tdbCount: number;\n\tdbTotalMs: number;\n\tdbFirstOffset: number | null;\n\tdbLastOffset: number | null;\n\tcacheHits: number;\n\tcacheMisses: number;\n}\n\nexport function createRequestMetrics(start: number): RequestMetrics {\n\treturn {\n\t\tstart,\n\t\tdbCount: 0,\n\t\tdbTotalMs: 0,\n\t\tdbFirstOffset: null,\n\t\tdbLastOffset: null,\n\t\tcacheHits: 0,\n\t\tcacheMisses: 0,\n\t};\n}\n\nexport interface EmDashRequestContext {\n\t/** Whether the current request is in visual editing mode */\n\teditMode: boolean;\n\t/** Preview token info, if this is a preview request */\n\tpreview?: {\n\t\tcollection: string;\n\t\tid: string;\n\t};\n\t/** Current locale from Astro's i18n routing (when configured) */\n\tlocale?: string;\n\t/**\n\t * Per-request database override.\n\t *\n\t * Set by middleware when D1 read replica sessions are enabled.\n\t * The runtime's `db` getter checks this first, falling back to\n\t * the singleton instance. Also used by the DO preview pattern.\n\t */\n\tdb?: unknown;\n\t/**\n\t * Indicates the per-request `db` points at an isolated database\n\t * instance whose schema may diverge from the configured one\n\t * (playground, DO preview sessions). When true, schema-derived caches\n\t * (manifest, taxonomy defs, etc.) must not be reused across requests.\n\t *\n\t * Plain D1 Sessions API routing does NOT set this — sessions are just\n\t * a routing hint over the same schema, so the module-scoped manifest\n\t * cache remains valid.\n\t */\n\tdbIsIsolated?: boolean;\n\t/**\n\t * Query recorder attached by middleware when EMDASH_QUERY_LOG_FILE is set.\n\t * The Kysely `log` hook appends an event per query; middleware flushes\n\t * to NDJSON after the response.\n\t */\n\tqueryRecorder?: QueryRecorder;\n\t/**\n\t * Per-request metrics for Server-Timing. Always attached by middleware\n\t * for requests that emit timing headers; bumped by the Kysely log hook\n\t * and `requestCached`.\n\t */\n\tmetrics?: RequestMetrics;\n}\n\nconst ALS_KEY = Symbol.for(\"emdash:request-context\");\n\nconst storage: AsyncLocalStorage<EmDashRequestContext> =\n\t// eslint-disable-next-line typescript-eslint(no-unsafe-type-assertion) -- globalThis singleton pattern\n\t((globalThis as Record<symbol, unknown>)[ALS_KEY] as\n\t\t| AsyncLocalStorage<EmDashRequestContext>\n\t\t| undefined) ??\n\t(() => {\n\t\tconst als = new AsyncLocalStorage<EmDashRequestContext>();\n\t\t(globalThis as Record<symbol, unknown>)[ALS_KEY] = als;\n\t\treturn als;\n\t})();\n\n/**\n * Run a function within an EmDash request context.\n * Called by middleware to wrap next().\n */\nexport function runWithContext<T>(ctx: EmDashRequestContext, fn: () => T): T {\n\treturn storage.run(ctx, fn);\n}\n\n/**\n * Get the current request context.\n * Returns undefined if no context is set (logged-out fast path).\n */\nexport function getRequestContext(): EmDashRequestContext | undefined {\n\treturn storage.getStore();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2CA,SAAgB,qBAAqB,OAA+B;AACnE,QAAO;EACN;EACA,SAAS;EACT,WAAW;EACX,eAAe;EACf,cAAc;EACd,WAAW;EACX,aAAa;EACb;;AA8CF,MAAM,UAAU,OAAO,IAAI,yBAAyB;AAEpD,MAAM,UAEH,WAAuC,mBAGlC;CACN,MAAM,MAAM,IAAI,mBAAyC;AACzD,CAAC,WAAuC,WAAW;AACnD,QAAO;IACJ;;;;;AAML,SAAgB,eAAkB,KAA2B,IAAgB;AAC5E,QAAO,QAAQ,IAAI,KAAK,GAAG;;;;;;AAO5B,SAAgB,oBAAsD;AACrE,QAAO,QAAQ,UAAU"}
|