appflare 0.1.1 → 0.1.12
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/wrangler.ts +1 -1
- 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/package.json +1 -1
- package/cli/templates/handlers/generators/registration/modules/realtime.ts +0 -631
|
@@ -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
|
+
`;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const realtimeTypesModule = `
|
|
2
|
+
type RealtimeSubscription = {
|
|
3
|
+
token: string;
|
|
4
|
+
signature: string;
|
|
5
|
+
queryName: string;
|
|
6
|
+
args: Record<string, unknown>;
|
|
7
|
+
authToken: string;
|
|
8
|
+
userId: string;
|
|
9
|
+
createdAt: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type RealtimeEmitPayload = {
|
|
13
|
+
token: string;
|
|
14
|
+
event: string;
|
|
15
|
+
payload: unknown;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type RealtimeStub = {
|
|
19
|
+
fetch: (request: Request) => Promise<Response>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type RealtimeDurableObjectNamespace = {
|
|
23
|
+
idFromName: (name: string) => unknown;
|
|
24
|
+
get: (id: unknown) => RealtimeStub;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type RealtimeQueryName = keyof typeof realtimeQueryHandlers extends never
|
|
28
|
+
? string
|
|
29
|
+
: Extract<keyof typeof realtimeQueryHandlers, string>;
|
|
30
|
+
`;
|