@loozo/angular-launch 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +546 -2
- package/client.d.ts +232 -0
- package/client.js +169 -0
- package/generated.d.ts +13 -0
- package/generated.js +90 -0
- package/generation/index.d.ts +116 -0
- package/generation/index.js +634 -0
- package/index.d.ts +2 -0
- package/index.js +1 -0
- package/json.d.ts +13 -0
- package/json.js +48 -0
- package/package.json +48 -4
- package/server/backend.d.ts +58 -0
- package/server/backend.js +166 -0
- package/server/definitions.d.ts +486 -0
- package/server/definitions.js +170 -0
- package/server/dispatch.d.ts +140 -0
- package/server/dispatch.js +249 -0
- package/server/errors.d.ts +21 -0
- package/server/errors.js +32 -0
- package/server/generated.d.ts +2 -0
- package/server/generated.js +3 -0
- package/server/index.d.ts +6 -0
- package/server/index.js +2 -0
package/json.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Deliberately strict wire subset: no toJSON hooks, Date, undefined, holes,
|
|
2
|
+
// nonfinite numbers, symbols, cycles or custom prototypes on either transport.
|
|
3
|
+
export function serializeJson(value) {
|
|
4
|
+
const active = new Set();
|
|
5
|
+
function visit(current) {
|
|
6
|
+
if (current === null || typeof current === "string" || typeof current === "boolean") {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (typeof current === "number" && Number.isFinite(current)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (typeof current !== "object" || current === null || active.has(current)) {
|
|
13
|
+
throw new TypeError("RPC values must be strict JSON");
|
|
14
|
+
}
|
|
15
|
+
active.add(current);
|
|
16
|
+
if (Array.isArray(current)) {
|
|
17
|
+
if (Object.getPrototypeOf(current) !== Array.prototype) {
|
|
18
|
+
throw new TypeError("RPC arrays must have a plain prototype");
|
|
19
|
+
}
|
|
20
|
+
for (let index = 0; index < current.length; index += 1) {
|
|
21
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, index);
|
|
22
|
+
if (!descriptor || !descriptor.enumerable || !("value" in descriptor)) {
|
|
23
|
+
throw new TypeError("RPC arrays require dense data properties");
|
|
24
|
+
}
|
|
25
|
+
visit(descriptor.value);
|
|
26
|
+
}
|
|
27
|
+
if (Reflect.ownKeys(current).length !== current.length + 1) {
|
|
28
|
+
throw new TypeError("RPC arrays cannot have extra properties");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (Object.getPrototypeOf(current) !== Object.prototype &&
|
|
33
|
+
Object.getPrototypeOf(current) !== null) {
|
|
34
|
+
throw new TypeError("RPC objects must have a plain prototype");
|
|
35
|
+
}
|
|
36
|
+
for (const key of Reflect.ownKeys(current)) {
|
|
37
|
+
const descriptor = Object.getOwnPropertyDescriptor(current, key);
|
|
38
|
+
if (typeof key !== "string" || !descriptor.enumerable || !("value" in descriptor)) {
|
|
39
|
+
throw new TypeError("RPC objects require enumerable string data properties");
|
|
40
|
+
}
|
|
41
|
+
visit(descriptor.value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
active.delete(current);
|
|
45
|
+
}
|
|
46
|
+
visit(value);
|
|
47
|
+
return JSON.stringify(value);
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loozo/angular-launch",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Typed queries, mutations and declared errors on Angular's native router, HTTP and SSR pipeline.",
|
|
5
|
+
"bugs": "https://github.com/theo-matzavinos/loozo/issues",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Theophilos Matzavinos",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/theo-matzavinos/loozo.git",
|
|
11
|
+
"directory": "packages/angular-launch"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./index.d.ts",
|
|
17
|
+
"default": "./index.js"
|
|
18
|
+
},
|
|
19
|
+
"./server": {
|
|
20
|
+
"types": "./server/index.d.ts",
|
|
21
|
+
"default": "./server/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./_server": {
|
|
24
|
+
"types": "./server/generated.d.ts",
|
|
25
|
+
"default": "./server/generated.js"
|
|
26
|
+
},
|
|
27
|
+
"./_generated": {
|
|
28
|
+
"types": "./generated.d.ts",
|
|
29
|
+
"default": "./generated.js"
|
|
30
|
+
},
|
|
31
|
+
"./generation": {
|
|
32
|
+
"types": "./generation/index.d.ts",
|
|
33
|
+
"node": "./generation/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public",
|
|
39
|
+
"registry": "https://registry.npmjs.org/"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"typescript": "6.0.3"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@angular/common": "^22.0.0",
|
|
46
|
+
"@angular/core": "^22.0.0",
|
|
47
|
+
"@loozo/angular-router": "0.0.4",
|
|
48
|
+
"rxjs": "^7.8.2"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ApplicationRef, EnvironmentProviders } from "@angular/core";
|
|
2
|
+
import { type ApplicationBinding, type Registry } from "./dispatch.js";
|
|
3
|
+
/**
|
|
4
|
+
* Provides the server-rendering transport that runs RPC calls in-process with the
|
|
5
|
+
* current request's context.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Internal: exported only from `@loozo/angular-launch/_server` for the generated
|
|
9
|
+
* `provideServer` in `server.generated.ts`; add that to your server application config,
|
|
10
|
+
* after `provideHttpClient()`, instead of calling this.
|
|
11
|
+
*
|
|
12
|
+
* It replaces Angular's `HttpBackend` on the server. Calls from generated clients, and
|
|
13
|
+
* any other request to `/_rpc/` on the current request's origin, are intercepted and
|
|
14
|
+
* run in-process in `"rendering"` mode, so mutations are refused with `FORBIDDEN`. All
|
|
15
|
+
* other requests, including ones to `/_rpc/` on another origin that don't come from a
|
|
16
|
+
* generated client, go to Angular's `FetchBackend`.
|
|
17
|
+
*
|
|
18
|
+
* For intercepted calls, Angular's `REQUEST_CONTEXT` must be the {@link RequestScope}
|
|
19
|
+
* created for Angular's `REQUEST`, or the calls fail with `INTERNAL`. A generated-client
|
|
20
|
+
* call outside `/_rpc/` on the current request's origin fails with `INVALID_INPUT`, as
|
|
21
|
+
* does any intercepted call with a query string, fragment, URL credentials, a method
|
|
22
|
+
* other than `POST`, a non-JSON content type, its own `Authorization`,
|
|
23
|
+
* `Proxy-Authorization` or `Cookie` header, or its own credentials setting.
|
|
24
|
+
*
|
|
25
|
+
* Without a live request, as during prerendering, calls from generated clients and
|
|
26
|
+
* requests to a relative `/_rpc/` path fail and the fault is recorded for
|
|
27
|
+
* {@link finishLaunchRendering}; other requests still go to `FetchBackend`.
|
|
28
|
+
*
|
|
29
|
+
* @param registry - The application's RPC groups.
|
|
30
|
+
* @param application - The application binding.
|
|
31
|
+
* @returns The environment providers for the server application.
|
|
32
|
+
*/
|
|
33
|
+
export declare function provideLaunchServer(registry: Registry, application: ApplicationBinding): EnvironmentProviders;
|
|
34
|
+
/**
|
|
35
|
+
* Waits for the server application to become stable and fails the render if an RPC was
|
|
36
|
+
* called without a live request.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* Angular's prerendering can swallow resolver errors and still report success. Return
|
|
40
|
+
* this from your server bootstrap so a page that calls an RPC during prerendering fails
|
|
41
|
+
* the build instead of producing a broken page. The application must include the
|
|
42
|
+
* generated `provideServer()`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* export default async function bootstrap(context: BootstrapContext) {
|
|
47
|
+
* const app = await bootstrapApplication(App, serverConfig, context);
|
|
48
|
+
*
|
|
49
|
+
* return finishLaunchRendering(app);
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @param app - The bootstrapped server application.
|
|
54
|
+
* @returns The same application once it is stable.
|
|
55
|
+
* @throws The recorded error when an RPC was called without a live request; the
|
|
56
|
+
* application is destroyed first.
|
|
57
|
+
*/
|
|
58
|
+
export declare function finishLaunchRendering(app: ApplicationRef): Promise<ApplicationRef>;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { inject, InjectionToken, REQUEST, REQUEST_CONTEXT, makeEnvironmentProviders, } from "@angular/core";
|
|
2
|
+
import { FetchBackend, HttpBackend, HttpErrorResponse, HttpResponse } from "@angular/common/http";
|
|
3
|
+
import { Observable } from "rxjs";
|
|
4
|
+
import { RPC_CALL } from "../generated.js";
|
|
5
|
+
import { dispatch, failure, isJsonMediaType, assertBoundScope, } from "./dispatch.js";
|
|
6
|
+
const renderFaults = new InjectionToken("Launch render faults");
|
|
7
|
+
/**
|
|
8
|
+
* Provides the server-rendering transport that runs RPC calls in-process with the
|
|
9
|
+
* current request's context.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Internal: exported only from `@loozo/angular-launch/_server` for the generated
|
|
13
|
+
* `provideServer` in `server.generated.ts`; add that to your server application config,
|
|
14
|
+
* after `provideHttpClient()`, instead of calling this.
|
|
15
|
+
*
|
|
16
|
+
* It replaces Angular's `HttpBackend` on the server. Calls from generated clients, and
|
|
17
|
+
* any other request to `/_rpc/` on the current request's origin, are intercepted and
|
|
18
|
+
* run in-process in `"rendering"` mode, so mutations are refused with `FORBIDDEN`. All
|
|
19
|
+
* other requests, including ones to `/_rpc/` on another origin that don't come from a
|
|
20
|
+
* generated client, go to Angular's `FetchBackend`.
|
|
21
|
+
*
|
|
22
|
+
* For intercepted calls, Angular's `REQUEST_CONTEXT` must be the {@link RequestScope}
|
|
23
|
+
* created for Angular's `REQUEST`, or the calls fail with `INTERNAL`. A generated-client
|
|
24
|
+
* call outside `/_rpc/` on the current request's origin fails with `INVALID_INPUT`, as
|
|
25
|
+
* does any intercepted call with a query string, fragment, URL credentials, a method
|
|
26
|
+
* other than `POST`, a non-JSON content type, its own `Authorization`,
|
|
27
|
+
* `Proxy-Authorization` or `Cookie` header, or its own credentials setting.
|
|
28
|
+
*
|
|
29
|
+
* Without a live request, as during prerendering, calls from generated clients and
|
|
30
|
+
* requests to a relative `/_rpc/` path fail and the fault is recorded for
|
|
31
|
+
* {@link finishLaunchRendering}; other requests still go to `FetchBackend`.
|
|
32
|
+
*
|
|
33
|
+
* @param registry - The application's RPC groups.
|
|
34
|
+
* @param application - The application binding.
|
|
35
|
+
* @returns The environment providers for the server application.
|
|
36
|
+
*/
|
|
37
|
+
export function provideLaunchServer(registry, application) {
|
|
38
|
+
return makeEnvironmentProviders([
|
|
39
|
+
{ provide: renderFaults, useFactory: () => ({}) },
|
|
40
|
+
{
|
|
41
|
+
provide: HttpBackend,
|
|
42
|
+
useFactory: () => {
|
|
43
|
+
const context = inject(REQUEST_CONTEXT);
|
|
44
|
+
const nativeRequest = inject(REQUEST);
|
|
45
|
+
const fallback = inject(FetchBackend);
|
|
46
|
+
const faults = inject(renderFaults);
|
|
47
|
+
return {
|
|
48
|
+
handle(request) {
|
|
49
|
+
const marked = request.context.get(RPC_CALL);
|
|
50
|
+
let target;
|
|
51
|
+
if (nativeRequest instanceof Request) {
|
|
52
|
+
target = new URL(request.urlWithParams, nativeRequest.url);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
target = new URL(request.urlWithParams, "http://launch.invalid");
|
|
56
|
+
const localRpc = target.origin === "http://launch.invalid" && target.pathname.startsWith("/_rpc/");
|
|
57
|
+
if (!marked && !localRpc) {
|
|
58
|
+
return fallback.handle(request);
|
|
59
|
+
}
|
|
60
|
+
const error = new Error("Launch RPC requires live request context; prerender RPC is unsupported");
|
|
61
|
+
faults.unsupportedContext = error;
|
|
62
|
+
return new Observable((subscriber) => subscriber.error(error));
|
|
63
|
+
}
|
|
64
|
+
const ownOrigin = target.origin === new URL(nativeRequest.url).origin;
|
|
65
|
+
const ownRpc = ownOrigin && target.pathname.startsWith("/_rpc/");
|
|
66
|
+
if (!marked && !ownRpc) {
|
|
67
|
+
return fallback.handle(request);
|
|
68
|
+
}
|
|
69
|
+
return new Observable((subscriber) => {
|
|
70
|
+
function deliver(reply) {
|
|
71
|
+
if (reply.status >= 400) {
|
|
72
|
+
subscriber.error(new HttpErrorResponse({
|
|
73
|
+
status: reply.status,
|
|
74
|
+
error: reply.body,
|
|
75
|
+
url: request.urlWithParams,
|
|
76
|
+
}));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
subscriber.next(new HttpResponse({
|
|
80
|
+
status: reply.status,
|
|
81
|
+
body: reply.body,
|
|
82
|
+
url: request.urlWithParams,
|
|
83
|
+
}));
|
|
84
|
+
subscriber.complete();
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
assertBoundScope(context, application, nativeRequest);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error("Launch SSR scope binding failed", error);
|
|
91
|
+
deliver(failure("INTERNAL"));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const authOverride = request.headers.has("authorization") ||
|
|
95
|
+
request.headers.has("proxy-authorization") ||
|
|
96
|
+
request.headers.has("cookie") ||
|
|
97
|
+
request.withCredentials ||
|
|
98
|
+
request.credentials !== undefined;
|
|
99
|
+
const contentType = request.headers.get("content-type") ?? request.detectContentTypeHeader();
|
|
100
|
+
if (!ownRpc ||
|
|
101
|
+
target.search !== "" ||
|
|
102
|
+
target.hash !== "" ||
|
|
103
|
+
target.username !== "" ||
|
|
104
|
+
target.password !== "" ||
|
|
105
|
+
request.method !== "POST" ||
|
|
106
|
+
!isJsonMediaType(contentType) ||
|
|
107
|
+
authOverride) {
|
|
108
|
+
deliver(failure("INVALID_INPUT"));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
let envelope;
|
|
112
|
+
try {
|
|
113
|
+
const body = request.serializeBody();
|
|
114
|
+
if (typeof body !== "string") {
|
|
115
|
+
deliver(failure("INVALID_INPUT"));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
envelope = JSON.parse(body);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
deliver(failure("INVALID_INPUT"));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
void dispatch(registry, target.pathname, envelope, context, application, nativeRequest, "rendering").then(deliver);
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
]);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Waits for the server application to become stable and fails the render if an RPC was
|
|
134
|
+
* called without a live request.
|
|
135
|
+
*
|
|
136
|
+
* @remarks
|
|
137
|
+
* Angular's prerendering can swallow resolver errors and still report success. Return
|
|
138
|
+
* this from your server bootstrap so a page that calls an RPC during prerendering fails
|
|
139
|
+
* the build instead of producing a broken page. The application must include the
|
|
140
|
+
* generated `provideServer()`.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* export default async function bootstrap(context: BootstrapContext) {
|
|
145
|
+
* const app = await bootstrapApplication(App, serverConfig, context);
|
|
146
|
+
*
|
|
147
|
+
* return finishLaunchRendering(app);
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @param app - The bootstrapped server application.
|
|
152
|
+
* @returns The same application once it is stable.
|
|
153
|
+
* @throws The recorded error when an RPC was called without a live request; the
|
|
154
|
+
* application is destroyed first.
|
|
155
|
+
*/
|
|
156
|
+
// Native prerender can swallow resolver errors. Propagate only the unsupported
|
|
157
|
+
// Launch context fault after native stability, without manufacturing a request.
|
|
158
|
+
export async function finishLaunchRendering(app) {
|
|
159
|
+
await app.whenStable();
|
|
160
|
+
const fault = app.injector.get(renderFaults).unsupportedContext;
|
|
161
|
+
if (fault) {
|
|
162
|
+
app.destroy();
|
|
163
|
+
throw fault;
|
|
164
|
+
}
|
|
165
|
+
return app;
|
|
166
|
+
}
|