appflare 0.2.48 → 0.2.49

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 (139) hide show
  1. package/Documentation.md +898 -898
  2. package/cli/commands/index.ts +247 -247
  3. package/cli/generate.ts +360 -360
  4. package/cli/index.ts +120 -120
  5. package/cli/load-config.ts +184 -184
  6. package/cli/schema-compiler.ts +1366 -1366
  7. package/cli/templates/auth/README.md +156 -156
  8. package/cli/templates/auth/config.ts +61 -61
  9. package/cli/templates/auth/route-config.ts +1 -1
  10. package/cli/templates/auth/route-handler.ts +1 -1
  11. package/cli/templates/auth/route-request-utils.ts +5 -5
  12. package/cli/templates/auth/route.config.ts +18 -18
  13. package/cli/templates/auth/route.handler.ts +18 -18
  14. package/cli/templates/auth/route.request-utils.ts +55 -55
  15. package/cli/templates/auth/route.ts +14 -14
  16. package/cli/templates/core/README.md +266 -266
  17. package/cli/templates/core/app-creation.ts +19 -19
  18. package/cli/templates/core/client/appflare.ts +112 -112
  19. package/cli/templates/core/client/handlers/index.ts +763 -763
  20. package/cli/templates/core/client/handlers.ts +1 -1
  21. package/cli/templates/core/client/index.ts +7 -7
  22. package/cli/templates/core/client/storage.ts +195 -195
  23. package/cli/templates/core/client/types.ts +187 -187
  24. package/cli/templates/core/client-modules/appflare.ts +1 -1
  25. package/cli/templates/core/client-modules/handlers.ts +1 -1
  26. package/cli/templates/core/client-modules/index.ts +1 -1
  27. package/cli/templates/core/client-modules/storage.ts +1 -1
  28. package/cli/templates/core/client-modules/types.ts +1 -1
  29. package/cli/templates/core/client.artifacts.ts +39 -39
  30. package/cli/templates/core/client.ts +4 -4
  31. package/cli/templates/core/drizzle.ts +15 -15
  32. package/cli/templates/core/export.ts +14 -14
  33. package/cli/templates/core/handlers.route.ts +24 -24
  34. package/cli/templates/core/handlers.ts +1 -1
  35. package/cli/templates/core/imports.ts +9 -9
  36. package/cli/templates/core/server.ts +38 -38
  37. package/cli/templates/core/types.ts +6 -6
  38. package/cli/templates/core/wrangler.ts +109 -109
  39. package/cli/templates/dashboard/builders/functions/index.ts +17 -17
  40. package/cli/templates/dashboard/builders/functions/render-page/header.ts +20 -20
  41. package/cli/templates/dashboard/builders/functions/render-page/index.ts +33 -33
  42. package/cli/templates/dashboard/builders/functions/render-page/request-panel.ts +271 -271
  43. package/cli/templates/dashboard/builders/functions/render-page/result-panel.ts +85 -85
  44. package/cli/templates/dashboard/builders/functions/render-page/scripts.ts +703 -703
  45. package/cli/templates/dashboard/builders/functions/tree-builder.ts +47 -47
  46. package/cli/templates/dashboard/builders/navigation.ts +155 -155
  47. package/cli/templates/dashboard/builders/storage/index.ts +13 -13
  48. package/cli/templates/dashboard/builders/storage/routes/create-directory-route.ts +29 -29
  49. package/cli/templates/dashboard/builders/storage/routes/delete-route.ts +18 -18
  50. package/cli/templates/dashboard/builders/storage/routes/download-route.ts +23 -23
  51. package/cli/templates/dashboard/builders/storage/routes/index.ts +22 -22
  52. package/cli/templates/dashboard/builders/storage/routes/list-route.ts +25 -25
  53. package/cli/templates/dashboard/builders/storage/routes/preview-route.ts +21 -21
  54. package/cli/templates/dashboard/builders/storage/routes/upload-route.ts +21 -21
  55. package/cli/templates/dashboard/builders/storage/runtime/helpers.ts +72 -72
  56. package/cli/templates/dashboard/builders/storage/runtime/storage-page.ts +130 -130
  57. package/cli/templates/dashboard/builders/table-routes/common/drawer-panel.ts +27 -27
  58. package/cli/templates/dashboard/builders/table-routes/common/pagination.ts +30 -30
  59. package/cli/templates/dashboard/builders/table-routes/common/search-bar.ts +23 -23
  60. package/cli/templates/dashboard/builders/table-routes/fragments.ts +257 -217
  61. package/cli/templates/dashboard/builders/table-routes/helpers.ts +45 -45
  62. package/cli/templates/dashboard/builders/table-routes/index.ts +8 -8
  63. package/cli/templates/dashboard/builders/table-routes/table/actions-cell.ts +71 -71
  64. package/cli/templates/dashboard/builders/table-routes/table/get-route.ts +291 -291
  65. package/cli/templates/dashboard/builders/table-routes/table/index.ts +80 -80
  66. package/cli/templates/dashboard/builders/table-routes/table/post-routes.ts +163 -163
  67. package/cli/templates/dashboard/builders/table-routes/table-route.ts +7 -7
  68. package/cli/templates/dashboard/builders/table-routes/users/get-route.ts +69 -69
  69. package/cli/templates/dashboard/builders/table-routes/users/html/modals.ts +57 -57
  70. package/cli/templates/dashboard/builders/table-routes/users/html/page.ts +27 -27
  71. package/cli/templates/dashboard/builders/table-routes/users/html/table.ts +128 -128
  72. package/cli/templates/dashboard/builders/table-routes/users/index.ts +32 -32
  73. package/cli/templates/dashboard/builders/table-routes/users/post-routes.ts +150 -150
  74. package/cli/templates/dashboard/builders/table-routes/users/redirect.ts +14 -14
  75. package/cli/templates/dashboard/builders/table-routes/users-route.ts +10 -10
  76. package/cli/templates/dashboard/components/dashboard-home.ts +23 -23
  77. package/cli/templates/dashboard/components/layout.ts +420 -420
  78. package/cli/templates/dashboard/components/login-page.ts +65 -65
  79. package/cli/templates/dashboard/index.ts +61 -61
  80. package/cli/templates/dashboard/types.ts +9 -9
  81. package/cli/templates/handlers/README.md +353 -353
  82. package/cli/templates/handlers/auth.ts +37 -37
  83. package/cli/templates/handlers/execution.ts +44 -42
  84. package/cli/templates/handlers/generators/context/context-creation.ts +101 -101
  85. package/cli/templates/handlers/generators/context/error-helpers.ts +11 -11
  86. package/cli/templates/handlers/generators/context/scheduler.ts +24 -24
  87. package/cli/templates/handlers/generators/context/storage-api.ts +82 -82
  88. package/cli/templates/handlers/generators/context/storage-helpers.ts +59 -59
  89. package/cli/templates/handlers/generators/context/types.ts +40 -40
  90. package/cli/templates/handlers/generators/context.ts +43 -43
  91. package/cli/templates/handlers/generators/execution.ts +15 -15
  92. package/cli/templates/handlers/generators/handlers.ts +14 -14
  93. package/cli/templates/handlers/generators/registration/modules/cron.ts +35 -35
  94. package/cli/templates/handlers/generators/registration/modules/realtime/auth.ts +75 -75
  95. package/cli/templates/handlers/generators/registration/modules/realtime/durable-object.ts +144 -144
  96. package/cli/templates/handlers/generators/registration/modules/realtime/index.ts +14 -14
  97. package/cli/templates/handlers/generators/registration/modules/realtime/publisher.ts +102 -102
  98. package/cli/templates/handlers/generators/registration/modules/realtime/routes.ts +164 -164
  99. package/cli/templates/handlers/generators/registration/modules/realtime/types.ts +30 -30
  100. package/cli/templates/handlers/generators/registration/modules/realtime/utils.ts +510 -510
  101. package/cli/templates/handlers/generators/registration/modules/scheduler.ts +65 -65
  102. package/cli/templates/handlers/generators/registration/modules/storage.ts +199 -199
  103. package/cli/templates/handlers/generators/registration/sections.ts +210 -210
  104. package/cli/templates/handlers/generators/types/context.ts +121 -121
  105. package/cli/templates/handlers/generators/types/core.ts +108 -108
  106. package/cli/templates/handlers/generators/types/operations.ts +135 -135
  107. package/cli/templates/handlers/generators/types/query-definitions/filter-and-where-types.ts +291 -291
  108. package/cli/templates/handlers/generators/types/query-definitions/query-api-types.ts +135 -135
  109. package/cli/templates/handlers/generators/types/query-definitions/query-helper-functions.ts +1382 -1382
  110. package/cli/templates/handlers/generators/types/query-definitions/schema-and-table-types.ts +278 -278
  111. package/cli/templates/handlers/generators/types/query-definitions.ts +13 -13
  112. package/cli/templates/handlers/generators/types/query-runtime/handled-error.ts +13 -13
  113. package/cli/templates/handlers/generators/types/query-runtime/runtime-aggregate-and-footer.ts +174 -174
  114. package/cli/templates/handlers/generators/types/query-runtime/runtime-read.ts +156 -156
  115. package/cli/templates/handlers/generators/types/query-runtime/runtime-setup.ts +45 -45
  116. package/cli/templates/handlers/generators/types/query-runtime/runtime-write.ts +958 -958
  117. package/cli/templates/handlers/generators/types/query-runtime.ts +15 -15
  118. package/cli/templates/handlers/index.ts +47 -47
  119. package/cli/templates/handlers/operations.ts +116 -116
  120. package/cli/templates/handlers/registration.ts +91 -91
  121. package/cli/templates/handlers/types.ts +17 -17
  122. package/cli/templates/handlers/utils.ts +48 -48
  123. package/cli/types.ts +110 -110
  124. package/cli/utils/handler-discovery.ts +501 -501
  125. package/cli/utils/json-utils.ts +24 -24
  126. package/cli/utils/path-utils.ts +19 -19
  127. package/cli/utils/schema-discovery.ts +402 -399
  128. package/dist/cli/index.js +77 -55
  129. package/dist/cli/index.mjs +77 -55
  130. package/index.ts +18 -18
  131. package/package.json +58 -58
  132. package/react/index.ts +5 -5
  133. package/react/use-infinite-query.ts +255 -255
  134. package/react/use-mutation.ts +89 -89
  135. package/react/use-query.ts +210 -210
  136. package/schema.ts +641 -641
  137. package/test-better-auth-hash.ts +2 -2
  138. package/tsconfig.json +6 -6
  139. package/tsup.config.ts +82 -82
@@ -1,144 +1,144 @@
1
- export const realtimeDurableObjectModule = `
2
- export class AppflareRealtimeDurableObject {
3
- private readonly subscriptions = new Map<string, RealtimeSubscription>();
4
- private readonly sockets = new Map<string, WebSocket>();
5
-
6
- public constructor(_state: unknown) {}
7
-
8
- public async fetch(request: Request): Promise<Response> {
9
- const url = new URL(request.url);
10
-
11
- if (request.method === "POST" && url.pathname === "/subscribe") {
12
- const payload = (await request.json().catch(() => null)) as RealtimeSubscription | null;
13
- if (!payload?.token || !payload.queryName ) {
14
- return new Response(JSON.stringify({ message: "Invalid subscription payload" }), {
15
- status: 400,
16
- headers: { "content-type": "application/json" },
17
- });
18
- }
19
-
20
- this.subscriptions.set(payload.token, payload);
21
- return new Response(JSON.stringify({ ok: true }), {
22
- status: 200,
23
- headers: { "content-type": "application/json" },
24
- });
25
- }
26
-
27
- if (request.method === "POST" && url.pathname === "/subscriptions") {
28
- return new Response(
29
- JSON.stringify({ subscriptions: Array.from(this.subscriptions.values()) }),
30
- {
31
- status: 200,
32
- headers: { "content-type": "application/json" },
33
- },
34
- );
35
- }
36
-
37
- if (request.method === "POST" && url.pathname === "/unsubscribe") {
38
- const payload = (await request.json().catch(() => null)) as {
39
- token?: unknown;
40
- authToken?: unknown;
41
- } | null;
42
- const token = typeof payload?.token === "string" ? payload.token : "";
43
- const authToken =
44
- typeof payload?.authToken === "string" ? payload.authToken : "";
45
-
46
- if (!token ) {
47
- return new Response(
48
- JSON.stringify({ message: "token is required" }),
49
- {
50
- status: 400,
51
- headers: { "content-type": "application/json" },
52
- },
53
- );
54
- }
55
-
56
- const existing = this.subscriptions.get(token);
57
- if (!existing || existing.authToken !== authToken) {
58
- return new Response(JSON.stringify({ message: "Subscription not found" }), {
59
- status: 404,
60
- headers: { "content-type": "application/json" },
61
- });
62
- }
63
-
64
- const socket = this.sockets.get(token);
65
- if (socket && socket.readyState === 1) {
66
- socket.close();
67
- }
68
-
69
- this.sockets.delete(token);
70
- this.subscriptions.delete(token);
71
-
72
- return new Response(JSON.stringify({ ok: true }), {
73
- status: 200,
74
- headers: { "content-type": "application/json" },
75
- });
76
- }
77
-
78
- if (request.method === "POST" && url.pathname === "/emit") {
79
- const payload = (await request.json().catch(() => null)) as RealtimeEmitPayload | null;
80
- if (!payload?.token || !payload.event) {
81
- return new Response(JSON.stringify({ message: "Invalid emit payload" }), {
82
- status: 400,
83
- headers: { "content-type": "application/json" },
84
- });
85
- }
86
-
87
- const socket = this.sockets.get(payload.token);
88
- if (socket && socket.readyState === 1) {
89
- socket.send(
90
- JSON.stringify({
91
- event: payload.event,
92
- payload: payload.payload,
93
- }),
94
- );
95
- }
96
-
97
- return new Response(JSON.stringify({ ok: true }), {
98
- status: 200,
99
- headers: { "content-type": "application/json" },
100
- });
101
- }
102
-
103
- if (request.method === "GET" && (url.pathname === "/ws" || url.pathname.endsWith("/ws"))) {
104
- const token = url.searchParams.get("token") ?? "";
105
- const authToken = url.searchParams.get("authToken") ?? "";
106
- const subscription = this.subscriptions.get(token);
107
- if (!subscription) {
108
- return new Response("Unauthorized", { status: 401 });
109
- }
110
-
111
- const pair = new WebSocketPair();
112
- const [clientSocket, serverSocket] = Object.values(pair);
113
- serverSocket.accept();
114
- this.sockets.set(token, serverSocket);
115
-
116
- const release = () => {
117
- this.sockets.delete(token);
118
- };
119
-
120
- serverSocket.addEventListener("close", release);
121
- serverSocket.addEventListener("error", release);
122
- serverSocket.addEventListener("message", (event) => {
123
- if (String(event.data ?? "").trim() === "ping") {
124
- serverSocket.send(JSON.stringify({ event: "pong" }));
125
- }
126
- });
127
-
128
- const requestedProtocol = request.headers.get("Sec-WebSocket-Protocol");
129
- const headers = new Headers();
130
- if (requestedProtocol) {
131
- headers.set("Sec-WebSocket-Protocol", requestedProtocol);
132
- }
133
-
134
- return new Response(null, {
135
- status: 101,
136
- webSocket: clientSocket,
137
- headers,
138
- } as ResponseInit & { webSocket: WebSocket });
139
- }
140
-
141
- return new Response("Not found", { status: 404 });
142
- }
143
- }
144
- `;
1
+ export const realtimeDurableObjectModule = `
2
+ export class AppflareRealtimeDurableObject {
3
+ private readonly subscriptions = new Map<string, RealtimeSubscription>();
4
+ private readonly sockets = new Map<string, WebSocket>();
5
+
6
+ public constructor(_state: unknown) {}
7
+
8
+ public async fetch(request: Request): Promise<Response> {
9
+ const url = new URL(request.url);
10
+
11
+ if (request.method === "POST" && url.pathname === "/subscribe") {
12
+ const payload = (await request.json().catch(() => null)) as RealtimeSubscription | null;
13
+ if (!payload?.token || !payload.queryName ) {
14
+ return new Response(JSON.stringify({ message: "Invalid subscription payload" }), {
15
+ status: 400,
16
+ headers: { "content-type": "application/json" },
17
+ });
18
+ }
19
+
20
+ this.subscriptions.set(payload.token, payload);
21
+ return new Response(JSON.stringify({ ok: true }), {
22
+ status: 200,
23
+ headers: { "content-type": "application/json" },
24
+ });
25
+ }
26
+
27
+ if (request.method === "POST" && url.pathname === "/subscriptions") {
28
+ return new Response(
29
+ JSON.stringify({ subscriptions: Array.from(this.subscriptions.values()) }),
30
+ {
31
+ status: 200,
32
+ headers: { "content-type": "application/json" },
33
+ },
34
+ );
35
+ }
36
+
37
+ if (request.method === "POST" && url.pathname === "/unsubscribe") {
38
+ const payload = (await request.json().catch(() => null)) as {
39
+ token?: unknown;
40
+ authToken?: unknown;
41
+ } | null;
42
+ const token = typeof payload?.token === "string" ? payload.token : "";
43
+ const authToken =
44
+ typeof payload?.authToken === "string" ? payload.authToken : "";
45
+
46
+ if (!token ) {
47
+ return new Response(
48
+ JSON.stringify({ message: "token is required" }),
49
+ {
50
+ status: 400,
51
+ headers: { "content-type": "application/json" },
52
+ },
53
+ );
54
+ }
55
+
56
+ const existing = this.subscriptions.get(token);
57
+ if (!existing || existing.authToken !== authToken) {
58
+ return new Response(JSON.stringify({ message: "Subscription not found" }), {
59
+ status: 404,
60
+ headers: { "content-type": "application/json" },
61
+ });
62
+ }
63
+
64
+ const socket = this.sockets.get(token);
65
+ if (socket && socket.readyState === 1) {
66
+ socket.close();
67
+ }
68
+
69
+ this.sockets.delete(token);
70
+ this.subscriptions.delete(token);
71
+
72
+ return new Response(JSON.stringify({ ok: true }), {
73
+ status: 200,
74
+ headers: { "content-type": "application/json" },
75
+ });
76
+ }
77
+
78
+ if (request.method === "POST" && url.pathname === "/emit") {
79
+ const payload = (await request.json().catch(() => null)) as RealtimeEmitPayload | null;
80
+ if (!payload?.token || !payload.event) {
81
+ return new Response(JSON.stringify({ message: "Invalid emit payload" }), {
82
+ status: 400,
83
+ headers: { "content-type": "application/json" },
84
+ });
85
+ }
86
+
87
+ const socket = this.sockets.get(payload.token);
88
+ if (socket && socket.readyState === 1) {
89
+ socket.send(
90
+ JSON.stringify({
91
+ event: payload.event,
92
+ payload: payload.payload,
93
+ }),
94
+ );
95
+ }
96
+
97
+ return new Response(JSON.stringify({ ok: true }), {
98
+ status: 200,
99
+ headers: { "content-type": "application/json" },
100
+ });
101
+ }
102
+
103
+ if (request.method === "GET" && (url.pathname === "/ws" || url.pathname.endsWith("/ws"))) {
104
+ const token = url.searchParams.get("token") ?? "";
105
+ const authToken = url.searchParams.get("authToken") ?? "";
106
+ const subscription = this.subscriptions.get(token);
107
+ if (!subscription) {
108
+ return new Response("Unauthorized", { status: 401 });
109
+ }
110
+
111
+ const pair = new WebSocketPair();
112
+ const [clientSocket, serverSocket] = Object.values(pair);
113
+ serverSocket.accept();
114
+ this.sockets.set(token, serverSocket);
115
+
116
+ const release = () => {
117
+ this.sockets.delete(token);
118
+ };
119
+
120
+ serverSocket.addEventListener("close", release);
121
+ serverSocket.addEventListener("error", release);
122
+ serverSocket.addEventListener("message", (event) => {
123
+ if (String(event.data ?? "").trim() === "ping") {
124
+ serverSocket.send(JSON.stringify({ event: "pong" }));
125
+ }
126
+ });
127
+
128
+ const requestedProtocol = request.headers.get("Sec-WebSocket-Protocol");
129
+ const headers = new Headers();
130
+ if (requestedProtocol) {
131
+ headers.set("Sec-WebSocket-Protocol", requestedProtocol);
132
+ }
133
+
134
+ return new Response(null, {
135
+ status: 101,
136
+ webSocket: clientSocket,
137
+ headers,
138
+ } as ResponseInit & { webSocket: WebSocket });
139
+ }
140
+
141
+ return new Response("Not found", { status: 404 });
142
+ }
143
+ }
144
+ `;
@@ -1,15 +1,15 @@
1
- import { realtimeAuthModule } from "./auth";
2
- import { realtimeDurableObjectModule } from "./durable-object";
3
- import { realtimePublisherModule } from "./publisher";
4
- import { realtimeRoutesModule } from "./routes";
5
- import { realtimeTypesModule } from "./types";
6
- import { realtimeUtilsModule } from "./utils";
7
-
8
- export const realtimeModule = [
9
- realtimeTypesModule,
10
- realtimeUtilsModule,
11
- realtimeAuthModule,
12
- realtimePublisherModule,
13
- realtimeRoutesModule,
14
- realtimeDurableObjectModule,
1
+ import { realtimeAuthModule } from "./auth";
2
+ import { realtimeDurableObjectModule } from "./durable-object";
3
+ import { realtimePublisherModule } from "./publisher";
4
+ import { realtimeRoutesModule } from "./routes";
5
+ import { realtimeTypesModule } from "./types";
6
+ import { realtimeUtilsModule } from "./utils";
7
+
8
+ export const realtimeModule = [
9
+ realtimeTypesModule,
10
+ realtimeUtilsModule,
11
+ realtimeAuthModule,
12
+ realtimePublisherModule,
13
+ realtimeRoutesModule,
14
+ realtimeDurableObjectModule,
15
15
  ].join("\n\n");
@@ -1,102 +1,102 @@
1
- export const realtimePublisherModule = `
2
- async function publishMutationEvents(
3
- c: { req: { raw: Request }; env: Record<string, unknown> },
4
- options: RegisterHandlersOptions,
5
- mutationEvents: DbMutationEvent[],
6
- ): Promise<void> {
7
- if (mutationEvents.length === 0) {
8
- return;
9
- }
10
-
11
- const stub = getRealtimeStub(c.env, options);
12
- if (!stub) {
13
- return;
14
- }
15
-
16
- const subscriptionsResponse = await stub.fetch(
17
- new Request("https://realtime.internal/subscriptions", {
18
- method: "POST",
19
- headers: {
20
- "content-type": "application/json",
21
- },
22
- }),
23
- );
24
- if (!subscriptionsResponse.ok) {
25
- return;
26
- }
27
-
28
- const payload = (await subscriptionsResponse.json()) as {
29
- subscriptions?: RealtimeSubscription[];
30
- };
31
- const subscriptions = Array.isArray(payload.subscriptions)
32
- ? payload.subscriptions
33
- : [];
34
-
35
- for (const subscription of subscriptions) {
36
- const operation = (realtimeQueryHandlers as Record<
37
- string,
38
- {
39
- definition: {
40
- handler: (ctx: AppflareContext, args: unknown) => Promise<unknown> | unknown;
41
- };
42
- schema: z.ZodTypeAny;
43
- }
44
- >)[subscription.queryName];
45
- if (!operation) {
46
- continue;
47
- }
48
-
49
- const authSession = await validateAuthToken(
50
- c.req.raw,
51
- c.env,
52
- options,
53
- subscription.authToken,
54
- );
55
-
56
- const subscriberCtx = await createExecutionContext(c as never, options);
57
- subscriberCtx.user = authSession?.user as never;
58
- subscriberCtx.session = authSession?.session as never;
59
-
60
- try {
61
- const parsedArgs = operation.schema.parse(subscription.args);
62
- const matchPlan = await buildRealtimeQueryMatchPlan(
63
- operation.definition.handler,
64
- parsedArgs,
65
- {
66
- user: subscriberCtx.user,
67
- session: subscriberCtx.session,
68
- },
69
- );
70
- const shouldPublish = mutationEvents.some((event) => {
71
- return doesSubscriptionMatchMutation(matchPlan, event);
72
- });
73
- if (!shouldPublish) {
74
- continue;
75
- }
76
-
77
- const result = await operation.definition.handler(subscriberCtx, parsedArgs);
78
- const emitPayload: RealtimeEmitPayload = {
79
- token: subscription.token,
80
- event: "query:update",
81
- payload: {
82
- queryName: subscription.queryName,
83
- signature: subscription.signature,
84
- data: result,
85
- },
86
- };
87
-
88
- await stub.fetch(
89
- new Request("https://realtime.internal/emit", {
90
- method: "POST",
91
- headers: {
92
- "content-type": "application/json",
93
- },
94
- body: JSON.stringify(emitPayload),
95
- }),
96
- );
97
- } catch (error) {
98
- console.warn("Failed to publish realtime update", subscription.queryName, error);
99
- }
100
- }
101
- }
102
- `;
1
+ export const realtimePublisherModule = `
2
+ async function publishMutationEvents(
3
+ c: { req: { raw: Request }; env: Record<string, unknown> },
4
+ options: RegisterHandlersOptions,
5
+ mutationEvents: DbMutationEvent[],
6
+ ): Promise<void> {
7
+ if (mutationEvents.length === 0) {
8
+ return;
9
+ }
10
+
11
+ const stub = getRealtimeStub(c.env, options);
12
+ if (!stub) {
13
+ return;
14
+ }
15
+
16
+ const subscriptionsResponse = await stub.fetch(
17
+ new Request("https://realtime.internal/subscriptions", {
18
+ method: "POST",
19
+ headers: {
20
+ "content-type": "application/json",
21
+ },
22
+ }),
23
+ );
24
+ if (!subscriptionsResponse.ok) {
25
+ return;
26
+ }
27
+
28
+ const payload = (await subscriptionsResponse.json()) as {
29
+ subscriptions?: RealtimeSubscription[];
30
+ };
31
+ const subscriptions = Array.isArray(payload.subscriptions)
32
+ ? payload.subscriptions
33
+ : [];
34
+
35
+ for (const subscription of subscriptions) {
36
+ const operation = (realtimeQueryHandlers as Record<
37
+ string,
38
+ {
39
+ definition: {
40
+ handler: (ctx: AppflareContext, args: unknown) => Promise<unknown> | unknown;
41
+ };
42
+ schema: z.ZodTypeAny;
43
+ }
44
+ >)[subscription.queryName];
45
+ if (!operation) {
46
+ continue;
47
+ }
48
+
49
+ const authSession = await validateAuthToken(
50
+ c.req.raw,
51
+ c.env,
52
+ options,
53
+ subscription.authToken,
54
+ );
55
+
56
+ const subscriberCtx = await createExecutionContext(c as never, options);
57
+ subscriberCtx.user = authSession?.user as never;
58
+ subscriberCtx.session = authSession?.session as never;
59
+
60
+ try {
61
+ const parsedArgs = operation.schema.parse(subscription.args);
62
+ const matchPlan = await buildRealtimeQueryMatchPlan(
63
+ operation.definition.handler,
64
+ parsedArgs,
65
+ {
66
+ user: subscriberCtx.user,
67
+ session: subscriberCtx.session,
68
+ },
69
+ );
70
+ const shouldPublish = mutationEvents.some((event) => {
71
+ return doesSubscriptionMatchMutation(matchPlan, event);
72
+ });
73
+ if (!shouldPublish) {
74
+ continue;
75
+ }
76
+
77
+ const result = await operation.definition.handler(subscriberCtx, parsedArgs);
78
+ const emitPayload: RealtimeEmitPayload = {
79
+ token: subscription.token,
80
+ event: "query:update",
81
+ payload: {
82
+ queryName: subscription.queryName,
83
+ signature: subscription.signature,
84
+ data: result,
85
+ },
86
+ };
87
+
88
+ await stub.fetch(
89
+ new Request("https://realtime.internal/emit", {
90
+ method: "POST",
91
+ headers: {
92
+ "content-type": "application/json",
93
+ },
94
+ body: JSON.stringify(emitPayload),
95
+ }),
96
+ );
97
+ } catch (error) {
98
+ console.warn("Failed to publish realtime update", subscription.queryName, error);
99
+ }
100
+ }
101
+ }
102
+ `;