@rebasepro/client 0.34.0 → 0.34.2
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/index.d.ts +1 -0
- package/dist/index.es.js +94 -10
- package/dist/index.es.js.map +1 -1
- package/dist/websocket.d.ts +48 -0
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -39,6 +39,7 @@ export type { ApiKeyKind, ApiKeyMasked, ApiKeyWithSecret, CreateApiKeyRequest, C
|
|
|
39
39
|
export type { FunctionInvokeOptions, FunctionsClient } from "./functions.js";
|
|
40
40
|
export type { PushClient, PushSubscribeOptions, PushRegistrationOptions } from "./push.js";
|
|
41
41
|
export { RebaseWebSocketClient } from "./websocket.js";
|
|
42
|
+
export type { RealtimeDisconnectInfo, RealtimeDisconnectCause } from "./websocket.js";
|
|
42
43
|
export { RebaseRealtimeChannel } from "./realtime-channel.js";
|
|
43
44
|
export type { PresenceState, PresenceDiff, BroadcastEvent, ChannelTransport, ChannelOptions, ChannelHistoryEntry, ChannelHistoryResult } from "./realtime-channel.js";
|
|
44
45
|
export type { ConflictResolution, OfflineApi, OfflineConfig, OfflineQueueOptions, OfflineStatus } from "./offline.js";
|
package/dist/index.es.js
CHANGED
|
@@ -2821,7 +2821,8 @@ var SOCKET_OPT_IN_PARAM = "rebase_caps";
|
|
|
2821
2821
|
var SOCKET_OPT_INS = [
|
|
2822
2822
|
"heartbeat",
|
|
2823
2823
|
"channel_seq",
|
|
2824
|
-
"cancel_sql"
|
|
2824
|
+
"cancel_sql",
|
|
2825
|
+
"ping"
|
|
2825
2826
|
];
|
|
2826
2827
|
/**
|
|
2827
2828
|
* The query parameter naming the group a socket belongs to. A platform that
|
|
@@ -2832,6 +2833,19 @@ var SOCKET_AFFINITY_PARAM = "affinity";
|
|
|
2832
2833
|
/** Slack on top of two heartbeat intervals before a silent socket is given up on. */
|
|
2833
2834
|
var HEARTBEAT_GRACE_MS = 5e3;
|
|
2834
2835
|
/**
|
|
2836
|
+
* How long a socket has to answer the `ping` sent when the page wakes, in ms,
|
|
2837
|
+
* before it is given up on. Long enough for a phone's radio to come back.
|
|
2838
|
+
*/
|
|
2839
|
+
var WAKE_PROBE_TIMEOUT_MS = 1e4;
|
|
2840
|
+
/** The code and reason of a `close` event, read without trusting its shape. */
|
|
2841
|
+
function closeEventDetails(event) {
|
|
2842
|
+
if (typeof event !== "object" || event === null) return {};
|
|
2843
|
+
return {
|
|
2844
|
+
code: "code" in event && typeof event.code === "number" ? event.code : void 0,
|
|
2845
|
+
reason: "reason" in event && typeof event.reason === "string" && event.reason !== "" ? event.reason : void 0
|
|
2846
|
+
};
|
|
2847
|
+
}
|
|
2848
|
+
/**
|
|
2835
2849
|
* `url` with the opt-ins named on it, and `affinity` when there is one.
|
|
2836
2850
|
* Appended rather than rebuilt through `URL`, which would rewrite the address
|
|
2837
2851
|
* the caller gave (a bare origin gains a `/`).
|
|
@@ -3034,7 +3048,16 @@ var RebaseWebSocketClient = class {
|
|
|
3034
3048
|
serverClientId;
|
|
3035
3049
|
/** When the current socket last delivered anything, heartbeats included. */
|
|
3036
3050
|
lastFrameAt = 0;
|
|
3051
|
+
/** When the current socket opened, for the `disconnect` event. */
|
|
3052
|
+
socketOpenedAt = 0;
|
|
3037
3053
|
livenessTimer = null;
|
|
3054
|
+
/** The current wake probe: when it was sent, and when it must have been answered by. */
|
|
3055
|
+
wakeProbe = null;
|
|
3056
|
+
/**
|
|
3057
|
+
* Why this client is about to close the current socket itself, read by its
|
|
3058
|
+
* close handler so the `disconnect` event names the cause.
|
|
3059
|
+
*/
|
|
3060
|
+
closingFor = null;
|
|
3038
3061
|
/** Give up on the current socket as though it had closed. Set per socket. */
|
|
3039
3062
|
dropSocket = null;
|
|
3040
3063
|
/** Whether the server agreed, in its `hello`, to `name` for this socket. */
|
|
@@ -3211,7 +3234,9 @@ var RebaseWebSocketClient = class {
|
|
|
3211
3234
|
window.addEventListener("online", this.onlineListener);
|
|
3212
3235
|
if (typeof document !== "undefined" && typeof document.addEventListener === "function") {
|
|
3213
3236
|
this.visibilityListener = () => {
|
|
3214
|
-
if (document.visibilityState
|
|
3237
|
+
if (document.visibilityState !== "visible") return;
|
|
3238
|
+
this.retryNow("Tab is visible again");
|
|
3239
|
+
this.probeAfterWake();
|
|
3215
3240
|
};
|
|
3216
3241
|
document.addEventListener("visibilitychange", this.visibilityListener);
|
|
3217
3242
|
}
|
|
@@ -3413,6 +3438,8 @@ var RebaseWebSocketClient = class {
|
|
|
3413
3438
|
this.hadConnection = true;
|
|
3414
3439
|
this.isConnected = true;
|
|
3415
3440
|
this.gaveUp = false;
|
|
3441
|
+
this.socketOpenedAt = Date.now();
|
|
3442
|
+
this.lastFrameAt = this.socketOpenedAt;
|
|
3416
3443
|
if (!this.outageReported) this.setState("connected");
|
|
3417
3444
|
this.refusedSession = null;
|
|
3418
3445
|
if (this.getAuthToken && !this.isAuthenticated) {
|
|
@@ -3431,6 +3458,7 @@ var RebaseWebSocketClient = class {
|
|
|
3431
3458
|
if (!(token && this.noteSessionRefused(token, error))) {
|
|
3432
3459
|
this.connectionDebug("WebSocket connected, not signed in yet:", error?.message || error);
|
|
3433
3460
|
if (token && this.ws === socket && !this.refusedSession) {
|
|
3461
|
+
this.closingFor = "client";
|
|
3434
3462
|
socket.close(4e3, "Could not authenticate; reconnecting");
|
|
3435
3463
|
return;
|
|
3436
3464
|
}
|
|
@@ -3457,8 +3485,17 @@ var RebaseWebSocketClient = class {
|
|
|
3457
3485
|
console.error("Error parsing WebSocket message:", error);
|
|
3458
3486
|
}
|
|
3459
3487
|
};
|
|
3460
|
-
const onClosed = () => {
|
|
3488
|
+
const onClosed = (event, cause) => {
|
|
3461
3489
|
this.connectionDebug("Disconnected from PostgreSQL backend");
|
|
3490
|
+
const now = Date.now();
|
|
3491
|
+
const info = {
|
|
3492
|
+
...closeEventDetails(event),
|
|
3493
|
+
cause: cause ?? this.closingFor ?? "server",
|
|
3494
|
+
openForMs: this.socketOpenedAt ? now - this.socketOpenedAt : 0,
|
|
3495
|
+
silentForMs: this.lastFrameAt ? now - this.lastFrameAt : 0
|
|
3496
|
+
};
|
|
3497
|
+
this.closingFor = null;
|
|
3498
|
+
this.socketOpenedAt = 0;
|
|
3462
3499
|
this.stopLivenessWatch();
|
|
3463
3500
|
this.forgetServerOptIns();
|
|
3464
3501
|
if (this.ws === socket) this.ws = null;
|
|
@@ -3466,7 +3503,7 @@ var RebaseWebSocketClient = class {
|
|
|
3466
3503
|
this.isAuthenticated = false;
|
|
3467
3504
|
this.authPromise = null;
|
|
3468
3505
|
this.suspendSubscribeWatchdogs();
|
|
3469
|
-
this.emit("disconnect");
|
|
3506
|
+
this.emit("disconnect", info);
|
|
3470
3507
|
for (const [reqId, request] of this.pendingRequests.entries()) {
|
|
3471
3508
|
this.pendingRequests.delete(reqId);
|
|
3472
3509
|
if (reqId.startsWith("auth_")) request.reject(/* @__PURE__ */ new Error("Connection closed during authentication"));
|
|
@@ -3476,16 +3513,16 @@ var RebaseWebSocketClient = class {
|
|
|
3476
3513
|
this.attemptReconnect();
|
|
3477
3514
|
};
|
|
3478
3515
|
this.ws.onclose = onClosed;
|
|
3479
|
-
this.dropSocket = () => {
|
|
3516
|
+
this.dropSocket = (cause) => {
|
|
3480
3517
|
if (this.ws !== socket) return;
|
|
3481
3518
|
socket.onclose = null;
|
|
3482
3519
|
socket.onmessage = null;
|
|
3483
3520
|
socket.onerror = null;
|
|
3484
3521
|
socket.onopen = null;
|
|
3485
3522
|
try {
|
|
3486
|
-
socket.close();
|
|
3523
|
+
socket.close(4003, cause === "probe" ? "No answer to a ping after waking" : "Two heartbeats missed");
|
|
3487
3524
|
} catch {}
|
|
3488
|
-
onClosed();
|
|
3525
|
+
onClosed(void 0, cause);
|
|
3489
3526
|
};
|
|
3490
3527
|
this.ws.onerror = (error) => {
|
|
3491
3528
|
console.error("WebSocket error:", error);
|
|
@@ -3508,25 +3545,72 @@ var RebaseWebSocketClient = class {
|
|
|
3508
3545
|
/**
|
|
3509
3546
|
* Redial when two heartbeats in a row have not arrived. Anything the
|
|
3510
3547
|
* server sends counts as a beat, so a busy socket is never judged on them.
|
|
3548
|
+
*
|
|
3549
|
+
* Never on a silence the page caused. A phone that locked, a tab the
|
|
3550
|
+
* browser froze or a laptop that slept runs no timers and reads no frames,
|
|
3551
|
+
* and on waking this interval used to fire before the frames waiting on
|
|
3552
|
+
* the socket were read: it saw a minute of silence and tore down a socket
|
|
3553
|
+
* that was fine, and every app on it saw a disconnect. A tick that comes
|
|
3554
|
+
* far later than it was due is a wake: the socket is asked (`ping`, which
|
|
3555
|
+
* a server that offers it answers at once) and given
|
|
3556
|
+
* {@link WAKE_PROBE_TIMEOUT_MS} to say anything — or, from a server that
|
|
3557
|
+
* does not offer `ping`, a whole deadline from now.
|
|
3511
3558
|
*/
|
|
3512
3559
|
watchLiveness(heartbeatMs) {
|
|
3513
3560
|
this.stopLivenessWatch();
|
|
3514
3561
|
const deadline = 2 * heartbeatMs + Math.min(HEARTBEAT_GRACE_MS, heartbeatMs);
|
|
3562
|
+
const period = Math.max(250, Math.floor(heartbeatMs / 2));
|
|
3515
3563
|
this.lastFrameAt = Date.now();
|
|
3564
|
+
let lastTick = Date.now();
|
|
3516
3565
|
this.livenessTimer = setInterval(() => {
|
|
3517
|
-
const
|
|
3566
|
+
const now = Date.now();
|
|
3567
|
+
const late = now - lastTick > 2 * period + 1e3;
|
|
3568
|
+
lastTick = now;
|
|
3569
|
+
if (late) {
|
|
3570
|
+
this.probeAfterWake();
|
|
3571
|
+
return;
|
|
3572
|
+
}
|
|
3573
|
+
const probe = this.wakeProbe;
|
|
3574
|
+
if (probe) if (this.lastFrameAt >= probe.sentAt) this.wakeProbe = null;
|
|
3575
|
+
else if (now > probe.deadline) {
|
|
3576
|
+
console.warn(`[Rebase] Realtime: no answer ${Math.round((now - probe.sentAt) / 1e3)} s after waking. Reconnecting.`);
|
|
3577
|
+
this.stopLivenessWatch();
|
|
3578
|
+
this.dropSocket?.("probe");
|
|
3579
|
+
return;
|
|
3580
|
+
} else return;
|
|
3581
|
+
const silentFor = now - this.lastFrameAt;
|
|
3518
3582
|
if (silentFor <= deadline) return;
|
|
3519
3583
|
console.warn(`[Rebase] Realtime: nothing from the server for ${Math.round(silentFor / 1e3)} s, two missed heartbeats. Reconnecting.`);
|
|
3520
3584
|
this.stopLivenessWatch();
|
|
3521
|
-
this.dropSocket?.();
|
|
3522
|
-
},
|
|
3585
|
+
this.dropSocket?.("heartbeat");
|
|
3586
|
+
}, period);
|
|
3523
3587
|
unref(this.livenessTimer);
|
|
3524
3588
|
}
|
|
3589
|
+
/**
|
|
3590
|
+
* The page has just woken (a late liveness tick, or the tab shown again):
|
|
3591
|
+
* ask an open socket whether it is still there instead of judging it on
|
|
3592
|
+
* the silence of the sleep. Does nothing while no heartbeats are watched.
|
|
3593
|
+
*/
|
|
3594
|
+
probeAfterWake() {
|
|
3595
|
+
if (!this.livenessTimer || !this.ws || !this.isConnected) return;
|
|
3596
|
+
const now = Date.now();
|
|
3597
|
+
if (this.wakeProbe && now <= this.wakeProbe.deadline) return;
|
|
3598
|
+
if (this.serverOptIns.has("ping")) try {
|
|
3599
|
+
this.ws.send(JSON.stringify({ type: "ping" }));
|
|
3600
|
+
this.wakeProbe = {
|
|
3601
|
+
sentAt: now,
|
|
3602
|
+
deadline: now + WAKE_PROBE_TIMEOUT_MS
|
|
3603
|
+
};
|
|
3604
|
+
return;
|
|
3605
|
+
} catch {}
|
|
3606
|
+
this.lastFrameAt = now;
|
|
3607
|
+
}
|
|
3525
3608
|
stopLivenessWatch() {
|
|
3526
3609
|
if (this.livenessTimer) {
|
|
3527
3610
|
clearInterval(this.livenessTimer);
|
|
3528
3611
|
this.livenessTimer = null;
|
|
3529
3612
|
}
|
|
3613
|
+
this.wakeProbe = null;
|
|
3530
3614
|
}
|
|
3531
3615
|
forgetServerOptIns() {
|
|
3532
3616
|
this.serverOptIns = /* @__PURE__ */ new Set();
|