appflare 0.1.0 → 0.1.11
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/cli/templates/core/client-modules/handlers/index.ts +12 -5
- package/cli/templates/handlers/README.md +88 -0
- package/cli/templates/handlers/auth.ts +2 -1
- package/cli/templates/handlers/generators/registration/modules/cron.ts +30 -0
- package/cli/templates/handlers/generators/registration/modules/realtime/auth.ts +75 -0
- package/cli/templates/handlers/generators/registration/modules/realtime/durable-object.ts +144 -0
- package/cli/templates/handlers/generators/registration/modules/realtime/index.ts +15 -0
- package/cli/templates/handlers/generators/registration/modules/realtime/publisher.ts +105 -0
- package/cli/templates/handlers/generators/registration/modules/realtime/routes.ts +171 -0
- package/cli/templates/handlers/generators/registration/modules/realtime/types.ts +30 -0
- package/cli/templates/handlers/generators/registration/modules/realtime/utils.ts +516 -0
- package/cli/templates/handlers/generators/registration/modules/scheduler.ts +56 -0
- package/cli/templates/handlers/generators/registration/modules/storage.ts +194 -0
- package/cli/templates/handlers/generators/registration/sections.ts +210 -0
- package/cli/templates/handlers/generators/types/context.ts +64 -0
- package/cli/templates/handlers/generators/types/core.ts +101 -0
- package/cli/templates/handlers/generators/types/operations.ts +135 -0
- package/cli/templates/handlers/generators/types/query-definitions.ts +1029 -0
- package/cli/templates/handlers/generators/types/query-runtime.ts +417 -0
- package/cli/templates/handlers/registration.ts +32 -1063
- package/cli/templates/handlers/types.ts +13 -958
- package/package.json +1 -1
|
@@ -230,11 +230,14 @@ function renderRouteFactory(operation: HttpOperation): string {
|
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
if (!resolvedAuthToken) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
"authToken is required for realtime subscribe (pass options.authToken or Authorization Bearer header)",
|
|
236
|
-
),
|
|
233
|
+
const authError = new Error(
|
|
234
|
+
"authToken is required for realtime subscribe (pass options.authToken or Authorization Bearer header)",
|
|
237
235
|
);
|
|
236
|
+
if (onError) {
|
|
237
|
+
onError(authError);
|
|
238
|
+
} else {
|
|
239
|
+
console.warn("[appflare:subscribe]", authError.message);
|
|
240
|
+
}
|
|
238
241
|
return;
|
|
239
242
|
}
|
|
240
243
|
|
|
@@ -295,7 +298,11 @@ function renderRouteFactory(operation: HttpOperation): string {
|
|
|
295
298
|
onError?.(new Error("Realtime websocket error"));
|
|
296
299
|
});
|
|
297
300
|
} catch (error) {
|
|
298
|
-
onError
|
|
301
|
+
if (onError) {
|
|
302
|
+
onError(error);
|
|
303
|
+
} else {
|
|
304
|
+
console.warn("[appflare:subscribe] Subscription failed:", error);
|
|
305
|
+
}
|
|
299
306
|
}
|
|
300
307
|
})();
|
|
301
308
|
|
|
@@ -191,6 +191,94 @@ Execution contexts store these as `ctx.mutationEvents`, and mutation routes call
|
|
|
191
191
|
|
|
192
192
|
---
|
|
193
193
|
|
|
194
|
+
## DB aggregate helpers
|
|
195
|
+
|
|
196
|
+
Generated `ctx.db.<table>` wrappers now include aggregate helpers:
|
|
197
|
+
|
|
198
|
+
- `count(args?)`
|
|
199
|
+
- `where?: WhereInput<TModel>`
|
|
200
|
+
- `field?: keyof TModel | string` (supports relation paths, e.g. `comments.id`)
|
|
201
|
+
- `distinct?: boolean`
|
|
202
|
+
- `with?: QueryWithInput<...>`
|
|
203
|
+
- returns `Promise<number>`
|
|
204
|
+
- `avg(args)`
|
|
205
|
+
- `where?: WhereInput<TModel>`
|
|
206
|
+
- `field: NumericFieldKey<TModel> | string` (supports relation paths, e.g. `comments.id`)
|
|
207
|
+
- `distinct?: boolean`
|
|
208
|
+
- `with?: QueryWithInput<...>`
|
|
209
|
+
- returns `Promise<number | null>`
|
|
210
|
+
|
|
211
|
+
Aggregate behavior with `with`:
|
|
212
|
+
|
|
213
|
+
- Relation `with.where` and nested `with` filters are treated as parent-row constraints for aggregates (EXISTS-style).
|
|
214
|
+
- For `count` with `with`, `distinct` defaults to `true` when `field` is provided.
|
|
215
|
+
- Nested relation paths are supported recursively for both `count` and `avg`.
|
|
216
|
+
|
|
217
|
+
Relation `with` aggregates on `findMany`/`findFirst`:
|
|
218
|
+
|
|
219
|
+
- You can request per-parent relation aggregates directly inside `with` using `_count` and `_avg`.
|
|
220
|
+
- Result rows include a sibling `<relationName>Aggregate` object.
|
|
221
|
+
- `_avg` returns `0` for parents with no related rows.
|
|
222
|
+
|
|
223
|
+
Example:
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
const total = await ctx.db.posts.count({
|
|
227
|
+
where: { ownerId: user.id },
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const uniqueOwners = await ctx.db.posts.count({
|
|
231
|
+
field: "ownerId",
|
|
232
|
+
distinct: true,
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const averageId = await ctx.db.posts.avg({
|
|
236
|
+
field: "id",
|
|
237
|
+
where: { ownerId: user.id },
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
const postsWithMatchingComments = await ctx.db.posts.count({
|
|
241
|
+
with: {
|
|
242
|
+
comments: {
|
|
243
|
+
where: {
|
|
244
|
+
id: { gte: 10000 },
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const averageCommentId = await ctx.db.posts.avg({
|
|
251
|
+
field: "comments.id",
|
|
252
|
+
with: {
|
|
253
|
+
comments: {
|
|
254
|
+
where: {
|
|
255
|
+
id: { gte: 10000 },
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const postsWithCommentStats = await ctx.db.posts.findMany({
|
|
262
|
+
with: {
|
|
263
|
+
comments: {
|
|
264
|
+
_count: true,
|
|
265
|
+
_avg: {
|
|
266
|
+
id: true,
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const firstPostCommentCount =
|
|
273
|
+
postsWithCommentStats[0]?.commentsAggregate.count ?? 0;
|
|
274
|
+
const firstPostAverageCommentId =
|
|
275
|
+
postsWithCommentStats[0]?.commentsAggregate.avg.id ?? 0;
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
`geoWithin.latitudeField` and `geoWithin.longitudeField` are now typed to table keys (instead of free-form strings). Invalid field names still no-op the geo filter at runtime, but now emit a warning.
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
194
282
|
## Durable Object responsibilities
|
|
195
283
|
|
|
196
284
|
`AppflareRealtimeDurableObject` keeps in-memory maps for:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export function generateAuth(): string {
|
|
2
2
|
return `
|
|
3
3
|
|
|
4
|
+
import { getHeaders } from "./server";
|
|
4
5
|
export async function resolveSession(
|
|
5
6
|
\trequest: Request,
|
|
6
7
|
\tdatabase: D1Database,
|
|
@@ -17,7 +18,7 @@ export async function resolveSession(
|
|
|
17
18
|
|
|
18
19
|
\ttry {
|
|
19
20
|
\t\tconst session = await auth.api.getSession({
|
|
20
|
-
\t\t\theaders: request.headers,
|
|
21
|
+
\t\t\theaders: getHeaders(request.headers),
|
|
21
22
|
\t\t});
|
|
22
23
|
|
|
23
24
|
\t\treturn {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const cronModule = `
|
|
2
|
+
export async function executeCronTriggers(
|
|
3
|
+
controller: { cron: string },
|
|
4
|
+
env: Record<string, unknown>,
|
|
5
|
+
options: RegisterHandlersOptions,
|
|
6
|
+
): Promise<void> {
|
|
7
|
+
const cronValue = controller?.cron;
|
|
8
|
+
if (!cronValue) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (cronHandlers.length === 0) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const ctx = createSchedulerExecutionContext(env, options);
|
|
17
|
+
|
|
18
|
+
for (const cronEntry of cronHandlers) {
|
|
19
|
+
if (!cronEntry.cronTriggers.includes(cronValue)) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
await cronEntry.definition.handler(ctx);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error("Cron task failed", cronEntry.taskName, error);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export const realtimeAuthModule = `
|
|
2
|
+
function getRealtimeStub(
|
|
3
|
+
env: Record<string, unknown>,
|
|
4
|
+
options: RegisterHandlersOptions,
|
|
5
|
+
): RealtimeStub | null {
|
|
6
|
+
const binding = options.realtimeBinding ?? "APPFLARE_REALTIME";
|
|
7
|
+
const namespace = env[binding] as RealtimeDurableObjectNamespace | undefined;
|
|
8
|
+
if (!namespace) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const objectName = options.realtimeObjectName ?? "global";
|
|
13
|
+
const objectId = namespace.idFromName(objectName);
|
|
14
|
+
return namespace.get(objectId);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function validateAuthToken(
|
|
18
|
+
request: Request,
|
|
19
|
+
env: Record<string, unknown>,
|
|
20
|
+
options: RegisterHandlersOptions,
|
|
21
|
+
authToken: string,
|
|
22
|
+
): Promise<{ user: unknown; session: unknown } | null> {
|
|
23
|
+
const database = env[options.databaseBinding] as D1Database | undefined;
|
|
24
|
+
if (!database) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const kvNamespace = options.kvBinding
|
|
29
|
+
? (env[options.kvBinding] as KVNamespace | undefined)
|
|
30
|
+
: undefined;
|
|
31
|
+
const headers = new Headers(request.headers);
|
|
32
|
+
headers.set("authorization", "Bearer " + authToken);
|
|
33
|
+
headers.delete("upgrade");
|
|
34
|
+
headers.delete("connection");
|
|
35
|
+
for (const key of [...headers.keys()]) {
|
|
36
|
+
if (key.toLowerCase().startsWith("sec-websocket")) {
|
|
37
|
+
headers.delete(key);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const tokenRequest = new Request(request.url, {
|
|
41
|
+
method: "GET",
|
|
42
|
+
headers,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const session = await resolveSession(
|
|
46
|
+
tokenRequest,
|
|
47
|
+
database,
|
|
48
|
+
kvNamespace,
|
|
49
|
+
request.cf as IncomingRequestCfProperties | undefined,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
if (!session?.user) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return session;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function extractUserId(user: unknown): string {
|
|
60
|
+
if (!isRecord(user)) {
|
|
61
|
+
return "unknown";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const id = user.id;
|
|
65
|
+
return typeof id === "string" && id.length > 0 ? id : "unknown";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function buildRealtimeWsUrl(requestUrl: string, websocketPath: string): string {
|
|
69
|
+
const url = new URL(requestUrl);
|
|
70
|
+
url.pathname = websocketPath;
|
|
71
|
+
url.search = "";
|
|
72
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
73
|
+
return url.toString();
|
|
74
|
+
}
|
|
75
|
+
`;
|
|
@@ -0,0 +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 || !payload.authToken) {
|
|
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 || !authToken) {
|
|
47
|
+
return new Response(
|
|
48
|
+
JSON.stringify({ message: "token and authToken are 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 || !authToken || subscription.authToken !== authToken) {
|
|
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
|
+
`;
|
|
@@ -0,0 +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,
|
|
15
|
+
].join("\n\n");
|
|
@@ -0,0 +1,105 @@
|
|
|
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
|
+
if (!authSession) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const subscriberCtx = await createExecutionContext(c as never, options);
|
|
60
|
+
subscriberCtx.user = authSession.user as never;
|
|
61
|
+
subscriberCtx.session = authSession.session as never;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const parsedArgs = operation.schema.parse(subscription.args);
|
|
65
|
+
const matchPlan = await buildRealtimeQueryMatchPlan(
|
|
66
|
+
operation.definition.handler,
|
|
67
|
+
parsedArgs,
|
|
68
|
+
{
|
|
69
|
+
user: subscriberCtx.user,
|
|
70
|
+
session: subscriberCtx.session,
|
|
71
|
+
},
|
|
72
|
+
);
|
|
73
|
+
const shouldPublish = mutationEvents.some((event) => {
|
|
74
|
+
return doesSubscriptionMatchMutation(matchPlan, event);
|
|
75
|
+
});
|
|
76
|
+
if (!shouldPublish) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const result = await operation.definition.handler(subscriberCtx, parsedArgs);
|
|
81
|
+
const emitPayload: RealtimeEmitPayload = {
|
|
82
|
+
token: subscription.token,
|
|
83
|
+
event: "query:update",
|
|
84
|
+
payload: {
|
|
85
|
+
queryName: subscription.queryName,
|
|
86
|
+
signature: subscription.signature,
|
|
87
|
+
data: result,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
await stub.fetch(
|
|
92
|
+
new Request("https://realtime.internal/emit", {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: {
|
|
95
|
+
"content-type": "application/json",
|
|
96
|
+
},
|
|
97
|
+
body: JSON.stringify(emitPayload),
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.warn("Failed to publish realtime update", subscription.queryName, error);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
`;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
export const realtimeRoutesModule = `
|
|
2
|
+
function registerRealtimeRoutes(
|
|
3
|
+
app: Hono<WorkerEnv>,
|
|
4
|
+
options: RegisterHandlersOptions,
|
|
5
|
+
): void {
|
|
6
|
+
const subscribePath = options.realtimeSubscribePath ?? "/realtime/subscribe";
|
|
7
|
+
const unsubscribePath = "/realtime/unsubscribe";
|
|
8
|
+
const websocketPath = options.realtimeWebsocketPath ?? "/realtime/ws";
|
|
9
|
+
const protocol = options.realtimeProtocol ?? "appflare.realtime.v1";
|
|
10
|
+
|
|
11
|
+
app.post(subscribePath, async (c) => {
|
|
12
|
+
const body = await c.req.json().catch(() => ({} as Record<string, unknown>));
|
|
13
|
+
const queryName = typeof body.queryName === "string" ? body.queryName : "";
|
|
14
|
+
const authToken = typeof body.authToken === "string" ? body.authToken : "";
|
|
15
|
+
const rawArgs = isRecord(body.args) ? body.args : {};
|
|
16
|
+
|
|
17
|
+
if (!queryName || !authToken) {
|
|
18
|
+
return c.json({ message: "queryName and authToken are required" }, 400);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const operation = (realtimeQueryHandlers as Record<
|
|
22
|
+
string,
|
|
23
|
+
{
|
|
24
|
+
definition: unknown;
|
|
25
|
+
schema: z.ZodTypeAny;
|
|
26
|
+
}
|
|
27
|
+
>)[queryName as RealtimeQueryName];
|
|
28
|
+
if (!operation) {
|
|
29
|
+
return c.json({ message: "Unknown queryName" }, 404);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const authSession = await validateAuthToken(
|
|
33
|
+
c.req.raw,
|
|
34
|
+
c.env,
|
|
35
|
+
options,
|
|
36
|
+
authToken,
|
|
37
|
+
);
|
|
38
|
+
if (!authSession) {
|
|
39
|
+
return c.json({ message: "Invalid auth token" }, 401);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let args: Record<string, unknown>;
|
|
43
|
+
try {
|
|
44
|
+
args = operation.schema.parse(rawArgs) as Record<string, unknown>;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (error instanceof ZodError) {
|
|
47
|
+
return c.json({ message: "Invalid query args", issues: error.issues }, 400);
|
|
48
|
+
}
|
|
49
|
+
return c.json({ message: "Invalid query args" }, 400);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const token = crypto.randomUUID();
|
|
53
|
+
const signature = createSubscriptionSignature(queryName, args);
|
|
54
|
+
const stub = getRealtimeStub(c.env, options);
|
|
55
|
+
if (!stub) {
|
|
56
|
+
return c.json({ message: "Realtime binding is not configured" }, 500);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await stub.fetch(
|
|
60
|
+
new Request("https://realtime.internal/subscribe", {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: {
|
|
63
|
+
"content-type": "application/json",
|
|
64
|
+
},
|
|
65
|
+
body: JSON.stringify({
|
|
66
|
+
token,
|
|
67
|
+
signature,
|
|
68
|
+
queryName,
|
|
69
|
+
args,
|
|
70
|
+
authToken,
|
|
71
|
+
userId: extractUserId(authSession.user),
|
|
72
|
+
createdAt: Date.now(),
|
|
73
|
+
}),
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const websocketUrl = buildRealtimeWsUrl(c.req.raw.url, websocketPath);
|
|
78
|
+
return c.json(
|
|
79
|
+
{
|
|
80
|
+
token,
|
|
81
|
+
signature,
|
|
82
|
+
websocket: {
|
|
83
|
+
url: websocketUrl,
|
|
84
|
+
protocol,
|
|
85
|
+
params: {
|
|
86
|
+
tokenParam: "token",
|
|
87
|
+
authTokenParam: "authToken",
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
200,
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
app.post(unsubscribePath, async (c) => {
|
|
96
|
+
const body = await c.req.json().catch(() => ({} as Record<string, unknown>));
|
|
97
|
+
const token = typeof body.token === "string" ? body.token : "";
|
|
98
|
+
const authToken = typeof body.authToken === "string" ? body.authToken : "";
|
|
99
|
+
|
|
100
|
+
if (!token || !authToken) {
|
|
101
|
+
return c.json({ message: "token and authToken are required" }, 400);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const authSession = await validateAuthToken(
|
|
105
|
+
c.req.raw,
|
|
106
|
+
c.env,
|
|
107
|
+
options,
|
|
108
|
+
authToken,
|
|
109
|
+
);
|
|
110
|
+
if (!authSession) {
|
|
111
|
+
return c.json({ message: "Invalid auth token" }, 401);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const stub = getRealtimeStub(c.env, options);
|
|
115
|
+
if (!stub) {
|
|
116
|
+
return c.json({ message: "Realtime binding is not configured" }, 500);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const response = await stub.fetch(
|
|
120
|
+
new Request("https://realtime.internal/unsubscribe", {
|
|
121
|
+
method: "POST",
|
|
122
|
+
headers: {
|
|
123
|
+
"content-type": "application/json",
|
|
124
|
+
},
|
|
125
|
+
body: JSON.stringify({
|
|
126
|
+
token,
|
|
127
|
+
authToken,
|
|
128
|
+
}),
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
const payload = (await response.json().catch(() => null)) as {
|
|
134
|
+
message?: string;
|
|
135
|
+
} | null;
|
|
136
|
+
return c.json(
|
|
137
|
+
{ message: payload?.message ?? "Unable to remove subscription" },
|
|
138
|
+
response.status,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return c.json({ ok: true }, 200);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
app.get(websocketPath, async (c) => {
|
|
146
|
+
const token = c.req.query("token") ?? "";
|
|
147
|
+
const authToken = c.req.query("authToken") ?? "";
|
|
148
|
+
|
|
149
|
+
if (!token || !authToken) {
|
|
150
|
+
return c.json({ message: "token and authToken are required" }, 400);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const authSession = await validateAuthToken(
|
|
154
|
+
c.req.raw,
|
|
155
|
+
c.env,
|
|
156
|
+
options,
|
|
157
|
+
authToken,
|
|
158
|
+
);
|
|
159
|
+
if (!authSession) {
|
|
160
|
+
return c.json({ message: "Invalid auth token" }, 401);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const stub = getRealtimeStub(c.env, options);
|
|
164
|
+
if (!stub) {
|
|
165
|
+
return c.json({ message: "Realtime binding is not configured" }, 500);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return stub.fetch(c.req.raw);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
`;
|