@base44-preview/sdk 0.8.35-pr.212.645b456 → 0.8.35-pr.212.c335c63
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 +32 -14
- 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
|
@@ -13,37 +13,55 @@ export function createRealtimeModule(config) {
|
|
|
13
13
|
const key = socketKey(handlerName, instanceId);
|
|
14
14
|
// close existing if any
|
|
15
15
|
(_a = activeSockets.get(key)) === null || _a === void 0 ? void 0 : _a.close();
|
|
16
|
+
// query as async fn: called on every (re)connect, fetches a fresh token each time
|
|
16
17
|
const ws = new PartySocket({
|
|
17
18
|
host: config.dispatcherWsUrl,
|
|
18
19
|
party: handlerName,
|
|
19
20
|
room: instanceId,
|
|
21
|
+
query: () => config.getToken(handlerName, instanceId).then((token) => ({ token })),
|
|
20
22
|
});
|
|
21
23
|
activeSockets.set(key, ws);
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
// Heartbeat / half-open detection. PartySocket only reconnects on a
|
|
25
|
+
// browser close/error event, so a silently-dead connection (TCP alive,
|
|
26
|
+
// no data — common behind proxies/LBs) hangs until the OS idle timeout
|
|
27
|
+
// (~60s). We ping periodically and force a reconnect if nothing comes
|
|
28
|
+
// back within DEAD_MS, cutting detection from ~60s to a few seconds.
|
|
29
|
+
// Pairs with the handler's setWebSocketAutoResponse("__ping"→"__pong"),
|
|
30
|
+
// so idle handlers (no app broadcasts) still keep the connection proven.
|
|
31
|
+
const PING_MS = 5000;
|
|
32
|
+
const DEAD_MS = 12000;
|
|
33
|
+
let lastMsg = Date.now();
|
|
34
|
+
const bumpAlive = () => { lastMsg = Date.now(); };
|
|
35
|
+
ws.addEventListener("open", bumpAlive);
|
|
26
36
|
ws.addEventListener("message", (ev) => {
|
|
37
|
+
bumpAlive();
|
|
38
|
+
let data;
|
|
27
39
|
try {
|
|
28
|
-
|
|
40
|
+
data = JSON.parse(ev.data);
|
|
29
41
|
}
|
|
30
42
|
catch (_a) {
|
|
31
|
-
// ignore malformed
|
|
43
|
+
return; // ignore malformed
|
|
32
44
|
}
|
|
45
|
+
// Swallow heartbeat acks — never surface them to the app.
|
|
46
|
+
if (data && typeof data === "object" && data.type === "__pong")
|
|
47
|
+
return;
|
|
48
|
+
callback(data);
|
|
33
49
|
});
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
const heartbeat = setInterval(() => {
|
|
51
|
+
if (Date.now() - lastMsg > DEAD_MS) {
|
|
52
|
+
bumpAlive(); // avoid a reconnect storm while the new socket comes up
|
|
53
|
+
ws.reconnect();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
38
56
|
try {
|
|
39
|
-
|
|
40
|
-
ws.updateProperties({ query: { token: newToken } });
|
|
57
|
+
ws.send(JSON.stringify({ type: "__ping" }));
|
|
41
58
|
}
|
|
42
59
|
catch (_a) {
|
|
43
|
-
//
|
|
60
|
+
// socket not open; the watchdog above will force a reconnect
|
|
44
61
|
}
|
|
45
|
-
});
|
|
62
|
+
}, PING_MS);
|
|
46
63
|
return () => {
|
|
64
|
+
clearInterval(heartbeat);
|
|
47
65
|
activeSockets.delete(key);
|
|
48
66
|
ws.close();
|
|
49
67
|
};
|
|
@@ -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
|
}
|