@loozo/angular-launch 0.0.5 → 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 +1068 -187
- 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 +55 -0
- package/channel.js +232 -0
- package/client.d.ts +63 -33
- package/connect.d.ts +201 -0
- package/connect.js +148 -0
- package/generation/index.d.ts +2 -123
- package/generation/index.js +3 -669
- 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 +8 -1
- package/index.js +3 -0
- package/json.d.ts +9 -0
- package/json.js +24 -0
- package/launch-queries.d.ts +16 -0
- package/launch-queries.js +3 -0
- package/navigation.d.ts +36 -0
- package/navigation.js +47 -0
- package/node/index.d.ts +60 -0
- package/node/index.js +178 -0
- package/node/upgrade-listener-slot.d.ts +1 -0
- package/node/upgrade-listener-slot.js +4 -0
- package/outcomes.d.ts +71 -0
- package/outcomes.js +146 -0
- package/package.json +21 -11
- package/redirect-translation.d.ts +53 -0
- package/redirect-translation.js +141 -0
- package/{generated.d.ts → remote-client.d.ts} +1 -1
- package/remote-client.js +179 -0
- 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 +62 -31
- package/server/backend.js +149 -55
- 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 +270 -294
- package/server/definitions.js +35 -115
- package/server/dispatch.d.ts +121 -66
- package/server/dispatch.js +267 -87
- package/server/event-stream.d.ts +63 -0
- package/server/event-stream.js +173 -0
- package/server/generated.d.ts +3 -1
- package/server/generated.js +3 -1
- package/server/index.d.ts +6 -3
- package/server/index.js +3 -2
- package/server/outcomes.d.ts +64 -2
- package/server/outcomes.js +85 -4
- package/server/read-cookies.d.ts +19 -0
- package/server/read-cookies.js +39 -0
- package/server/response-effects.d.ts +105 -0
- package/server/response-effects.js +78 -0
- package/server/stages.d.ts +62 -0
- package/server/stages.js +51 -0
- package/server/upgrade.d.ts +57 -0
- package/server/upgrade.js +116 -0
- package/source-text.d.ts +1 -0
- package/source-text.js +56 -0
- package/stream.d.ts +34 -0
- package/stream.js +161 -0
- package/tanstack-query/README.md +113 -21
- package/tanstack-query/create-queries.d.ts +2 -0
- package/tanstack-query/{generated.js → create-queries.js} +13 -11
- 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 +50 -33
- 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/generated.js +0 -90
- package/generation/context-file.d.ts +0 -2
- package/generation/context-file.js +0 -3
- package/tanstack-query/generated.d.ts +0 -5
package/server/definitions.js
CHANGED
|
@@ -1,91 +1,60 @@
|
|
|
1
1
|
import { createErrors, declaredFailures } from "./errors.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Creates a middleware: code that runs around a procedure call, to refuse it or wrap the
|
|
4
|
+
* rest of it.
|
|
4
5
|
*
|
|
5
6
|
* @remarks
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* import { defineContext } from "@loozo/angular-launch/server";
|
|
18
|
-
* import { sessions } from "./sessions.server";
|
|
19
|
-
*
|
|
20
|
-
* export default defineContext(async (request: Request) => ({
|
|
21
|
-
* user: await sessions.authenticate(request),
|
|
22
|
-
* }));
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @typeParam Value - The factory's return type, possibly a promise.
|
|
26
|
-
* @param create - Creates the context for one request. A rejection or throw fails that
|
|
27
|
-
* request: an RPC request returns `INTERNAL`.
|
|
28
|
-
* @returns The context definition to default-export.
|
|
29
|
-
*/
|
|
30
|
-
export function defineContext(create) {
|
|
31
|
-
return { create };
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Creates a middleware: code that runs around a procedure call, to refuse it, add typed
|
|
35
|
-
* values to its context, or wrap the rest of it.
|
|
36
|
-
*
|
|
37
|
-
* @remarks
|
|
38
|
-
* List middleware in `defineRpc`'s `use` to run it for every procedure in the group, or
|
|
39
|
-
* in a procedure's `use` to run it for that procedure. A call runs the group's
|
|
40
|
-
* middleware with no input, then validates the input, then runs the procedure's
|
|
41
|
-
* middleware with the validated input, then the handler. Middleware runs for every
|
|
42
|
-
* call, also during server rendering, where `request` is the page request.
|
|
7
|
+
* List middleware in a route's `.use()` to run it for that route's own procedures, or in
|
|
8
|
+
* a procedure's `use` to run it for that procedure. Those lists also take a bare `run`
|
|
9
|
+
* function, typed with the route's context and, on a procedure, the validated input, so use
|
|
10
|
+
* `middleware` for middleware you share. A call runs the owning route's stages,
|
|
11
|
+
* then its `.use()` middleware with no input, then validates the input, then runs the
|
|
12
|
+
* procedure's middleware with the validated input, then the handler. Middleware runs for
|
|
13
|
+
* every call, also during server rendering, where `request` is the page request, and when
|
|
14
|
+
* Angular prerenders a page, where it's a cookie-less `GET` of the page's URL. A route's
|
|
15
|
+
* `.use()` isn't inherited: a child route's procedures don't run it.
|
|
43
16
|
*
|
|
44
17
|
* `run` must call `next()` exactly once and return its result: calling it twice or
|
|
45
|
-
* returning anything else fails the call with `INTERNAL`. `next(
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* middleware adds the same keys, and a value that differs per branch goes in one
|
|
49
|
-
* `next()` call with a union-typed value, such as
|
|
50
|
-
* `next({ user: preview ? null : context.user })`: separate `next({ user: null })` and
|
|
51
|
-
* `next({ user: context.user })` calls don't infer `User | null` when `context.user` is
|
|
52
|
-
* a `User`. `next()` rejects when anything after it throws, so `await next()` in a `try`
|
|
53
|
-
* wraps the rest of the call.
|
|
18
|
+
* returning anything else fails the call with `INTERNAL`. `next()` rejects when anything
|
|
19
|
+
* after it throws, so `await next()` in a `try` wraps the rest of the call. Middleware
|
|
20
|
+
* can't add to the context: derive values in a `.context()` stage instead.
|
|
54
21
|
*
|
|
55
|
-
* Refuse a call with `throw forbidden()`, which fails it with `FORBIDDEN
|
|
56
|
-
*
|
|
57
|
-
* to handlers.
|
|
22
|
+
* Refuse a call with `throw forbidden()`, which fails it with `FORBIDDEN`, or answer it
|
|
23
|
+
* with `throw notFound()` or `throw redirect(destination, values)`. Any other exception fails it with
|
|
24
|
+
* `INTERNAL` and is logged on the server. Declared errors belong to handlers.
|
|
58
25
|
*
|
|
59
26
|
* @example
|
|
60
27
|
* ```ts
|
|
61
|
-
* import {
|
|
28
|
+
* import { middleware } from "@loozo/angular-launch/server";
|
|
62
29
|
*
|
|
63
|
-
* export const
|
|
64
|
-
*
|
|
65
|
-
* throw forbidden();
|
|
66
|
-
* }
|
|
30
|
+
* export const timing = middleware(async ({ next, response }) => {
|
|
31
|
+
* const start = performance.now();
|
|
67
32
|
*
|
|
68
|
-
*
|
|
33
|
+
* try {
|
|
34
|
+
* return await next();
|
|
35
|
+
* } finally {
|
|
36
|
+
* response.headers.append("server-timing", `rpc;dur=${performance.now() - start}`);
|
|
37
|
+
* }
|
|
69
38
|
* });
|
|
70
39
|
* ```
|
|
71
40
|
*
|
|
72
|
-
* @typeParam Needs - The context it needs
|
|
73
|
-
* {
|
|
74
|
-
*
|
|
75
|
-
* @typeParam Input - The input it needs
|
|
76
|
-
* `unknown`.
|
|
41
|
+
* @typeParam Needs - The context it needs. Defaults to `{}`, also when the call is written
|
|
42
|
+
* inline in `.use()`: pass the keys it reads, as in `middleware<{ user: User }>(…)`, or
|
|
43
|
+
* write a bare function there instead.
|
|
44
|
+
* @typeParam Input - The input it needs. Defaults to `unknown`.
|
|
77
45
|
* @param run - Runs the middleware for one call.
|
|
78
|
-
* @returns The middleware, to list in a `use
|
|
46
|
+
* @returns The middleware, to list in `.use()` or a procedure's `use`.
|
|
79
47
|
*/
|
|
80
48
|
export function middleware(run) {
|
|
81
49
|
return { run };
|
|
82
50
|
}
|
|
83
|
-
function procedure(kind, { input, use = [], errors = {}, handler }) {
|
|
51
|
+
function procedure(kind, { input, message, use = [], errors = {}, handler }) {
|
|
84
52
|
const { factories, failures } = createErrors(errors);
|
|
85
53
|
const run = handler;
|
|
86
54
|
return {
|
|
87
55
|
kind,
|
|
88
56
|
input,
|
|
57
|
+
message,
|
|
89
58
|
errors,
|
|
90
59
|
use,
|
|
91
60
|
handler: (args) => run({ ...args, errors: factories }),
|
|
@@ -93,58 +62,9 @@ function procedure(kind, { input, use = [], errors = {}, handler }) {
|
|
|
93
62
|
};
|
|
94
63
|
}
|
|
95
64
|
// The checked option types above tie each result to its builder's type parameters.
|
|
96
|
-
const builder = {
|
|
65
|
+
export const builder = {
|
|
97
66
|
query: (options) => procedure("query", options),
|
|
98
67
|
mutation: (options) => procedure("mutation", options),
|
|
68
|
+
stream: (options) => procedure("stream", options),
|
|
69
|
+
channel: (options) => procedure("channel", options),
|
|
99
70
|
};
|
|
100
|
-
/**
|
|
101
|
-
* Defines the RPC group for the route declared in the same directory.
|
|
102
|
-
*
|
|
103
|
-
* @remarks
|
|
104
|
-
* Default-export the result from a file next to the route's `route.ts`; one group per
|
|
105
|
-
* route. That route must be one that gets generated files: not a redirect, and with a
|
|
106
|
-
* page or descendants with pages that aren't all redirects. Generation reads the call
|
|
107
|
-
* statically without running it: import `defineRpc` by name from this entry point and
|
|
108
|
-
* call it directly without type arguments, with a literal identifier other than
|
|
109
|
-
* `__proto__` for the namespace; pass an arrow function whose parameter destructures
|
|
110
|
-
* `query` and/or `mutation` without renaming and whose body is a parenthesized object
|
|
111
|
-
* literal of procedures, each a direct `query(...)` or `mutation(...)` call with inline
|
|
112
|
-
* options; and write the group options inline. Each route's generated `injectRpc()`
|
|
113
|
-
* exposes its own group and its ancestors' groups under their namespaces. Each namespace
|
|
114
|
-
* must be unique across the app: calls go to `/_rpc/<namespace>/<procedure>`.
|
|
115
|
-
*
|
|
116
|
-
* The group's `use` middleware runs for every procedure in the group, before input
|
|
117
|
-
* validation, and the procedures' handlers and middleware see what it adds to the
|
|
118
|
-
* context. It isn't inherited through the route tree.
|
|
119
|
-
*
|
|
120
|
-
* Exposure isn't authorization: anyone who can reach the server can call any
|
|
121
|
-
* procedure, so protect every group and procedure that needs it.
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```ts
|
|
125
|
-
* import { defineRpc } from "@loozo/angular-launch/server";
|
|
126
|
-
* import { signedIn } from "../../auth.server";
|
|
127
|
-
*
|
|
128
|
-
* export default defineRpc(
|
|
129
|
-
* "todos",
|
|
130
|
-
* ({ query }) => ({
|
|
131
|
-
* list: query({
|
|
132
|
-
* handler: ({ context }) => context.db.todos.listForUser(context.user.id),
|
|
133
|
-
* }),
|
|
134
|
-
* }),
|
|
135
|
-
* { use: [signedIn] },
|
|
136
|
-
* );
|
|
137
|
-
* ```
|
|
138
|
-
*
|
|
139
|
-
* @typeParam Name - The namespace literal.
|
|
140
|
-
* @typeParam Use - The group's middleware, written inline.
|
|
141
|
-
* @typeParam Procedures - The procedures, keyed by name.
|
|
142
|
-
* @param name - The namespace the generated client exposes the group under.
|
|
143
|
-
* @param build - Returns the procedures, keyed by name, built with the `query` and
|
|
144
|
-
* `mutation` builders it receives.
|
|
145
|
-
* @param options - `use`: middleware that runs for every procedure in the group.
|
|
146
|
-
* @returns The group, to default-export.
|
|
147
|
-
*/
|
|
148
|
-
export function defineRpc(name, build, { use, } = {}) {
|
|
149
|
-
return { name, procedures: build(builder), use: use ?? [] };
|
|
150
|
-
}
|
package/server/dispatch.d.ts
CHANGED
|
@@ -1,53 +1,51 @@
|
|
|
1
1
|
import { type FrameworkCode } from "../client.js";
|
|
2
2
|
import { type InputIssue } from "../input-issues.js";
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @remarks
|
|
8
|
-
* Internal; not exported from a public entry point. Generation writes it as
|
|
9
|
-
* `registry` in `server.generated.ts`.
|
|
10
|
-
*/
|
|
11
|
-
export type Registry = Readonly<Record<string, Group<string, Record<string, ProcedureDefinition>>>>;
|
|
3
|
+
import type { ApplicationBinding } from "./application.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";
|
|
12
7
|
declare const scopeMarker: unique symbol;
|
|
13
8
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @remarks
|
|
17
|
-
* Internal; not exported from a public entry point. Generation writes it as
|
|
18
|
-
* `appBinding` in `server.generated.ts`.
|
|
19
|
-
*
|
|
20
|
-
* @typeParam Definition - The application's context definition.
|
|
21
|
-
*/
|
|
22
|
-
export interface ApplicationBinding<Definition extends ContextDefinition<unknown> = ContextDefinition<unknown>> {
|
|
23
|
-
/** The generated application identity. */
|
|
24
|
-
readonly id: string;
|
|
25
|
-
/** The application's context definition. */
|
|
26
|
-
readonly context: Definition;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* A live server request bound to the context created for it.
|
|
9
|
+
* A live server request bound to its application, holding the request's lazy context.
|
|
30
10
|
*
|
|
31
11
|
* @remarks
|
|
32
|
-
* Created by the generated `
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* @typeParam Value - The context value.
|
|
12
|
+
* Created by the generated `createRequestScope` in `server.generated.ts`, which runs no
|
|
13
|
+
* stage: each route's `.context()` stage runs at most once per scope, the first time an
|
|
14
|
+
* RPC call needs it. Render a page by passing `scope.request` together with `scope` as
|
|
15
|
+
* Angular's request context; RPC calls during that render fail with `INTERNAL` if the two
|
|
16
|
+
* don't belong together.
|
|
38
17
|
*/
|
|
39
|
-
export interface RequestScope
|
|
18
|
+
export interface RequestScope {
|
|
40
19
|
/** The identity of the application the scope was created for. */
|
|
41
20
|
readonly appId: string;
|
|
42
21
|
/** A random identifier tying `request` to this scope. */
|
|
43
22
|
readonly binding: string;
|
|
44
23
|
/** A clone of the original request that carries the scope binding. */
|
|
45
24
|
readonly request: Request;
|
|
46
|
-
/**
|
|
47
|
-
readonly
|
|
48
|
-
/** Internal brand of genuine request scopes; not public API. */
|
|
49
|
-
readonly [scopeMarker]: true;
|
|
25
|
+
/** Internal: holds the scope's stage runner and rendering calls; not public API. */
|
|
26
|
+
readonly [scopeMarker]: object;
|
|
50
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Runs one call made while rendering a page in the copy of the package that created
|
|
30
|
+
* `scope`, with that copy's application binding.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* Internal; not exported from a public entry point. Angular bundles the server entry, which
|
|
34
|
+
* creates the scope and answers RPCs, apart from the server application, which renders, so
|
|
35
|
+
* modules both import exist once in each bundle. Running the call in the creating copy
|
|
36
|
+
* runs the stages, middleware and handlers of the bundle that answers RPCs, so a page and
|
|
37
|
+
* an RPC of one server share their module state. The call otherwise runs as
|
|
38
|
+
* {@link runProcedure} in `"rendering"` mode, which checks the scope again.
|
|
39
|
+
*
|
|
40
|
+
* @param scope - A scope that passed {@link assertBoundScope} for the rendering application.
|
|
41
|
+
* @param path - The request path.
|
|
42
|
+
* @param envelope - The parsed request body.
|
|
43
|
+
* @param request - The page request the scope is bound to.
|
|
44
|
+
* @param signal - The call's abort signal.
|
|
45
|
+
* @param effects - Collects the call's cookies and headers.
|
|
46
|
+
* @returns The reply to deliver.
|
|
47
|
+
*/
|
|
48
|
+
export declare function runRenderingCall(scope: RequestScope, path: string, envelope: unknown, request: Request, signal: AbortSignal, effects: ResponseEffects): Promise<Reply>;
|
|
51
49
|
/**
|
|
52
50
|
* Checks whether a value has the shape of a {@link RequestScope}.
|
|
53
51
|
*
|
|
@@ -60,31 +58,49 @@ export interface RequestScope<Value = unknown> {
|
|
|
60
58
|
*/
|
|
61
59
|
export declare function isRequestScope(value: unknown): value is RequestScope;
|
|
62
60
|
/**
|
|
63
|
-
*
|
|
64
|
-
* {@link RequestScope}.
|
|
61
|
+
* Binds a live server request into a {@link RequestScope}, running nothing.
|
|
65
62
|
*
|
|
66
63
|
* @remarks
|
|
67
64
|
* Internal: exported only from `@loozo/angular-launch/_server` for the generated
|
|
68
|
-
* `
|
|
65
|
+
* `createRequestScope` in `server.generated.ts`; call that instead.
|
|
69
66
|
* The scope's `request` is a clone of `request` with an added
|
|
70
|
-
* `x-loozo-launch-scope` header, so the original stays usable.
|
|
71
|
-
*
|
|
67
|
+
* `x-loozo-launch-scope` header, so the original stays usable. Stages receive that clone
|
|
68
|
+
* and its signal. Calls made while rendering with the scope run with `application`, in
|
|
69
|
+
* this copy of the package: see {@link runRenderingCall}.
|
|
72
70
|
*
|
|
73
|
-
* @typeParam Definition - The application's context definition.
|
|
74
71
|
* @param application - The application binding.
|
|
75
72
|
* @param request - The live server request.
|
|
76
73
|
* @returns The request scope.
|
|
77
|
-
* @throws
|
|
78
|
-
* `TypeError` when `request`'s body was already read and can't be cloned.
|
|
74
|
+
* @throws TypeError when `request`'s body was already read and can't be cloned.
|
|
79
75
|
*/
|
|
80
|
-
export declare function createRequestScope
|
|
76
|
+
export declare function createRequestScope(application: ApplicationBinding, request: Request): RequestScope;
|
|
81
77
|
export declare function assertBoundScope(scope: unknown, application: ApplicationBinding, request: Request): asserts scope is RequestScope;
|
|
82
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* The HTTP status and JSON body {@link runProcedure} produces for one call. Internal.
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* Only status 200 is a result; every other status carries a frame `runProcedure` built itself,
|
|
83
|
+
* so a result shaped like a frame stays data.
|
|
84
|
+
*/
|
|
83
85
|
export interface Reply {
|
|
84
|
-
/**
|
|
86
|
+
/** 200 for a result, 303 for a redirect, otherwise the error's or not-found's status. */
|
|
85
87
|
readonly status: number;
|
|
86
|
-
/**
|
|
88
|
+
/**
|
|
89
|
+
* The result, an `{ error: { code, message, data } }` frame, `{ redirect: { to } }` (with
|
|
90
|
+
* `words` too for a translated application's destination with static segments) or
|
|
91
|
+
* `{ notFound: null }`.
|
|
92
|
+
*/
|
|
87
93
|
readonly body: unknown;
|
|
94
|
+
/**
|
|
95
|
+
* The stage that failed the call with an unexpected error, for diagnostics such as a
|
|
96
|
+
* prerender fault. Never sent: only `body` reaches the wire.
|
|
97
|
+
*/
|
|
98
|
+
readonly failedStage?: {
|
|
99
|
+
/** The failing stage's route file. */
|
|
100
|
+
readonly route: string;
|
|
101
|
+
/** What the stage threw. */
|
|
102
|
+
readonly error: unknown;
|
|
103
|
+
};
|
|
88
104
|
}
|
|
89
105
|
export declare function failure(code: Exclude<FrameworkCode, "INVALID_INPUT">): Reply;
|
|
90
106
|
export declare function invalidInput(issues?: readonly InputIssue[]): Reply;
|
|
@@ -100,37 +116,59 @@ export declare function isJsonMediaType(contentType: string | null): boolean;
|
|
|
100
116
|
*
|
|
101
117
|
* In order: the scope must belong to `application` and `request` (`INTERNAL`); the
|
|
102
118
|
* path must be `/_rpc/<namespace>/<procedure>` for a registered procedure
|
|
103
|
-
* (`RPC_NOT_FOUND`); a mutation in `"rendering"` mode is refused
|
|
119
|
+
* (`RPC_NOT_FOUND`); a mutation, stream or channel in `"rendering"` mode is refused
|
|
120
|
+
* (`FORBIDDEN`); a channel in `"http"` mode, or any other kind in `"socket"` mode, is
|
|
121
|
+
* `RPC_NOT_FOUND`; the
|
|
104
122
|
* envelope must be strict JSON (`INTERNAL` otherwise, such as for `{ input: undefined }`,
|
|
105
123
|
* a cycle or an accessor), and exactly `{ input }` (`INVALID_INPUT` with no issues);
|
|
106
|
-
* the
|
|
124
|
+
* the owning route's stages run, root first, each at most once per scope (a stage that
|
|
125
|
+
* throws `forbidden()`, `notFound()` or `redirect()` answers with that outcome, and any
|
|
126
|
+
* other failure gives `INTERNAL` with the reply's `failedStage` naming it); the owning
|
|
127
|
+
* route's `.use()` middleware runs with no input; the input must pass the schema
|
|
107
128
|
* (`INVALID_INPUT` with the schema's issues), or be `null` without one (`INVALID_INPUT`
|
|
108
129
|
* with no issues); the procedure's middleware runs with the validated input; then the
|
|
109
130
|
* handler. The handler's strict JSON result is returned with status 200, and its
|
|
110
131
|
* declared errors with their status. `forbidden()` thrown from middleware or the
|
|
111
|
-
* handler gives `FORBIDDEN
|
|
112
|
-
*
|
|
132
|
+
* handler gives `FORBIDDEN`, `notFound()` a 404 with `{ notFound: null }` and
|
|
133
|
+
* `redirect()` a 303 with `{ redirect: { to } }` (plus `words` in a translated application);
|
|
134
|
+
* a middleware that calls `next` twice
|
|
135
|
+
* or doesn't return its result gives `INTERNAL`.
|
|
136
|
+
*
|
|
137
|
+
* A stream's handler returns an iterable instead of a result. Its iterator goes to
|
|
138
|
+
* `start`, and the call goes on until the stream ends: the reply is then a 200 with
|
|
139
|
+
* `null` body, or the failure the stream ended with. Without `start`, a stream fails
|
|
140
|
+
* with `INTERNAL`.
|
|
141
|
+
*
|
|
142
|
+
* A channel runs like a stream, and its handler also receives `messages`, read from
|
|
143
|
+
* `inbox` and validated with its message schema. A message that fails validation, or a
|
|
144
|
+
* protocol violation the inbox reports, ends the call with `INVALID_INPUT`.
|
|
145
|
+
*
|
|
146
|
+
* Stages, middleware and handlers set cookies and headers through `effects.control`; the
|
|
147
|
+
* caller commits and applies them. A stage writes to the call that first ran it.
|
|
113
148
|
*
|
|
114
|
-
* @param registry - The application's RPC groups.
|
|
115
149
|
* @param path - The request path.
|
|
116
150
|
* @param envelope - The parsed request body.
|
|
117
|
-
* @param scope - The request scope whose
|
|
151
|
+
* @param scope - The request scope whose stages the call runs.
|
|
118
152
|
* @param application - The application binding the scope must belong to.
|
|
119
153
|
* @param request - The request the scope must be bound to.
|
|
120
|
-
* @param mode - `"http"` for browser requests (the default)
|
|
121
|
-
* made during server rendering.
|
|
154
|
+
* @param mode - `"http"` for browser requests (the default), `"socket"` for websocket
|
|
155
|
+
* upgrades, or `"rendering"` for calls made during server rendering.
|
|
122
156
|
* @param signal - The call's abort signal. Defaults to `request`'s signal.
|
|
157
|
+
* @param effects - Collects the call's cookies and headers. Defaults to fresh effects
|
|
158
|
+
* nothing reads.
|
|
159
|
+
* @param start - Receives a stream's or channel's iterator once its handler returned it.
|
|
160
|
+
* @param inbox - A channel's incoming messages.
|
|
123
161
|
* @returns The reply to send.
|
|
124
162
|
*/
|
|
125
|
-
export declare function
|
|
163
|
+
export declare function runProcedure(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>;
|
|
126
164
|
/**
|
|
127
|
-
* Options for the generated `handleRequest` in `server.generated.ts`.
|
|
165
|
+
* Options for the generated `handleRequest` and `handleUpgrade` in `server.generated.ts`.
|
|
128
166
|
*/
|
|
129
167
|
export interface HandleOptions {
|
|
130
168
|
/**
|
|
131
169
|
* Hostnames RPCs are answered for, matched like Angular's `allowedHosts`: exactly,
|
|
132
170
|
* `*.example.com` for subdomains only, or `*` for any host. An empty list refuses every
|
|
133
|
-
* RPC. Pass the same array to `AngularNodeAppEngine`.
|
|
171
|
+
* RPC. Pass the same array to `AngularNodeAppEngine`, `handleRequest` and `handleUpgrade`.
|
|
134
172
|
*/
|
|
135
173
|
readonly allowedHosts: readonly string[];
|
|
136
174
|
/**
|
|
@@ -140,6 +178,11 @@ export interface HandleOptions {
|
|
|
140
178
|
*/
|
|
141
179
|
readonly origins?: readonly string[];
|
|
142
180
|
}
|
|
181
|
+
/** The plain-text 400 for an RPC to a host that isn't allowed, logged. */
|
|
182
|
+
export declare function hostRefusal(hostname: string): Response;
|
|
183
|
+
export declare function hostAllowed(hostname: string, allowedHosts: readonly string[]): boolean;
|
|
184
|
+
/** Why `request` can't be proven same-origin, or `undefined` when it can. */
|
|
185
|
+
export declare function crossSiteReason(request: Request, origins: readonly string[]): string | undefined;
|
|
143
186
|
/**
|
|
144
187
|
* Answers an RPC HTTP request, or returns `null` for any other request.
|
|
145
188
|
*
|
|
@@ -153,19 +196,31 @@ export interface HandleOptions {
|
|
|
153
196
|
* app's own origin, otherwise the reply is `FORBIDDEN`: an `Origin` listed in `origins`
|
|
154
197
|
* passes; otherwise `Sec-Fetch-Site`, when sent, must be `same-origin`; otherwise
|
|
155
198
|
* `Origin`, when sent, must equal the request URL's origin; a request with neither
|
|
156
|
-
* header is refused. No
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* no issues.
|
|
199
|
+
* header is refused. No stage runs for any of these refusals. A fresh request scope is
|
|
200
|
+
* then created, which runs nothing, and a body that isn't JSON gives `INVALID_INPUT` with
|
|
201
|
+
* no issues before any stage runs.
|
|
160
202
|
* JSON responses carry `Cache-Control: private, no-store`, `Vary: Cookie` and
|
|
161
203
|
* `X-Content-Type-Options: nosniff`.
|
|
162
204
|
*
|
|
163
|
-
* @
|
|
164
|
-
*
|
|
205
|
+
* Once the call reached {@link runProcedure}, its response carries the headers and cookies the
|
|
206
|
+
* call set, whatever its outcome. Launch's own headers replace the call's headers of the
|
|
207
|
+
* same name, and each cookie adds a `Set-Cookie` header.
|
|
208
|
+
*
|
|
209
|
+
* A stream answers like any call until its handler returns its iterable. From then on the
|
|
210
|
+
* response is a 200 `text/event-stream` (see `eventStream`) with
|
|
211
|
+
* `Cache-Control: private, no-store, no-transform`, `Vary: Cookie`,
|
|
212
|
+
* `X-Content-Type-Options: nosniff` and `X-Accel-Buffering: no`, and the call's signal
|
|
213
|
+
* also aborts when the host cancels the body. The response's cookies and headers are the
|
|
214
|
+
* ones set when the handler returned its iterable: setting a cookie later throws, and
|
|
215
|
+
* later header changes are ignored.
|
|
216
|
+
*
|
|
165
217
|
* @param application - The application binding.
|
|
166
218
|
* @param request - The incoming server request.
|
|
167
219
|
* @param options - The allowed hosts and further allowed origins.
|
|
168
220
|
* @returns The RPC response, or `null` when the path doesn't start with `/_rpc/`.
|
|
169
221
|
*/
|
|
170
|
-
export declare function handleRpc
|
|
222
|
+
export declare function handleRpc(application: ApplicationBinding, request: Request, { allowedHosts, origins }: HandleOptions): Promise<Response | null>;
|
|
223
|
+
/** The call's headers, then Launch's own over them, then the call's cookies. */
|
|
224
|
+
export declare function responseHeaders(own: Readonly<Record<string, string>>, effects: ResponseEffects | undefined): Headers;
|
|
225
|
+
export declare function response(reply: Reply, effects?: ResponseEffects): Response;
|
|
171
226
|
export {};
|