@rebasepro/client 0.13.1-canary.gcd6689e → 0.13.1-canary.gcef69d4
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.es.js +114 -61
- package/dist/index.es.js.map +1 -1
- package/dist/offline-query.d.ts +18 -3
- package/package.json +4 -4
- package/src/auth-listener-errors.test.ts +57 -0
- package/src/auth-refresh-overflow.test.ts +89 -0
- package/src/auth.ts +30 -1
- package/src/collection-listen-meta.test.ts +105 -0
- package/src/collection-observe.test.ts +138 -0
- package/src/collection.ts +88 -46
- package/src/index.ts +48 -15
- package/src/like-pattern-redos.test.ts +61 -0
- package/src/offline-query.test.ts +28 -2
- package/src/offline-query.ts +35 -8
- package/src/offline.ts +6 -4
- package/src/realtime-optout.test.ts +15 -15
- package/src/realtime-subscription-key.test.ts +92 -0
- package/src/transport-baseurl.test.ts +49 -1
- package/src/transport.ts +22 -0
- package/src/websocket-url.test.ts +97 -0
- package/src/websocket.ts +18 -9
package/dist/index.es.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_STORAGE_SOURCE_KEY, EntityReference, EntityRelation, GeoPoint, PUBLIC_STORAGE_PREFIX, RebaseApiError, RebaseApiError as RebaseApiError$1, RebaseClientError, RebaseClientError as RebaseClientError$1, Vector, isPublicStoragePath, toCanonicalOp } from "@rebasepro/types";
|
|
2
|
-
import { COMPOSITE_ID_SEPARATOR, QueryBuilder, RebasePaginationError, and, buildCompositeId, collectAllPages, cond, or, paginateFind, serializeFilter, serializeLogicalCondition, serializeOrderBy } from "@rebasepro/common";
|
|
2
|
+
import { COMPOSITE_ID_SEPARATOR, QueryBuilder, RebasePaginationError, and, buildCompositeId, collectAllPages, cond, or, paginateFind, resolveFindWindow, serializeFilter, serializeLogicalCondition, serializeOrderBy } from "@rebasepro/common";
|
|
3
3
|
import { toSnakeCase } from "@rebasepro/utils";
|
|
4
4
|
//#region src/reviver.ts
|
|
5
5
|
function rebaseReviver(_key, value) {
|
|
@@ -131,6 +131,13 @@ function resolveBaseUrl(configured) {
|
|
|
131
131
|
function createTransport(config, environment) {
|
|
132
132
|
const fetchFn = config.fetch || globalThis.fetch;
|
|
133
133
|
const apiPath = config.apiPath || "/api";
|
|
134
|
+
for (const field of ["baseUrl", "storageUrlOrigin"]) {
|
|
135
|
+
const value = config[field];
|
|
136
|
+
if (!value || !apiPath) continue;
|
|
137
|
+
const trimmed = value.replace(/\/+$/, "");
|
|
138
|
+
if (!trimmed.endsWith(apiPath)) continue;
|
|
139
|
+
console.warn(`[Rebase] ${field} ${JSON.stringify(value)} already ends with the API path ${JSON.stringify(apiPath)}, which is appended to it — requests will go to ${trimmed}${apiPath}/… and 404. Pass the origin only (${JSON.stringify(trimmed.slice(0, trimmed.length - apiPath.length) || "/")}), or set \`apiPath\` if the server really does mount the API one level deeper.`);
|
|
140
|
+
}
|
|
134
141
|
let token = config.token;
|
|
135
142
|
let tokenGetter;
|
|
136
143
|
let onUnauthorizedHandler = config.onUnauthorized;
|
|
@@ -316,6 +323,11 @@ function createAuth(transport, options) {
|
|
|
316
323
|
const authFlowMode = opts.authFlowMode || "json";
|
|
317
324
|
const STORAGE_KEY = "rebase_auth";
|
|
318
325
|
const REFRESH_BUFFER_MS = 12e4;
|
|
326
|
+
/**
|
|
327
|
+
* The largest delay `setTimeout` can hold — 2^31 - 1 ms, about 24.8 days.
|
|
328
|
+
* Anything larger is silently clamped to 1ms by Node and every browser.
|
|
329
|
+
*/
|
|
330
|
+
const MAX_TIMER_DELAY_MS = 2147483647;
|
|
319
331
|
const MAX_REFRESH_RETRIES = 5;
|
|
320
332
|
const REFRESH_RETRY_BASE_MS = 1e3;
|
|
321
333
|
const REFRESH_RETRY_MAX_MS = 3e4;
|
|
@@ -343,7 +355,9 @@ function createAuth(transport, options) {
|
|
|
343
355
|
function emit(event, session) {
|
|
344
356
|
for (const fn of listeners) try {
|
|
345
357
|
fn(event, session);
|
|
346
|
-
} catch (e) {
|
|
358
|
+
} catch (e) {
|
|
359
|
+
console.error("Error in auth state change listener:", e);
|
|
360
|
+
}
|
|
347
361
|
}
|
|
348
362
|
function saveSession(session) {
|
|
349
363
|
if (!persistSession || authFlowMode === "cookie") return;
|
|
@@ -447,6 +461,10 @@ function createAuth(transport, options) {
|
|
|
447
461
|
attemptScheduledRefresh(0);
|
|
448
462
|
return;
|
|
449
463
|
}
|
|
464
|
+
if (delay > MAX_TIMER_DELAY_MS) {
|
|
465
|
+
refreshTimeout = setTimeout(() => scheduleRefresh(expiresAt), MAX_TIMER_DELAY_MS);
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
450
468
|
refreshTimeout = setTimeout(() => {
|
|
451
469
|
attemptScheduledRefresh(0);
|
|
452
470
|
}, delay);
|
|
@@ -1337,8 +1355,15 @@ function createCollectionClient(transport, slug, ws) {
|
|
|
1337
1355
|
},
|
|
1338
1356
|
observe(params, onResult, onError, options) {
|
|
1339
1357
|
let closed = false;
|
|
1340
|
-
|
|
1358
|
+
let liveDelivered = false;
|
|
1359
|
+
let signature;
|
|
1360
|
+
const deliver = (result, fromLive) => {
|
|
1341
1361
|
if (closed) return;
|
|
1362
|
+
if (fromLive) liveDelivered = true;
|
|
1363
|
+
else if (liveDelivered) return;
|
|
1364
|
+
const next = `${result.meta?.total ?? ""}|${JSON.stringify(result.data)}`;
|
|
1365
|
+
if (signature !== void 0 && next === signature) return;
|
|
1366
|
+
signature = next;
|
|
1342
1367
|
onResult({
|
|
1343
1368
|
...result,
|
|
1344
1369
|
fromCache: false,
|
|
@@ -1346,10 +1371,10 @@ function createCollectionClient(transport, slug, ws) {
|
|
|
1346
1371
|
partial: false
|
|
1347
1372
|
});
|
|
1348
1373
|
};
|
|
1349
|
-
client.find(params).then(
|
|
1374
|
+
client.find(params).then((result) => deliver(result, false)).catch((error) => {
|
|
1350
1375
|
if (!closed) onError?.(error);
|
|
1351
1376
|
});
|
|
1352
|
-
const live = options?.realtime !== false && client.listen ? client.listen(params,
|
|
1377
|
+
const live = options?.realtime !== false && client.listen ? client.listen(params, (result) => deliver(result, true), onError) : void 0;
|
|
1353
1378
|
return () => {
|
|
1354
1379
|
closed = true;
|
|
1355
1380
|
live?.();
|
|
@@ -1357,17 +1382,24 @@ function createCollectionClient(transport, slug, ws) {
|
|
|
1357
1382
|
},
|
|
1358
1383
|
observeById(id, onResult, onError, options) {
|
|
1359
1384
|
let closed = false;
|
|
1360
|
-
|
|
1385
|
+
let liveDelivered = false;
|
|
1386
|
+
let signature;
|
|
1387
|
+
const deliver = (row, fromLive) => {
|
|
1361
1388
|
if (closed) return;
|
|
1389
|
+
if (fromLive) liveDelivered = true;
|
|
1390
|
+
else if (liveDelivered) return;
|
|
1391
|
+
const next = row === void 0 ? "\0missing" : JSON.stringify(row);
|
|
1392
|
+
if (signature !== void 0 && next === signature) return;
|
|
1393
|
+
signature = next;
|
|
1362
1394
|
onResult(row, {
|
|
1363
1395
|
fromCache: false,
|
|
1364
1396
|
hasPendingWrites: false
|
|
1365
1397
|
});
|
|
1366
1398
|
};
|
|
1367
|
-
client.findById(id).then(
|
|
1399
|
+
client.findById(id).then((row) => deliver(row, false)).catch((error) => {
|
|
1368
1400
|
if (!closed) onError?.(error);
|
|
1369
1401
|
});
|
|
1370
|
-
const live = options?.realtime !== false && client.listenById ? client.listenById(id,
|
|
1402
|
+
const live = options?.realtime !== false && client.listenById ? client.listenById(id, (row) => deliver(row, true), onError) : void 0;
|
|
1371
1403
|
return () => {
|
|
1372
1404
|
closed = true;
|
|
1373
1405
|
live?.();
|
|
@@ -1398,51 +1430,43 @@ function createCollectionClient(transport, slug, ws) {
|
|
|
1398
1430
|
client.listen = (params, onUpdate, onError) => {
|
|
1399
1431
|
let active = true;
|
|
1400
1432
|
let lastUpdateId = 0;
|
|
1433
|
+
let lastKnownTotal;
|
|
1434
|
+
const window = resolveFindWindow(params);
|
|
1401
1435
|
const unsub = ws.listenCollection({
|
|
1402
1436
|
path: slug,
|
|
1403
1437
|
filter: params?.where,
|
|
1438
|
+
logical: params?.logical,
|
|
1404
1439
|
limit: params?.limit,
|
|
1405
|
-
|
|
1440
|
+
offset: window.driverOffset,
|
|
1406
1441
|
orderBy: params?.orderBy?.[0],
|
|
1407
1442
|
order: params?.orderBy?.[1],
|
|
1408
1443
|
searchString: params?.searchString
|
|
1409
1444
|
}, (incomingRows) => {
|
|
1410
1445
|
const currentUpdateId = ++lastUpdateId;
|
|
1411
|
-
const requestedLimit =
|
|
1412
|
-
const offset =
|
|
1446
|
+
const requestedLimit = window.limit;
|
|
1447
|
+
const offset = window.offset;
|
|
1413
1448
|
const rows = incomingRows;
|
|
1414
|
-
const
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
if (active && currentUpdateId === lastUpdateId) onUpdate({
|
|
1449
|
+
const emit = (total, hasMore) => {
|
|
1450
|
+
if (!active || currentUpdateId !== lastUpdateId) return;
|
|
1451
|
+
onUpdate({
|
|
1418
1452
|
data: rows,
|
|
1419
1453
|
meta: {
|
|
1420
1454
|
total,
|
|
1421
1455
|
limit: requestedLimit,
|
|
1422
1456
|
offset,
|
|
1423
|
-
hasMore
|
|
1457
|
+
hasMore
|
|
1424
1458
|
}
|
|
1425
1459
|
});
|
|
1460
|
+
};
|
|
1461
|
+
const emitWithoutCount = () => emit(offset + rows.length, rows.length >= requestedLimit);
|
|
1462
|
+
if (client.count) client.count(params).then((total) => {
|
|
1463
|
+
lastKnownTotal = total;
|
|
1464
|
+
emit(total, offset + rows.length < total);
|
|
1426
1465
|
}).catch(() => {
|
|
1427
|
-
if (
|
|
1428
|
-
|
|
1429
|
-
meta: {
|
|
1430
|
-
total: heuristicTotal,
|
|
1431
|
-
limit: requestedLimit,
|
|
1432
|
-
offset,
|
|
1433
|
-
hasMore: heuristicHasMore
|
|
1434
|
-
}
|
|
1435
|
-
});
|
|
1436
|
-
});
|
|
1437
|
-
else onUpdate({
|
|
1438
|
-
data: rows,
|
|
1439
|
-
meta: {
|
|
1440
|
-
total: heuristicTotal,
|
|
1441
|
-
limit: requestedLimit,
|
|
1442
|
-
offset,
|
|
1443
|
-
hasMore: heuristicHasMore
|
|
1444
|
-
}
|
|
1466
|
+
if (lastKnownTotal !== void 0) emit(lastKnownTotal, offset + rows.length < lastKnownTotal);
|
|
1467
|
+
else emitWithoutCount();
|
|
1445
1468
|
});
|
|
1469
|
+
else emitWithoutCount();
|
|
1446
1470
|
}, onError);
|
|
1447
1471
|
return () => {
|
|
1448
1472
|
active = false;
|
|
@@ -2833,15 +2857,10 @@ var RebaseWebSocketClient = class {
|
|
|
2833
2857
|
}
|
|
2834
2858
|
}
|
|
2835
2859
|
createCollectionSubscriptionKey(props) {
|
|
2860
|
+
const { collection, ...query } = props;
|
|
2836
2861
|
const key = {
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
limit: props.limit,
|
|
2840
|
-
startAfter: props.startAfter,
|
|
2841
|
-
orderBy: props.orderBy,
|
|
2842
|
-
order: props.order,
|
|
2843
|
-
searchString: props.searchString,
|
|
2844
|
-
collection: props.collection?.name
|
|
2862
|
+
...query,
|
|
2863
|
+
collection: collection?.name
|
|
2845
2864
|
};
|
|
2846
2865
|
return JSON.stringify(key, (_, value) => {
|
|
2847
2866
|
if (value && typeof value === "object" && !Array.isArray(value)) return Object.keys(value).sort().reduce((sorted, k) => {
|
|
@@ -3764,14 +3783,23 @@ function looseEquals(a, b) {
|
|
|
3764
3783
|
*/
|
|
3765
3784
|
function likeToRegExp(pattern, caseInsensitive) {
|
|
3766
3785
|
let source = "^";
|
|
3786
|
+
let lastWasWildcard = false;
|
|
3767
3787
|
for (let i = 0; i < pattern.length; i++) {
|
|
3768
3788
|
const char = pattern[i];
|
|
3769
3789
|
if (char === "\\" && i + 1 < pattern.length) {
|
|
3770
3790
|
source += pattern[i + 1].replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3771
3791
|
i++;
|
|
3772
|
-
|
|
3773
|
-
else if (char === "
|
|
3774
|
-
|
|
3792
|
+
lastWasWildcard = false;
|
|
3793
|
+
} else if (char === "%") {
|
|
3794
|
+
if (!lastWasWildcard) source += "[\\s\\S]*";
|
|
3795
|
+
lastWasWildcard = true;
|
|
3796
|
+
} else if (char === "_") {
|
|
3797
|
+
source += "[\\s\\S]";
|
|
3798
|
+
lastWasWildcard = false;
|
|
3799
|
+
} else {
|
|
3800
|
+
source += char.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3801
|
+
lastWasWildcard = false;
|
|
3802
|
+
}
|
|
3775
3803
|
}
|
|
3776
3804
|
return new RegExp(source + "$", caseInsensitive ? "i" : "");
|
|
3777
3805
|
}
|
|
@@ -3903,12 +3931,20 @@ function sortRows(rows, orderBy) {
|
|
|
3903
3931
|
function tiebreak(a, b) {
|
|
3904
3932
|
return compareValues(a.id, b.id) ?? 0;
|
|
3905
3933
|
}
|
|
3906
|
-
/**
|
|
3934
|
+
/**
|
|
3935
|
+
* Resolve `page`/`offset`/`limit` the way the server does.
|
|
3936
|
+
*
|
|
3937
|
+
* It did not: this defaulted an absent limit to 20 while `/api/data` pages by
|
|
3938
|
+
* 50, so the same `observe()` answered with 20 rows from the local database and
|
|
3939
|
+
* 50 from the network — a list that changed length depending on which side
|
|
3940
|
+
* answered, with `page` striding differently on each. Delegated now, so the
|
|
3941
|
+
* sentence above is true by construction rather than by agreement.
|
|
3942
|
+
*/
|
|
3907
3943
|
function resolvePagination(params) {
|
|
3908
|
-
const limit = params
|
|
3944
|
+
const { limit, offset } = resolveFindWindow(params);
|
|
3909
3945
|
return {
|
|
3910
3946
|
limit,
|
|
3911
|
-
offset
|
|
3947
|
+
offset
|
|
3912
3948
|
};
|
|
3913
3949
|
}
|
|
3914
3950
|
/**
|
|
@@ -4536,9 +4572,8 @@ var OfflineManager = class {
|
|
|
4536
4572
|
if (!state) return {
|
|
4537
4573
|
data: [],
|
|
4538
4574
|
meta: {
|
|
4575
|
+
...resolvePagination(params),
|
|
4539
4576
|
total: 0,
|
|
4540
|
-
limit: params?.limit ?? 20,
|
|
4541
|
-
offset: params?.offset ?? 0,
|
|
4542
4577
|
hasMore: false
|
|
4543
4578
|
},
|
|
4544
4579
|
fromCache: true,
|
|
@@ -4730,17 +4765,17 @@ var OfflineManager = class {
|
|
|
4730
4765
|
return row;
|
|
4731
4766
|
}
|
|
4732
4767
|
recordSnapshot(slug, params, result) {
|
|
4768
|
+
const window = resolvePagination(params);
|
|
4733
4769
|
const meta = result.meta ?? {
|
|
4734
4770
|
total: result.data?.length ?? 0,
|
|
4735
|
-
|
|
4736
|
-
offset: 0,
|
|
4771
|
+
...window,
|
|
4737
4772
|
hasMore: false
|
|
4738
4773
|
};
|
|
4739
4774
|
const snapshot = {
|
|
4740
4775
|
ids: (result.data ?? []).map((row) => row.id).filter((id) => id !== void 0),
|
|
4741
4776
|
total: meta.total ?? result.data?.length ?? 0,
|
|
4742
|
-
limit: meta.limit ??
|
|
4743
|
-
offset: meta.offset ??
|
|
4777
|
+
limit: meta.limit ?? window.limit,
|
|
4778
|
+
offset: meta.offset ?? window.offset,
|
|
4744
4779
|
hasMore: meta.hasMore ?? false
|
|
4745
4780
|
};
|
|
4746
4781
|
const state = this.collectionState(slug);
|
|
@@ -5204,23 +5239,37 @@ var OfflineManager = class {
|
|
|
5204
5239
|
/**
|
|
5205
5240
|
* Derive a WebSocket URL from an HTTP base URL.
|
|
5206
5241
|
* `http://` → `ws://`, `https://` → `wss://`.
|
|
5242
|
+
*
|
|
5243
|
+
* A backend mounted under a path is the reason `baseUrl` accepts one, so the
|
|
5244
|
+
* path is kept. It used to be kept for an absolute `baseUrl` and dropped for a
|
|
5245
|
+
* relative one — resolved through `.origin` — so one deployment dialled two
|
|
5246
|
+
* different sockets depending on whether its config said `"/backend"` or
|
|
5247
|
+
* `"https://app.example.com/backend"`.
|
|
5248
|
+
*
|
|
5249
|
+
* Returns `""` when there is nothing to resolve against: a relative `baseUrl`
|
|
5250
|
+
* outside a browser has no origin, and inventing one would dial somewhere
|
|
5251
|
+
* arbitrary. The caller warns rather than leaving that silent.
|
|
5207
5252
|
*/
|
|
5208
5253
|
function deriveWebSocketUrl(baseUrl) {
|
|
5254
|
+
const toWsProtocol = (url) => {
|
|
5255
|
+
const secure = /^(https|wss):/i.test(url);
|
|
5256
|
+
return url.replace(/^https?:\/\//i, secure ? "wss://" : "ws://").replace(/^wss?:\/\//i, secure ? "wss://" : "ws://").replace(/\/$/, "");
|
|
5257
|
+
};
|
|
5209
5258
|
if (typeof window !== "undefined") {
|
|
5210
|
-
let absoluteUrl
|
|
5259
|
+
let absoluteUrl;
|
|
5211
5260
|
if (!baseUrl) absoluteUrl = window.location.origin;
|
|
5212
5261
|
else if (/^https?:\/\//i.test(baseUrl) || /^wss?:\/\//i.test(baseUrl)) absoluteUrl = baseUrl;
|
|
5213
5262
|
else try {
|
|
5214
|
-
|
|
5263
|
+
const resolved = new URL(baseUrl, window.location.href);
|
|
5264
|
+
absoluteUrl = resolved.origin + resolved.pathname;
|
|
5215
5265
|
} catch {
|
|
5216
5266
|
absoluteUrl = window.location.origin;
|
|
5217
5267
|
}
|
|
5218
|
-
|
|
5219
|
-
return absoluteUrl.replace(/^https?:\/\//i, `${protocol}//`).replace(/^wss?:\/\//i, `${protocol}//`).replace(/\/$/, "");
|
|
5268
|
+
return toWsProtocol(absoluteUrl);
|
|
5220
5269
|
}
|
|
5221
5270
|
if (!baseUrl) return "";
|
|
5222
5271
|
if (!/^https?:\/\//i.test(baseUrl) && !/^wss?:\/\//i.test(baseUrl)) return "";
|
|
5223
|
-
return baseUrl
|
|
5272
|
+
return toWsProtocol(baseUrl);
|
|
5224
5273
|
}
|
|
5225
5274
|
function createRebaseClient(options) {
|
|
5226
5275
|
const transport = createTransport(options, { credentialOutOfBand: options.auth?.authFlowMode === "cookie" });
|
|
@@ -5248,7 +5297,11 @@ function createRebaseClient(options) {
|
|
|
5248
5297
|
});
|
|
5249
5298
|
return storageSourcesPromise;
|
|
5250
5299
|
};
|
|
5251
|
-
const
|
|
5300
|
+
const realtimeEnabled = options.realtime !== false;
|
|
5301
|
+
const resolvedWsUrl = realtimeEnabled ? options.websocketUrl ?? deriveWebSocketUrl(options.baseUrl) : void 0;
|
|
5302
|
+
const realtimeUnreachable = realtimeEnabled && !resolvedWsUrl;
|
|
5303
|
+
const unreachableReason = `no WebSocket URL could be derived from baseUrl ${JSON.stringify(options.baseUrl ?? null)} — outside a browser there is no page origin to resolve a relative URL against. Pass an absolute \`baseUrl\`, set \`websocketUrl\` explicitly, or pass \`realtime: false\` to say this was intended.`;
|
|
5304
|
+
if (realtimeUnreachable) console.warn(`[Rebase] Realtime is enabled but ${unreachableReason} Live queries will fall back to a single fetch and channels will throw.`);
|
|
5252
5305
|
let ws;
|
|
5253
5306
|
/** One channel object per name — see `realtime.channel`. */
|
|
5254
5307
|
const realtimeChannels = /* @__PURE__ */ new Map();
|
|
@@ -5368,7 +5421,7 @@ function createRebaseClient(options) {
|
|
|
5368
5421
|
* cut off the others.
|
|
5369
5422
|
*/
|
|
5370
5423
|
channel: (name, options) => {
|
|
5371
|
-
if (!ws) throw new RebaseClientError("Realtime is disabled on this client (realtime: false), so channels are unavailable.");
|
|
5424
|
+
if (!ws) throw new RebaseClientError(realtimeUnreachable ? `Realtime is enabled but ${unreachableReason}` : "Realtime is disabled on this client (realtime: false), so channels are unavailable.");
|
|
5372
5425
|
let existing = realtimeChannels.get(name);
|
|
5373
5426
|
if (!existing) {
|
|
5374
5427
|
existing = new RebaseRealtimeChannel(name, ws, options);
|