@teyik0/furin 0.2.0-alpha.3 → 0.2.0-alpha.5
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/LICENSE +21 -21
- package/README.md +37 -33
- package/dist/adapter/bun.d.ts +17 -1
- package/dist/adapter/package.d.ts +26 -0
- package/dist/build/client.d.ts +1 -1
- package/dist/build/compile-entry.d.ts +3 -3
- package/dist/build/compile-entry.ts +114 -109
- package/dist/build/entry-template.d.ts +35 -9
- package/dist/build/entry-template.ts +206 -156
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +643 -247
- package/dist/build/scan-server.d.ts +10 -4
- package/dist/build/server-routes-entry.d.ts +3 -3
- package/dist/build/server-routes-entry.ts +30 -36
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +36 -1
- package/dist/cli/config.d.ts +6 -1
- package/dist/cli/index.js +662 -260
- package/dist/client/link.js +163 -16
- package/dist/client/router/abort.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +29 -0
- package/dist/config.d.ts +11 -2
- package/dist/config.js +5 -1
- package/dist/furin.d.ts +37 -19
- package/dist/furin.js +982 -378
- package/dist/server/auto-invalidate/registry.d.ts +8 -1
- package/dist/server/cache/dev-loader.d.ts +3 -2
- package/dist/server/cache/invalidation.d.ts +19 -0
- package/dist/server/cache/isr.d.ts +6 -2
- package/dist/server/cache/registry.d.ts +3 -3
- package/dist/server/cache/ssg.d.ts +6 -2
- package/dist/server/cache/store-view.d.ts +11 -0
- package/dist/server/instance.d.ts +86 -0
- package/dist/server/internal.d.ts +16 -1
- package/dist/server/internal.js +72 -5
- package/dist/server/render/template.d.ts +3 -2
- package/dist/server/sync/adapter.d.ts +62 -0
- package/dist/server/sync/config.d.ts +11 -0
- package/dist/server/sync/fingerprint.d.ts +6 -0
- package/dist/server/sync/index.d.ts +2 -2
- package/dist/server/sync/memory-adapter.d.ts +17 -0
- package/dist/server/sync/plugin.d.ts +12 -195
- package/dist/server/sync/response.d.ts +4 -0
- package/dist/server/sync/stream.d.ts +24 -5
- package/dist/shared/prefix.d.ts +15 -0
- package/package.json +6 -1
- package/src/adapter/README.md +13 -13
- package/src/adapter/bun.ts +113 -50
- package/src/adapter/package.ts +206 -0
- package/src/build/client.ts +158 -152
- package/src/build/compile-entry.ts +114 -109
- package/src/build/entry-template.ts +206 -156
- package/src/build/hydrate.ts +342 -340
- package/src/build/index.ts +223 -152
- package/src/build/route-types.ts +145 -145
- package/src/build/scan-server.ts +111 -95
- package/src/build/server-routes-entry.ts +30 -36
- package/src/build/shared.ts +80 -80
- package/src/build/ssg-cache.ts +36 -32
- package/src/build/types.ts +125 -89
- package/src/cli/config.ts +9 -3
- package/src/cli/index.ts +21 -3
- package/src/client/router/abort.ts +5 -0
- package/src/client/router/link-utils.ts +184 -184
- package/src/client/router/provider.tsx +883 -804
- package/src/client/router/sync-catch-up.ts +113 -0
- package/src/client/router/types.ts +276 -276
- package/src/client/sync.ts +98 -98
- package/src/client.ts +369 -369
- package/src/config.ts +14 -1
- package/src/furin.ts +619 -465
- package/src/server/auto-invalidate/registry.ts +23 -1
- package/src/server/auto-invalidate/runtime.ts +159 -134
- package/src/server/cache/dev-loader.ts +77 -38
- package/src/server/cache/invalidation.ts +71 -30
- package/src/server/cache/isr.ts +20 -5
- package/src/server/cache/registry.ts +17 -20
- package/src/server/cache/ssg.ts +19 -4
- package/src/server/cache/store-view.ts +30 -0
- package/src/server/dev-page-plugin.ts +454 -454
- package/src/server/instance.ts +252 -0
- package/src/server/internal.ts +100 -5
- package/src/server/render/assemble.ts +160 -160
- package/src/server/render/isr.ts +323 -323
- package/src/server/render/not-found.ts +9 -3
- package/src/server/render/ssr.ts +603 -585
- package/src/server/render/template.ts +57 -24
- package/src/server/router/plugin.ts +384 -384
- package/src/server/sync/adapter.ts +65 -0
- package/src/server/sync/config.ts +60 -29
- package/src/server/sync/fingerprint.ts +44 -0
- package/src/server/sync/index.ts +15 -14
- package/src/server/sync/memory-adapter.ts +196 -0
- package/src/server/sync/plugin.ts +199 -108
- package/src/server/sync/response.ts +77 -0
- package/src/server/sync/stream.ts +162 -144
- package/src/shared/deferred-ndjson.ts +7 -0
- package/src/shared/prefix.ts +38 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
// ── Furin instance model ─────────────────────────────────────────────────────
|
|
4
|
+
// Each `furin({ pagesDir, prefix })` call registers one instance. All
|
|
5
|
+
// previously module-global state (build ID, caches, template, sync path, …)
|
|
6
|
+
// hangs off the instance via `instanceSlot()` so several furin apps can be
|
|
7
|
+
// mounted in one Elysia process without stomping each other.
|
|
8
|
+
//
|
|
9
|
+
// This module is a dependency LEAF: it only imports node:async_hooks. State
|
|
10
|
+
// modules (cache/ssg.ts, render/template.ts, …) import it to declare their
|
|
11
|
+
// per-instance slots — never the other way around.
|
|
12
|
+
|
|
13
|
+
export interface FurinInstance {
|
|
14
|
+
buildId: string;
|
|
15
|
+
/** Absolute pagesDir — also the compile-context key. */
|
|
16
|
+
readonly pagesDir: string;
|
|
17
|
+
/** Mount prefix, `""` for the root app or `/admin`-style (no trailing slash). */
|
|
18
|
+
readonly prefix: string;
|
|
19
|
+
/** @internal Traffic epoch at registration time (see registerInstance). */
|
|
20
|
+
registrationEpoch: number;
|
|
21
|
+
/** Generic per-instance state bag backing `instanceSlot()`. */
|
|
22
|
+
readonly state: Map<symbol, unknown>;
|
|
23
|
+
/** Logical sync stream path (unprefixed) injected into HTML, or undefined. */
|
|
24
|
+
syncStreamPath: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface RequestScope {
|
|
28
|
+
instance: FurinInstance;
|
|
29
|
+
pending: Set<string>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const _requestScope = new AsyncLocalStorage<RequestScope>();
|
|
33
|
+
|
|
34
|
+
const WHITESPACE_RE = /\s/;
|
|
35
|
+
|
|
36
|
+
/** Registered instances, keyed by prefix. */
|
|
37
|
+
const _instances = new Map<string, FurinInstance>();
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Fallback bucket used when no instance was ever registered (unit tests
|
|
41
|
+
* importing state modules directly, build-time rendering). Lazily created so
|
|
42
|
+
* a plain `import` of a state module allocates nothing.
|
|
43
|
+
*/
|
|
44
|
+
let _defaultInstance: FurinInstance | null = null;
|
|
45
|
+
|
|
46
|
+
export function createInstance(prefix: string, pagesDir: string): FurinInstance {
|
|
47
|
+
return {
|
|
48
|
+
prefix,
|
|
49
|
+
pagesDir,
|
|
50
|
+
syncStreamPath: undefined,
|
|
51
|
+
buildId: "",
|
|
52
|
+
registrationEpoch: 0,
|
|
53
|
+
state: new Map(),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Bumped on every served request. Lets registerInstance tell a REAL prefix
|
|
58
|
+
// collision (two apps composed in one startup, no traffic in between) from a
|
|
59
|
+
// stale registration left behind by a previous test/server in this process
|
|
60
|
+
// (traffic flowed since — the old mount is dead, replace it).
|
|
61
|
+
let _trafficEpoch = 0;
|
|
62
|
+
|
|
63
|
+
/** @internal Called by the request wrap for every served request. */
|
|
64
|
+
export function markTraffic(): void {
|
|
65
|
+
_trafficEpoch++;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function defaultInstance(): FurinInstance {
|
|
69
|
+
if (!_defaultInstance) {
|
|
70
|
+
_defaultInstance = createInstance("", "");
|
|
71
|
+
}
|
|
72
|
+
return _defaultInstance;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The process-wide fallback bucket. State written outside any instance scope
|
|
77
|
+
* before the first `furin()` registration lands here — config-like readers
|
|
78
|
+
* (e.g. the production template) treat it as a default.
|
|
79
|
+
*/
|
|
80
|
+
export function defaultInstanceBucket(): FurinInstance {
|
|
81
|
+
return defaultInstance();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Normalizes a user-provided mount prefix. Accepts `""`, `"/"` (both → root)
|
|
86
|
+
* or `/segment(/segment)*`. Throws on anything else so misconfiguration fails
|
|
87
|
+
* at startup instead of producing silently unreachable routes.
|
|
88
|
+
*/
|
|
89
|
+
export function normalizePrefix(prefix: string | undefined): string {
|
|
90
|
+
if (prefix === undefined || prefix === "" || prefix === "/") {
|
|
91
|
+
return "";
|
|
92
|
+
}
|
|
93
|
+
if (!prefix.startsWith("/")) {
|
|
94
|
+
throw new Error(`[furin] prefix must start with "/" (got "${prefix}").`);
|
|
95
|
+
}
|
|
96
|
+
// Check the RAW value for "//" so "/admin//" is rejected instead of being
|
|
97
|
+
// trimmed to "/admin/" — a trailing slash never matches the path-boundary
|
|
98
|
+
// checks in resolveInstanceByPath, i.e. every route would be unreachable.
|
|
99
|
+
if (prefix.includes("//") || WHITESPACE_RE.test(prefix)) {
|
|
100
|
+
throw new Error(`[furin] invalid prefix "${prefix}".`);
|
|
101
|
+
}
|
|
102
|
+
return prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Throws when mounting `pagesDir` under `prefix` would collide with a LIVE
|
|
107
|
+
* registration. Rules:
|
|
108
|
+
* - same pagesDir → idempotent re-mount (mirrors Elysia's name-based dedup);
|
|
109
|
+
* - different pagesDir, NO traffic since the existing registration → two apps
|
|
110
|
+
* composed into one server are claiming the same prefix: hard error;
|
|
111
|
+
* - different pagesDir, traffic flowed since → the existing registration is a
|
|
112
|
+
* leftover from a torn-down server (tests): replacement is allowed.
|
|
113
|
+
*/
|
|
114
|
+
export function assertPrefixAvailable(prefix: string, pagesDir: string): void {
|
|
115
|
+
const existing = _instances.get(prefix);
|
|
116
|
+
if (existing && existing.pagesDir !== pagesDir && existing.registrationEpoch === _trafficEpoch) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`[furin] prefix "${prefix || "/"}" is already mounted by pagesDir "${existing.pagesDir}" ` +
|
|
119
|
+
`(attempted to mount "${pagesDir}"). Give each furin() instance a unique prefix.`
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Registers an instance under its prefix (see assertPrefixAvailable). */
|
|
125
|
+
export function registerInstance(instance: FurinInstance): FurinInstance {
|
|
126
|
+
assertPrefixAvailable(instance.prefix, instance.pagesDir);
|
|
127
|
+
instance.registrationEpoch = _trafficEpoch;
|
|
128
|
+
_instances.set(instance.prefix, instance);
|
|
129
|
+
return instance;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Resolves the instance owning `pathname` by longest-prefix match on a path
|
|
134
|
+
* boundary. Falls back to the root (`""`) instance when one is mounted, else
|
|
135
|
+
* the default bucket — never an arbitrary prefixed sibling, whose template/
|
|
136
|
+
* cache/build state would otherwise leak into parent-app routes.
|
|
137
|
+
*/
|
|
138
|
+
export function resolveInstanceByPath(pathname: string): FurinInstance {
|
|
139
|
+
let best: FurinInstance | null = null;
|
|
140
|
+
for (const [prefix, instance] of _instances) {
|
|
141
|
+
if (prefix === "") {
|
|
142
|
+
best ??= instance;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (
|
|
146
|
+
(pathname === prefix ||
|
|
147
|
+
(pathname.startsWith(prefix) && pathname.charCodeAt(prefix.length) === 47)) &&
|
|
148
|
+
(!best || prefix.length > best.prefix.length || best.prefix === "")
|
|
149
|
+
) {
|
|
150
|
+
best = instance;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (best) {
|
|
154
|
+
return best;
|
|
155
|
+
}
|
|
156
|
+
return defaultInstance();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The instance the current code runs for. Resolution order:
|
|
161
|
+
* 1. request/render ALS scope, 2. sole registered instance, 3. default bucket.
|
|
162
|
+
* With ≥2 instances and no scope, state access is ambiguous — the default
|
|
163
|
+
* bucket keeps out-of-request writes (e.g. build-time template setup)
|
|
164
|
+
* self-consistent instead of leaking into an arbitrary app.
|
|
165
|
+
*/
|
|
166
|
+
export function currentInstance(): FurinInstance {
|
|
167
|
+
const scope = _requestScope.getStore();
|
|
168
|
+
if (scope) {
|
|
169
|
+
return scope.instance;
|
|
170
|
+
}
|
|
171
|
+
if (_instances.size === 1) {
|
|
172
|
+
const only = _instances.values().next().value;
|
|
173
|
+
if (only) {
|
|
174
|
+
return only;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return defaultInstance();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** All registered instances (used by cross-instance ops like revalidateTag). */
|
|
181
|
+
export function allInstances(): FurinInstance[] {
|
|
182
|
+
if (_instances.size === 0) {
|
|
183
|
+
return [defaultInstance()];
|
|
184
|
+
}
|
|
185
|
+
return [..._instances.values()];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @internal Every live state bucket: registered instances plus the default
|
|
190
|
+
* fallback bucket. Reset helpers iterate this so state written outside any
|
|
191
|
+
* registration (tests, config-before-mount) is covered too.
|
|
192
|
+
*/
|
|
193
|
+
export function allStateBuckets(): FurinInstance[] {
|
|
194
|
+
const buckets = [..._instances.values()];
|
|
195
|
+
const fallback = defaultInstance();
|
|
196
|
+
if (!buckets.includes(fallback)) {
|
|
197
|
+
buckets.push(fallback);
|
|
198
|
+
}
|
|
199
|
+
return buckets;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @internal test-only — forgets registered instances (fresh mounts get a
|
|
204
|
+
* clean registry) while PRESERVING the default bucket, so process-wide
|
|
205
|
+
* defaults installed once (e.g. a test template in `beforeAll`) survive.
|
|
206
|
+
*/
|
|
207
|
+
export function __clearInstanceRegistry(): void {
|
|
208
|
+
_instances.clear();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function hasRequestScope(): boolean {
|
|
212
|
+
return _requestScope.getStore() !== undefined;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Pending client-invalidation paths for the current request, if any. */
|
|
216
|
+
export function requestPendingInvalidations(): Set<string> | undefined {
|
|
217
|
+
return _requestScope.getStore()?.pending;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Runs `fn` inside a fresh request scope bound to `instance`. */
|
|
221
|
+
export function runWithInstanceScope<T>(instance: FurinInstance, fn: () => T): T {
|
|
222
|
+
return _requestScope.run({ instance, pending: new Set<string>() }, fn);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Runs `fn` bound to `instance` for synthetic (non-request) work — SSG cache
|
|
227
|
+
* warming, background ISR revalidation kicked off outside a live request.
|
|
228
|
+
*/
|
|
229
|
+
export function withInstance<T>(instance: FurinInstance, fn: () => T): T {
|
|
230
|
+
return runWithInstanceScope(instance, fn);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Declares a lazily-initialized per-instance state slot. Returns an accessor
|
|
235
|
+
* that resolves against the current instance (or an explicit one). This is
|
|
236
|
+
* how state modules attach caches/registries to instances without this module
|
|
237
|
+
* importing them (avoids import cycles).
|
|
238
|
+
*/
|
|
239
|
+
export function instanceSlot<T>(
|
|
240
|
+
init: (instance: FurinInstance) => T
|
|
241
|
+
): (instance?: FurinInstance) => T {
|
|
242
|
+
const key = Symbol("furin-instance-slot");
|
|
243
|
+
return (instance?: FurinInstance) => {
|
|
244
|
+
const target = instance ?? currentInstance();
|
|
245
|
+
if (target.state.has(key)) {
|
|
246
|
+
return target.state.get(key) as T;
|
|
247
|
+
}
|
|
248
|
+
const value = init(target);
|
|
249
|
+
target.state.set(key, value);
|
|
250
|
+
return value;
|
|
251
|
+
};
|
|
252
|
+
}
|
package/src/server/internal.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SsgCacheEntry } from "./cache/index.ts";
|
|
2
|
+
import { __clearInstanceRegistry } from "./instance.ts";
|
|
2
3
|
|
|
3
4
|
// ── Compile-time context for compiled binaries ──────────────────────────────
|
|
4
5
|
// The generated compile entry calls `__setCompileContext()` before importing
|
|
@@ -20,6 +21,8 @@ export interface CompileContext {
|
|
|
20
21
|
buildId?: string;
|
|
21
22
|
embedded?: EmbeddedAppData;
|
|
22
23
|
modules: Record<string, unknown>;
|
|
24
|
+
/** Mount prefix this app was built for (`""` = root). */
|
|
25
|
+
prefix?: string;
|
|
23
26
|
/** Root-level conventions discovered at compile time. */
|
|
24
27
|
rootConventions?: { errorPath?: string; notFoundPath?: string };
|
|
25
28
|
rootPath: string;
|
|
@@ -39,16 +42,108 @@ export interface CompileContext {
|
|
|
39
42
|
ssgCache?: Record<string, SsgCacheEntry>;
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
|
|
45
|
+
// Contexts are keyed by (pagesDir, prefix) — pagesDir being the directory
|
|
46
|
+
// containing root.tsx, normalized to posix separators. The SAME pagesDir may
|
|
47
|
+
// legally be mounted under two different prefixes (assertPrefixAvailable only
|
|
48
|
+
// rejects same-prefix collisions), so pagesDir alone would let the second
|
|
49
|
+
// registration overwrite the first. Several packaged furin apps can each ship
|
|
50
|
+
// a self-registering module calling `__setCompileContext()` — registration is
|
|
51
|
+
// ADDITIVE, one context per (app, mount).
|
|
52
|
+
const _compileContexts = new Map<string, CompileContext>();
|
|
53
|
+
|
|
54
|
+
function normalizeContextKey(path: string): string {
|
|
55
|
+
return path.replaceAll("\\", "/");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function pagesDirOf(ctx: CompileContext): string {
|
|
59
|
+
const rootPath = normalizeContextKey(ctx.rootPath);
|
|
60
|
+
const lastSlash = rootPath.lastIndexOf("/");
|
|
61
|
+
return lastSlash === -1 ? rootPath : rootPath.slice(0, lastSlash);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// "\u0000" cannot appear in a path or a prefix, so the composite key is
|
|
65
|
+
// unambiguous even for pagesDirs containing spaces.
|
|
66
|
+
function contextKey(pagesDir: string, prefix: string): string {
|
|
67
|
+
return `${pagesDir}\u0000${prefix}`;
|
|
68
|
+
}
|
|
43
69
|
|
|
44
70
|
export function __setCompileContext(ctx: CompileContext): void {
|
|
45
|
-
|
|
71
|
+
_compileContexts.set(contextKey(pagesDirOf(ctx), ctx.prefix ?? ""), ctx);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Looks up the compile context for a (pagesDir, prefix) mount. Resolution
|
|
76
|
+
* order, from most to least specific:
|
|
77
|
+
* 1. exact (pagesDir, prefix) key — distinguishes the same app mounted twice;
|
|
78
|
+
* 2. sole context registered for `pagesDir` — callers that don't pass a
|
|
79
|
+
* prefix, and contexts emitted before the prefix field existed;
|
|
80
|
+
* 3. sole context registered for `prefix` — deployed binaries run from a
|
|
81
|
+
* different cwd than the build, so the absolute pagesDir key misses there,
|
|
82
|
+
* but the mount prefix is stable across machines;
|
|
83
|
+
* 4. the sole registered context (single-app back-compat).
|
|
84
|
+
*/
|
|
85
|
+
export function getCompileContext(pagesDir?: string, prefix?: string): CompileContext | null {
|
|
86
|
+
if (pagesDir !== undefined) {
|
|
87
|
+
const dir = normalizeContextKey(pagesDir);
|
|
88
|
+
if (prefix !== undefined) {
|
|
89
|
+
const exact = _compileContexts.get(contextKey(dir, prefix));
|
|
90
|
+
if (exact) {
|
|
91
|
+
return exact;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const dirMatch = soleContextForPagesDir(dir);
|
|
95
|
+
if (dirMatch) {
|
|
96
|
+
return dirMatch;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (prefix !== undefined) {
|
|
100
|
+
const prefixMatch = contextForPrefix(prefix);
|
|
101
|
+
if (prefixMatch) {
|
|
102
|
+
return prefixMatch;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (_compileContexts.size === 1) {
|
|
106
|
+
return _compileContexts.values().next().value ?? null;
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The sole context registered for `dir`, or null when none matches — or when
|
|
113
|
+
* SEVERAL do (the same pagesDir mounted at two prefixes): guessing there
|
|
114
|
+
* would hand one mount the other's assets/build ID.
|
|
115
|
+
*/
|
|
116
|
+
function soleContextForPagesDir(dir: string): CompileContext | null {
|
|
117
|
+
let match: CompileContext | null = null;
|
|
118
|
+
for (const ctx of _compileContexts.values()) {
|
|
119
|
+
if (pagesDirOf(ctx) !== dir) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (match) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
match = ctx;
|
|
126
|
+
}
|
|
127
|
+
return match;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function contextForPrefix(prefix: string): CompileContext | null {
|
|
131
|
+
for (const ctx of _compileContexts.values()) {
|
|
132
|
+
if ((ctx.prefix ?? "") === prefix) {
|
|
133
|
+
return ctx;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
46
137
|
}
|
|
47
138
|
|
|
48
|
-
|
|
49
|
-
|
|
139
|
+
/** All registered contexts, keyed by the composite (pagesDir, prefix) key. */
|
|
140
|
+
export function getAllCompileContexts(): ReadonlyMap<string, CompileContext> {
|
|
141
|
+
return _compileContexts;
|
|
50
142
|
}
|
|
51
143
|
|
|
52
144
|
export function __resetCompileContext(): void {
|
|
53
|
-
|
|
145
|
+
_compileContexts.clear();
|
|
146
|
+
// A test tearing down compile contexts is tearing down its furin mounts —
|
|
147
|
+
// forget their prefix registrations too so the next test mounts cleanly.
|
|
148
|
+
__clearInstanceRegistry();
|
|
54
149
|
}
|