@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.
Files changed (85) hide show
  1. package/README.md +715 -255
  2. package/builders/application.js +32 -3
  3. package/builders/channel-forwarding.d.ts +15 -0
  4. package/builders/channel-forwarding.js +61 -0
  5. package/builders/options.d.ts +6 -2
  6. package/builders/options.js +16 -5
  7. package/builders/serve.js +198 -140
  8. package/channel.d.ts +3 -1
  9. package/channel.js +3 -2
  10. package/client.d.ts +14 -24
  11. package/generation/index.d.ts +2 -123
  12. package/generation/index.js +3 -685
  13. package/generation/redirects.d.ts +14 -0
  14. package/generation/redirects.js +43 -0
  15. package/generation/run.d.ts +168 -0
  16. package/generation/run.js +659 -0
  17. package/index.d.ts +2 -0
  18. package/index.js +1 -0
  19. package/launch-queries.d.ts +16 -0
  20. package/launch-queries.js +3 -0
  21. package/navigation.d.ts +3 -3
  22. package/navigation.js +1 -1
  23. package/node/index.d.ts +3 -0
  24. package/node/index.js +6 -3
  25. package/node/upgrade-listener-slot.d.ts +1 -0
  26. package/node/upgrade-listener-slot.js +4 -0
  27. package/outcomes.d.ts +23 -7
  28. package/outcomes.js +58 -12
  29. package/package.json +15 -11
  30. package/redirect-translation.d.ts +53 -0
  31. package/redirect-translation.js +141 -0
  32. package/{generated.js → remote-client.js} +27 -21
  33. package/route-types.d.ts +130 -0
  34. package/route-types.js +1 -0
  35. package/route.d.ts +45 -0
  36. package/route.js +113 -0
  37. package/schemas/application.json +19 -3
  38. package/schemas/serve.json +3 -3
  39. package/server/application.d.ts +107 -0
  40. package/server/application.js +108 -0
  41. package/server/backend.d.ts +25 -13
  42. package/server/backend.js +39 -24
  43. package/server/channel-messages.d.ts +1 -1
  44. package/server/channel-messages.js +1 -1
  45. package/server/channel-socket.d.ts +1 -1
  46. package/server/definitions.d.ts +146 -298
  47. package/server/definitions.js +29 -117
  48. package/server/dispatch.d.ts +67 -63
  49. package/server/dispatch.js +121 -61
  50. package/server/event-stream.d.ts +2 -2
  51. package/server/event-stream.js +1 -1
  52. package/server/generated.d.ts +2 -1
  53. package/server/generated.js +2 -1
  54. package/server/index.d.ts +3 -2
  55. package/server/index.js +2 -1
  56. package/server/outcomes.d.ts +26 -13
  57. package/server/outcomes.js +43 -16
  58. package/server/read-cookies.d.ts +19 -0
  59. package/server/read-cookies.js +39 -0
  60. package/server/response-effects.d.ts +2 -2
  61. package/server/stages.d.ts +62 -0
  62. package/server/stages.js +51 -0
  63. package/server/upgrade.d.ts +11 -12
  64. package/server/upgrade.js +13 -14
  65. package/source-text.d.ts +1 -0
  66. package/source-text.js +56 -0
  67. package/stream.d.ts +6 -3
  68. package/stream.js +8 -6
  69. package/tanstack-query/README.md +15 -15
  70. package/tanstack-query/create-queries.d.ts +2 -0
  71. package/tanstack-query/{generated.js → create-queries.js} +8 -9
  72. package/tanstack-query/hydration-window.d.ts +11 -0
  73. package/tanstack-query/hydration-window.js +9 -0
  74. package/tanstack-query/hydration.d.ts +4 -14
  75. package/tanstack-query/hydration.js +9 -12
  76. package/tanstack-query/index.d.ts +1 -1
  77. package/tanstack-query/queries.d.ts +42 -27
  78. package/testing/call-procedure.d.ts +154 -0
  79. package/testing/call-procedure.js +97 -0
  80. package/testing/index.d.ts +2 -0
  81. package/testing/index.js +1 -0
  82. package/generation/context-file.d.ts +0 -2
  83. package/generation/context-file.js +0 -3
  84. package/tanstack-query/generated.d.ts +0 -5
  85. /package/{generated.d.ts → remote-client.d.ts} +0 -0
@@ -1,8 +1,8 @@
1
1
  import type { ApplicationRef, EnvironmentProviders } from "@angular/core";
2
- import { type ApplicationBinding, type Registry } from "./dispatch.js";
2
+ import type { ApplicationBinding } from "./application.js";
3
3
  /**
4
- * Provides the server-rendering transport that runs RPC calls in-process with the
5
- * current request's context.
4
+ * Provides the server-rendering transport that runs RPC calls in-process in the current
5
+ * request's scope.
6
6
  *
7
7
  * @remarks
8
8
  * Internal: exported only from `@loozo/angular-launch/_server` for the generated
@@ -17,6 +17,10 @@ import { type ApplicationBinding, type Registry } from "./dispatch.js";
17
17
  *
18
18
  * For intercepted calls during server rendering, Angular's `REQUEST_CONTEXT` must be the
19
19
  * {@link RequestScope} created for Angular's `REQUEST`, or the calls fail with `INTERNAL`.
20
+ * They run in the copy of the package that created the scope, with the application binding
21
+ * it was created for, usually the server entry's: Angular bundles the server entry apart
22
+ * from the server application, and running there gives a page the same stages, handlers
23
+ * and module state as the server's RPCs.
20
24
  * A generated-client call outside `/_rpc/` on the page's origin fails with
21
25
  * `INVALID_INPUT` with no issues, as does any intercepted call with a query string,
22
26
  * fragment, URL credentials, a method other than `POST`, a non-JSON content type, its own
@@ -31,17 +35,18 @@ import { type ApplicationBinding, type Registry } from "./dispatch.js";
31
35
  * fails with `INTERNAL`.
32
36
  *
33
37
  * Without `REQUEST`, as when Angular prerenders a page, the first intercepted call creates
34
- * the render's context from a cookie-less `GET` of the page's URL, and every call in the
35
- * render shares it. Its signal aborts when the application is destroyed. A call that
36
- * doesn't answer 200, or sets a cookie, records a fault for
37
- * {@link finishLaunchRendering}, as does a context factory that throws. Only faults
38
- * recorded before the app is first stable fail the build.
38
+ * the render's request scope from a cookie-less `GET` of the page's URL, and every call in
39
+ * the render shares it and runs in this copy, with `application`. Its signal aborts when
40
+ * the application is destroyed. A call that doesn't answer 200, or sets a cookie, records a
41
+ * fault for {@link finishLaunchRendering}, as does a context stage that throws, named by
42
+ * its route.
43
+ * Only faults recorded before the app is first stable fail the build.
39
44
  *
40
- * @param registry - The application's RPC groups.
41
- * @param application - The application binding.
45
+ * @param application - The application binding: a live scope must belong to it, and
46
+ * prerendering runs its calls with it.
42
47
  * @returns The environment providers for the server application.
43
48
  */
44
- export declare function provideLaunchServer(registry: Registry, application: ApplicationBinding): EnvironmentProviders;
49
+ export declare function provideLaunchServer(application: ApplicationBinding): EnvironmentProviders;
45
50
  /**
46
51
  * Waits for the server application to become stable and fails the render if a Launch call
47
52
  * failed while Angular prerendered the page.
@@ -60,6 +65,12 @@ export declare function provideLaunchServer(registry: Registry, application: App
60
65
  * errors, so a call made in one isn't checked and the page is still written; don't make
61
66
  * Launch calls there.
62
67
  *
68
+ * During Angular's route discovery, which boots the app without its root component, it
69
+ * returns the application at once, without waiting or checking: discovery releases a
70
+ * blocking initial navigation (`withEnabledBlockingInitialNavigation`) only after the
71
+ * bootstrap returns, and waits for stability itself. Calls made then run with the anonymous
72
+ * context, and Angular only logs their failures.
73
+ *
63
74
  * @example
64
75
  * ```ts
65
76
  * export default async function bootstrap(context: BootstrapContext) {
@@ -70,8 +81,9 @@ export declare function provideLaunchServer(registry: Registry, application: App
70
81
  * ```
71
82
  *
72
83
  * @param app - The bootstrapped server application.
73
- * @returns The same application once it is stable.
84
+ * @returns The same application once it is stable, or at once during route discovery.
74
85
  * @throws The first recorded fault, naming the call and its reply, the cookie or the
75
- * context factory's error; the application is destroyed first.
86
+ * context stage that threw, with its error as the cause; the application is destroyed
87
+ * first.
76
88
  */
77
89
  export declare function finishLaunchRendering(app: ApplicationRef): Promise<ApplicationRef>;
package/server/backend.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import { DestroyRef, DOCUMENT, inject, InjectionToken, REQUEST, REQUEST_CONTEXT, RESPONSE_INIT, makeEnvironmentProviders, } from "@angular/core";
2
2
  import { FetchBackend, HttpBackend, HttpErrorResponse, HttpResponse } from "@angular/common/http";
3
3
  import { Observable } from "rxjs";
4
- import { RPC_CALL } from "../generated.js";
4
+ import { RPC_CALL } from "../remote-client.js";
5
5
  import { parseJson } from "../json.js";
6
- import { dispatch, failure, invalidInput, isJsonMediaType, assertBoundScope, createRequestScope, } from "./dispatch.js";
6
+ import { runRenderingCall, failure, invalidInput, isJsonMediaType, assertBoundScope, createRequestScope, } from "./dispatch.js";
7
7
  import { createResponseEffects } from "./response-effects.js";
8
8
  const renderFaults = new InjectionToken("Launch render faults");
9
9
  /**
10
- * Provides the server-rendering transport that runs RPC calls in-process with the
11
- * current request's context.
10
+ * Provides the server-rendering transport that runs RPC calls in-process in the current
11
+ * request's scope.
12
12
  *
13
13
  * @remarks
14
14
  * Internal: exported only from `@loozo/angular-launch/_server` for the generated
@@ -23,6 +23,10 @@ const renderFaults = new InjectionToken("Launch render faults");
23
23
  *
24
24
  * For intercepted calls during server rendering, Angular's `REQUEST_CONTEXT` must be the
25
25
  * {@link RequestScope} created for Angular's `REQUEST`, or the calls fail with `INTERNAL`.
26
+ * They run in the copy of the package that created the scope, with the application binding
27
+ * it was created for, usually the server entry's: Angular bundles the server entry apart
28
+ * from the server application, and running there gives a page the same stages, handlers
29
+ * and module state as the server's RPCs.
26
30
  * A generated-client call outside `/_rpc/` on the page's origin fails with
27
31
  * `INVALID_INPUT` with no issues, as does any intercepted call with a query string,
28
32
  * fragment, URL credentials, a method other than `POST`, a non-JSON content type, its own
@@ -37,17 +41,18 @@ const renderFaults = new InjectionToken("Launch render faults");
37
41
  * fails with `INTERNAL`.
38
42
  *
39
43
  * Without `REQUEST`, as when Angular prerenders a page, the first intercepted call creates
40
- * the render's context from a cookie-less `GET` of the page's URL, and every call in the
41
- * render shares it. Its signal aborts when the application is destroyed. A call that
42
- * doesn't answer 200, or sets a cookie, records a fault for
43
- * {@link finishLaunchRendering}, as does a context factory that throws. Only faults
44
- * recorded before the app is first stable fail the build.
44
+ * the render's request scope from a cookie-less `GET` of the page's URL, and every call in
45
+ * the render shares it and runs in this copy, with `application`. Its signal aborts when
46
+ * the application is destroyed. A call that doesn't answer 200, or sets a cookie, records a
47
+ * fault for {@link finishLaunchRendering}, as does a context stage that throws, named by
48
+ * its route.
49
+ * Only faults recorded before the app is first stable fail the build.
45
50
  *
46
- * @param registry - The application's RPC groups.
47
- * @param application - The application binding.
51
+ * @param application - The application binding: a live scope must belong to it, and
52
+ * prerendering runs its calls with it.
48
53
  * @returns The environment providers for the server application.
49
54
  */
50
- export function provideLaunchServer(registry, application) {
55
+ export function provideLaunchServer(application) {
51
56
  return makeEnvironmentProviders([
52
57
  { provide: renderFaults, useFactory: () => ({}) },
53
58
  {
@@ -159,20 +164,18 @@ export function provideLaunchServer(registry, application) {
159
164
  }
160
165
  else {
161
166
  prerenderScope ??= createRequestScope(application, new Request(page, { signal: destroyed.signal }));
162
- try {
163
- scope = await prerenderScope;
164
- }
165
- catch (error) {
166
- recordFault("its context factory threw", error);
167
- console.error("Launch prerender context creation failed", error);
168
- deliver(failure("INTERNAL"));
169
- return;
170
- }
167
+ scope = prerenderScope;
171
168
  pageRequest = scope.request;
172
169
  }
173
- const reply = await dispatch(registry, target.pathname, envelope, scope, application, pageRequest, "rendering", AbortSignal.any([pageRequest.signal, call.signal]), effects);
170
+ // Runs in the copy of the package that created the scope: for a live page, the
171
+ // server entry's, whose stages and handlers answer this server's RPCs.
172
+ const reply = await runRenderingCall(scope, target.pathname, envelope, pageRequest, AbortSignal.any([pageRequest.signal, call.signal]), effects);
174
173
  settled = true;
175
174
  effects.commit();
175
+ // Recorded first, so the build reports the stage rather than the 500.
176
+ if (prerendering && reply.failedStage) {
177
+ recordFault(`the context stage of ${reply.failedStage.route} threw`, reply.failedStage.error);
178
+ }
176
179
  if (effects.cookies.length === 0) {
177
180
  deliver(reply);
178
181
  return;
@@ -231,6 +234,12 @@ export function provideLaunchServer(registry, application) {
231
234
  * errors, so a call made in one isn't checked and the page is still written; don't make
232
235
  * Launch calls there.
233
236
  *
237
+ * During Angular's route discovery, which boots the app without its root component, it
238
+ * returns the application at once, without waiting or checking: discovery releases a
239
+ * blocking initial navigation (`withEnabledBlockingInitialNavigation`) only after the
240
+ * bootstrap returns, and waits for stability itself. Calls made then run with the anonymous
241
+ * context, and Angular only logs their failures.
242
+ *
234
243
  * @example
235
244
  * ```ts
236
245
  * export default async function bootstrap(context: BootstrapContext) {
@@ -241,11 +250,17 @@ export function provideLaunchServer(registry, application) {
241
250
  * ```
242
251
  *
243
252
  * @param app - The bootstrapped server application.
244
- * @returns The same application once it is stable.
253
+ * @returns The same application once it is stable, or at once during route discovery.
245
254
  * @throws The first recorded fault, naming the call and its reply, the cookie or the
246
- * context factory's error; the application is destroyed first.
255
+ * context stage that threw, with its error as the cause; the application is destroyed
256
+ * first.
247
257
  */
248
258
  export async function finishLaunchRendering(app) {
259
+ // Only route discovery bootstraps without a root component.
260
+ const discoveringRoutes = app.components.length === 0;
261
+ if (discoveringRoutes) {
262
+ return app;
263
+ }
249
264
  await app.whenStable();
250
265
  const fault = app.injector.get(renderFaults).fault;
251
266
  if (fault) {
@@ -27,7 +27,7 @@ export interface ChannelInbox {
27
27
  * the protocol. Internal.
28
28
  *
29
29
  * @remarks
30
- * `dispatch` answers it with `INVALID_INPUT` and its issues instead of logging it as an
30
+ * `runProcedure` answers it with `INVALID_INPUT` and its issues instead of logging it as an
31
31
  * unexpected failure.
32
32
  */
33
33
  export declare class MessageRefused extends Error {
@@ -4,7 +4,7 @@ import { inputIssues } from "../input-issues.js";
4
4
  * the protocol. Internal.
5
5
  *
6
6
  * @remarks
7
- * `dispatch` answers it with `INVALID_INPUT` and its issues instead of logging it as an
7
+ * `runProcedure` answers it with `INVALID_INPUT` and its issues instead of logging it as an
8
8
  * unexpected failure.
9
9
  */
10
10
  export class MessageRefused extends Error {
@@ -28,7 +28,7 @@ export interface ChannelSocket {
28
28
  * Takes over the started channel: from now on, the caller leaving before `accept` ends it.
29
29
  *
30
30
  * @param stream - The handler's iterator and the settler of its part of the call.
31
- * @param outcome - The whole call's reply, from `dispatch`.
31
+ * @param outcome - The whole call's reply, from `runProcedure`.
32
32
  */
33
33
  start(stream: StreamStart, outcome: Promise<Reply>): void;
34
34
  /**