@base44-preview/sdk 0.8.35-pr.212.645b456 → 0.8.35-pr.212.daec52b
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/dist/modules/realtime.js +8 -17
- package/dist/realtime-handler.d.ts +10 -0
- package/dist/realtime-handler.js +10 -0
- package/package.json +1 -1
package/dist/modules/realtime.js
CHANGED
|
@@ -4,6 +4,10 @@ const activeSockets = new Map();
|
|
|
4
4
|
function socketKey(handlerName, instanceId) {
|
|
5
5
|
return `${handlerName}:${instanceId}`;
|
|
6
6
|
}
|
|
7
|
+
// partyserver maps binding names via camelCaseToKebabCase before routing
|
|
8
|
+
function toKebab(str) {
|
|
9
|
+
return str.replace(/[A-Z]/g, (l) => `-${l.toLowerCase()}`).replace(/^-/, "");
|
|
10
|
+
}
|
|
7
11
|
export function createRealtimeModule(config) {
|
|
8
12
|
return new Proxy({}, {
|
|
9
13
|
get(_, handlerName) {
|
|
@@ -13,16 +17,15 @@ export function createRealtimeModule(config) {
|
|
|
13
17
|
const key = socketKey(handlerName, instanceId);
|
|
14
18
|
// close existing if any
|
|
15
19
|
(_a = activeSockets.get(key)) === null || _a === void 0 ? void 0 : _a.close();
|
|
20
|
+
// query as async fn: called on every (re)connect, fetches a fresh token each time
|
|
21
|
+
// party must be kebab-case: partyserver maps binding names via camelCaseToKebabCase
|
|
16
22
|
const ws = new PartySocket({
|
|
17
23
|
host: config.dispatcherWsUrl,
|
|
18
|
-
party: handlerName,
|
|
24
|
+
party: toKebab(handlerName),
|
|
19
25
|
room: instanceId,
|
|
26
|
+
query: () => config.getToken(handlerName, instanceId).then((token) => ({ token })),
|
|
20
27
|
});
|
|
21
28
|
activeSockets.set(key, ws);
|
|
22
|
-
// Fetch token and attach on connect
|
|
23
|
-
config.getToken(handlerName, instanceId).then((token) => {
|
|
24
|
-
ws.updateProperties({ query: { token } });
|
|
25
|
-
});
|
|
26
29
|
ws.addEventListener("message", (ev) => {
|
|
27
30
|
try {
|
|
28
31
|
callback(JSON.parse(ev.data));
|
|
@@ -31,18 +34,6 @@ export function createRealtimeModule(config) {
|
|
|
31
34
|
// ignore malformed
|
|
32
35
|
}
|
|
33
36
|
});
|
|
34
|
-
// Re-fetch token on reconnect
|
|
35
|
-
ws.addEventListener("close", async () => {
|
|
36
|
-
if (activeSockets.get(key) !== ws)
|
|
37
|
-
return; // replaced
|
|
38
|
-
try {
|
|
39
|
-
const newToken = await config.getToken(handlerName, instanceId);
|
|
40
|
-
ws.updateProperties({ query: { token: newToken } });
|
|
41
|
-
}
|
|
42
|
-
catch (_a) {
|
|
43
|
-
// ignore token refresh failure
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
37
|
return () => {
|
|
47
38
|
activeSockets.delete(key);
|
|
48
39
|
ws.close();
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* At deploy time the bundler replaces this import with the compiled
|
|
9
9
|
* Cloudflare Durable Object implementation — this file provides types only.
|
|
10
10
|
*/
|
|
11
|
+
import type { Base44Client } from "./client.types.js";
|
|
11
12
|
export interface Conn {
|
|
12
13
|
userId: string;
|
|
13
14
|
appId: string;
|
|
@@ -15,13 +16,22 @@ export interface Conn {
|
|
|
15
16
|
send(data: unknown): void;
|
|
16
17
|
reject(code: number, reason: string): void;
|
|
17
18
|
}
|
|
19
|
+
export interface Storage {
|
|
20
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
21
|
+
put(key: string, value: unknown): Promise<void>;
|
|
22
|
+
delete(key: string): Promise<boolean>;
|
|
23
|
+
}
|
|
18
24
|
export declare abstract class RealtimeHandler<_State = unknown, Message = unknown> {
|
|
19
25
|
abstract handleConnect(conn: Conn): void | Promise<void>;
|
|
20
26
|
abstract handleMessage(conn: Conn, msg: Message): void | Promise<void>;
|
|
21
27
|
abstract handleClose(conn: Conn): void | Promise<void>;
|
|
22
28
|
abstract handleTick(): void | Promise<void>;
|
|
29
|
+
onStart(): void | Promise<void>;
|
|
23
30
|
protected broadcast(_data: unknown): void;
|
|
24
31
|
protected getConnections(): Conn[];
|
|
25
32
|
protected startLoop(_ms: number): Promise<void>;
|
|
26
33
|
protected stopLoop(): Promise<void>;
|
|
34
|
+
protected get instanceId(): string;
|
|
35
|
+
protected get storage(): Storage;
|
|
36
|
+
protected createServiceClient(): Base44Client;
|
|
27
37
|
}
|
package/dist/realtime-handler.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* Cloudflare Durable Object implementation — this file provides types only.
|
|
10
10
|
*/
|
|
11
11
|
export class RealtimeHandler {
|
|
12
|
+
onStart() { }
|
|
12
13
|
broadcast(_data) {
|
|
13
14
|
throw new Error("RealtimeHandler.broadcast() is only available inside a deployed handler");
|
|
14
15
|
}
|
|
@@ -21,4 +22,13 @@ export class RealtimeHandler {
|
|
|
21
22
|
stopLoop() {
|
|
22
23
|
throw new Error("RealtimeHandler.stopLoop() is only available inside a deployed handler");
|
|
23
24
|
}
|
|
25
|
+
get instanceId() {
|
|
26
|
+
throw new Error("RealtimeHandler.instanceId is only available inside a deployed handler");
|
|
27
|
+
}
|
|
28
|
+
get storage() {
|
|
29
|
+
throw new Error("RealtimeHandler.storage is only available inside a deployed handler");
|
|
30
|
+
}
|
|
31
|
+
createServiceClient() {
|
|
32
|
+
throw new Error("RealtimeHandler.createServiceClient() is only available inside a deployed handler");
|
|
33
|
+
}
|
|
24
34
|
}
|