@real-router/angular 0.24.0 → 0.24.1

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.
@@ -1,27 +0,0 @@
1
- import { NgTemplateOutlet } from "@angular/common";
2
- import { afterNextRender, Component, input, signal } from "@angular/core";
3
-
4
- import type { TemplateRef } from "@angular/core";
5
-
6
- @Component({
7
- selector: "client-only",
8
- template: `
9
- @if (mounted()) {
10
- <ng-content />
11
- } @else if (fallback()) {
12
- <ng-container [ngTemplateOutlet]="fallback() ?? null" />
13
- }
14
- `,
15
- imports: [NgTemplateOutlet],
16
- })
17
- export class ClientOnly {
18
- readonly fallback = input<TemplateRef<unknown>>();
19
-
20
- readonly mounted = signal(false);
21
-
22
- constructor() {
23
- afterNextRender(() => {
24
- this.mounted.set(true);
25
- });
26
- }
27
- }
@@ -1,123 +0,0 @@
1
- import { Component, inject, input, isDevMode } from "@angular/core";
2
-
3
- import { HTTP_STATUS_SINK } from "../utils/createHttpStatusSink";
4
-
5
- import type { OnInit } from "@angular/core";
6
-
7
- /**
8
- * Render-time HTTP status declaration. Mount inside a route component
9
- * (typical use case: a glob `*` route's NotFound page) when the status is
10
- * decided by the rendered tree rather than a loader.
11
- *
12
- * Writes `code` to the optionally injected `HTTP_STATUS_SINK` in `ngOnInit`
13
- * (after the input binding has fired) and renders nothing. Without a provider
14
- * registered (the standard client-side case) the component is a silent no-op
15
- * — same component tree hydrates without touching the DOM or warning about
16
- * mismatches.
17
- *
18
- * Loader-driven errors (`LoaderNotFound` → 404, `LoaderRedirect` → 30x) keep
19
- * working as before; this component covers render-time decisions only.
20
- *
21
- * Last write wins when several `<http-status-code />` instances mount in the
22
- * same render pass — sink reflects the last component whose `ngOnInit` ran.
23
- *
24
- * ```ts
25
- * // entry-server.ts
26
- * import { bootstrapApplication } from "@angular/platform-browser";
27
- * import {
28
- * createHttpStatusSink,
29
- * provideHttpStatusSink,
30
- * } from "@real-router/angular/ssr";
31
- *
32
- * const sink = createHttpStatusSink();
33
- * await bootstrapApplication(AppRoot, {
34
- * providers: [
35
- * provideRealRouterFactory({ ... }),
36
- * provideHttpStatusSink(sink),
37
- * ],
38
- * });
39
- * response.status(sink.code ?? 200).send(html);
40
- * ```
41
- *
42
- * ```html
43
- * <!-- inside not-found.component.ts template -->
44
- * <http-status-code [code]="404" />
45
- * ```
46
- *
47
- * **Per-request wiring with `AngularNodeAppEngine`:** the sink must be
48
- * passed via the second arg of `handle(req, requestContext)` — Angular
49
- * surfaces it through the `REQUEST_CONTEXT` token. Attaching to `req`
50
- * directly does NOT work: `AngularNodeAppEngine.handle` constructs a fresh
51
- * Web `Request` from the Express `IncomingMessage` and discards every
52
- * custom property. See the `ssr/` example's `app.config.ts` factory for
53
- * the canonical pattern (`inject(REQUEST_CONTEXT, { optional: true })`
54
- * → `(ctx as { httpStatusSink? } | null)?.httpStatusSink`).
55
- *
56
- * **`@angular/ssr` streaming + `@defer` blocks:** `@defer` blocks hydrate
57
- * lazily on the client; their server-side rendering is fully synchronous,
58
- * so `<http-status-code />` inside or outside a `@defer` writes to the
59
- * sink before `AngularNodeAppEngine.handle` resolves. No streaming
60
- * ordering concern in Angular's current SSR model.
61
- *
62
- * **JIT vs AOT:** the `code` input is declared as `input<number>()` (not
63
- * `input.required<number>()`) because `input.required` trips `NG0950` in
64
- * JIT/TestBed even after `componentRef.setInput(...)`. `ngOnInit` skips
65
- * the write when `code()` is `undefined`. AOT (production build) binds
66
- * the value normally and the skip never fires.
67
- *
68
- * **Valid `code` range:** Node's `res.end()` throws `Invalid status code`
69
- * on `NaN`, `0`, negative values, or values `> 999` — this surfaces as a
70
- * 5xx / dropped connection, not silent corruption. Pass a real HTTP status
71
- * integer (commonly 4xx/5xx; 100-999 is what Node accepts).
72
- */
73
- @Component({
74
- selector: "http-status-code",
75
- template: "",
76
- })
77
- export class HttpStatusCode implements OnInit {
78
- /**
79
- * HTTP status to apply to the response. Common values: 404, 410, 451, 503.
80
- *
81
- * Declared as optional so the signal is safe to read in `ngOnInit` under
82
- * both AOT (template binding fires before init hooks) and JIT/TestBed
83
- * (`componentRef.setInput("code", N)` writes the value before the first
84
- * change detection). `input.required` would trip `NG0950` in the JIT path
85
- * because the required-flag is asserted independently of the runtime
86
- * value. Consumers should always pass a value — `undefined` makes
87
- * `ngOnInit` skip the sink write rather than throw.
88
- */
89
- readonly code = input<number>();
90
-
91
- // Optional injection — when no `provideHttpStatusSink(...)` is registered
92
- // (client side) the field is null and `ngOnInit` skips the write.
93
- private readonly sink = inject(HTTP_STATUS_SINK, { optional: true });
94
-
95
- ngOnInit(): void {
96
- if (!this.sink) {
97
- return;
98
- }
99
-
100
- const value = this.code();
101
-
102
- if (value !== undefined) {
103
- // Dev-only validation (parity with the other adapters, #1441): Node's
104
- // res.end() throws `Invalid status code` on NaN / 0 / non-integer / >999.
105
- // Surface the bad value at the source instead of at the response boundary.
106
- // Angular uses `isDevMode()` rather than `process.env.NODE_ENV` — ng-packagr
107
- // does not replace the latter and `process` is undefined in the browser.
108
- // JIT/TestBed reads `code()` as undefined, so this branch (and the sink
109
- // write below) is reachable only under AOT — covered by the AOT
110
- // ssr-examples once an `<http-status-code>` consumer lands there.
111
- if (
112
- isDevMode() &&
113
- (!Number.isInteger(value) || value < 100 || value > 999)
114
- ) {
115
- console.error(
116
- `[real-router] <http-status-code [code]="${String(value)}" /> received an invalid HTTP status code. Node's res.end() rejects values that are not an integer in [100, 999] — pass a real HTTP status (commonly 4xx/5xx).`,
117
- );
118
- }
119
-
120
- this.sink.code = value;
121
- }
122
- }
123
- }
@@ -1,27 +0,0 @@
1
- import { NgTemplateOutlet } from "@angular/common";
2
- import { afterNextRender, Component, input, signal } from "@angular/core";
3
-
4
- import type { TemplateRef } from "@angular/core";
5
-
6
- @Component({
7
- selector: "server-only",
8
- template: `
9
- @if (!mounted()) {
10
- <ng-content />
11
- } @else if (fallback()) {
12
- <ng-container [ngTemplateOutlet]="fallback() ?? null" />
13
- }
14
- `,
15
- imports: [NgTemplateOutlet],
16
- })
17
- export class ServerOnly {
18
- readonly fallback = input<TemplateRef<unknown>>();
19
-
20
- readonly mounted = signal(false);
21
-
22
- constructor() {
23
- afterNextRender(() => {
24
- this.mounted.set(true);
25
- });
26
- }
27
- }
@@ -1,92 +0,0 @@
1
- import { computed, effect, signal } from "@angular/core";
2
-
3
- import { injectRoute } from "@real-router/angular";
4
-
5
- import type { Signal } from "@angular/core";
6
-
7
- interface DeferredContext {
8
- ssrDataDeferred?: Record<string, Promise<unknown>>;
9
- }
10
-
11
- const NEVER_PROMISE = new Promise<never>(() => {
12
- // Intentionally never resolves — settles as `undefined` indefinitely when
13
- // a key is requested that the loader never declared. Surfaces consumer/
14
- // loader key drift as a visible "loading" state in the UI.
15
- });
16
-
17
- /**
18
- * Read a deferred promise published by `defer({ deferred: { <key>: Promise } })`
19
- * inside an SSR data loader. Returns an Angular `Signal<T | undefined>` that
20
- * tracks the active route — re-keying picks up the new state's deferred map.
21
- *
22
- * The signal starts `undefined` and updates to the resolved value once the
23
- * promise settles. Use with native Angular control flow:
24
- *
25
- * ```ts
26
- * @Component({
27
- * template: `
28
- * @if (reviews()) {
29
- * <ul>
30
- * @for (r of reviews(); track r.id) {
31
- * <li>{{ r.author }}</li>
32
- * }
33
- * </ul>
34
- * } @else {
35
- * <p>Loading reviews…</p>
36
- * }
37
- * `,
38
- * })
39
- * export class Reviews {
40
- * readonly reviews = injectDeferred<Review[]>("reviews");
41
- * }
42
- * ```
43
- *
44
- * **Asymmetric Angular** (see `.claude/SSR_FEATURE_GAPS_RU.md` §8): Angular
45
- * does not ship `<Await>` / `<Streamed>` adapter components — Angular has no
46
- * direct analogue to React's `use(promise)` or Svelte's `{#await}`. Use
47
- * `@if (signal()) { … } @else { … }` or the `async` pipe with
48
- * `from(deferredPromise)` instead.
49
- */
50
- export function injectDeferred<T = unknown>(
51
- key: string,
52
- ): Signal<T | undefined> {
53
- const { routeState } = injectRoute();
54
-
55
- // Re-derive the promise reference whenever the route changes — invalidate()
56
- // + reload, navigation to a new route, etc. all refresh the underlying
57
- // deferred map, and we want the signal to track the *latest* promise.
58
- const promiseSignal = computed<Promise<T>>(() => {
59
- const context = routeState().route.context as DeferredContext;
60
- const deferred = context.ssrDataDeferred;
61
-
62
- return (deferred?.[key] ?? NEVER_PROMISE) as Promise<T>;
63
- });
64
-
65
- const value = signal<T | undefined>(undefined);
66
-
67
- effect((onCleanup) => {
68
- const promise = promiseSignal();
69
- let cancelled = false;
70
-
71
- onCleanup(() => {
72
- cancelled = true;
73
- });
74
-
75
- promise.then(
76
- (resolved) => {
77
- if (!cancelled) {
78
- value.set(resolved);
79
- }
80
- },
81
- /* v8 ignore next 4 -- @preserve: rejection branch — `effect` swallows
82
- async errors silently, so leaving the signal as `undefined` is the
83
- only observable behaviour. Real error surfacing is the loader's
84
- responsibility (throw → navigation rejects → app error boundary). */
85
- () => {
86
- // Intentional swallow — see v8 ignore note above.
87
- },
88
- );
89
- });
90
-
91
- return value.asReadonly();
92
- }
@@ -1,43 +0,0 @@
1
- import { makeEnvironmentProviders } from "@angular/core";
2
-
3
- import { HTTP_STATUS_SINK } from "../utils/createHttpStatusSink";
4
-
5
- import type { HttpStatusSink } from "../utils/createHttpStatusSink";
6
- import type { EnvironmentProviders } from "@angular/core";
7
-
8
- /**
9
- * Environment providers for a request-scoped `HttpStatusSink`. Pair with
10
- * `createHttpStatusSink()` and read `sink.code` after the SSR render pass
11
- * completes.
12
- *
13
- * Application bootstrap:
14
- *
15
- * ```ts
16
- * const sink = createHttpStatusSink();
17
- *
18
- * await bootstrapApplication(AppRoot, {
19
- * providers: [
20
- * provideRealRouterFactory({ ... }),
21
- * provideHttpStatusSink(sink),
22
- * ],
23
- * });
24
- *
25
- * response.status(sink.code ?? 200).send(html);
26
- * ```
27
- *
28
- * Equivalent to:
29
- *
30
- * ```ts
31
- * { provide: HTTP_STATUS_SINK, useValue: sink }
32
- * ```
33
- *
34
- * Use the explicit `useValue` form when you need to compose with other
35
- * application providers in a single `providers: [...]` block.
36
- */
37
- export function provideHttpStatusSink(
38
- sink: HttpStatusSink,
39
- ): EnvironmentProviders {
40
- return makeEnvironmentProviders([
41
- { provide: HTTP_STATUS_SINK, useValue: sink },
42
- ]);
43
- }
@@ -1,6 +0,0 @@
1
- {
2
- "$schema": "https://raw.githubusercontent.com/ng-packagr/ng-packagr/main/src/ng-package.schema.json",
3
- "lib": {
4
- "entryFile": "public_api.ts"
5
- }
6
- }
package/ssr/public_api.ts DELETED
@@ -1,35 +0,0 @@
1
- // SSR-feature entry — Angular 21+
2
- //
3
- // Server-side and SSR-aware components/functions. Mirrors the
4
- // `/ssr` subpath split shipped by every other adapter (#604 + #610).
5
- // Trigger reached: `<ClientOnly>`, `<ServerOnly>`, `injectDeferred()`
6
- // — three SSR-feature exports, ≥3 threshold per
7
- // `.claude/SSR_FEATURE_GAPS_RU.md` §8.
8
- //
9
- // Asymmetric Angular note: Angular has no native `<Suspense>` /
10
- // `use(promise)` analogue, so this entry exposes the signal-based
11
- // `injectDeferred()` instead of `<Await>` / `<Streamed>` adapter
12
- // components. Consumers compose with `@if (signal()) { … } @else { … }`,
13
- // the `async` pipe (`from(deferredPromise)`), or native `@defer`
14
- // blocks for chunk-level lazy hydration.
15
-
16
- // Components
17
- export { ClientOnly } from "./components/ClientOnly";
18
-
19
- export { ServerOnly } from "./components/ServerOnly";
20
-
21
- export { HttpStatusCode } from "./components/HttpStatusCode";
22
-
23
- // Functions
24
- export { injectDeferred } from "./functions/injectDeferred";
25
-
26
- export { provideHttpStatusSink } from "./functions/provideHttpStatusSink";
27
-
28
- // Utilities
29
- export {
30
- HTTP_STATUS_SINK,
31
- createHttpStatusSink,
32
- } from "./utils/createHttpStatusSink";
33
-
34
- // Types
35
- export type { HttpStatusSink } from "./utils/createHttpStatusSink";
@@ -1,61 +0,0 @@
1
- import { InjectionToken } from "@angular/core";
2
-
3
- /**
4
- * Render-scoped HTTP status sink. Created per request on the server and
5
- * provided via `provideHttpStatusSink(sink)` (or directly through
6
- * `{ provide: HTTP_STATUS_SINK, useValue: sink }`). Read after the SSR pass
7
- * (`renderApplication` / `AngularNodeAppEngine` rendering) to apply the value
8
- * to the HTTP response.
9
- *
10
- * Last write wins: if the rendered tree mounts more than one
11
- * `<http-status-code [code]="N" />`, the value reflects the last component
12
- * that ran during the render pass.
13
- *
14
- * No-op on the client — `<http-status-code />` injects `HTTP_STATUS_SINK`
15
- * with `{ optional: true }` and skips the write when no provider is
16
- * registered, so the same component tree can be hydrated without changing
17
- * behaviour.
18
- *
19
- * Constraints:
20
- * - **Per-request only.** Don't share a sink across requests; the rendered
21
- * tree mutates `code` in place. Module-level singletons leak status
22
- * between concurrent requests.
23
- * - **Don't `Object.freeze` the sink.** The component writes to `.code`;
24
- * freezing makes the assignment throw under ESM strict mode.
25
- * - **Pass through `REQUEST_CONTEXT`, not via `req` properties.** Wire the
26
- * sink with `angularApp.handle(req, { httpStatusSink })` and read it back
27
- * in the `HTTP_STATUS_SINK` factory via `inject(REQUEST_CONTEXT)`.
28
- * `AngularNodeAppEngine` builds a fresh Web `Request` from the
29
- * `IncomingMessage` and discards every custom property, so attaching to
30
- * `req` directly silently no-ops.
31
- */
32
- export interface HttpStatusSink {
33
- code: number | undefined;
34
- }
35
-
36
- export function createHttpStatusSink(): HttpStatusSink {
37
- return { code: undefined };
38
- }
39
-
40
- /**
41
- * DI token for the request-scoped HTTP status sink. Application-side wiring:
42
- *
43
- * ```ts
44
- * import { bootstrapApplication } from "@angular/platform-browser";
45
- * import { provideHttpStatusSink, createHttpStatusSink } from "@real-router/angular/ssr";
46
- *
47
- * const sink = createHttpStatusSink();
48
- *
49
- * await bootstrapApplication(AppRoot, {
50
- * providers: [
51
- * provideRealRouterFactory({ ... }),
52
- * provideHttpStatusSink(sink),
53
- * ],
54
- * });
55
- *
56
- * response.status(sink.code ?? 200).send(html);
57
- * ```
58
- */
59
- export const HTTP_STATUS_SINK = new InjectionToken<HttpStatusSink>(
60
- "HTTP_STATUS_SINK",
61
- );