@rebasepro/client 0.25.1 → 0.27.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.
@@ -88,6 +88,9 @@ export interface PendingMutation {
88
88
  *
89
89
  * An `update` or `delete` carries only the key: its queued request is the
90
90
  * one it sent, since nothing is merged into an op that has `sent`.
91
+ *
92
+ * A queued create or update is marked too, before its first replay goes
93
+ * out, for the same reason: from then on the server may hold it.
91
94
  */
92
95
  sent?: {
93
96
  idempotencyKey: string;
@@ -97,6 +100,13 @@ export interface PendingMutation {
97
100
  id?: string | number;
98
101
  /** `createMany`: the conflict target the batch was sent with. */
99
102
  onConflict?: readonly string[];
103
+ /**
104
+ * A `create` replayed folded into a `replayBatchSize` run: that run's
105
+ * key. While the run is still whole in the queue it is sent again as
106
+ * that same request; otherwise each member replays alone, under its
107
+ * own key.
108
+ */
109
+ run?: string;
100
110
  };
101
111
  queuedAt: number;
102
112
  /**
package/dist/offline.d.ts CHANGED
@@ -100,7 +100,8 @@ export interface OfflineConfig {
100
100
  * A row last seen in a `find()` or `listen()` list has no version, and an
101
101
  * edit made on top of another queued write to the same row has none
102
102
  * either; those replay without a precondition. A caller's own `ifMatch` is
103
- * kept on a queued edit too.
103
+ * kept on a queued edit or delete too — without this option as well, when
104
+ * a 412 over it is a refusal like any other.
104
105
  *
105
106
  * The queue waits while this runs, so a prompt can ask the user. Without
106
107
  * it — the default — replay sends no precondition and the last writer
@@ -379,6 +380,22 @@ export declare class OfflineManager {
379
380
  private ticket;
380
381
  /** The ticket of whoever the held scope turns out to be. */
381
382
  private nextTicket;
383
+ private currentTicket;
384
+ /**
385
+ * Whether a write may go to the server now. Not while the scope is the last
386
+ * user's, unconfirmed: it would go out with no credentials, as nobody. It
387
+ * queues for its author instead, as it does offline.
388
+ */
389
+ private sendsWrites;
390
+ private readonly reachedUnconfirmedListeners;
391
+ /**
392
+ * `listener` runs when a request made while the scope is unconfirmed
393
+ * reached the server: the connection is back, so the session can be asked
394
+ * about. The client wires it to the same confirmation the status listener
395
+ * runs — the status may never have said the connection was gone.
396
+ */
397
+ onReachedUnconfirmed(listener: () => void): () => void;
398
+ private reachedUnconfirmed;
382
399
  /** Whether an operation's user is still the signed-in one. */
383
400
  private isCurrent;
384
401
  /**
@@ -587,6 +604,14 @@ export declare class OfflineManager {
587
604
  */
588
605
  private replayRun;
589
606
  private replay;
607
+ /**
608
+ * Record a queued create as sent, with the request about to go out, and
609
+ * persist it — before the request, so a lost answer cannot leave it
610
+ * unmarked. One already marked (sent online before it was queued, or by
611
+ * an earlier replay) keeps what it holds: that is the request the server
612
+ * may have stored.
613
+ */
614
+ private markCreateSent;
590
615
  /**
591
616
  * Send a queued write under its key.
592
617
  *
@@ -217,6 +217,19 @@ export declare class RebaseRealtimeChannel {
217
217
  */
218
218
  private send;
219
219
  join(): Promise<void>;
220
+ /** Consecutive re-joins asked for by refusals, for the backoff; reset after a quiet minute. */
221
+ private rejoinAttempts;
222
+ private rejoinTimer?;
223
+ private lastRejoinAt;
224
+ /**
225
+ * Re-join after the server refused this channel for a reason a retry can
226
+ * change: it could not check permissions at that moment, or it has no record
227
+ * of the join (a join that was itself refused, so every broadcast after it
228
+ * came back "not a member"). The join was fire-and-forget and `joined` was
229
+ * already set, so nothing else would ever send it again. Backs off 1, 2, 4…
230
+ * up to 30 seconds, one re-join at a time.
231
+ */
232
+ private scheduleRejoin;
220
233
  private rejoin;
221
234
  /**
222
235
  * Ask the server for everything after {@link lastSeq}.
@@ -259,6 +272,14 @@ export declare class RebaseRealtimeChannel {
259
272
  * A refusal therefore arrives on {@link onError}, not as a rejection here.
260
273
  */
261
274
  broadcast(event: string, payload: unknown): Promise<void>;
275
+ /**
276
+ * The socket ids this channel has broadcast from — the current one and the
277
+ * ones before a reconnect. A message in history from any of them is this
278
+ * client's own, and a sender is never sent its own messages: replayed after
279
+ * a reconnect whose `broadcast_ack` was lost, it was applied twice.
280
+ */
281
+ private readonly ownSenderIds;
282
+ private rememberOwnSender;
262
283
  /**
263
284
  * Observe refusals about this channel.
264
285
  *
@@ -336,3 +357,10 @@ export declare class RebaseRealtimeChannel {
336
357
  private deliver;
337
358
  private emitPresence;
338
359
  }
360
+ /**
361
+ * Whether a channel refusal is one a re-join can recover from: the server's
362
+ * auth store could not answer (`ROLE_LOOKUP_FAILED`, `DATABASE_UNAVAILABLE`), or
363
+ * it no longer counts this client a member. A channel the client may not use
364
+ * (any other `CHANNEL_FORBIDDEN`) is not retried.
365
+ */
366
+ export declare function rejoinsAfter(code: string | undefined, message: string): boolean;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Compile-time assertions about `client.storage.putObject`.
3
+ *
4
+ * A plain module under `src`, imported by nothing, for the reason
5
+ * `query-contract.types.ts` gives: `pnpm typecheck` reads `src`, and a type
6
+ * assertion in a jest test is checked by nothing.
7
+ *
8
+ * The SDK's upload takes any `Blob` and an optional key — with none, the server
9
+ * names the object after the file — and the SDK docs say so. Its parameter was
10
+ * the server controllers' `UploadFileProps`, whose `key` is required and whose
11
+ * `file` is a `File`, so both documented calls were compile errors.
12
+ */
13
+ import type { StorageSource } from "./index.js";
14
+ export declare const keyless: () => unknown;
15
+ export declare const named: () => unknown;
16
+ export declare const asSource: StorageSource;
package/dist/storage.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { StorageSource } from "@rebasepro/types";
1
+ import { ClientStorageSource } from "@rebasepro/types";
2
2
  import { Transport } from "./transport.js";
3
3
  /**
4
4
  * Create a StorageSource that talks to the Rebase backend REST API.
@@ -27,4 +27,4 @@ import { Transport } from "./transport.js";
27
27
  * characters and a trailing `%`.
28
28
  */
29
29
  export declare function encodeStorageKey(key: string): string;
30
- export declare function createStorage(transport: Transport, storageId?: string): StorageSource;
30
+ export declare function createStorage(transport: Transport, storageId?: string): ClientStorageSource;
@@ -1,6 +1,21 @@
1
1
  import { AggregateParams, FindParams as TypesFindParams, FindResponse as TypesFindResponse, type IncludeSpec } from "@rebasepro/types";
2
2
  export { RebaseApiError } from "@rebasepro/types";
3
3
  export type { RebaseErrorInit } from "@rebasepro/types";
4
+ /** Options for the realtime socket: `createRebaseClient({ realtime: { ... } })`. */
5
+ export interface RealtimeClientOptions {
6
+ /**
7
+ * The group this client's socket belongs to, such as a room code.
8
+ *
9
+ * Named on the socket's address as `?affinity=`, so a platform that routes
10
+ * on it can send a group's sockets to the same instance and keep their
11
+ * messages off the channel bus. Elsewhere it does nothing. It decides where
12
+ * a socket lands, not what it may join, and an address ends up in access
13
+ * logs: use a code, not a secret or anything personal.
14
+ *
15
+ * When it is only known later, use `client.realtime.setAffinity()`.
16
+ */
17
+ affinity?: string;
18
+ }
4
19
  export interface RebaseClientConfig {
5
20
  /**
6
21
  * Origin of the Rebase server — scheme, host and port **only**.
@@ -59,8 +74,10 @@ export interface RebaseClientConfig {
59
74
  *
60
75
  * Long-lived processes that do want realtime can instead call
61
76
  * `client.close()` when shutting down.
77
+ *
78
+ * An object leaves realtime on and configures it.
62
79
  */
63
- realtime?: boolean;
80
+ realtime?: boolean | RealtimeClientOptions;
64
81
  /**
65
82
  * "Yes, I meant to be anonymous."
66
83
  *
@@ -2,6 +2,8 @@ import { DeleteProps, CollectionConfig, FetchCollectionProps, ListenCollectionPr
2
2
  import type { SqlScriptResult } from "@rebasepro/types";
3
3
  export interface RebaseWebSocketConfig {
4
4
  websocketUrl: string;
5
+ /** The group this socket belongs to — see `RealtimeClientOptions.affinity`. */
6
+ affinity?: string;
5
7
  /**
6
8
  * Where the token the socket authenticates with comes from, asked each
7
9
  * time it authenticates. A falsy answer leaves it anonymous.
@@ -16,9 +18,17 @@ export interface RebaseWebSocketConfig {
16
18
  * `RebaseClientConfig.impersonate`. Sent with every `AUTHENTICATE`.
17
19
  */
18
20
  impersonate?: string;
21
+ /**
22
+ * Log the connection's progress — opened, signed in, closed, redialling —
23
+ * with `console.debug`. Off by default. See `CreateRebaseClientOptions.debug`.
24
+ */
25
+ debug?: boolean;
19
26
  }
27
+ /** The code of a channel `ERROR` frame, if it is one and carries a code. */
28
+ export declare function channelErrorCode(message: ChannelMessage): string | undefined;
20
29
  export declare class RebaseWebSocketClient {
21
30
  private websocketUrl;
31
+ private affinity?;
22
32
  private ws;
23
33
  /**
24
34
  * Where the socket's token comes from. A client built by
@@ -149,7 +159,25 @@ export declare class RebaseWebSocketClient {
149
159
  onUnauthorized?: () => Promise<boolean>;
150
160
  private refreshInProgress;
151
161
  private readonly impersonate?;
162
+ private readonly debug;
163
+ /**
164
+ * Connection chatter: the socket opened, signed in, closed, is redialling.
165
+ *
166
+ * Silent unless asked for, with the `debug` option or
167
+ * {@link debugSwitchedOn}. It used to go to `console.debug` in every
168
+ * browser: a public page printed "Connected to PostgreSQL backend" and
169
+ * "WebSocket auto-authenticated" to each visitor's console, naming the
170
+ * backend on every load. In Node, where `console.debug` is `console.log`,
171
+ * it opened a script's stdout ahead of what the script prints. Warnings and
172
+ * errors are not chatter and are always written.
173
+ */
174
+ private connectionDebug;
152
175
  constructor(config: RebaseWebSocketConfig);
176
+ /**
177
+ * Name the group this socket belongs to, or clear it with `null`. Read at
178
+ * each dial, so a socket already open stays where it is until it redials.
179
+ */
180
+ setAffinity(key: string | null): void;
153
181
  /**
154
182
  * Open the socket if it is not open (or opening) already.
155
183
  *
@@ -181,9 +209,35 @@ export declare class RebaseWebSocketClient {
181
209
  * once per socket and token, as a warning that names the code.
182
210
  */
183
211
  private noteSessionRefused;
212
+ /** Whether the current run of ceiling refusals has been said in the console. */
213
+ private socketCeilingWarned;
214
+ /**
215
+ * The server refused this socket for its per-user ceiling. Said once per
216
+ * run of refusals, as a warning, and reported to every registration at
217
+ * once as an outage — a refusal is not a blip that the next dial fixes.
218
+ */
219
+ private noteSocketCeiling;
184
220
  /**
185
221
  * Authenticate the WebSocket connection
186
222
  */
223
+ /**
224
+ * {@link authenticate}, asked again a few times when the server could not
225
+ * answer (its auth store busy, `ROLE_LOOKUP_FAILED`, a timeout) — not when
226
+ * it refused the session or the socket ceiling, which a retry cannot
227
+ * change. Stops as soon as `socket` is no longer this client's.
228
+ */
229
+ private authenticateRetrying;
230
+ /** A sign-in again after a channel was refused for not being signed in, in flight. */
231
+ private channelAuthRecovery;
232
+ /**
233
+ * A channel frame refused `UNAUTHORIZED` while this client holds a way to
234
+ * get a token: the socket is open but not signed in (an authentication that
235
+ * failed on a reconnect, a token that expired). Nothing else signs it in —
236
+ * channel frames expect no answer, so the refusal reaches only the channel —
237
+ * and every later frame is refused the same way. Sign in again, once for
238
+ * all the channels that noticed, and have them re-join.
239
+ */
240
+ private recoverChannelAuth;
187
241
  authenticate(token: string): Promise<void>;
188
242
  /**
189
243
  * Replace where this socket's token comes from, and authenticate an open,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rebasepro/client",
3
- "version": "0.25.1",
3
+ "version": "0.27.0",
4
4
  "description": "HTTP SDK client for the Rebase custom backend",
5
5
  "keywords": [
6
6
  "rebase",
@@ -41,9 +41,9 @@
41
41
  "./package.json": "./package.json"
42
42
  },
43
43
  "dependencies": {
44
- "@rebasepro/types": "0.25.1",
45
- "@rebasepro/utils": "0.25.1",
46
- "@rebasepro/common": "0.25.1"
44
+ "@rebasepro/types": "0.27.0",
45
+ "@rebasepro/utils": "0.27.0",
46
+ "@rebasepro/common": "0.27.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@jest/globals": "^30.4.1",