@loozo/angular-launch 0.0.6 → 0.1.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 +715 -255
- package/builders/application.js +32 -3
- package/builders/channel-forwarding.d.ts +15 -0
- package/builders/channel-forwarding.js +61 -0
- package/builders/options.d.ts +6 -2
- package/builders/options.js +16 -5
- package/builders/serve.js +198 -140
- package/channel.d.ts +3 -1
- package/channel.js +3 -2
- package/client.d.ts +14 -24
- package/generation/index.d.ts +2 -123
- package/generation/index.js +3 -685
- package/generation/redirects.d.ts +14 -0
- package/generation/redirects.js +43 -0
- package/generation/run.d.ts +168 -0
- package/generation/run.js +659 -0
- package/index.d.ts +2 -0
- package/index.js +1 -0
- package/launch-queries.d.ts +16 -0
- package/launch-queries.js +3 -0
- package/navigation.d.ts +3 -3
- package/navigation.js +1 -1
- package/node/index.d.ts +3 -0
- package/node/index.js +6 -3
- package/node/upgrade-listener-slot.d.ts +1 -0
- package/node/upgrade-listener-slot.js +4 -0
- package/outcomes.d.ts +23 -7
- package/outcomes.js +58 -12
- package/package.json +15 -11
- package/redirect-translation.d.ts +53 -0
- package/redirect-translation.js +141 -0
- package/{generated.js → remote-client.js} +27 -21
- package/route-types.d.ts +130 -0
- package/route-types.js +1 -0
- package/route.d.ts +45 -0
- package/route.js +113 -0
- package/schemas/application.json +19 -3
- package/schemas/serve.json +3 -3
- package/server/application.d.ts +107 -0
- package/server/application.js +108 -0
- package/server/backend.d.ts +25 -13
- package/server/backend.js +39 -24
- package/server/channel-messages.d.ts +1 -1
- package/server/channel-messages.js +1 -1
- package/server/channel-socket.d.ts +1 -1
- package/server/definitions.d.ts +146 -298
- package/server/definitions.js +29 -117
- package/server/dispatch.d.ts +67 -63
- package/server/dispatch.js +121 -61
- package/server/event-stream.d.ts +2 -2
- package/server/event-stream.js +1 -1
- package/server/generated.d.ts +2 -1
- package/server/generated.js +2 -1
- package/server/index.d.ts +3 -2
- package/server/index.js +2 -1
- package/server/outcomes.d.ts +26 -13
- package/server/outcomes.js +43 -16
- package/server/read-cookies.d.ts +19 -0
- package/server/read-cookies.js +39 -0
- package/server/response-effects.d.ts +2 -2
- package/server/stages.d.ts +62 -0
- package/server/stages.js +51 -0
- package/server/upgrade.d.ts +11 -12
- package/server/upgrade.js +13 -14
- package/source-text.d.ts +1 -0
- package/source-text.js +56 -0
- package/stream.d.ts +6 -3
- package/stream.js +8 -6
- package/tanstack-query/README.md +15 -15
- package/tanstack-query/create-queries.d.ts +2 -0
- package/tanstack-query/{generated.js → create-queries.js} +8 -9
- package/tanstack-query/hydration-window.d.ts +11 -0
- package/tanstack-query/hydration-window.js +9 -0
- package/tanstack-query/hydration.d.ts +4 -14
- package/tanstack-query/hydration.js +9 -12
- package/tanstack-query/index.d.ts +1 -1
- package/tanstack-query/queries.d.ts +42 -27
- package/testing/call-procedure.d.ts +154 -0
- package/testing/call-procedure.js +97 -0
- package/testing/index.d.ts +2 -0
- package/testing/index.js +1 -0
- package/generation/context-file.d.ts +0 -2
- package/generation/context-file.js +0 -3
- package/tanstack-query/generated.d.ts +0 -5
- /package/{generated.d.ts → remote-client.d.ts} +0 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// SP and HTAB only (RFC 6265bis WSP), as browsers trim; String#trim would also strip U+00A0.
|
|
2
|
+
const whitespace = /^[\t ]+|[\t ]+$/g;
|
|
3
|
+
/**
|
|
4
|
+
* Reads the request's cookies from its `Cookie` header.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Values are returned as sent, without decoding, as `setCookie` doesn't encode. A repeated
|
|
8
|
+
* name keeps its first occurrence in the header. The header doesn't carry the cookies' paths
|
|
9
|
+
* or domains, so which cookie that is depends on the browser's order. Pairs without a name
|
|
10
|
+
* are skipped. During server rendering every call reads the page request, so a cookie set
|
|
11
|
+
* earlier in the render isn't seen.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const token = readCookies(request).get("session");
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @param request - The request, such as `request` in a stage, middleware or handler.
|
|
19
|
+
* @returns A new map of cookie values by name.
|
|
20
|
+
*/
|
|
21
|
+
export function readCookies(request) {
|
|
22
|
+
const cookies = new Map();
|
|
23
|
+
const header = request.headers.get("cookie");
|
|
24
|
+
if (header === null) {
|
|
25
|
+
return cookies;
|
|
26
|
+
}
|
|
27
|
+
for (const pair of header.split(";")) {
|
|
28
|
+
const separator = pair.indexOf("=");
|
|
29
|
+
if (separator === -1) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const name = pair.slice(0, separator).replace(whitespace, "");
|
|
33
|
+
if (name === "" || cookies.has(name)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
cookies.set(name, pair.slice(separator + 1).replace(whitespace, ""));
|
|
37
|
+
}
|
|
38
|
+
return cookies;
|
|
39
|
+
}
|
|
@@ -41,7 +41,7 @@ export interface CookieOptions {
|
|
|
41
41
|
*
|
|
42
42
|
* The effects are sent once the call settled, or, for a stream, once its handler returned
|
|
43
43
|
* its iterable. After that, setting a cookie throws, and header changes are ignored. So a
|
|
44
|
-
* stream sets its cookies in middleware before `next()`, or in a handler that isn't a
|
|
44
|
+
* stream sets its cookies in a stage, in middleware before `next()`, or in a handler that isn't a
|
|
45
45
|
* generator, before it returns its iterable: an `async function*` handler's body runs
|
|
46
46
|
* after its response started.
|
|
47
47
|
*
|
|
@@ -84,7 +84,7 @@ export interface ResponseControl {
|
|
|
84
84
|
* The response effects one call collects. Internal.
|
|
85
85
|
*
|
|
86
86
|
* @remarks
|
|
87
|
-
* The transport that created them passes `control` to `
|
|
87
|
+
* The transport that created them passes `control` to `runProcedure`, calls `commit` when it
|
|
88
88
|
* produces the response, and reads `cookies` and `headers` then.
|
|
89
89
|
*/
|
|
90
90
|
export interface ResponseEffects {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { StageArgs } from "./definitions.js";
|
|
2
|
+
import type { ResponseControl } from "./response-effects.js";
|
|
3
|
+
/**
|
|
4
|
+
* One route occurrence's `.context()` stage. It receives the context of the stages above it
|
|
5
|
+
* and returns the route's whole context, which replaces it.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Internal; not exported from an entry point. The application binding makes one per
|
|
9
|
+
* authored route that declares a stage, so a stage function shared by two routes still
|
|
10
|
+
* runs once per route.
|
|
11
|
+
*/
|
|
12
|
+
export interface Stage {
|
|
13
|
+
/** The authored stage function. */
|
|
14
|
+
readonly run: (args: StageArgs<object>) => object | PromiseLike<object>;
|
|
15
|
+
/** The declaring route's file, workspace-relative, for diagnostics. */
|
|
16
|
+
readonly route: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The part of {@link StageArgs} owned by the request scope.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* Internal; not exported from an entry point.
|
|
23
|
+
*/
|
|
24
|
+
export type StageScope = Pick<StageArgs<object>, "request" | "signal">;
|
|
25
|
+
/**
|
|
26
|
+
* What running a chain of stages settles to: the last stage's context, or the stage that
|
|
27
|
+
* failed with what it threw.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* Internal; not exported from an entry point. Plain data, so it crosses package copies.
|
|
31
|
+
* A propagated failure is the failing stage's own result object, so the stage it names is
|
|
32
|
+
* the one that threw however many chains reuse it.
|
|
33
|
+
*/
|
|
34
|
+
export type StageResult = {
|
|
35
|
+
/** The context the chain's last stage returned. */
|
|
36
|
+
readonly context: object;
|
|
37
|
+
} | {
|
|
38
|
+
/** The stage that threw or rejected. */
|
|
39
|
+
readonly failed: Stage;
|
|
40
|
+
/** What it threw or rejected with, unchanged. */
|
|
41
|
+
readonly error: unknown;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Runs `chain` from its first stage for one call and settles to its {@link StageResult}.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* Internal; not exported from an entry point.
|
|
48
|
+
* Each stage runs at most once per runner, for the first call that reaches it and with that
|
|
49
|
+
* call's `response`; later chains sharing a stage reuse its memoized result, failures
|
|
50
|
+
* included. A stage must always have the same stages before it. The promise never rejects.
|
|
51
|
+
*/
|
|
52
|
+
export type StageRunner = (chain: readonly [Stage, ...Stage[]], response: ResponseControl) => Promise<StageResult>;
|
|
53
|
+
/**
|
|
54
|
+
* Creates the stage runner for one request scope.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* Internal; not exported from an entry point.
|
|
58
|
+
*
|
|
59
|
+
* @param scope - The request and signal every stage of the scope receives.
|
|
60
|
+
* @returns The scope's stage runner.
|
|
61
|
+
*/
|
|
62
|
+
export declare function createStageRunner(scope: StageScope): StageRunner;
|
package/server/stages.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates the stage runner for one request scope.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Internal; not exported from an entry point.
|
|
6
|
+
*
|
|
7
|
+
* @param scope - The request and signal every stage of the scope receives.
|
|
8
|
+
* @returns The scope's stage runner.
|
|
9
|
+
*/
|
|
10
|
+
export function createStageRunner(scope) {
|
|
11
|
+
// Each stage's result: the context it returned, or the first failure up to it.
|
|
12
|
+
const memo = new Map();
|
|
13
|
+
return (chain, response) => {
|
|
14
|
+
let parent;
|
|
15
|
+
for (const stage of chain) {
|
|
16
|
+
let entry = memo.get(stage);
|
|
17
|
+
if (entry === undefined) {
|
|
18
|
+
if (parent === undefined) {
|
|
19
|
+
// The first stage is called now, synchronously, and receives `{}`.
|
|
20
|
+
entry = settle(stage, invoke(stage, { ...scope, response, context: {} }));
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
entry = parent.then((result) => {
|
|
24
|
+
// An ancestor failed: pass its own result on, so it keeps naming that stage.
|
|
25
|
+
if ("failed" in result) {
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
const { context } = result;
|
|
29
|
+
return settle(stage, invoke(stage, { ...scope, response, context }));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// Stored before it settles, so a failure is memoized too.
|
|
33
|
+
memo.set(stage, entry);
|
|
34
|
+
}
|
|
35
|
+
parent = entry;
|
|
36
|
+
}
|
|
37
|
+
// A non-empty chain always sets it.
|
|
38
|
+
return parent;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function settle(stage, own) {
|
|
42
|
+
return own.then((context) => ({ context }), (error) => ({ failed: stage, error }));
|
|
43
|
+
}
|
|
44
|
+
function invoke(stage, args) {
|
|
45
|
+
try {
|
|
46
|
+
return Promise.resolve(stage.run(args));
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
return Promise.reject(error);
|
|
50
|
+
}
|
|
51
|
+
}
|
package/server/upgrade.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { type
|
|
1
|
+
import type { ApplicationBinding } from "./application.js";
|
|
2
|
+
import { type HandleOptions } from "./dispatch.js";
|
|
3
3
|
/**
|
|
4
4
|
* A channel call that passed its checks: the host upgrades the request to a websocket and
|
|
5
5
|
* hands Launch the socket.
|
|
@@ -29,30 +29,29 @@ export interface Upgrade {
|
|
|
29
29
|
* Internal: exported only from `@loozo/angular-launch/_server` for the generated
|
|
30
30
|
* `handleUpgrade` in `server.generated.ts`; call that instead.
|
|
31
31
|
*
|
|
32
|
-
* Any path starting with `/_rpc/` is a channel request. Before any
|
|
32
|
+
* Any path starting with `/_rpc/` is a channel request. Before any stage runs, the
|
|
33
33
|
* request is refused with an HTTP response, as `handleRequest` refuses RPCs: a plain-text 400
|
|
34
34
|
* `Host not allowed` when its hostname isn't in `allowedHosts`; `INVALID_INPUT` with no issues
|
|
35
35
|
* unless it's a `GET` with `Upgrade: websocket` and a query of exactly `input` holding JSON;
|
|
36
36
|
* and `FORBIDDEN` unless it's proven same-origin by `handleRequest`'s rules.
|
|
37
37
|
*
|
|
38
|
-
* A fresh
|
|
39
|
-
* isn't a channel, then
|
|
40
|
-
* handler, which returns its iterable. From then
|
|
41
|
-
* the
|
|
42
|
-
*
|
|
43
|
-
* receives the failure frame
|
|
38
|
+
* A fresh request scope is then created and the call run: `RPC_NOT_FOUND` for a path that
|
|
39
|
+
* isn't a channel, then the owning route's stages and `.use()` middleware, input
|
|
40
|
+
* validation, procedure middleware and the handler, which returns its iterable. From then
|
|
41
|
+
* on the result is an {@link Upgrade} carrying the call's headers and cookies, including
|
|
42
|
+
* the stages'; setting a cookie later throws. A failure before that, including a stage
|
|
43
|
+
* that throws (`INTERNAL`), is an `Upgrade` too, whose socket receives the failure frame
|
|
44
|
+
* and closes with 1000.
|
|
44
45
|
*
|
|
45
46
|
* Until the socket is accepted the call's signal follows `request.signal`; a request that
|
|
46
47
|
* aborts before the handler is reached skips the handler, and one that aborts after it
|
|
47
48
|
* returned its iterable returns that iterator before its body ran. Either way the socket,
|
|
48
49
|
* if accepted, closes without a frame. Afterwards the signal follows the socket closing.
|
|
49
50
|
*
|
|
50
|
-
* @typeParam Definition - The application's context definition.
|
|
51
|
-
* @param registry - The application's RPC groups.
|
|
52
51
|
* @param application - The application binding.
|
|
53
52
|
* @param request - The upgrade request, as a web `Request`.
|
|
54
53
|
* @param options - The allowed hosts and further allowed origins.
|
|
55
54
|
* @returns A refusal response, the upgrade, or `null` when the path doesn't start with
|
|
56
55
|
* `/_rpc/`.
|
|
57
56
|
*/
|
|
58
|
-
export declare function handleChannelUpgrade
|
|
57
|
+
export declare function handleChannelUpgrade(application: ApplicationBinding, request: Request, { allowedHosts, origins }: HandleOptions): Promise<Response | Upgrade | null>;
|
package/server/upgrade.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parseJson } from "../json.js";
|
|
2
2
|
import { createChannelSocket } from "./channel-socket.js";
|
|
3
|
-
import { createRequestScope, crossSiteReason,
|
|
3
|
+
import { createRequestScope, crossSiteReason, runProcedure, failure, hostAllowed, hostRefusal, invalidInput, response, responseHeaders, } from "./dispatch.js";
|
|
4
4
|
import { failureFrame } from "./event-stream.js";
|
|
5
5
|
import { createResponseEffects } from "./response-effects.js";
|
|
6
6
|
/** The `{ input }` envelope a channel's query carries, or `undefined` when it's malformed. */
|
|
@@ -41,33 +41,32 @@ function refusedUpgrade(effects, reply) {
|
|
|
41
41
|
* Internal: exported only from `@loozo/angular-launch/_server` for the generated
|
|
42
42
|
* `handleUpgrade` in `server.generated.ts`; call that instead.
|
|
43
43
|
*
|
|
44
|
-
* Any path starting with `/_rpc/` is a channel request. Before any
|
|
44
|
+
* Any path starting with `/_rpc/` is a channel request. Before any stage runs, the
|
|
45
45
|
* request is refused with an HTTP response, as `handleRequest` refuses RPCs: a plain-text 400
|
|
46
46
|
* `Host not allowed` when its hostname isn't in `allowedHosts`; `INVALID_INPUT` with no issues
|
|
47
47
|
* unless it's a `GET` with `Upgrade: websocket` and a query of exactly `input` holding JSON;
|
|
48
48
|
* and `FORBIDDEN` unless it's proven same-origin by `handleRequest`'s rules.
|
|
49
49
|
*
|
|
50
|
-
* A fresh
|
|
51
|
-
* isn't a channel, then
|
|
52
|
-
* handler, which returns its iterable. From then
|
|
53
|
-
* the
|
|
54
|
-
*
|
|
55
|
-
* receives the failure frame
|
|
50
|
+
* A fresh request scope is then created and the call run: `RPC_NOT_FOUND` for a path that
|
|
51
|
+
* isn't a channel, then the owning route's stages and `.use()` middleware, input
|
|
52
|
+
* validation, procedure middleware and the handler, which returns its iterable. From then
|
|
53
|
+
* on the result is an {@link Upgrade} carrying the call's headers and cookies, including
|
|
54
|
+
* the stages'; setting a cookie later throws. A failure before that, including a stage
|
|
55
|
+
* that throws (`INTERNAL`), is an `Upgrade` too, whose socket receives the failure frame
|
|
56
|
+
* and closes with 1000.
|
|
56
57
|
*
|
|
57
58
|
* Until the socket is accepted the call's signal follows `request.signal`; a request that
|
|
58
59
|
* aborts before the handler is reached skips the handler, and one that aborts after it
|
|
59
60
|
* returned its iterable returns that iterator before its body ran. Either way the socket,
|
|
60
61
|
* if accepted, closes without a frame. Afterwards the signal follows the socket closing.
|
|
61
62
|
*
|
|
62
|
-
* @typeParam Definition - The application's context definition.
|
|
63
|
-
* @param registry - The application's RPC groups.
|
|
64
63
|
* @param application - The application binding.
|
|
65
64
|
* @param request - The upgrade request, as a web `Request`.
|
|
66
65
|
* @param options - The allowed hosts and further allowed origins.
|
|
67
66
|
* @returns A refusal response, the upgrade, or `null` when the path doesn't start with
|
|
68
67
|
* `/_rpc/`.
|
|
69
68
|
*/
|
|
70
|
-
export async function handleChannelUpgrade(
|
|
69
|
+
export async function handleChannelUpgrade(application, request, { allowedHosts, origins = [] }) {
|
|
71
70
|
const url = new URL(request.url);
|
|
72
71
|
if (!url.pathname.startsWith("/_rpc/")) {
|
|
73
72
|
return null;
|
|
@@ -90,9 +89,9 @@ export async function handleChannelUpgrade(registry, application, request, { all
|
|
|
90
89
|
const socket = createChannelSocket(request.signal);
|
|
91
90
|
let reply;
|
|
92
91
|
try {
|
|
93
|
-
const scope =
|
|
92
|
+
const scope = createRequestScope(application, request);
|
|
94
93
|
const started = Promise.withResolvers();
|
|
95
|
-
const outcome =
|
|
94
|
+
const outcome = runProcedure(url.pathname, envelope, scope, application, scope.request, "socket", socket.signal, effects, started.resolve, socket.inbox);
|
|
96
95
|
// Failures before the handler returned its iterable are frames on an upgraded socket.
|
|
97
96
|
const first = await Promise.race([outcome, started.promise]);
|
|
98
97
|
if ("iterator" in first) {
|
|
@@ -109,7 +108,7 @@ export async function handleChannelUpgrade(registry, application, request, { all
|
|
|
109
108
|
reply = first;
|
|
110
109
|
}
|
|
111
110
|
catch (error) {
|
|
112
|
-
console.error("Launch
|
|
111
|
+
console.error("Launch request scope failed", error);
|
|
113
112
|
reply = failure("INTERNAL");
|
|
114
113
|
}
|
|
115
114
|
effects.commit();
|
package/source-text.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/source-text.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Installs a source-text `$localize` tag in the unlocalized server host, so the destination
|
|
2
|
+
// copies a translated app's server.generated.ts reaches evaluate to canonical URLs. Internal:
|
|
3
|
+
// exported only as `@loozo/angular-launch/_source-text`, which a translated app's generated
|
|
4
|
+
// server.generated.ts imports first. Side effects only.
|
|
5
|
+
//
|
|
6
|
+
// Angular never runs polyfills in the SSR host entry, and inlining replaces
|
|
7
|
+
// `@angular/localize/init` with a `{ locale }` object in the bundles that do run them. Importing
|
|
8
|
+
// that init module here would print Angular's "Direct import" warning and, in each localized
|
|
9
|
+
// server application that also reaches server.generated.ts, replace the `.locale` Angular sets.
|
|
10
|
+
// So this tag mirrors `ɵ$localize` from `@angular/localize` 22.2 without importing it: it applies
|
|
11
|
+
// a `translate` hook when one was attached, strips a leading `:meaning|description@@id:` block
|
|
12
|
+
// from each part (honoring `\:` escapes, as Angular's `findEndOfBlock` does) and joins the parts
|
|
13
|
+
// with their expressions. It touches only `globalThis.$localize`, never a bare `$localize`
|
|
14
|
+
// identifier, so Angular's inliner has nothing to rewrite here.
|
|
15
|
+
const host = globalThis;
|
|
16
|
+
// Where a part's leading metadata block ends: the index of its closing ":".
|
|
17
|
+
function endOfBlock(cooked, raw) {
|
|
18
|
+
for (let at = 1, rawAt = 1; at < cooked.length; at++, rawAt++) {
|
|
19
|
+
if (raw[rawAt] === "\\") {
|
|
20
|
+
rawAt++;
|
|
21
|
+
}
|
|
22
|
+
else if (cooked[at] === ":") {
|
|
23
|
+
return at;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
throw new Error(`Unterminated $localize metadata block in "${raw}".`);
|
|
27
|
+
}
|
|
28
|
+
function stripBlock(cooked, raw) {
|
|
29
|
+
if (raw.charAt(0) !== ":") {
|
|
30
|
+
return cooked;
|
|
31
|
+
}
|
|
32
|
+
return cooked.substring(endOfBlock(cooked, raw) + 1);
|
|
33
|
+
}
|
|
34
|
+
// An existing tag, such as Angular's own from an earlier `@angular/localize/init`, is kept.
|
|
35
|
+
if (typeof host.$localize !== "function") {
|
|
36
|
+
const existing = host.$localize;
|
|
37
|
+
const tag = (parts, ...expressions) => {
|
|
38
|
+
let messageParts = parts;
|
|
39
|
+
let values = expressions;
|
|
40
|
+
if (tag.translate) {
|
|
41
|
+
[messageParts, values] = tag.translate(messageParts, values);
|
|
42
|
+
}
|
|
43
|
+
let message = stripBlock(messageParts[0], messageParts.raw[0]);
|
|
44
|
+
for (let index = 1; index < messageParts.length; index++) {
|
|
45
|
+
// `+`, as Angular's tag joins them: its conversion differs from String() for some objects.
|
|
46
|
+
message +=
|
|
47
|
+
values[index - 1] + stripBlock(messageParts[index], messageParts.raw[index]);
|
|
48
|
+
}
|
|
49
|
+
return message;
|
|
50
|
+
};
|
|
51
|
+
if (typeof existing === "object" && existing !== null && typeof existing.locale === "string") {
|
|
52
|
+
tag.locale = existing.locale;
|
|
53
|
+
}
|
|
54
|
+
host.$localize = tag;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
package/stream.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { RpcError, type ErrorMetadata, type StreamCallOptions } from "./client.js";
|
|
2
|
+
import type { TranslateRedirect } from "./redirect-translation.js";
|
|
2
3
|
/**
|
|
3
4
|
* The rejection for a failure frame: an `RpcError` passed through `own`, an `RpcRedirect`
|
|
4
5
|
* or an `RpcNotFound`, or `undefined` when the frame is none of them.
|
|
5
6
|
*
|
|
6
7
|
* @remarks
|
|
7
|
-
* Internal. Streams and channels recognize their failure frames with it
|
|
8
|
+
* Internal. Streams and channels recognize their failure frames with it; `translate` puts a
|
|
9
|
+
* redirect's listed words in this build's language.
|
|
8
10
|
*/
|
|
9
|
-
export declare function failureRejection(body: unknown, status: number, declared: readonly ErrorMetadata[], own: (error: RpcError<string, unknown>) => RpcError<string, unknown
|
|
11
|
+
export declare function failureRejection(body: unknown, status: number, declared: readonly ErrorMetadata[], own: (error: RpcError<string, unknown>) => RpcError<string, unknown>, translate?: TranslateRedirect): Error | undefined;
|
|
10
12
|
/**
|
|
11
13
|
* Runs one stream call over `fetch` and yields its values.
|
|
12
14
|
*
|
|
@@ -27,5 +29,6 @@ export declare function failureRejection(body: unknown, status: number, declared
|
|
|
27
29
|
* @param own - Associates a converted error with its procedure for `isError`.
|
|
28
30
|
* @param options - The call options.
|
|
29
31
|
* @param opened - Called once the response turned out to be the stream's events.
|
|
32
|
+
* @param translate - Puts a redirect's listed words in this build's language.
|
|
30
33
|
*/
|
|
31
|
-
export declare function openStream(url: string, input: unknown, declared: readonly ErrorMetadata[], own: (error: RpcError<string, unknown>) => RpcError<string, unknown>, { signal }: StreamCallOptions, opened?: () => void): AsyncGenerator<unknown, void, undefined>;
|
|
34
|
+
export declare function openStream(url: string, input: unknown, declared: readonly ErrorMetadata[], own: (error: RpcError<string, unknown>) => RpcError<string, unknown>, { signal }: StreamCallOptions, opened?: () => void, translate?: TranslateRedirect): AsyncGenerator<unknown, void, undefined>;
|
package/stream.js
CHANGED
|
@@ -41,14 +41,15 @@ function eventData(data) {
|
|
|
41
41
|
* or an `RpcNotFound`, or `undefined` when the frame is none of them.
|
|
42
42
|
*
|
|
43
43
|
* @remarks
|
|
44
|
-
* Internal. Streams and channels recognize their failure frames with it
|
|
44
|
+
* Internal. Streams and channels recognize their failure frames with it; `translate` puts a
|
|
45
|
+
* redirect's listed words in this build's language.
|
|
45
46
|
*/
|
|
46
|
-
export function failureRejection(body, status, declared, own) {
|
|
47
|
+
export function failureRejection(body, status, declared, own, translate) {
|
|
47
48
|
const frame = recognizedError(body, status, declared);
|
|
48
49
|
if (frame) {
|
|
49
50
|
return own(new RpcError(frame.code, status, frame.data, frame.message));
|
|
50
51
|
}
|
|
51
|
-
return recognizedOutcome(body, status);
|
|
52
|
+
return recognizedOutcome(body, status, translate);
|
|
52
53
|
}
|
|
53
54
|
/**
|
|
54
55
|
* Runs one stream call over `fetch` and yields its values.
|
|
@@ -70,8 +71,9 @@ export function failureRejection(body, status, declared, own) {
|
|
|
70
71
|
* @param own - Associates a converted error with its procedure for `isError`.
|
|
71
72
|
* @param options - The call options.
|
|
72
73
|
* @param opened - Called once the response turned out to be the stream's events.
|
|
74
|
+
* @param translate - Puts a redirect's listed words in this build's language.
|
|
73
75
|
*/
|
|
74
|
-
export async function* openStream(url, input, declared, own, { signal }, opened = () => undefined) {
|
|
76
|
+
export async function* openStream(url, input, declared, own, { signal }, opened = () => undefined, translate) {
|
|
75
77
|
if (signal?.aborted) {
|
|
76
78
|
throw new DOMException("RPC stream aborted", "AbortError");
|
|
77
79
|
}
|
|
@@ -99,7 +101,7 @@ export async function* openStream(url, input, declared, own, { signal }, opened
|
|
|
99
101
|
}
|
|
100
102
|
throw new TypeError(`Launch stream request failed with status ${response.status}`);
|
|
101
103
|
}
|
|
102
|
-
const error = failureRejection(body, response.status, declared, own);
|
|
104
|
+
const error = failureRejection(body, response.status, declared, own, translate);
|
|
103
105
|
if (!error) {
|
|
104
106
|
throw new TypeError(`Launch stream request failed with status ${response.status}`);
|
|
105
107
|
}
|
|
@@ -141,7 +143,7 @@ export async function* openStream(url, input, declared, own, { signal }, opened
|
|
|
141
143
|
if (typeof frame === "object" && frame !== null && "status" in frame) {
|
|
142
144
|
const { status, ...body } = frame;
|
|
143
145
|
if (typeof status === "number") {
|
|
144
|
-
error = failureRejection(body, status, declared, own);
|
|
146
|
+
error = failureRejection(body, status, declared, own, translate);
|
|
145
147
|
}
|
|
146
148
|
}
|
|
147
149
|
if (!error) {
|
package/tanstack-query/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# TanStack Query
|
|
2
2
|
|
|
3
|
-
`@loozo/angular-launch/tanstack-query` connects Launch procedures to [TanStack Query for Angular](https://tanstack.com/query/latest/docs/framework/angular/overview) (`@tanstack/angular-query`). Generation
|
|
3
|
+
`@loozo/angular-launch/tanstack-query` connects Launch procedures to [TanStack Query for Angular](https://tanstack.com/query/latest/docs/framework/angular/overview) (`@tanstack/angular-query`). Generation adds typed query and mutation options and keys to your routes' handles, and server-rendered query data reaches the browser through TanStack's own hydration, with no request on load even with `staleTime: 0`.
|
|
4
4
|
|
|
5
5
|
It's optional. Without it, Launch never imports TanStack.
|
|
6
6
|
|
|
@@ -23,7 +23,7 @@ Turn on the `tanstackQuery` generation option in your `@loozo/angular-launch:app
|
|
|
23
23
|
}
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
Each
|
|
26
|
+
Each route's handle then also has `queries`, and its `route.generated.ts` exports its type `Queries`, for the same namespaces as `remote`. With the option off, route copies have neither and don't import this entry point.
|
|
27
27
|
|
|
28
28
|
Add TanStack's provider and `provideLaunchQueries()` to the application's providers, which the server config inherits:
|
|
29
29
|
|
|
@@ -48,27 +48,27 @@ export const config: ApplicationConfig = {
|
|
|
48
48
|
};
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
Pass `provideTanStackQuery` a factory, as above, not a shared client: the factory runs once per application, so every server render gets its own cache and one user's data never reaches another's page. `provideLaunchQueries()` belongs in the application's providers, not a route's or component's. `
|
|
51
|
+
Pass `provideTanStackQuery` a factory, as above, not a shared client: the factory runs once per application, so every server render gets its own cache and one user's data never reaches another's page. `provideLaunchQueries()` belongs in the application's providers, not a route's or component's. Reading `route.queries` throws without it; `route.remote` doesn't need it.
|
|
52
52
|
|
|
53
53
|
## Queries and mutations
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
Read `queries` from the route's handle and pass its options to TanStack. Like `remote`, it's built on first read with the handle's injector, so reading it later, outside an injection context, is fine:
|
|
56
56
|
|
|
57
57
|
```ts
|
|
58
58
|
// src/routes/+authenticated/todos/page.ts
|
|
59
59
|
import { Component, inject } from "@angular/core";
|
|
60
60
|
import { injectMutation, injectQuery, QueryClient } from "@tanstack/angular-query";
|
|
61
|
-
import {
|
|
61
|
+
import { injectRoute } from "./route.generated";
|
|
62
62
|
|
|
63
63
|
@Component({/* … */})
|
|
64
64
|
export default class Todos {
|
|
65
|
-
readonly #
|
|
66
|
-
readonly #queries = injectQueries();
|
|
65
|
+
readonly #route = injectRoute();
|
|
67
66
|
readonly #client = inject(QueryClient);
|
|
68
|
-
readonly todos = injectQuery(() => this.#queries.todos.list.queryOptions());
|
|
67
|
+
readonly todos = injectQuery(() => this.#route.queries.todos.list.queryOptions());
|
|
69
68
|
readonly #complete = injectMutation(() =>
|
|
70
|
-
this.#queries.todos.complete.mutationOptions({
|
|
71
|
-
onSuccess: () =>
|
|
69
|
+
this.#route.queries.todos.complete.mutationOptions({
|
|
70
|
+
onSuccess: () =>
|
|
71
|
+
this.#client.invalidateQueries({ queryKey: this.#route.queries.todos.$key() }),
|
|
72
72
|
}),
|
|
73
73
|
);
|
|
74
74
|
|
|
@@ -76,7 +76,7 @@ export default class Todos {
|
|
|
76
76
|
try {
|
|
77
77
|
await this.#complete.mutateAsync({ id });
|
|
78
78
|
} catch (error) {
|
|
79
|
-
if (this.#
|
|
79
|
+
if (this.#route.remote.todos.complete.isError(error) && error.code === "NOT_FOUND") {
|
|
80
80
|
const missingId: string = error.data.id;
|
|
81
81
|
}
|
|
82
82
|
}
|
|
@@ -116,7 +116,7 @@ Full query keys are tagged with the procedure's output type, so `client.getQuery
|
|
|
116
116
|
|
|
117
117
|
## Invalidation
|
|
118
118
|
|
|
119
|
-
Launch invalidates nothing itself; TanStack does, with the keys above. Invalidate in a mutation's `onSuccess`, by
|
|
119
|
+
Launch invalidates nothing itself; TanStack does, with the keys above. Invalidate in a mutation's `onSuccess`, by namespace, procedure or input:
|
|
120
120
|
|
|
121
121
|
```ts
|
|
122
122
|
onSuccess: () => client.invalidateQueries({ queryKey: queries.todos.$key() }),
|
|
@@ -163,7 +163,7 @@ provideTanStackQuery(() => {
|
|
|
163
163
|
}),
|
|
164
164
|
```
|
|
165
165
|
|
|
166
|
-
- Every Launch key starts with `"launch"`, so this matches every Launch query and mutation, of every
|
|
166
|
+
- Every Launch key starts with `"launch"`, so this matches every Launch query and mutation, of every namespace, and nothing else in the cache. Only active queries, ones a component observes, refetch; the others are marked stale and refetch when they next mount.
|
|
167
167
|
- `onSettled` also runs after a failed write, which can still mean the data changed: a declared `NOT_FOUND` for a todo deleted elsewhere does.
|
|
168
168
|
- TanStack awaits the cache's callbacks, before the mutation's own, so the mutation stays pending until the read finishes.
|
|
169
169
|
- Skipping while another write is still on its way keeps an earlier write's read from replacing a later write's optimistic data, and the last write reads once for all. Count only writes that haven't reached `onSettled`: a mutation stays pending until its callbacks finish, so with `isMutating() > 1` two writes that settle together, in different scopes, each see the other and both skip the read.
|
|
@@ -248,9 +248,9 @@ Everything else is TanStack's or your `QueryClient`'s. Overrides replace either
|
|
|
248
248
|
## Limitations
|
|
249
249
|
|
|
250
250
|
- No infinite queries, mutation cancellation or persistence. Optimistic updates and invalidation are TanStack's, as [above](#invalidation): Launch adds no helpers for them.
|
|
251
|
-
- No options for [streams](../README.md#calling-streams): `
|
|
251
|
+
- No options for [streams](../README.md#calling-streams): `route.queries` leaves them out. TanStack's `streamedQuery` keeps a query fetching until its stream ends, and the Angular adapter keeps the app from becoming stable while a query is fetching, which would hold hydration open as long as the stream runs. It's also still `experimental_streamedQuery` in query-core 5.104. Follow the stream with [`connect()`](../README.md#following-streams-and-channels-with-connect) instead.
|
|
252
252
|
- No options for [channels](../README.md#calling-channels) either, for the same reasons; use `connect()`.
|
|
253
|
+
- A narrowed active-route handle's `queries` uses the `QueryClient` of the `injectActiveRoute()` caller, not one a route provides: a route-level `QueryClient` reaches only that route's own `injectRoute()`.
|
|
253
254
|
- Errors aren't dehydrated: a failed server query runs again in the browser.
|
|
254
255
|
- Only `@tanstack/angular-query`, not `@tanstack/angular-query-experimental`.
|
|
255
256
|
- No special handling for `@defer`: queries in a deferred block follow the same window, so ones that mount after the app is stable refetch when stale.
|
|
256
|
-
- TanStack also exports an `injectQueries` (for running several queries); import one of the two under another name if you need both in a file.
|
|
@@ -2,13 +2,12 @@ import { inject } from "@angular/core";
|
|
|
2
2
|
import { QueryClient, skipToken } from "@tanstack/angular-query";
|
|
3
3
|
import { RpcError } from "../client.js";
|
|
4
4
|
import { RpcNotFound, RpcRedirect } from "../outcomes.js";
|
|
5
|
-
import { hydrationWindow } from "./hydration.js";
|
|
6
|
-
// Builds
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
5
|
+
import { hydrationWindow } from "./hydration-window.js";
|
|
6
|
+
// Builds a route's `queries` over its `remote` client. `route.queries` calls it in the
|
|
7
|
+
// handle creator's injection context, once it checked that `provideLaunchQueries()`, which
|
|
8
|
+
// provides both tokens, is there.
|
|
9
|
+
export const createQueries = (rpc, namespaces) => {
|
|
10
|
+
const hydration = inject(hydrationWindow);
|
|
12
11
|
const client = inject(QueryClient);
|
|
13
12
|
// An arrow function keeps the null check's narrowing; a function declaration is hoisted.
|
|
14
13
|
const refetchOnMount = (query) => {
|
|
@@ -55,7 +54,7 @@ export function createQueries(rpc, groups) {
|
|
|
55
54
|
};
|
|
56
55
|
}
|
|
57
56
|
const result = Object.create(null);
|
|
58
|
-
for (const [namespace, methods] of Object.entries(
|
|
57
|
+
for (const [namespace, methods] of Object.entries(namespaces)) {
|
|
59
58
|
const group = Object.create(null);
|
|
60
59
|
group["$key"] = () => ["launch", namespace];
|
|
61
60
|
for (const [name, method] of Object.entries(methods)) {
|
|
@@ -127,4 +126,4 @@ export function createQueries(rpc, groups) {
|
|
|
127
126
|
result[namespace] = group;
|
|
128
127
|
}
|
|
129
128
|
return result;
|
|
130
|
-
}
|
|
129
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { InjectionToken } from "@angular/core";
|
|
2
|
+
/**
|
|
3
|
+
* Whether the app is still hydrating: open in the browser from bootstrap until the app is
|
|
4
|
+
* first stable, never open on the server.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Internal; provided by `provideLaunchQueries()` and read by route query options.
|
|
8
|
+
*/
|
|
9
|
+
export declare const hydrationWindow: InjectionToken<{
|
|
10
|
+
open: boolean;
|
|
11
|
+
}>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InjectionToken } from "@angular/core";
|
|
2
|
+
/**
|
|
3
|
+
* Whether the app is still hydrating: open in the browser from bootstrap until the app is
|
|
4
|
+
* first stable, never open on the server.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Internal; provided by `provideLaunchQueries()` and read by route query options.
|
|
8
|
+
*/
|
|
9
|
+
export const hydrationWindow = new InjectionToken("Launch hydration window");
|
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
import { InjectionToken } from "@angular/core";
|
|
2
1
|
import type { EnvironmentProviders } from "@angular/core";
|
|
3
2
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @remarks
|
|
8
|
-
* Internal; provided by {@link provideLaunchQueries} and read by generated query options.
|
|
9
|
-
*/
|
|
10
|
-
export declare const hydrationWindow: InjectionToken<{
|
|
11
|
-
open: boolean;
|
|
12
|
-
}>;
|
|
13
|
-
/**
|
|
14
|
-
* Provides what generated `injectQueries()` functions need: the app's hydration window.
|
|
3
|
+
* Provides what route handles' `queries` need: the TanStack factories and the app's
|
|
4
|
+
* hydration window.
|
|
15
5
|
*
|
|
16
6
|
* @remarks
|
|
17
7
|
* Add it to the application's providers next to TanStack's `provideTanStackQuery(...)`,
|
|
18
|
-
* not to a route or component. `
|
|
8
|
+
* not to a route or component. Reading `route.queries` throws without it.
|
|
19
9
|
*
|
|
20
|
-
* In the browser,
|
|
10
|
+
* In the browser, query options don't refetch on mount while the app hydrates,
|
|
21
11
|
* from bootstrap until the app is first stable, so data dehydrated during server rendering
|
|
22
12
|
* is used as is even with `staleTime: 0`. After that, `refetchOnMount` follows your
|
|
23
13
|
* `QueryClient` defaults. This is the same window Angular's HTTP transfer cache uses.
|