@loozo/angular-launch 0.0.4 → 0.0.6
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 +695 -102
- package/builders/application.d.ts +4 -0
- package/builders/application.js +71 -0
- package/builders/options.d.ts +10 -0
- package/builders/options.js +64 -0
- package/builders/serve.d.ts +8 -0
- package/builders/serve.js +248 -0
- package/builders.json +15 -0
- package/channel.d.ts +53 -0
- package/channel.js +231 -0
- package/client.d.ts +59 -16
- package/client.js +3 -2
- package/connect.d.ts +201 -0
- package/connect.js +148 -0
- package/generated.d.ts +1 -1
- package/generated.js +86 -3
- package/generation/context-file.d.ts +2 -0
- package/generation/context-file.js +3 -0
- package/generation/index.js +77 -36
- package/index.d.ts +6 -1
- package/index.js +2 -0
- package/json.d.ts +9 -0
- package/json.js +24 -0
- package/navigation.d.ts +36 -0
- package/navigation.js +47 -0
- package/node/index.d.ts +57 -0
- package/node/index.js +175 -0
- package/outcomes.d.ts +55 -0
- package/outcomes.js +100 -0
- package/package.json +14 -4
- package/schemas/application.json +756 -0
- package/schemas/serve.json +153 -0
- package/server/backend.d.ts +42 -23
- package/server/backend.js +137 -47
- package/server/channel-messages.d.ts +54 -0
- package/server/channel-messages.js +84 -0
- package/server/channel-socket.d.ts +67 -0
- package/server/channel-socket.js +322 -0
- package/server/definitions.d.ts +463 -181
- package/server/definitions.js +90 -103
- package/server/dispatch.d.ts +99 -20
- package/server/dispatch.js +337 -89
- package/server/event-stream.d.ts +63 -0
- package/server/event-stream.js +173 -0
- package/server/generated.d.ts +1 -0
- package/server/generated.js +1 -0
- package/server/index.d.ts +6 -3
- package/server/index.js +2 -1
- package/server/outcomes.d.ts +75 -0
- package/server/outcomes.js +90 -0
- package/server/response-effects.d.ts +105 -0
- package/server/response-effects.js +78 -0
- package/server/upgrade.d.ts +58 -0
- package/server/upgrade.js +117 -0
- package/stream.d.ts +31 -0
- package/stream.js +159 -0
- package/tanstack-query/README.md +110 -22
- package/tanstack-query/generated.js +5 -2
- package/tanstack-query/queries.d.ts +8 -6
package/server/definitions.js
CHANGED
|
@@ -9,8 +9,12 @@ import { createErrors, declaredFailures } from "./errors.js";
|
|
|
9
9
|
* from this entry point and call it without type arguments; a context assigned to a
|
|
10
10
|
* variable and exported later, or `defineContext<MyContext>(...)`, fails generation.
|
|
11
11
|
* The factory runs once per live server request, for page renders
|
|
12
|
-
* and RPC requests alike, and its value is
|
|
13
|
-
*
|
|
12
|
+
* and RPC requests alike, and its value is the `context` middleware and handlers start
|
|
13
|
+
* from. Keep per-request state in the context rather than in module variables.
|
|
14
|
+
*
|
|
15
|
+
* When Angular prerenders a page that calls an RPC, the factory runs at build time with a
|
|
16
|
+
* cookie-less `GET` of the page's URL on a local origin Angular picks, so a prerendered
|
|
17
|
+
* page shows what an anonymous visitor would see.
|
|
14
18
|
*
|
|
15
19
|
* @example
|
|
16
20
|
* ```ts
|
|
@@ -30,100 +34,77 @@ import { createErrors, declaredFailures } from "./errors.js";
|
|
|
30
34
|
export function defineContext(create) {
|
|
31
35
|
return { create };
|
|
32
36
|
}
|
|
33
|
-
function procedure(kind, options) {
|
|
34
|
-
const definitions = (options.errors ?? {});
|
|
35
|
-
const { factories, failures } = createErrors(definitions);
|
|
36
|
-
return {
|
|
37
|
-
kind,
|
|
38
|
-
input: options.input,
|
|
39
|
-
errors: definitions,
|
|
40
|
-
canActivate: options.canActivate ?? [],
|
|
41
|
-
handler: ({ context, input }) => options.handler({ context, input, errors: factories }),
|
|
42
|
-
[declaredFailures]: failures,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
37
|
/**
|
|
46
|
-
*
|
|
38
|
+
* Creates a middleware: code that runs around a procedure call, to refuse it, add typed
|
|
39
|
+
* values to its context, or wrap the rest of it.
|
|
47
40
|
*
|
|
48
41
|
* @remarks
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
42
|
+
* List middleware in `defineRpc`'s `use` to run it for every procedure in the group, or
|
|
43
|
+
* in a procedure's `use` to run it for that procedure. A call runs the group's
|
|
44
|
+
* middleware with no input, then validates the input, then runs the procedure's
|
|
45
|
+
* middleware with the validated input, then the handler. Middleware runs for every
|
|
46
|
+
* call, also during server rendering, where `request` is the page request, and when
|
|
47
|
+
* Angular prerenders a page, where it's a cookie-less `GET` of the page's URL.
|
|
48
|
+
*
|
|
49
|
+
* `run` must call `next()` exactly once and return its result: calling it twice or
|
|
50
|
+
* returning anything else fails the call with `INTERNAL`. `next(added)` merges `added`
|
|
51
|
+
* into the context by spread, so the context must be a plain object. The context is
|
|
52
|
+
* read-only at the top level: change it through `next()`. Every `next(...)` in a
|
|
53
|
+
* middleware adds the same keys, and a value that differs per branch goes in one
|
|
54
|
+
* `next()` call with a union-typed value, such as
|
|
55
|
+
* `next({ user: preview ? null : context.user })`: separate `next({ user: null })` and
|
|
56
|
+
* `next({ user: context.user })` calls don't infer `User | null` when `context.user` is
|
|
57
|
+
* a `User`. `next()` rejects when anything after it throws, so `await next()` in a `try`
|
|
58
|
+
* wraps the rest of the call.
|
|
59
|
+
*
|
|
60
|
+
* Refuse a call with `throw forbidden()`, which fails it with `FORBIDDEN`, or answer it
|
|
61
|
+
* with `throw notFound()` or `throw redirect(to)`. Any other exception fails it with
|
|
62
|
+
* `INTERNAL` and is logged on the server. Declared errors belong to handlers.
|
|
64
63
|
*
|
|
65
64
|
* @example
|
|
66
65
|
* ```ts
|
|
67
|
-
*
|
|
68
|
-
* handler: ({ context }) => context.db.todos.listForUser(requireUser(context).id),
|
|
69
|
-
* }),
|
|
70
|
-
* ```
|
|
66
|
+
* import { forbidden, middleware } from "@loozo/angular-launch/server";
|
|
71
67
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* `errors` declarations and `handler`.
|
|
77
|
-
* @returns The procedure, to use as a value in `defineRpc`.
|
|
78
|
-
* @throws TypeError when a declared error is invalid: its code isn't an identifier, is
|
|
79
|
-
* `__proto__` or a framework code, its status isn't an integer from 400 to 599, its
|
|
80
|
-
* message is empty, or its data isn't a Standard Schema V1.
|
|
81
|
-
*/
|
|
82
|
-
export function query(options) {
|
|
83
|
-
return procedure("query", options);
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Defines a procedure that writes data.
|
|
68
|
+
* export const signedIn = middleware(async ({ context, next }) => {
|
|
69
|
+
* if (context.user === null) {
|
|
70
|
+
* throw forbidden();
|
|
71
|
+
* }
|
|
87
72
|
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* the validated input (`undefined` without an input schema) and the
|
|
91
|
-
* error factories for the declared errors. A call validates the input, runs the
|
|
92
|
-
* group's guards, then the procedure's guards, then the handler. Inputs and results
|
|
93
|
-
* must be strict JSON; a result that isn't fails the call with `INTERNAL`. Throwing an
|
|
94
|
-
* error created by `errors` returns that declared error; any other exception fails the
|
|
95
|
-
* call with `INTERNAL` and is logged on the server.
|
|
96
|
-
*
|
|
97
|
-
* An explicit `input` schema must have a concrete type, without `undefined`, `null`,
|
|
98
|
-
* `any` or `unknown` branches, or generation fails: `Schema<string> | undefined` is
|
|
99
|
-
* rejected. The schema's caller input type may still be a JSON union, as in
|
|
100
|
-
* `Schema<string | number, number>`.
|
|
101
|
-
*
|
|
102
|
-
* Mutations never use the HTTP transfer cache. During server rendering they fail with
|
|
103
|
-
* `FORBIDDEN` before validation, guards or the handler run.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```ts
|
|
107
|
-
* add: mutation({
|
|
108
|
-
* input: titleSchema,
|
|
109
|
-
* handler: ({ input, context }) =>
|
|
110
|
-
* context.db.todos.addForUser(requireUser(context).id, input.title),
|
|
111
|
-
* }),
|
|
73
|
+
* return next({ user: context.user });
|
|
74
|
+
* });
|
|
112
75
|
* ```
|
|
113
76
|
*
|
|
114
|
-
* @typeParam
|
|
115
|
-
* @
|
|
116
|
-
* @typeParam
|
|
117
|
-
* @
|
|
118
|
-
* `
|
|
119
|
-
* @
|
|
120
|
-
* @
|
|
121
|
-
* `__proto__` or a framework code, its status isn't an integer from 400 to 599, its
|
|
122
|
-
* message is empty, or its data isn't a Standard Schema V1.
|
|
77
|
+
* @typeParam Needs - The context it needs, from an annotated argument. Defaults to
|
|
78
|
+
* {@link Context}.
|
|
79
|
+
* @typeParam Added - The values it adds, inferred from what it passes to `next()`.
|
|
80
|
+
* @typeParam Input - The input it needs, from an annotated argument. Defaults to
|
|
81
|
+
* `unknown`.
|
|
82
|
+
* @param run - Runs the middleware for one call.
|
|
83
|
+
* @returns The middleware, to list in a `use` array.
|
|
123
84
|
*/
|
|
124
|
-
export function
|
|
125
|
-
return
|
|
85
|
+
export function middleware(run) {
|
|
86
|
+
return { run };
|
|
87
|
+
}
|
|
88
|
+
function procedure(kind, { input, message, use = [], errors = {}, handler }) {
|
|
89
|
+
const { factories, failures } = createErrors(errors);
|
|
90
|
+
const run = handler;
|
|
91
|
+
return {
|
|
92
|
+
kind,
|
|
93
|
+
input,
|
|
94
|
+
message,
|
|
95
|
+
errors,
|
|
96
|
+
use,
|
|
97
|
+
handler: (args) => run({ ...args, errors: factories }),
|
|
98
|
+
[declaredFailures]: failures,
|
|
99
|
+
};
|
|
126
100
|
}
|
|
101
|
+
// The checked option types above tie each result to its builder's type parameters.
|
|
102
|
+
const builder = {
|
|
103
|
+
query: (options) => procedure("query", options),
|
|
104
|
+
mutation: (options) => procedure("mutation", options),
|
|
105
|
+
stream: (options) => procedure("stream", options),
|
|
106
|
+
channel: (options) => procedure("channel", options),
|
|
107
|
+
};
|
|
127
108
|
/**
|
|
128
109
|
* Defines the RPC group for the route declared in the same directory.
|
|
129
110
|
*
|
|
@@ -131,41 +112,47 @@ export function mutation(options) {
|
|
|
131
112
|
* Default-export the result from a file next to the route's `route.ts`; one group per
|
|
132
113
|
* route. That route must be one that gets generated files: not a redirect, and with a
|
|
133
114
|
* page or descendants with pages that aren't all redirects. Generation reads the call
|
|
134
|
-
* statically without running it: import `defineRpc
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
115
|
+
* statically without running it: import `defineRpc` by name from this entry point and
|
|
116
|
+
* call it directly without type arguments, with a literal identifier other than
|
|
117
|
+
* `__proto__` for the namespace; pass an arrow function whose parameter destructures
|
|
118
|
+
* `query` and/or `mutation` without renaming and whose body is a parenthesized object
|
|
119
|
+
* literal of procedures, each a direct `query(...)` or `mutation(...)` call with inline
|
|
120
|
+
* options; and write the group options inline. Each route's generated `injectRpc()`
|
|
121
|
+
* exposes its own group and its ancestors' groups under their namespaces. Each namespace
|
|
122
|
+
* must be unique across the app: calls go to `/_rpc/<namespace>/<procedure>`.
|
|
123
|
+
*
|
|
124
|
+
* The group's `use` middleware runs for every procedure in the group, before input
|
|
125
|
+
* validation, and the procedures' handlers and middleware see what it adds to the
|
|
126
|
+
* context. It isn't inherited through the route tree.
|
|
141
127
|
*
|
|
142
128
|
* Exposure isn't authorization: anyone who can reach the server can call any
|
|
143
|
-
* procedure, so
|
|
129
|
+
* procedure, so protect every group and procedure that needs it.
|
|
144
130
|
*
|
|
145
131
|
* @example
|
|
146
132
|
* ```ts
|
|
147
|
-
* import { defineRpc
|
|
148
|
-
* import {
|
|
133
|
+
* import { defineRpc } from "@loozo/angular-launch/server";
|
|
134
|
+
* import { signedIn } from "../../auth.server";
|
|
149
135
|
*
|
|
150
136
|
* export default defineRpc(
|
|
151
137
|
* "todos",
|
|
152
|
-
* {
|
|
138
|
+
* ({ query }) => ({
|
|
153
139
|
* list: query({
|
|
154
|
-
* handler: ({ context }) => context.db.todos.listForUser(
|
|
140
|
+
* handler: ({ context }) => context.db.todos.listForUser(context.user.id),
|
|
155
141
|
* }),
|
|
156
|
-
* },
|
|
157
|
-
* {
|
|
142
|
+
* }),
|
|
143
|
+
* { use: [signedIn] },
|
|
158
144
|
* );
|
|
159
145
|
* ```
|
|
160
146
|
*
|
|
161
147
|
* @typeParam Name - The namespace literal.
|
|
148
|
+
* @typeParam Use - The group's middleware, written inline.
|
|
162
149
|
* @typeParam Procedures - The procedures, keyed by name.
|
|
163
150
|
* @param name - The namespace the generated client exposes the group under.
|
|
164
|
-
* @param
|
|
165
|
-
*
|
|
166
|
-
* group.
|
|
151
|
+
* @param build - Returns the procedures, keyed by name, built with the `query` and
|
|
152
|
+
* `mutation` builders it receives.
|
|
153
|
+
* @param options - `use`: middleware that runs for every procedure in the group.
|
|
167
154
|
* @returns The group, to default-export.
|
|
168
155
|
*/
|
|
169
|
-
export function defineRpc(name,
|
|
170
|
-
return { name, procedures,
|
|
156
|
+
export function defineRpc(name, build, { use, } = {}) {
|
|
157
|
+
return { name, procedures: build(builder), use: use ?? [] };
|
|
171
158
|
}
|
package/server/dispatch.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { type FrameworkCode } from "../client.js";
|
|
2
2
|
import { type InputIssue } from "../input-issues.js";
|
|
3
3
|
import type { ContextDefinition, ContextOf, Group, ProcedureDefinition } from "./definitions.js";
|
|
4
|
+
import { type ChannelInbox } from "./channel-messages.js";
|
|
5
|
+
import { type StreamStart } from "./event-stream.js";
|
|
6
|
+
import { type ResponseEffects } from "./response-effects.js";
|
|
4
7
|
/**
|
|
5
8
|
* The application's RPC groups, keyed by namespace.
|
|
6
9
|
*
|
|
@@ -79,11 +82,20 @@ export declare function isRequestScope(value: unknown): value is RequestScope;
|
|
|
79
82
|
*/
|
|
80
83
|
export declare function createRequestScope<Definition extends ContextDefinition<unknown>>(application: ApplicationBinding<Definition>, request: Request): Promise<RequestScope<ContextOf<Definition>>>;
|
|
81
84
|
export declare function assertBoundScope(scope: unknown, application: ApplicationBinding, request: Request): asserts scope is RequestScope;
|
|
82
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* The HTTP status and JSON body {@link dispatch} produces for one call. Internal.
|
|
87
|
+
*
|
|
88
|
+
* @remarks
|
|
89
|
+
* Only status 200 is a result; every other status carries a frame dispatch built itself,
|
|
90
|
+
* so a result shaped like a frame stays data.
|
|
91
|
+
*/
|
|
83
92
|
export interface Reply {
|
|
84
|
-
/**
|
|
93
|
+
/** 200 for a result, 303 for a redirect, otherwise the error's or not-found's status. */
|
|
85
94
|
readonly status: number;
|
|
86
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* The result, an `{ error: { code, message, data } }` frame, `{ redirect: { to } }` or
|
|
97
|
+
* `{ notFound: null }`.
|
|
98
|
+
*/
|
|
87
99
|
readonly body: unknown;
|
|
88
100
|
}
|
|
89
101
|
export declare function failure(code: Exclude<FrameworkCode, "INVALID_INPUT">): Reply;
|
|
@@ -100,14 +112,31 @@ export declare function isJsonMediaType(contentType: string | null): boolean;
|
|
|
100
112
|
*
|
|
101
113
|
* In order: the scope must belong to `application` and `request` (`INTERNAL`); the
|
|
102
114
|
* path must be `/_rpc/<namespace>/<procedure>` for a registered procedure
|
|
103
|
-
* (`RPC_NOT_FOUND`); a mutation in `"rendering"` mode is refused
|
|
115
|
+
* (`RPC_NOT_FOUND`); a mutation, stream or channel in `"rendering"` mode is refused
|
|
116
|
+
* (`FORBIDDEN`); a channel in `"http"` mode, or any other kind in `"socket"` mode, is
|
|
117
|
+
* `RPC_NOT_FOUND`; the
|
|
104
118
|
* envelope must be strict JSON (`INTERNAL` otherwise, such as for `{ input: undefined }`,
|
|
105
|
-
* a cycle or an accessor)
|
|
106
|
-
*
|
|
107
|
-
* or `null` without one (`INVALID_INPUT`
|
|
108
|
-
*
|
|
109
|
-
* handler's strict JSON result is returned with status 200, and its
|
|
110
|
-
* with their status.
|
|
119
|
+
* a cycle or an accessor), and exactly `{ input }` (`INVALID_INPUT` with no issues);
|
|
120
|
+
* the group's middleware runs with no input; the input must pass the schema
|
|
121
|
+
* (`INVALID_INPUT` with the schema's issues), or be `null` without one (`INVALID_INPUT`
|
|
122
|
+
* with no issues); the procedure's middleware runs with the validated input; then the
|
|
123
|
+
* handler. The handler's strict JSON result is returned with status 200, and its
|
|
124
|
+
* declared errors with their status. `forbidden()` thrown from middleware or the
|
|
125
|
+
* handler gives `FORBIDDEN`, `notFound()` a 404 with `{ notFound: null }` and
|
|
126
|
+
* `redirect(to)` a 303 with `{ redirect: { to } }`; a middleware that calls `next` twice
|
|
127
|
+
* or doesn't return its result gives `INTERNAL`.
|
|
128
|
+
*
|
|
129
|
+
* A stream's handler returns an iterable instead of a result. Its iterator goes to
|
|
130
|
+
* `start`, and the call goes on until the stream ends: the reply is then a 200 with
|
|
131
|
+
* `null` body, or the failure the stream ended with. Without `start`, a stream fails
|
|
132
|
+
* with `INTERNAL`.
|
|
133
|
+
*
|
|
134
|
+
* A channel runs like a stream, and its handler also receives `messages`, read from
|
|
135
|
+
* `inbox` and validated with its message schema. A message that fails validation, or a
|
|
136
|
+
* protocol violation the inbox reports, ends the call with `INVALID_INPUT`.
|
|
137
|
+
*
|
|
138
|
+
* Middleware and handlers set cookies and headers through `effects.control`; the caller
|
|
139
|
+
* commits and applies them.
|
|
111
140
|
*
|
|
112
141
|
* @param registry - The application's RPC groups.
|
|
113
142
|
* @param path - The request path.
|
|
@@ -115,29 +144,79 @@ export declare function isJsonMediaType(contentType: string | null): boolean;
|
|
|
115
144
|
* @param scope - The request scope whose context the call receives.
|
|
116
145
|
* @param application - The application binding the scope must belong to.
|
|
117
146
|
* @param request - The request the scope must be bound to.
|
|
118
|
-
* @param mode - `"http"` for browser requests (the default)
|
|
119
|
-
* made during server rendering.
|
|
147
|
+
* @param mode - `"http"` for browser requests (the default), `"socket"` for websocket
|
|
148
|
+
* upgrades, or `"rendering"` for calls made during server rendering.
|
|
149
|
+
* @param signal - The call's abort signal. Defaults to `request`'s signal.
|
|
150
|
+
* @param effects - Collects the call's cookies and headers. Defaults to fresh effects
|
|
151
|
+
* nothing reads.
|
|
152
|
+
* @param start - Receives a stream's or channel's iterator once its handler returned it.
|
|
153
|
+
* @param inbox - A channel's incoming messages.
|
|
120
154
|
* @returns The reply to send.
|
|
121
155
|
*/
|
|
122
|
-
export declare function dispatch(registry: Registry, path: string, envelope: unknown, scope: RequestScope, application: ApplicationBinding, request: Request, mode?: "http" | "rendering"): Promise<Reply>;
|
|
156
|
+
export declare function dispatch(registry: Registry, path: string, envelope: unknown, scope: RequestScope, application: ApplicationBinding, request: Request, mode?: "http" | "socket" | "rendering", signal?: AbortSignal, effects?: ResponseEffects, start?: (stream: StreamStart) => void, inbox?: ChannelInbox): Promise<Reply>;
|
|
157
|
+
/**
|
|
158
|
+
* Options for the generated `handleRequest` and `handleUpgrade` in `server.generated.ts`.
|
|
159
|
+
*/
|
|
160
|
+
export interface HandleOptions {
|
|
161
|
+
/**
|
|
162
|
+
* Hostnames RPCs are answered for, matched like Angular's `allowedHosts`: exactly,
|
|
163
|
+
* `*.example.com` for subdomains only, or `*` for any host. An empty list refuses every
|
|
164
|
+
* RPC. Pass the same array to `AngularNodeAppEngine`, `handleRequest` and `handleUpgrade`.
|
|
165
|
+
*/
|
|
166
|
+
readonly allowedHosts: readonly string[];
|
|
167
|
+
/**
|
|
168
|
+
* Further origins allowed to call, as exact serialized origins such as
|
|
169
|
+
* `https://app.example.com`: a public origin behind a proxy that rewrites `Host`, or a
|
|
170
|
+
* sibling site. Defaults to none.
|
|
171
|
+
*/
|
|
172
|
+
readonly origins?: readonly string[];
|
|
173
|
+
}
|
|
174
|
+
/** The plain-text 400 for an RPC to a host that isn't allowed, logged. */
|
|
175
|
+
export declare function hostRefusal(hostname: string): Response;
|
|
176
|
+
export declare function hostAllowed(hostname: string, allowedHosts: readonly string[]): boolean;
|
|
177
|
+
/** Why `request` can't be proven same-origin, or `undefined` when it can. */
|
|
178
|
+
export declare function crossSiteReason(request: Request, origins: readonly string[]): string | undefined;
|
|
123
179
|
/**
|
|
124
180
|
* Answers an RPC HTTP request, or returns `null` for any other request.
|
|
125
181
|
*
|
|
126
182
|
* @remarks
|
|
127
183
|
* Internal: exported only from `@loozo/angular-launch/_server` for the generated
|
|
128
184
|
* `handleRequest` in `server.generated.ts`; call that instead.
|
|
129
|
-
* Any path starting with `/_rpc/` is treated as an RPC request.
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
185
|
+
* Any path starting with `/_rpc/` is treated as an RPC request. Its URL's hostname must
|
|
186
|
+
* be in `allowedHosts`, otherwise the reply is a plain-text 400 `Host not allowed`. It
|
|
187
|
+
* must then be a `POST` with an `application/json` content type and no query string,
|
|
188
|
+
* otherwise the reply is `INVALID_INPUT` with no issues. It must then come from the
|
|
189
|
+
* app's own origin, otherwise the reply is `FORBIDDEN`: an `Origin` listed in `origins`
|
|
190
|
+
* passes; otherwise `Sec-Fetch-Site`, when sent, must be `same-origin`; otherwise
|
|
191
|
+
* `Origin`, when sent, must equal the request URL's origin; a request with neither
|
|
192
|
+
* header is refused. No context is created for any of these refusals. A fresh context
|
|
193
|
+
* is then created before the body is parsed; if that fails the reply is `INTERNAL`, even
|
|
194
|
+
* when the body isn't JSON. Otherwise a body that isn't JSON gives `INVALID_INPUT` with
|
|
195
|
+
* no issues.
|
|
196
|
+
* JSON responses carry `Cache-Control: private, no-store`, `Vary: Cookie` and
|
|
197
|
+
* `X-Content-Type-Options: nosniff`.
|
|
198
|
+
*
|
|
199
|
+
* Once the call reached `dispatch`, its response carries the headers and cookies the
|
|
200
|
+
* call set, whatever its outcome. Launch's own headers replace the call's headers of the
|
|
201
|
+
* same name, and each cookie adds a `Set-Cookie` header.
|
|
202
|
+
*
|
|
203
|
+
* A stream answers like any call until its handler returns its iterable. From then on the
|
|
204
|
+
* response is a 200 `text/event-stream` (see `eventStream`) with
|
|
205
|
+
* `Cache-Control: private, no-store, no-transform`, `Vary: Cookie`,
|
|
206
|
+
* `X-Content-Type-Options: nosniff` and `X-Accel-Buffering: no`, and the call's signal
|
|
207
|
+
* also aborts when the host cancels the body. The response's cookies and headers are the
|
|
208
|
+
* ones set when the handler returned its iterable: setting a cookie later throws, and
|
|
209
|
+
* later header changes are ignored.
|
|
135
210
|
*
|
|
136
211
|
* @typeParam Definition - The application's context definition.
|
|
137
212
|
* @param registry - The application's RPC groups.
|
|
138
213
|
* @param application - The application binding.
|
|
139
214
|
* @param request - The incoming server request.
|
|
215
|
+
* @param options - The allowed hosts and further allowed origins.
|
|
140
216
|
* @returns The RPC response, or `null` when the path doesn't start with `/_rpc/`.
|
|
141
217
|
*/
|
|
142
|
-
export declare function handleRpc<Definition extends ContextDefinition<unknown>>(registry: Registry, application: ApplicationBinding<Definition>, request: Request): Promise<Response | null>;
|
|
218
|
+
export declare function handleRpc<Definition extends ContextDefinition<unknown>>(registry: Registry, application: ApplicationBinding<Definition>, request: Request, { allowedHosts, origins }: HandleOptions): Promise<Response | null>;
|
|
219
|
+
/** The call's headers, then Launch's own over them, then the call's cookies. */
|
|
220
|
+
export declare function responseHeaders(own: Readonly<Record<string, string>>, effects: ResponseEffects | undefined): Headers;
|
|
221
|
+
export declare function response(reply: Reply, effects?: ResponseEffects): Response;
|
|
143
222
|
export {};
|