@rebasepro/client 0.34.1 → 0.35.0
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/admin.d.ts +4 -1
- package/dist/index.d.ts +14 -0
- package/dist/index.es.js +134 -12
- package/dist/index.es.js.map +1 -1
- package/dist/{offline-ET6xI1ZO.js → offline-DSkf2JrL.js} +2 -2
- package/dist/offline-DSkf2JrL.js.map +1 -0
- package/dist/rpc.d.ts +19 -0
- package/dist/websocket.d.ts +48 -0
- package/package.json +4 -4
- package/dist/offline-ET6xI1ZO.js.map +0 -1
package/dist/admin.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Transport } from "./transport.js";
|
|
2
|
-
import type { AdminGuest, AdminUser, DeleteGuestsResult, RoleSummary } from "@rebasepro/types";
|
|
2
|
+
import type { AdminGuest, AdminUser, DeleteGuestsResult, ImportUserInput, ImportUsersResult, RoleSummary } from "@rebasepro/types";
|
|
3
3
|
export type { AdminUser };
|
|
4
4
|
export interface CreateAdminOptions {
|
|
5
5
|
adminPath?: string;
|
|
@@ -55,6 +55,9 @@ export declare function createAdmin(transport: Transport, options?: CreateAdminO
|
|
|
55
55
|
invitationSent?: boolean;
|
|
56
56
|
emailDeliveryFailed?: boolean;
|
|
57
57
|
}>;
|
|
58
|
+
importUsers: (users: ImportUserInput[], options?: {
|
|
59
|
+
dryRun?: boolean;
|
|
60
|
+
}) => Promise<ImportUsersResult>;
|
|
58
61
|
updateUser: (userId: string, data: {
|
|
59
62
|
email?: string;
|
|
60
63
|
displayName?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { createSchema } from "./schema.js";
|
|
|
7
7
|
import { createApiKeys, CreateApiKeysOptions, createPersonalKeys, CreatePersonalKeysOptions } from "./api-keys.js";
|
|
8
8
|
import { TableClient } from "./table.js";
|
|
9
9
|
import { createFunctionsClient } from "./functions.js";
|
|
10
|
+
import { type RpcCall } from "./rpc.js";
|
|
10
11
|
import { type PushClient } from "./push.js";
|
|
11
12
|
import { RebaseWebSocketClient } from "./websocket.js";
|
|
12
13
|
import { RebaseRealtimeChannel, type ChannelOptions } from "./realtime-channel.js";
|
|
@@ -37,8 +38,10 @@ export { SCHEMA_UNAVAILABLE, isSchemaUnavailable } from "./schema.js";
|
|
|
37
38
|
export type { SchemaAPI, SchemaTablesOptions, TableDescriptor } from "@rebasepro/types";
|
|
38
39
|
export type { ApiKeyKind, ApiKeyMasked, ApiKeyWithSecret, CreateApiKeyRequest, CreateApiKeysOptions, CreatePersonalKeyRequest, CreatePersonalKeysOptions, UpdateApiKeyRequest } from "./api-keys.js";
|
|
39
40
|
export type { FunctionInvokeOptions, FunctionsClient } from "./functions.js";
|
|
41
|
+
export type { RpcCall } from "./rpc.js";
|
|
40
42
|
export type { PushClient, PushSubscribeOptions, PushRegistrationOptions } from "./push.js";
|
|
41
43
|
export { RebaseWebSocketClient } from "./websocket.js";
|
|
44
|
+
export type { RealtimeDisconnectInfo, RealtimeDisconnectCause } from "./websocket.js";
|
|
42
45
|
export { RebaseRealtimeChannel } from "./realtime-channel.js";
|
|
43
46
|
export type { PresenceState, PresenceDiff, BroadcastEvent, ChannelTransport, ChannelOptions, ChannelHistoryEntry, ChannelHistoryResult } from "./realtime-channel.js";
|
|
44
47
|
export type { ConflictResolution, OfflineApi, OfflineConfig, OfflineQueueOptions, OfflineStatus } from "./offline.js";
|
|
@@ -178,6 +181,17 @@ export type CreateRebaseClientResult<DB = Record<string, unknown>> = Omit<Rebase
|
|
|
178
181
|
apiKeys: ReturnType<typeof createApiKeys>;
|
|
179
182
|
personalKeys: ReturnType<typeof createPersonalKeys>;
|
|
180
183
|
functions: ReturnType<typeof createFunctionsClient>;
|
|
184
|
+
/**
|
|
185
|
+
* Call a SQL function the backend declares with `defineRpc` — `POST
|
|
186
|
+
* /rpc/<name>` with the named arguments — as the signed-in user, or
|
|
187
|
+
* anonymously. Resolves to the function's result: an array for a
|
|
188
|
+
* set-returning function, the row or the value for any other.
|
|
189
|
+
*
|
|
190
|
+
* ```ts
|
|
191
|
+
* const proposal = await client.rpc<Proposal | null>("get_public_proposal", { token });
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
rpc: RpcCall;
|
|
181
195
|
/** Web Push from the browser: subscribe and unsubscribe through a service worker. Needs `push: true` on the backend. */
|
|
182
196
|
push: PushClient;
|
|
183
197
|
ws?: RebaseWebSocketClient;
|
package/dist/index.es.js
CHANGED
|
@@ -155,7 +155,7 @@ function createMemoryStorage() {
|
|
|
155
155
|
}
|
|
156
156
|
function detectStorage() {
|
|
157
157
|
try {
|
|
158
|
-
if (typeof localStorage !== "undefined") {
|
|
158
|
+
if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
|
|
159
159
|
localStorage.setItem("__rebase_test__", "1");
|
|
160
160
|
localStorage.removeItem("__rebase_test__");
|
|
161
161
|
return localStorage;
|
|
@@ -1800,6 +1800,24 @@ function createAdmin(transport, options) {
|
|
|
1800
1800
|
});
|
|
1801
1801
|
}
|
|
1802
1802
|
/**
|
|
1803
|
+
* Import accounts from another system, keeping their ids (`id`, often a
|
|
1804
|
+
* uuid) so the rows that point at them still do. No password, no email:
|
|
1805
|
+
* an imported account signs in by magic link, an email code, a provider or
|
|
1806
|
+
* a password reset. An account whose id or email is already taken is
|
|
1807
|
+
* `skipped`, never updated. At most `MAX_USERS_PER_IMPORT` (100) accounts
|
|
1808
|
+
* per call; `dryRun: true` checks them and writes nothing.
|
|
1809
|
+
*/
|
|
1810
|
+
async function importUsers(users, options) {
|
|
1811
|
+
const body = {
|
|
1812
|
+
users,
|
|
1813
|
+
...options?.dryRun ? { dryRun: true } : {}
|
|
1814
|
+
};
|
|
1815
|
+
return transport.request(adminPath + "/users/import", {
|
|
1816
|
+
method: "POST",
|
|
1817
|
+
body: JSON.stringify(body)
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
/**
|
|
1803
1821
|
* Change an account. `disabled: true` switches it off — no sign-in, no
|
|
1804
1822
|
* refresh, and every token it holds refused at once — without deleting it.
|
|
1805
1823
|
*/
|
|
@@ -1860,6 +1878,7 @@ function createAdmin(transport, options) {
|
|
|
1860
1878
|
listUsersPaginated,
|
|
1861
1879
|
getUser,
|
|
1862
1880
|
createUser,
|
|
1881
|
+
importUsers,
|
|
1863
1882
|
updateUser,
|
|
1864
1883
|
deleteUser,
|
|
1865
1884
|
resetMfa,
|
|
@@ -2231,6 +2250,23 @@ function createFunctionsClient(transport) {
|
|
|
2231
2250
|
} };
|
|
2232
2251
|
}
|
|
2233
2252
|
//#endregion
|
|
2253
|
+
//#region src/rpc.ts
|
|
2254
|
+
/** The SQL identifier an RPC name is. */
|
|
2255
|
+
var RPC_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
2256
|
+
/** @internal */
|
|
2257
|
+
function createRpcCall(transport) {
|
|
2258
|
+
return (name, args) => {
|
|
2259
|
+
if (!RPC_NAME.test(name)) throw new RebaseClientError$1(`Invalid RPC name "${name}": an RPC is named after its SQL function — letters, digits and _, not starting with a digit.`, {
|
|
2260
|
+
code: "INVALID_RPC_NAME",
|
|
2261
|
+
details: { name }
|
|
2262
|
+
});
|
|
2263
|
+
return transport.request(`/rpc/${name}`, {
|
|
2264
|
+
method: "POST",
|
|
2265
|
+
body: JSON.stringify(args ?? {})
|
|
2266
|
+
});
|
|
2267
|
+
};
|
|
2268
|
+
}
|
|
2269
|
+
//#endregion
|
|
2234
2270
|
//#region src/push.ts
|
|
2235
2271
|
/** A VAPID key, as base64url, in the form `PushManager.subscribe` takes. */
|
|
2236
2272
|
function keyBytes(base64url) {
|
|
@@ -2821,7 +2857,8 @@ var SOCKET_OPT_IN_PARAM = "rebase_caps";
|
|
|
2821
2857
|
var SOCKET_OPT_INS = [
|
|
2822
2858
|
"heartbeat",
|
|
2823
2859
|
"channel_seq",
|
|
2824
|
-
"cancel_sql"
|
|
2860
|
+
"cancel_sql",
|
|
2861
|
+
"ping"
|
|
2825
2862
|
];
|
|
2826
2863
|
/**
|
|
2827
2864
|
* The query parameter naming the group a socket belongs to. A platform that
|
|
@@ -2832,6 +2869,19 @@ var SOCKET_AFFINITY_PARAM = "affinity";
|
|
|
2832
2869
|
/** Slack on top of two heartbeat intervals before a silent socket is given up on. */
|
|
2833
2870
|
var HEARTBEAT_GRACE_MS = 5e3;
|
|
2834
2871
|
/**
|
|
2872
|
+
* How long a socket has to answer the `ping` sent when the page wakes, in ms,
|
|
2873
|
+
* before it is given up on. Long enough for a phone's radio to come back.
|
|
2874
|
+
*/
|
|
2875
|
+
var WAKE_PROBE_TIMEOUT_MS = 1e4;
|
|
2876
|
+
/** The code and reason of a `close` event, read without trusting its shape. */
|
|
2877
|
+
function closeEventDetails(event) {
|
|
2878
|
+
if (typeof event !== "object" || event === null) return {};
|
|
2879
|
+
return {
|
|
2880
|
+
code: "code" in event && typeof event.code === "number" ? event.code : void 0,
|
|
2881
|
+
reason: "reason" in event && typeof event.reason === "string" && event.reason !== "" ? event.reason : void 0
|
|
2882
|
+
};
|
|
2883
|
+
}
|
|
2884
|
+
/**
|
|
2835
2885
|
* `url` with the opt-ins named on it, and `affinity` when there is one.
|
|
2836
2886
|
* Appended rather than rebuilt through `URL`, which would rewrite the address
|
|
2837
2887
|
* the caller gave (a bare origin gains a `/`).
|
|
@@ -3034,7 +3084,16 @@ var RebaseWebSocketClient = class {
|
|
|
3034
3084
|
serverClientId;
|
|
3035
3085
|
/** When the current socket last delivered anything, heartbeats included. */
|
|
3036
3086
|
lastFrameAt = 0;
|
|
3087
|
+
/** When the current socket opened, for the `disconnect` event. */
|
|
3088
|
+
socketOpenedAt = 0;
|
|
3037
3089
|
livenessTimer = null;
|
|
3090
|
+
/** The current wake probe: when it was sent, and when it must have been answered by. */
|
|
3091
|
+
wakeProbe = null;
|
|
3092
|
+
/**
|
|
3093
|
+
* Why this client is about to close the current socket itself, read by its
|
|
3094
|
+
* close handler so the `disconnect` event names the cause.
|
|
3095
|
+
*/
|
|
3096
|
+
closingFor = null;
|
|
3038
3097
|
/** Give up on the current socket as though it had closed. Set per socket. */
|
|
3039
3098
|
dropSocket = null;
|
|
3040
3099
|
/** Whether the server agreed, in its `hello`, to `name` for this socket. */
|
|
@@ -3211,7 +3270,9 @@ var RebaseWebSocketClient = class {
|
|
|
3211
3270
|
window.addEventListener("online", this.onlineListener);
|
|
3212
3271
|
if (typeof document !== "undefined" && typeof document.addEventListener === "function") {
|
|
3213
3272
|
this.visibilityListener = () => {
|
|
3214
|
-
if (document.visibilityState
|
|
3273
|
+
if (document.visibilityState !== "visible") return;
|
|
3274
|
+
this.retryNow("Tab is visible again");
|
|
3275
|
+
this.probeAfterWake();
|
|
3215
3276
|
};
|
|
3216
3277
|
document.addEventListener("visibilitychange", this.visibilityListener);
|
|
3217
3278
|
}
|
|
@@ -3413,6 +3474,8 @@ var RebaseWebSocketClient = class {
|
|
|
3413
3474
|
this.hadConnection = true;
|
|
3414
3475
|
this.isConnected = true;
|
|
3415
3476
|
this.gaveUp = false;
|
|
3477
|
+
this.socketOpenedAt = Date.now();
|
|
3478
|
+
this.lastFrameAt = this.socketOpenedAt;
|
|
3416
3479
|
if (!this.outageReported) this.setState("connected");
|
|
3417
3480
|
this.refusedSession = null;
|
|
3418
3481
|
if (this.getAuthToken && !this.isAuthenticated) {
|
|
@@ -3431,6 +3494,7 @@ var RebaseWebSocketClient = class {
|
|
|
3431
3494
|
if (!(token && this.noteSessionRefused(token, error))) {
|
|
3432
3495
|
this.connectionDebug("WebSocket connected, not signed in yet:", error?.message || error);
|
|
3433
3496
|
if (token && this.ws === socket && !this.refusedSession) {
|
|
3497
|
+
this.closingFor = "client";
|
|
3434
3498
|
socket.close(4e3, "Could not authenticate; reconnecting");
|
|
3435
3499
|
return;
|
|
3436
3500
|
}
|
|
@@ -3457,8 +3521,17 @@ var RebaseWebSocketClient = class {
|
|
|
3457
3521
|
console.error("Error parsing WebSocket message:", error);
|
|
3458
3522
|
}
|
|
3459
3523
|
};
|
|
3460
|
-
const onClosed = () => {
|
|
3524
|
+
const onClosed = (event, cause) => {
|
|
3461
3525
|
this.connectionDebug("Disconnected from PostgreSQL backend");
|
|
3526
|
+
const now = Date.now();
|
|
3527
|
+
const info = {
|
|
3528
|
+
...closeEventDetails(event),
|
|
3529
|
+
cause: cause ?? this.closingFor ?? "server",
|
|
3530
|
+
openForMs: this.socketOpenedAt ? now - this.socketOpenedAt : 0,
|
|
3531
|
+
silentForMs: this.lastFrameAt ? now - this.lastFrameAt : 0
|
|
3532
|
+
};
|
|
3533
|
+
this.closingFor = null;
|
|
3534
|
+
this.socketOpenedAt = 0;
|
|
3462
3535
|
this.stopLivenessWatch();
|
|
3463
3536
|
this.forgetServerOptIns();
|
|
3464
3537
|
if (this.ws === socket) this.ws = null;
|
|
@@ -3466,7 +3539,7 @@ var RebaseWebSocketClient = class {
|
|
|
3466
3539
|
this.isAuthenticated = false;
|
|
3467
3540
|
this.authPromise = null;
|
|
3468
3541
|
this.suspendSubscribeWatchdogs();
|
|
3469
|
-
this.emit("disconnect");
|
|
3542
|
+
this.emit("disconnect", info);
|
|
3470
3543
|
for (const [reqId, request] of this.pendingRequests.entries()) {
|
|
3471
3544
|
this.pendingRequests.delete(reqId);
|
|
3472
3545
|
if (reqId.startsWith("auth_")) request.reject(/* @__PURE__ */ new Error("Connection closed during authentication"));
|
|
@@ -3476,16 +3549,16 @@ var RebaseWebSocketClient = class {
|
|
|
3476
3549
|
this.attemptReconnect();
|
|
3477
3550
|
};
|
|
3478
3551
|
this.ws.onclose = onClosed;
|
|
3479
|
-
this.dropSocket = () => {
|
|
3552
|
+
this.dropSocket = (cause) => {
|
|
3480
3553
|
if (this.ws !== socket) return;
|
|
3481
3554
|
socket.onclose = null;
|
|
3482
3555
|
socket.onmessage = null;
|
|
3483
3556
|
socket.onerror = null;
|
|
3484
3557
|
socket.onopen = null;
|
|
3485
3558
|
try {
|
|
3486
|
-
socket.close();
|
|
3559
|
+
socket.close(4003, cause === "probe" ? "No answer to a ping after waking" : "Two heartbeats missed");
|
|
3487
3560
|
} catch {}
|
|
3488
|
-
onClosed();
|
|
3561
|
+
onClosed(void 0, cause);
|
|
3489
3562
|
};
|
|
3490
3563
|
this.ws.onerror = (error) => {
|
|
3491
3564
|
console.error("WebSocket error:", error);
|
|
@@ -3508,25 +3581,72 @@ var RebaseWebSocketClient = class {
|
|
|
3508
3581
|
/**
|
|
3509
3582
|
* Redial when two heartbeats in a row have not arrived. Anything the
|
|
3510
3583
|
* server sends counts as a beat, so a busy socket is never judged on them.
|
|
3584
|
+
*
|
|
3585
|
+
* Never on a silence the page caused. A phone that locked, a tab the
|
|
3586
|
+
* browser froze or a laptop that slept runs no timers and reads no frames,
|
|
3587
|
+
* and on waking this interval used to fire before the frames waiting on
|
|
3588
|
+
* the socket were read: it saw a minute of silence and tore down a socket
|
|
3589
|
+
* that was fine, and every app on it saw a disconnect. A tick that comes
|
|
3590
|
+
* far later than it was due is a wake: the socket is asked (`ping`, which
|
|
3591
|
+
* a server that offers it answers at once) and given
|
|
3592
|
+
* {@link WAKE_PROBE_TIMEOUT_MS} to say anything — or, from a server that
|
|
3593
|
+
* does not offer `ping`, a whole deadline from now.
|
|
3511
3594
|
*/
|
|
3512
3595
|
watchLiveness(heartbeatMs) {
|
|
3513
3596
|
this.stopLivenessWatch();
|
|
3514
3597
|
const deadline = 2 * heartbeatMs + Math.min(HEARTBEAT_GRACE_MS, heartbeatMs);
|
|
3598
|
+
const period = Math.max(250, Math.floor(heartbeatMs / 2));
|
|
3515
3599
|
this.lastFrameAt = Date.now();
|
|
3600
|
+
let lastTick = Date.now();
|
|
3516
3601
|
this.livenessTimer = setInterval(() => {
|
|
3517
|
-
const
|
|
3602
|
+
const now = Date.now();
|
|
3603
|
+
const late = now - lastTick > 2 * period + 1e3;
|
|
3604
|
+
lastTick = now;
|
|
3605
|
+
if (late) {
|
|
3606
|
+
this.probeAfterWake();
|
|
3607
|
+
return;
|
|
3608
|
+
}
|
|
3609
|
+
const probe = this.wakeProbe;
|
|
3610
|
+
if (probe) if (this.lastFrameAt >= probe.sentAt) this.wakeProbe = null;
|
|
3611
|
+
else if (now > probe.deadline) {
|
|
3612
|
+
console.warn(`[Rebase] Realtime: no answer ${Math.round((now - probe.sentAt) / 1e3)} s after waking. Reconnecting.`);
|
|
3613
|
+
this.stopLivenessWatch();
|
|
3614
|
+
this.dropSocket?.("probe");
|
|
3615
|
+
return;
|
|
3616
|
+
} else return;
|
|
3617
|
+
const silentFor = now - this.lastFrameAt;
|
|
3518
3618
|
if (silentFor <= deadline) return;
|
|
3519
3619
|
console.warn(`[Rebase] Realtime: nothing from the server for ${Math.round(silentFor / 1e3)} s, two missed heartbeats. Reconnecting.`);
|
|
3520
3620
|
this.stopLivenessWatch();
|
|
3521
|
-
this.dropSocket?.();
|
|
3522
|
-
},
|
|
3621
|
+
this.dropSocket?.("heartbeat");
|
|
3622
|
+
}, period);
|
|
3523
3623
|
unref(this.livenessTimer);
|
|
3524
3624
|
}
|
|
3625
|
+
/**
|
|
3626
|
+
* The page has just woken (a late liveness tick, or the tab shown again):
|
|
3627
|
+
* ask an open socket whether it is still there instead of judging it on
|
|
3628
|
+
* the silence of the sleep. Does nothing while no heartbeats are watched.
|
|
3629
|
+
*/
|
|
3630
|
+
probeAfterWake() {
|
|
3631
|
+
if (!this.livenessTimer || !this.ws || !this.isConnected) return;
|
|
3632
|
+
const now = Date.now();
|
|
3633
|
+
if (this.wakeProbe && now <= this.wakeProbe.deadline) return;
|
|
3634
|
+
if (this.serverOptIns.has("ping")) try {
|
|
3635
|
+
this.ws.send(JSON.stringify({ type: "ping" }));
|
|
3636
|
+
this.wakeProbe = {
|
|
3637
|
+
sentAt: now,
|
|
3638
|
+
deadline: now + WAKE_PROBE_TIMEOUT_MS
|
|
3639
|
+
};
|
|
3640
|
+
return;
|
|
3641
|
+
} catch {}
|
|
3642
|
+
this.lastFrameAt = now;
|
|
3643
|
+
}
|
|
3525
3644
|
stopLivenessWatch() {
|
|
3526
3645
|
if (this.livenessTimer) {
|
|
3527
3646
|
clearInterval(this.livenessTimer);
|
|
3528
3647
|
this.livenessTimer = null;
|
|
3529
3648
|
}
|
|
3649
|
+
this.wakeProbe = null;
|
|
3530
3650
|
}
|
|
3531
3651
|
forgetServerOptIns() {
|
|
3532
3652
|
this.serverOptIns = /* @__PURE__ */ new Set();
|
|
@@ -5251,7 +5371,7 @@ var LazyOfflineManager = class {
|
|
|
5251
5371
|
keepsLastUser;
|
|
5252
5372
|
constructor(config, createInner) {
|
|
5253
5373
|
this.keepsLastUser = config.keepLastUser === true;
|
|
5254
|
-
import("./offline-
|
|
5374
|
+
import("./offline-DSkf2JrL.js").then(({ OfflineManager: Engine }) => {
|
|
5255
5375
|
const engine = new Engine(config, createInner);
|
|
5256
5376
|
this.engine = engine;
|
|
5257
5377
|
const queued = this.queued;
|
|
@@ -5449,6 +5569,7 @@ function createRebaseClient(options) {
|
|
|
5449
5569
|
const personalKeys = createPersonalKeys(transport, options.personalKeys);
|
|
5450
5570
|
const storage = createStorage(transport);
|
|
5451
5571
|
const functions = createFunctionsClient(transport);
|
|
5572
|
+
const rpc = createRpcCall(transport);
|
|
5452
5573
|
const push = createPushClient(transport);
|
|
5453
5574
|
const createStorageSource = (storageId) => storageId === DEFAULT_STORAGE_SOURCE_KEY ? storage : createStorage(transport, storageId);
|
|
5454
5575
|
const storageRegistry = new ClientStorageSourceRegistry();
|
|
@@ -5700,6 +5821,7 @@ function createRebaseClient(options) {
|
|
|
5700
5821
|
apiKeys,
|
|
5701
5822
|
personalKeys,
|
|
5702
5823
|
functions,
|
|
5824
|
+
rpc,
|
|
5703
5825
|
push,
|
|
5704
5826
|
storage,
|
|
5705
5827
|
storageRegistry,
|