@nifrajs/web 2.2.0 → 2.3.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/README.md +3 -3
- package/dist/build-vite.d.ts +4 -4
- package/dist/build-vite.d.ts.map +1 -1
- package/dist/build-vite.js +20 -74
- package/dist/build-vite.js.map +1 -1
- package/dist/build.d.ts +64 -42
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +100 -78
- package/dist/build.js.map +1 -1
- package/dist/bun-dev-entry.d.ts +1 -1
- package/dist/bun-dev-entry.js +1 -1
- package/dist/chunk-names.js +1 -1
- package/dist/client.d.ts +3 -9
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +201 -29
- package/dist/client.js.map +1 -1
- package/dist/conformance.d.ts +2 -2
- package/dist/conformance.js +3 -3
- package/dist/deferred.d.ts +11 -11
- package/dist/deferred.d.ts.map +1 -1
- package/dist/deferred.js +16 -16
- package/dist/dev-error.d.ts +2 -2
- package/dist/dev-error.js +3 -3
- package/dist/dev-port.d.ts +2 -2
- package/dist/dev-port.js +1 -1
- package/dist/dev.d.ts +3 -3
- package/dist/dev.js +9 -9
- package/dist/draft.d.ts +12 -12
- package/dist/draft.js +11 -11
- package/dist/fn-state.d.ts +56 -0
- package/dist/fn-state.d.ts.map +1 -0
- package/dist/fn-state.js +77 -0
- package/dist/fn-state.js.map +1 -0
- package/dist/fn.d.ts +111 -0
- package/dist/fn.d.ts.map +1 -0
- package/dist/fn.js +125 -0
- package/dist/fn.js.map +1 -0
- package/dist/fonts-google.d.ts +9 -9
- package/dist/fonts-google.js +10 -10
- package/dist/fonts.d.ts +7 -7
- package/dist/fonts.js +1 -1
- package/dist/forms.d.ts +8 -8
- package/dist/fs.d.ts +1 -1
- package/dist/fs.js +1 -1
- package/dist/index.d.ts +151 -70
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +317 -139
- package/dist/index.js.map +1 -1
- package/dist/internal/head-attributes.d.ts +15 -0
- package/dist/internal/head-attributes.d.ts.map +1 -0
- package/dist/internal/head-attributes.js +99 -0
- package/dist/internal/head-attributes.js.map +1 -0
- package/dist/internal/script-types.d.ts +14 -0
- package/dist/internal/script-types.d.ts.map +1 -0
- package/dist/internal/script-types.js +17 -0
- package/dist/internal/script-types.js.map +1 -0
- package/dist/internal/server-boundary.d.ts +60 -0
- package/dist/internal/server-boundary.d.ts.map +1 -0
- package/dist/internal/server-boundary.js +174 -0
- package/dist/internal/server-boundary.js.map +1 -0
- package/dist/internal/timing-safe-equal.d.ts +1 -1
- package/dist/internal/timing-safe-equal.js +1 -1
- package/dist/internal/vite-import.d.ts +63 -0
- package/dist/internal/vite-import.d.ts.map +1 -0
- package/dist/internal/vite-import.js +94 -0
- package/dist/internal/vite-import.js.map +1 -0
- package/dist/islands.d.ts +5 -5
- package/dist/islands.js +5 -5
- package/dist/isr.d.ts +15 -15
- package/dist/isr.js +7 -7
- package/dist/manifest.d.ts +73 -35
- package/dist/manifest.d.ts.map +1 -1
- package/dist/manifest.js +10 -16
- package/dist/manifest.js.map +1 -1
- package/dist/module-graph.d.ts +5 -5
- package/dist/module-graph.js +4 -4
- package/dist/navigation.d.ts +120 -5
- package/dist/navigation.d.ts.map +1 -1
- package/dist/navigation.js +37 -5
- package/dist/navigation.js.map +1 -1
- package/dist/plugins/css-modules.d.ts +3 -3
- package/dist/plugins/css-modules.js +0 -0
- package/dist/plugins/kit.d.ts +8 -8
- package/dist/plugins/kit.js +9 -9
- package/dist/plugins/scss.js +3 -3
- package/dist/plugins/vite-leak-guard.d.ts +1 -1
- package/dist/plugins/vite-leak-guard.js +1 -1
- package/dist/plugins/vite-server-fn.d.ts +23 -0
- package/dist/plugins/vite-server-fn.d.ts.map +1 -0
- package/dist/plugins/vite-server-fn.js +51 -0
- package/dist/plugins/vite-server-fn.js.map +1 -0
- package/dist/plugins/vite-server-only.d.ts +49 -0
- package/dist/plugins/vite-server-only.d.ts.map +1 -0
- package/dist/plugins/vite-server-only.js +57 -0
- package/dist/plugins/vite-server-only.js.map +1 -0
- package/dist/prerender.d.ts +5 -5
- package/dist/prerender.js +7 -7
- package/dist/query.d.ts +10 -10
- package/dist/query.js +15 -15
- package/dist/route-manifest.d.ts +1 -1
- package/dist/route-manifest.js +2 -2
- package/dist/router.d.ts +30 -21
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +57 -28
- package/dist/router.js.map +1 -1
- package/dist/search.d.ts +94 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +232 -0
- package/dist/search.js.map +1 -0
- package/dist/server-only.d.ts +6 -6
- package/dist/server-only.js +7 -7
- package/dist/service-worker.d.ts +56 -0
- package/dist/service-worker.d.ts.map +1 -0
- package/dist/service-worker.js +127 -0
- package/dist/service-worker.js.map +1 -0
- package/dist/vite.d.ts +15 -13
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +31 -21
- package/dist/vite.js.map +1 -1
- package/package.json +33 -4
- package/server-manifest.d.ts +31 -0
- package/src/build-vite.ts +24 -77
- package/src/build.ts +137 -105
- package/src/bun-dev-entry.ts +1 -1
- package/src/chunk-names.ts +1 -1
- package/src/client.ts +215 -32
- package/src/conformance.ts +3 -3
- package/src/deferred.ts +17 -17
- package/src/dev-error.ts +3 -3
- package/src/dev-port.ts +2 -2
- package/src/dev.ts +12 -12
- package/src/draft.ts +15 -15
- package/src/fn-state.ts +107 -0
- package/src/fn.ts +199 -0
- package/src/fonts-google.ts +13 -13
- package/src/fonts.ts +7 -7
- package/src/forms.ts +8 -8
- package/src/fs.ts +2 -2
- package/src/index.ts +460 -181
- package/src/internal/head-attributes.ts +109 -0
- package/src/internal/script-types.ts +18 -0
- package/src/internal/server-boundary.ts +187 -0
- package/src/internal/timing-safe-equal.ts +1 -1
- package/src/internal/vite-import.ts +92 -0
- package/src/islands.ts +7 -7
- package/src/isr.ts +17 -17
- package/src/manifest.ts +83 -42
- package/src/module-graph.ts +6 -6
- package/src/navigation.ts +151 -5
- package/src/plugins/css-modules.ts +0 -0
- package/src/plugins/kit.ts +11 -11
- package/src/plugins/scss.ts +3 -3
- package/src/plugins/vite-leak-guard.ts +2 -2
- package/src/plugins/vite-server-fn.ts +67 -0
- package/src/plugins/vite-server-only.ts +75 -0
- package/src/prerender.ts +10 -10
- package/src/query.ts +22 -22
- package/src/route-manifest.ts +3 -3
- package/src/router.ts +91 -41
- package/src/search.ts +297 -0
- package/src/server-only.ts +7 -7
- package/src/service-worker.ts +165 -0
- package/src/vite.ts +41 -29
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nifrajs/web/fn-state` - the pending/error state around a server-function call.
|
|
3
|
+
*
|
|
4
|
+
* Calling a server function needs no binding: the client stub is `(input) => Promise<Output>`, and a
|
|
5
|
+
* click handler can await it. What a component usually wants on top is whether a call is in flight,
|
|
6
|
+
* what came back, and what went wrong - which is state, and state is the part every framework spells
|
|
7
|
+
* differently.
|
|
8
|
+
*
|
|
9
|
+
* So the state machine lives here, framework-agnostic, and each adapter contributes only its
|
|
10
|
+
* subscription primitive. Five copies of "is it pending" would drift, and the drift would be five
|
|
11
|
+
* subtly different answers to the same question.
|
|
12
|
+
*
|
|
13
|
+
* ## Two behaviours worth stating
|
|
14
|
+
*
|
|
15
|
+
* **The last call wins.** Concurrent calls are not queued or cancelled; a response that is no longer
|
|
16
|
+
* the newest is discarded rather than written. Without that, a slow first call landing after a fast
|
|
17
|
+
* second one would overwrite fresh data with stale - the classic out-of-order bug, and one that only
|
|
18
|
+
* appears under load, which is to say in production.
|
|
19
|
+
*
|
|
20
|
+
* **`call` still rejects.** The error is recorded for rendering AND the promise rejects, so `await`
|
|
21
|
+
* behaves the way `await` should and a caller who wants to branch can. A caller who only renders from
|
|
22
|
+
* state should attach a `.catch(() => {})`, the same as `useFetcher`'s `submit`.
|
|
23
|
+
*/
|
|
24
|
+
/** What a component renders from. */
|
|
25
|
+
export interface ServerFnState<Output> {
|
|
26
|
+
/** A call is in flight. */
|
|
27
|
+
readonly pending: boolean;
|
|
28
|
+
/** The most recent successful result, kept across a later pending call so the UI need not flicker. */
|
|
29
|
+
readonly data: Output | undefined;
|
|
30
|
+
/** The most recent failure, cleared when a call succeeds. */
|
|
31
|
+
readonly error: Error | undefined;
|
|
32
|
+
}
|
|
33
|
+
/** A subscribable call site. One per component instance, created by the framework binding. */
|
|
34
|
+
export interface ServerFnStore<Input, Output> {
|
|
35
|
+
/** Register a listener; returns the unsubscribe. */
|
|
36
|
+
subscribe(listener: () => void): () => void;
|
|
37
|
+
/**
|
|
38
|
+
* The current state. The SAME object is returned until something changes, because
|
|
39
|
+
* `useSyncExternalStore` and its equivalents compare by reference - a fresh object each call is an
|
|
40
|
+
* infinite render loop.
|
|
41
|
+
*/
|
|
42
|
+
snapshot(): ServerFnState<Output>;
|
|
43
|
+
/** Invoke the server function. Records state, and rejects on failure. */
|
|
44
|
+
call(input: Input): Promise<Output>;
|
|
45
|
+
/** Back to idle, discarding data and error. */
|
|
46
|
+
reset(): void;
|
|
47
|
+
}
|
|
48
|
+
/** The idle state, shared so a server render and the first client render agree by reference. */
|
|
49
|
+
export declare function idleServerFnState<Output>(): ServerFnState<Output>;
|
|
50
|
+
/**
|
|
51
|
+
* Wrap a server function (or any async function) in a subscribable pending/error state.
|
|
52
|
+
*
|
|
53
|
+
* Framework-free by design: the adapters call this and subscribe with their own primitive.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createServerFnStore<Input, Output>(fn: (input: Input) => Promise<Output> | Output): ServerFnStore<Input, Output>;
|
|
56
|
+
//# sourceMappingURL=fn-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fn-state.d.ts","sourceRoot":"","sources":["../src/fn-state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,qCAAqC;AACrC,MAAM,WAAW,aAAa,CAAC,MAAM;IACnC,2BAA2B;IAC3B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,sGAAsG;IACtG,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,6DAA6D;IAC7D,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAAA;CAClC;AAED,8FAA8F;AAC9F,MAAM,WAAW,aAAa,CAAC,KAAK,EAAE,MAAM;IAC1C,oDAAoD;IACpD,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAA;IAC3C;;;;OAIG;IACH,QAAQ,IAAI,aAAa,CAAC,MAAM,CAAC,CAAA;IACjC,yEAAyE;IACzE,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACnC,+CAA+C;IAC/C,KAAK,IAAI,IAAI,CAAA;CACd;AAID,gGAAgG;AAChG,wBAAgB,iBAAiB,CAAC,MAAM,KAAK,aAAa,CAAC,MAAM,CAAC,CAEjE;AAKD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAC/C,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,GAC7C,aAAa,CAAC,KAAK,EAAE,MAAM,CAAC,CAuC9B"}
|
package/dist/fn-state.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nifrajs/web/fn-state` - the pending/error state around a server-function call.
|
|
3
|
+
*
|
|
4
|
+
* Calling a server function needs no binding: the client stub is `(input) => Promise<Output>`, and a
|
|
5
|
+
* click handler can await it. What a component usually wants on top is whether a call is in flight,
|
|
6
|
+
* what came back, and what went wrong - which is state, and state is the part every framework spells
|
|
7
|
+
* differently.
|
|
8
|
+
*
|
|
9
|
+
* So the state machine lives here, framework-agnostic, and each adapter contributes only its
|
|
10
|
+
* subscription primitive. Five copies of "is it pending" would drift, and the drift would be five
|
|
11
|
+
* subtly different answers to the same question.
|
|
12
|
+
*
|
|
13
|
+
* ## Two behaviours worth stating
|
|
14
|
+
*
|
|
15
|
+
* **The last call wins.** Concurrent calls are not queued or cancelled; a response that is no longer
|
|
16
|
+
* the newest is discarded rather than written. Without that, a slow first call landing after a fast
|
|
17
|
+
* second one would overwrite fresh data with stale - the classic out-of-order bug, and one that only
|
|
18
|
+
* appears under load, which is to say in production.
|
|
19
|
+
*
|
|
20
|
+
* **`call` still rejects.** The error is recorded for rendering AND the promise rejects, so `await`
|
|
21
|
+
* behaves the way `await` should and a caller who wants to branch can. A caller who only renders from
|
|
22
|
+
* state should attach a `.catch(() => {})`, the same as `useFetcher`'s `submit`.
|
|
23
|
+
*/
|
|
24
|
+
const IDLE = { pending: false, data: undefined, error: undefined };
|
|
25
|
+
/** The idle state, shared so a server render and the first client render agree by reference. */
|
|
26
|
+
export function idleServerFnState() {
|
|
27
|
+
return IDLE;
|
|
28
|
+
}
|
|
29
|
+
const asError = (cause) => cause instanceof Error ? cause : new Error(String(cause), { cause });
|
|
30
|
+
/**
|
|
31
|
+
* Wrap a server function (or any async function) in a subscribable pending/error state.
|
|
32
|
+
*
|
|
33
|
+
* Framework-free by design: the adapters call this and subscribe with their own primitive.
|
|
34
|
+
*/
|
|
35
|
+
export function createServerFnStore(fn) {
|
|
36
|
+
const listeners = new Set();
|
|
37
|
+
let state = idleServerFnState();
|
|
38
|
+
/** Identifies the newest call, so an older one landing later cannot write over it. */
|
|
39
|
+
let generation = 0;
|
|
40
|
+
const set = (next) => {
|
|
41
|
+
state = next;
|
|
42
|
+
for (const listener of listeners)
|
|
43
|
+
listener();
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
subscribe(listener) {
|
|
47
|
+
listeners.add(listener);
|
|
48
|
+
return () => {
|
|
49
|
+
listeners.delete(listener);
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
snapshot: () => state,
|
|
53
|
+
async call(input) {
|
|
54
|
+
const mine = ++generation;
|
|
55
|
+
// `data` is deliberately kept while pending: dropping it makes a list blank on every refetch.
|
|
56
|
+
set({ pending: true, data: state.data, error: undefined });
|
|
57
|
+
try {
|
|
58
|
+
const data = await fn(input);
|
|
59
|
+
if (mine === generation)
|
|
60
|
+
set({ pending: false, data, error: undefined });
|
|
61
|
+
return data;
|
|
62
|
+
}
|
|
63
|
+
catch (cause) {
|
|
64
|
+
const error = asError(cause);
|
|
65
|
+
if (mine === generation)
|
|
66
|
+
set({ pending: false, data: state.data, error });
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
reset() {
|
|
71
|
+
// Bump the generation so a call already in flight cannot resurrect the state it was reset from.
|
|
72
|
+
generation += 1;
|
|
73
|
+
set(idleServerFnState());
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=fn-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fn-state.js","sourceRoot":"","sources":["../src/fn-state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AA4BH,MAAM,IAAI,GAAyB,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,CAAA;AAExF,gGAAgG;AAChG,MAAM,UAAU,iBAAiB;IAC/B,OAAO,IAA6B,CAAA;AACtC,CAAC;AAED,MAAM,OAAO,GAAG,CAAC,KAAc,EAAS,EAAE,CACxC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;AAEtE;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CACjC,EAA8C;IAE9C,MAAM,SAAS,GAAG,IAAI,GAAG,EAAc,CAAA;IACvC,IAAI,KAAK,GAA0B,iBAAiB,EAAU,CAAA;IAC9D,sFAAsF;IACtF,IAAI,UAAU,GAAG,CAAC,CAAA;IAElB,MAAM,GAAG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CAAC"}
|
package/dist/fn.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nifrajs/web/fn` - server functions: write a function, call it from a component.
|
|
3
|
+
*
|
|
4
|
+
* ## Every server function is a public endpoint
|
|
5
|
+
*
|
|
6
|
+
* This is the first thing to internalise, because the API deliberately reads like a local call. A
|
|
7
|
+
* mounted function is an HTTP route anyone can POST to, with arguments entirely under the caller's
|
|
8
|
+
* control. Its id has to be in the client bundle for the browser to call it, so there is no obscurity
|
|
9
|
+
* to lean on. Treat one exactly as you would a hand-written `app.post`.
|
|
10
|
+
*
|
|
11
|
+
* What follows from that, and what this module does about it:
|
|
12
|
+
*
|
|
13
|
+
* - **Input is validated, always.** `input` is not optional decoration; without a schema a function
|
|
14
|
+
* takes no arguments at all. Unvalidated arguments on a public endpoint are mass assignment.
|
|
15
|
+
* - **`application/json` only.** A cross-origin HTML form can only send urlencoded, multipart or
|
|
16
|
+
* text/plain, so requiring JSON forces a CORS preflight the browser blocks. Both alternatives were
|
|
17
|
+
* measured rather than assumed: a body schema alone still accepts a cross-origin urlencoded form
|
|
18
|
+
* (200, attacker-controlled fields), and `c.boundedJson` alone accepts the `text/plain` trick where
|
|
19
|
+
* a form's `name=value` is crafted to parse as JSON. Neither is sufficient by itself.
|
|
20
|
+
* - **Same-origin only.** A present `Origin` must match the request's own host. Defence in depth
|
|
21
|
+
* behind the JSON requirement, and it costs one comparison. Server functions exist for your own
|
|
22
|
+
* frontend; a different origin calling your backend is the typed-client story, not this one.
|
|
23
|
+
* - **No closures.** A function is a module-level export taking explicit arguments. Next serialises
|
|
24
|
+
* closed-over variables to the browser and back, which it now has to encrypt; refusing the feature
|
|
25
|
+
* removes the whole class rather than defending it.
|
|
26
|
+
*
|
|
27
|
+
* ## The client half
|
|
28
|
+
*
|
|
29
|
+
* A `*.fn.ts` module is never bundled for the browser. The client build replaces it with one stub per
|
|
30
|
+
* export, each POSTing to the route below, so the bodies and everything they import stay on the server.
|
|
31
|
+
*
|
|
32
|
+
* One pipeline cannot do this: `nifra dev --bun`. Bun's dev-server bundler accepts no plugins - a
|
|
33
|
+
* runtime `Bun.plugin` onLoad does not reach it, which was measured rather than assumed - so a `*.fn`
|
|
34
|
+
* module there would ship WHOLE, secrets included. That command refuses to start on an app containing
|
|
35
|
+
* one, the same way it already refuses CSS Modules. `nifra build` and `nifra dev` (Vite) are unaffected.
|
|
36
|
+
*
|
|
37
|
+
* ## Why this is not a new lane
|
|
38
|
+
*
|
|
39
|
+
* A mounted function registers through the ordinary public `register()`, so it is a route like any
|
|
40
|
+
* other and inherits the body cap, schema validation, capability declarations, the effect ledger, and
|
|
41
|
+
* `nifra assure`. Nothing here touches the kernel or the request path, so an app that mounts none of
|
|
42
|
+
* them pays exactly nothing - which is the whole reason to build it this way rather than as a
|
|
43
|
+
* bespoke dispatcher.
|
|
44
|
+
*/
|
|
45
|
+
import { type Context, type RouteSchema, type StandardSchemaV1 } from "@nifrajs/core/server";
|
|
46
|
+
/** The URL prefix every mounted function lives under. Namespaced per mount, then by export name. */
|
|
47
|
+
export declare const SERVER_FN_PREFIX = "/_nifra/fn";
|
|
48
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
49
|
+
/** What a server function declares about itself. */
|
|
50
|
+
export interface ServerFnConfig<Input> {
|
|
51
|
+
/**
|
|
52
|
+
* Validates the single argument. Omit it and the function takes no argument - never "any argument":
|
|
53
|
+
* the caller controls this value completely, so an unvalidated one is an open door.
|
|
54
|
+
*/
|
|
55
|
+
readonly input?: StandardSchemaV1<unknown, Input>;
|
|
56
|
+
/** Effect tokens, forwarded to the route so `nifra assure` and the effect ledger see them. */
|
|
57
|
+
readonly capabilities?: readonly string[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A declared server function. Callable directly on the server (the same value your own server-side
|
|
61
|
+
* code can await); on the client, phase 2's build transform replaces this module with typed stubs
|
|
62
|
+
* that POST to the mounted route.
|
|
63
|
+
*
|
|
64
|
+
* This is the SERVER declaration type, so context is required. Client builds replace the module with
|
|
65
|
+
* a one-argument {@link ClientServerFn}; UI bindings accept either shape and adapt at that boundary.
|
|
66
|
+
* Keeping the types separate prevents a direct server call from type-checking while handing the
|
|
67
|
+
* declaration `undefined` for a context its implementation requires.
|
|
68
|
+
*/
|
|
69
|
+
export interface ServerFn<Input, Output> {
|
|
70
|
+
(input: Input, context: Context): MaybePromise<Output>;
|
|
71
|
+
readonly [SERVER_FN]: ServerFnConfig<Input>;
|
|
72
|
+
}
|
|
73
|
+
/** The one-argument callable emitted into a client bundle for a {@link ServerFn}. */
|
|
74
|
+
export type ClientServerFn<Input, Output> = (input: Input) => MaybePromise<Output>;
|
|
75
|
+
/** A UI binding boundary: source declarations and generated client stubs are both accepted. */
|
|
76
|
+
export type ServerFnReference<Input, Output> = ServerFn<Input, Output> | ClientServerFn<Input, Output>;
|
|
77
|
+
/** Brand identifying a value produced by {@link serverFn}, so mounting cannot pick up stray exports. */
|
|
78
|
+
export declare const SERVER_FN: unique symbol;
|
|
79
|
+
/**
|
|
80
|
+
* Declare a server function.
|
|
81
|
+
*
|
|
82
|
+
* export const addTodo = serverFn(
|
|
83
|
+
* { input: t.object({ text: t.string({ minLength: 1 }) }), capabilities: ["db.write"] },
|
|
84
|
+
* async ({ text }, c) => db.todos.insert({ text }),
|
|
85
|
+
* )
|
|
86
|
+
*
|
|
87
|
+
* The second argument receives the validated input and the ordinary nifra `Context` - `c.env`,
|
|
88
|
+
* `c.clientIp`, `c.budget`, cookies, and the capability guard are all present, because this is a route.
|
|
89
|
+
*/
|
|
90
|
+
export declare function serverFn<Input = void, Output = unknown>(config: ServerFnConfig<Input>, fn: (input: Input, context: Context) => MaybePromise<Output>): ServerFn<Input, Output>;
|
|
91
|
+
/** The minimum a server needs to expose for functions to be mounted onto it. */
|
|
92
|
+
export interface ServerFnHost {
|
|
93
|
+
register(method: "POST", path: string, schema: RouteSchema | undefined, handler: (context: never) => unknown): void;
|
|
94
|
+
}
|
|
95
|
+
/** Every server function a module exports, keyed by export name. */
|
|
96
|
+
export type ServerFnModule = Readonly<Record<string, unknown>>;
|
|
97
|
+
/**
|
|
98
|
+
* Mount a module's server functions under `namespace`, returning a plugin for `app.use(...)`.
|
|
99
|
+
*
|
|
100
|
+
* import * as todos from "./actions/todos.fn"
|
|
101
|
+
* app.use(serverFunctions("todos", todos)) // -> POST /_nifra/fn/todos/addTodo
|
|
102
|
+
*
|
|
103
|
+
* The namespace is explicit rather than derived from the file path: a path-derived one would put the
|
|
104
|
+
* build machine's layout in a public URL, and this keeps the route readable in logs and greppable in
|
|
105
|
+
* the codebase. Phase 2's transform supplies it automatically from the file's location in the project.
|
|
106
|
+
*
|
|
107
|
+
* Exports that are not server functions are ignored, so `export type` and helpers can live alongside.
|
|
108
|
+
*/
|
|
109
|
+
export declare function serverFunctions<S extends ServerFnHost>(namespace: string, module: ServerFnModule): (app: S) => S;
|
|
110
|
+
export {};
|
|
111
|
+
//# sourceMappingURL=fn.d.ts.map
|
package/dist/fn.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fn.d.ts","sourceRoot":"","sources":["../src/fn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,OAAO,EACL,KAAK,OAAO,EAEZ,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,sBAAsB,CAAA;AAE7B,oGAAoG;AACpG,eAAO,MAAM,gBAAgB,eAAe,CAAA;AAE5C,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;AAErC,oDAAoD;AACpD,MAAM,WAAW,cAAc,CAAC,KAAK;IACnC;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,gBAAgB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;IACjD,8FAA8F;IAC9F,QAAQ,CAAC,YAAY,CAAC,EAAE,SA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;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EACpD,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,cAAc,GACrB,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CA4Cf"}
|
package/dist/fn.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nifrajs/web/fn` - server functions: write a function, call it from a component.
|
|
3
|
+
*
|
|
4
|
+
* ## Every server function is a public endpoint
|
|
5
|
+
*
|
|
6
|
+
* This is the first thing to internalise, because the API deliberately reads like a local call. A
|
|
7
|
+
* mounted function is an HTTP route anyone can POST to, with arguments entirely under the caller's
|
|
8
|
+
* control. Its id has to be in the client bundle for the browser to call it, so there is no obscurity
|
|
9
|
+
* to lean on. Treat one exactly as you would a hand-written `app.post`.
|
|
10
|
+
*
|
|
11
|
+
* What follows from that, and what this module does about it:
|
|
12
|
+
*
|
|
13
|
+
* - **Input is validated, always.** `input` is not optional decoration; without a schema a function
|
|
14
|
+
* takes no arguments at all. Unvalidated arguments on a public endpoint are mass assignment.
|
|
15
|
+
* - **`application/json` only.** A cross-origin HTML form can only send urlencoded, multipart or
|
|
16
|
+
* text/plain, so requiring JSON forces a CORS preflight the browser blocks. Both alternatives were
|
|
17
|
+
* measured rather than assumed: a body schema alone still accepts a cross-origin urlencoded form
|
|
18
|
+
* (200, attacker-controlled fields), and `c.boundedJson` alone accepts the `text/plain` trick where
|
|
19
|
+
* a form's `name=value` is crafted to parse as JSON. Neither is sufficient by itself.
|
|
20
|
+
* - **Same-origin only.** A present `Origin` must match the request's own host. Defence in depth
|
|
21
|
+
* behind the JSON requirement, and it costs one comparison. Server functions exist for your own
|
|
22
|
+
* frontend; a different origin calling your backend is the typed-client story, not this one.
|
|
23
|
+
* - **No closures.** A function is a module-level export taking explicit arguments. Next serialises
|
|
24
|
+
* closed-over variables to the browser and back, which it now has to encrypt; refusing the feature
|
|
25
|
+
* removes the whole class rather than defending it.
|
|
26
|
+
*
|
|
27
|
+
* ## The client half
|
|
28
|
+
*
|
|
29
|
+
* A `*.fn.ts` module is never bundled for the browser. The client build replaces it with one stub per
|
|
30
|
+
* export, each POSTing to the route below, so the bodies and everything they import stay on the server.
|
|
31
|
+
*
|
|
32
|
+
* One pipeline cannot do this: `nifra dev --bun`. Bun's dev-server bundler accepts no plugins - a
|
|
33
|
+
* runtime `Bun.plugin` onLoad does not reach it, which was measured rather than assumed - so a `*.fn`
|
|
34
|
+
* module there would ship WHOLE, secrets included. That command refuses to start on an app containing
|
|
35
|
+
* one, the same way it already refuses CSS Modules. `nifra build` and `nifra dev` (Vite) are unaffected.
|
|
36
|
+
*
|
|
37
|
+
* ## Why this is not a new lane
|
|
38
|
+
*
|
|
39
|
+
* A mounted function registers through the ordinary public `register()`, so it is a route like any
|
|
40
|
+
* other and inherits the body cap, schema validation, capability declarations, the effect ledger, and
|
|
41
|
+
* `nifra assure`. Nothing here touches the kernel or the request path, so an app that mounts none of
|
|
42
|
+
* them pays exactly nothing - which is the whole reason to build it this way rather than as a
|
|
43
|
+
* bespoke dispatcher.
|
|
44
|
+
*/
|
|
45
|
+
import { isSameOriginRequest, } from "@nifrajs/core/server";
|
|
46
|
+
/** The URL prefix every mounted function lives under. Namespaced per mount, then by export name. */
|
|
47
|
+
export const SERVER_FN_PREFIX = "/_nifra/fn";
|
|
48
|
+
/** Brand identifying a value produced by {@link serverFn}, so mounting cannot pick up stray exports. */
|
|
49
|
+
export const SERVER_FN = Symbol.for("@nifrajs/web/server-fn");
|
|
50
|
+
/**
|
|
51
|
+
* Declare a server function.
|
|
52
|
+
*
|
|
53
|
+
* export const addTodo = serverFn(
|
|
54
|
+
* { input: t.object({ text: t.string({ minLength: 1 }) }), capabilities: ["db.write"] },
|
|
55
|
+
* async ({ text }, c) => db.todos.insert({ text }),
|
|
56
|
+
* )
|
|
57
|
+
*
|
|
58
|
+
* The second argument receives the validated input and the ordinary nifra `Context` - `c.env`,
|
|
59
|
+
* `c.clientIp`, `c.budget`, cookies, and the capability guard are all present, because this is a route.
|
|
60
|
+
*/
|
|
61
|
+
export function serverFn(config, fn) {
|
|
62
|
+
return Object.assign(fn, { [SERVER_FN]: config });
|
|
63
|
+
}
|
|
64
|
+
const isServerFn = (value) => typeof value === "function" && SERVER_FN in value;
|
|
65
|
+
/** A namespace segment: lowercase, dot/dash separated. Constrained so a mount cannot invent a path. */
|
|
66
|
+
const NAMESPACE = /^[a-z0-9]+(?:[-.][a-z0-9]+)*$/;
|
|
67
|
+
/** An export name that is safe in a URL path without escaping. */
|
|
68
|
+
const EXPORT_NAME = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
69
|
+
/**
|
|
70
|
+
* Same-origin, from `@nifrajs/core` so this seam and the WebSocket handshake cannot answer differently
|
|
71
|
+
* for one request. Host must match; the Origin's scheme may be equal or stronger, never weaker - which
|
|
72
|
+
* is what keeps a TLS-terminating proxy working without reading a forwarded header.
|
|
73
|
+
*/
|
|
74
|
+
const sameOrigin = isSameOriginRequest;
|
|
75
|
+
/**
|
|
76
|
+
* Mount a module's server functions under `namespace`, returning a plugin for `app.use(...)`.
|
|
77
|
+
*
|
|
78
|
+
* import * as todos from "./actions/todos.fn"
|
|
79
|
+
* app.use(serverFunctions("todos", todos)) // -> POST /_nifra/fn/todos/addTodo
|
|
80
|
+
*
|
|
81
|
+
* The namespace is explicit rather than derived from the file path: a path-derived one would put the
|
|
82
|
+
* build machine's layout in a public URL, and this keeps the route readable in logs and greppable in
|
|
83
|
+
* the codebase. Phase 2's transform supplies it automatically from the file's location in the project.
|
|
84
|
+
*
|
|
85
|
+
* Exports that are not server functions are ignored, so `export type` and helpers can live alongside.
|
|
86
|
+
*/
|
|
87
|
+
export function serverFunctions(namespace, module) {
|
|
88
|
+
if (!NAMESPACE.test(namespace)) {
|
|
89
|
+
throw new Error(`[nifra/fn] invalid server-function namespace ${JSON.stringify(namespace)} - use lowercase dot/dash segments (it becomes a URL path).`);
|
|
90
|
+
}
|
|
91
|
+
const mounted = Object.entries(module).filter((entry) => isServerFn(entry[1]));
|
|
92
|
+
const apply = (app) => {
|
|
93
|
+
for (const [name, fn] of mounted) {
|
|
94
|
+
if (!EXPORT_NAME.test(name)) {
|
|
95
|
+
throw new Error(`[nifra/fn] server function ${JSON.stringify(name)} in namespace ${JSON.stringify(namespace)} is not a valid identifier.`);
|
|
96
|
+
}
|
|
97
|
+
const config = fn[SERVER_FN];
|
|
98
|
+
const schema = {
|
|
99
|
+
...(config.input !== undefined ? { body: config.input } : {}),
|
|
100
|
+
...(config.capabilities !== undefined ? { capabilities: config.capabilities } : {}),
|
|
101
|
+
};
|
|
102
|
+
app.register("POST", `${SERVER_FN_PREFIX}/${namespace}/${name}`, schema, ((c) => {
|
|
103
|
+
// Ordered cheapest-first, and BEFORE the declared function runs. The body has already been
|
|
104
|
+
// parsed by the schema layer, but parsing is not a side effect - nothing the caller asked for
|
|
105
|
+
// has happened yet, so rejecting here is a complete refusal.
|
|
106
|
+
const request = c.req;
|
|
107
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
108
|
+
if (!contentType.includes("application/json")) {
|
|
109
|
+
// A form cannot send this content type cross-origin without a preflight. Rejecting anything
|
|
110
|
+
// else is what makes form-driven CSRF against a server function impossible rather than
|
|
111
|
+
// merely unlikely.
|
|
112
|
+
return c.json({ ok: false, error: "unsupported_media_type" }, 415);
|
|
113
|
+
}
|
|
114
|
+
const origin = request.headers.get("origin");
|
|
115
|
+
if (origin !== null && !sameOrigin(origin, request)) {
|
|
116
|
+
return c.json({ ok: false, error: "forbidden_origin" }, 403);
|
|
117
|
+
}
|
|
118
|
+
return fn(config.input === undefined ? undefined : c.body, c);
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
return app;
|
|
122
|
+
};
|
|
123
|
+
return Object.assign(apply, { pluginName: `nifra:server-fn:${namespace}` });
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=fn.js.map
|
package/dist/fn.js.map
ADDED
|
@@ -0,0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AE,KAAK,EAAE,wBAAwB,EAAE,EAAE,GAAG,CAAC,CAAA;gBACpE,CAAC;gBACD,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;gBAC5C,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;oBACpD,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,kBAAkB,EAAE,EAAE,GAAG,CAAC,CAAA;gBAC9D,CAAC;gBACD,OAAO,EAAE,CAAC,MAAM,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAE,SAAmB,CAAC,CAAC,CAAE,CAAC,CAAC,IAAc,EAAE,CAAC,CAAC,CAAA;YACrF,CAAC,CAAgC,CAAC,CAAA;QACpC,CAAC;QACD,OAAO,GAAG,CAAA;IACZ,CAAC,CAAA;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,UAAU,EAAE,mBAAmB,SAAS,EAAE,EAAE,CAAC,CAAA;AAC7E,CAAC"}
|
package/dist/fonts-google.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Build-time Google Fonts automation
|
|
2
|
+
* Build-time Google Fonts automation - the `next/font/google` equivalent. At **build time** (never on
|
|
3
3
|
* the request path) it: builds the Google Fonts CSS2 URL, downloads the stylesheet, parses the
|
|
4
4
|
* `@font-face` rules, downloads each `.woff2`, content-hashes it, writes it next to your assets, and
|
|
5
5
|
* hands back a **self-hosted** `@font-face` stylesheet + the matching `<link rel="preload">`s. The
|
|
6
|
-
* result is identical to dropping the files in yourself and calling {@link fontFace}
|
|
6
|
+
* result is identical to dropping the files in yourself and calling {@link fontFace} - no runtime CDN
|
|
7
7
|
* hotlink, no layout shift, hashed filenames for immutable caching.
|
|
8
8
|
*
|
|
9
|
-
* // fonts.build.ts
|
|
9
|
+
* // fonts.build.ts - run once at build time (e.g. a prebuild step)
|
|
10
10
|
* import { loadGoogleFont } from "@nifrajs/web/fonts"
|
|
11
11
|
* const inter = await loadGoogleFont(
|
|
12
12
|
* { family: "Inter", weights: [400, 700], subsets: ["latin"] },
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* // inter.preloads → spread into a root layout's `meta.link`
|
|
17
17
|
*
|
|
18
18
|
* Security: this fetches remote content and writes it to disk, so every input is validated and the
|
|
19
|
-
* font-file host is **allowlisted to `fonts.gstatic.com` over https**
|
|
19
|
+
* font-file host is **allowlisted to `fonts.gstatic.com` over https** - a tampered/MITM'd stylesheet
|
|
20
20
|
* cannot make the build fetch an arbitrary URL (SSRF) or write an attacker-chosen blob. Downloads are
|
|
21
21
|
* size-capped. Filenames are derived only from validated tokens + a content hash (no path traversal).
|
|
22
22
|
*/
|
|
@@ -26,7 +26,7 @@ import type { LinkDescriptor } from "./manifest.js";
|
|
|
26
26
|
export interface GoogleFontOptions {
|
|
27
27
|
/** Family name exactly as Google lists it, e.g. `"Inter"`, `"Open Sans"`, `"Roboto Mono"`. */
|
|
28
28
|
readonly family: string;
|
|
29
|
-
/** Weights to request
|
|
29
|
+
/** Weights to request - numbers (`400`), numeric strings, a variable range (`"100 900"`), or the
|
|
30
30
|
* keywords `"normal"`/`"bold"`. Defaults to `[400]`. */
|
|
31
31
|
readonly weights?: readonly (number | string)[];
|
|
32
32
|
/** Styles to request. Defaults to `["normal"]`. */
|
|
@@ -38,7 +38,7 @@ export interface GoogleFontOptions {
|
|
|
38
38
|
/** `font-display` strategy for the generated faces. Defaults to `"swap"`. */
|
|
39
39
|
readonly display?: FontDisplay;
|
|
40
40
|
/** Glyph subsetting: request only the glyphs needed to render exactly this text (Google's `&text=`).
|
|
41
|
-
* Ideal for a logo/heading font
|
|
41
|
+
* Ideal for a logo/heading font - produces one tiny file. */
|
|
42
42
|
readonly text?: string;
|
|
43
43
|
/** CLS metric overrides forwarded to every generated `@font-face` (the layout-shift fix). */
|
|
44
44
|
readonly sizeAdjust?: string;
|
|
@@ -77,7 +77,7 @@ export interface LoadGoogleFontResult {
|
|
|
77
77
|
readonly css: string;
|
|
78
78
|
/** Every file written to `outDir`. */
|
|
79
79
|
readonly assets: readonly FontAsset[];
|
|
80
|
-
/** `fontPreload()` link-attribute sets
|
|
80
|
+
/** `fontPreload()` link-attribute sets - spread the ones you want into a layout's `meta.link`.
|
|
81
81
|
* Preloading *every* weight/subset is wasteful; usually preload just the primary subset + weight. */
|
|
82
82
|
readonly preloads: readonly LinkDescriptor[];
|
|
83
83
|
}
|
|
@@ -93,13 +93,13 @@ export interface LoadGoogleFontIO {
|
|
|
93
93
|
/** Per-file download cap in bytes. Defaults to 5 MB (real woff2 are well under 1 MB). */
|
|
94
94
|
readonly maxBytesPerFile?: number;
|
|
95
95
|
}
|
|
96
|
-
/** `true` iff `raw` is an `https://fonts.gstatic.com/…` URL
|
|
96
|
+
/** `true` iff `raw` is an `https://fonts.gstatic.com/…` URL - the only host we'll download from. */
|
|
97
97
|
export declare function isAllowedFontUrl(raw: string): boolean;
|
|
98
98
|
/** Build the Google Fonts CSS2 request URL. Pure + fully validated, so it's safe to feed a dynamic
|
|
99
99
|
* family/weights/text. Exported for advanced callers who fetch + parse the stylesheet themselves. */
|
|
100
100
|
export declare function googleFontsCssUrl(options: GoogleFontOptions): string;
|
|
101
101
|
/** Parse Google's stylesheet into structured faces, capturing the `/* subset */` label that precedes
|
|
102
|
-
* each `@font-face`. Pure
|
|
102
|
+
* each `@font-face`. Pure - exported so callers can run their own download/write pipeline. */
|
|
103
103
|
export declare function parseGoogleFontCss(css: string): ParsedFontFace[];
|
|
104
104
|
/**
|
|
105
105
|
* Download a Google font, self-host it, and return a CLS-safe `@font-face` stylesheet + preloads.
|
package/dist/fonts-google.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Build-time Google Fonts automation
|
|
2
|
+
* Build-time Google Fonts automation - the `next/font/google` equivalent. At **build time** (never on
|
|
3
3
|
* the request path) it: builds the Google Fonts CSS2 URL, downloads the stylesheet, parses the
|
|
4
4
|
* `@font-face` rules, downloads each `.woff2`, content-hashes it, writes it next to your assets, and
|
|
5
5
|
* hands back a **self-hosted** `@font-face` stylesheet + the matching `<link rel="preload">`s. The
|
|
6
|
-
* result is identical to dropping the files in yourself and calling {@link fontFace}
|
|
6
|
+
* result is identical to dropping the files in yourself and calling {@link fontFace} - no runtime CDN
|
|
7
7
|
* hotlink, no layout shift, hashed filenames for immutable caching.
|
|
8
8
|
*
|
|
9
|
-
* // fonts.build.ts
|
|
9
|
+
* // fonts.build.ts - run once at build time (e.g. a prebuild step)
|
|
10
10
|
* import { loadGoogleFont } from "@nifrajs/web/fonts"
|
|
11
11
|
* const inter = await loadGoogleFont(
|
|
12
12
|
* { family: "Inter", weights: [400, 700], subsets: ["latin"] },
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* // inter.preloads → spread into a root layout's `meta.link`
|
|
17
17
|
*
|
|
18
18
|
* Security: this fetches remote content and writes it to disk, so every input is validated and the
|
|
19
|
-
* font-file host is **allowlisted to `fonts.gstatic.com` over https**
|
|
19
|
+
* font-file host is **allowlisted to `fonts.gstatic.com` over https** - a tampered/MITM'd stylesheet
|
|
20
20
|
* cannot make the build fetch an arbitrary URL (SSRF) or write an attacker-chosen blob. Downloads are
|
|
21
21
|
* size-capped. Filenames are derived only from validated tokens + a content hash (no path traversal).
|
|
22
22
|
*/
|
|
@@ -39,7 +39,7 @@ const VALID_DISPLAY = new Set([
|
|
|
39
39
|
"fallback",
|
|
40
40
|
"optional",
|
|
41
41
|
]);
|
|
42
|
-
/** `true` iff `raw` is an `https://fonts.gstatic.com/…` URL
|
|
42
|
+
/** `true` iff `raw` is an `https://fonts.gstatic.com/…` URL - the only host we'll download from. */
|
|
43
43
|
export function isAllowedFontUrl(raw) {
|
|
44
44
|
let url;
|
|
45
45
|
try {
|
|
@@ -63,7 +63,7 @@ function validateFamily(family) {
|
|
|
63
63
|
function normalizeWeight(weight) {
|
|
64
64
|
if (typeof weight === "number") {
|
|
65
65
|
if (!Number.isInteger(weight) || weight < 1 || weight > 1000) {
|
|
66
|
-
throw new Error(`loadGoogleFont: invalid weight ${weight} (1
|
|
66
|
+
throw new Error(`loadGoogleFont: invalid weight ${weight} (1-1000)`);
|
|
67
67
|
}
|
|
68
68
|
return String(weight);
|
|
69
69
|
}
|
|
@@ -134,7 +134,7 @@ function declOf(body, prop) {
|
|
|
134
134
|
return m?.[1]?.trim();
|
|
135
135
|
}
|
|
136
136
|
/** Parse Google's stylesheet into structured faces, capturing the `/* subset */` label that precedes
|
|
137
|
-
* each `@font-face`. Pure
|
|
137
|
+
* each `@font-face`. Pure - exported so callers can run their own download/write pipeline. */
|
|
138
138
|
export function parseGoogleFontCss(css) {
|
|
139
139
|
const faces = [];
|
|
140
140
|
for (const match of css.matchAll(FACE_RE)) {
|
|
@@ -165,7 +165,7 @@ export function parseGoogleFontCss(css) {
|
|
|
165
165
|
}
|
|
166
166
|
return faces;
|
|
167
167
|
}
|
|
168
|
-
/** First 16 hex chars of the SHA-256 of the bytes
|
|
168
|
+
/** First 16 hex chars of the SHA-256 of the bytes - a content hash for an immutable, cache-busting
|
|
169
169
|
* filename. Web Crypto, so it runs identically on Bun/Node/edge build hosts. `Uint8Array<ArrayBuffer>`
|
|
170
170
|
* (not the generic `ArrayBufferLike`) to satisfy WebCrypto's `BufferSource` under TS 5.7+ generics. */
|
|
171
171
|
async function contentHash(bytes) {
|
|
@@ -180,7 +180,7 @@ const slug = (s) => s
|
|
|
180
180
|
.toLowerCase()
|
|
181
181
|
.replace(/[^a-z0-9]+/g, "-")
|
|
182
182
|
.replace(/^-+|-+$/g, "");
|
|
183
|
-
/** Host of a URL for error messages
|
|
183
|
+
/** Host of a URL for error messages - we never echo the full untrusted URL+path. */
|
|
184
184
|
const hostOf = (raw) => {
|
|
185
185
|
try {
|
|
186
186
|
return new URL(raw).host;
|
|
@@ -229,7 +229,7 @@ export async function loadGoogleFont(options, io) {
|
|
|
229
229
|
if (faces.length === 0) {
|
|
230
230
|
throw new Error(`loadGoogleFont: Google returned no @font-face rules for ${JSON.stringify(family)}`);
|
|
231
231
|
}
|
|
232
|
-
// Filter to requested named subsets
|
|
232
|
+
// Filter to requested named subsets - but only when not glyph-subsetting (text mode labels faces
|
|
233
233
|
// `[0]`, `[1]`, … which carry no subset name).
|
|
234
234
|
if (wantedSubsets && wantedSubsets.length > 0 && options.text === undefined) {
|
|
235
235
|
faces = faces.filter((f) => wantedSubsets.includes(f.subset));
|
package/dist/fonts.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Font optimization primitives
|
|
2
|
+
* Font optimization primitives - generate a **CLS-safe `@font-face`** rule and the matching
|
|
3
3
|
* **preload `<link>`** for a self-hosted font. The two fixes that matter most (the ones `next/font`
|
|
4
4
|
* automates) are right here: `font-display: swap` (text paints immediately, no invisible-text FOIT)
|
|
5
5
|
* plus a preload so the file downloads with the document, and optional metric overrides
|
|
6
6
|
* (`size-adjust`/`ascent-override`/…) so swapping from the fallback to the web font doesn't shift layout.
|
|
7
7
|
*
|
|
8
|
-
* Self-host the file (drop a `.woff2` in your assets
|
|
8
|
+
* Self-host the file (drop a `.woff2` in your assets - never hotlink Google's CDN), then:
|
|
9
9
|
*
|
|
10
|
-
* // fonts.css (imported by your app
|
|
10
|
+
* // fonts.css (imported by your app - the CSS pipeline bundles + links it)
|
|
11
11
|
* import { fontFace } from "@nifrajs/web"
|
|
12
12
|
* export default fontFace({ family: "Inter", src: [{ url: "/fonts/inter.woff2" }], weight: "100 900" })
|
|
13
13
|
*
|
|
14
|
-
* // a root layout's meta
|
|
14
|
+
* // a root layout's meta - becomes a <link rel="preload" as="font"> in <head>
|
|
15
15
|
* export const meta = { link: [fontPreload({ href: "/fonts/inter.woff2" })] }
|
|
16
16
|
*/
|
|
17
17
|
import type { LinkDescriptor } from "./manifest.js";
|
|
@@ -32,7 +32,7 @@ export interface FontFace {
|
|
|
32
32
|
readonly style?: "normal" | "italic" | "oblique";
|
|
33
33
|
/** Defaults to `"swap"` (no flash of invisible text). */
|
|
34
34
|
readonly display?: FontDisplay;
|
|
35
|
-
/** Restrict the glyphs this face covers, e.g. `"U+0000-00FF"`
|
|
35
|
+
/** Restrict the glyphs this face covers, e.g. `"U+0000-00FF"` - lets the browser skip the download
|
|
36
36
|
* for pages without those characters. */
|
|
37
37
|
readonly unicodeRange?: string;
|
|
38
38
|
/** Metric overrides that eliminate layout shift when the fallback swaps to the web font (the
|
|
@@ -53,13 +53,13 @@ export interface FontPreloadInput {
|
|
|
53
53
|
readonly href: string;
|
|
54
54
|
/** MIME type (`font/woff2`, …). Inferred from the extension when omitted. */
|
|
55
55
|
readonly type?: string;
|
|
56
|
-
/** `crossorigin` mode
|
|
56
|
+
/** `crossorigin` mode - defaults to `"anonymous"`. Fonts are always fetched in CORS mode, and a
|
|
57
57
|
* preload whose `crossorigin` doesn't match the actual fetch is **wasted** (downloaded twice), so
|
|
58
58
|
* this is on by default. */
|
|
59
59
|
readonly crossOrigin?: "anonymous" | "use-credentials";
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
|
-
* Build a font preload as a `<link>` attribute set for a route/layout's `meta.link`
|
|
62
|
+
* Build a font preload as a `<link>` attribute set for a route/layout's `meta.link` - nifra injects it
|
|
63
63
|
* into `<head>` (`<link rel="preload" as="font" type="font/woff2" crossorigin="anonymous">`). Values are
|
|
64
64
|
* escaped at injection by the head renderer. Preloading the font file removes a render-blocking round
|
|
65
65
|
* trip (the browser would otherwise discover the font only after parsing the CSS).
|
package/dist/fonts.js
CHANGED
|
@@ -69,7 +69,7 @@ export function fontFace(face) {
|
|
|
69
69
|
return `@font-face {\n ${declarations.join(";\n ")};\n}`;
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
|
-
* Build a font preload as a `<link>` attribute set for a route/layout's `meta.link`
|
|
72
|
+
* Build a font preload as a `<link>` attribute set for a route/layout's `meta.link` - nifra injects it
|
|
73
73
|
* into `<head>` (`<link rel="preload" as="font" type="font/woff2" crossorigin="anonymous">`). Values are
|
|
74
74
|
* escaped at injection by the head renderer. Preloading the font file removes a render-blocking round
|
|
75
75
|
* trip (the browser would otherwise discover the font only after parsing the CSS).
|