@rebasepro/client 0.10.1-canary.811b3da → 0.10.1-canary.8d27c46
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/auth.d.ts +1 -0
- package/dist/index.es.js +64 -26
- package/dist/index.es.js.map +1 -1
- package/dist/transport.d.ts +16 -0
- package/package.json +5 -4
- package/src/auth.ts +39 -0
- package/src/index.ts +6 -16
- package/src/offline-sync-engine.test.ts +53 -2
- package/src/offline.ts +46 -6
- package/src/storage.ts +11 -3
- package/src/transport.ts +17 -0
package/dist/auth.d.ts
CHANGED
|
@@ -129,6 +129,7 @@ export declare function createAuth(transport: Transport, options?: CreateAuthOpt
|
|
|
129
129
|
}>;
|
|
130
130
|
signOut: () => Promise<void>;
|
|
131
131
|
refreshSession: () => Promise<RebaseSession>;
|
|
132
|
+
handleUnauthorized: () => Promise<boolean>;
|
|
132
133
|
getUser: () => Promise<User>;
|
|
133
134
|
findUserByEmail: (email: string) => Promise<PublicUserProfile | null>;
|
|
134
135
|
updateUser: (updates: {
|
package/dist/index.es.js
CHANGED
|
@@ -170,6 +170,9 @@ function createTransport(config) {
|
|
|
170
170
|
get apiPath() {
|
|
171
171
|
return apiPath;
|
|
172
172
|
},
|
|
173
|
+
get storageUrlOrigin() {
|
|
174
|
+
return config.storageUrlOrigin?.replace(/\/$/, "") || void 0;
|
|
175
|
+
},
|
|
173
176
|
get fetchFn() {
|
|
174
177
|
return fetchFn;
|
|
175
178
|
},
|
|
@@ -318,6 +321,34 @@ function createAuth(transport, options) {
|
|
|
318
321
|
transport.setToken(null);
|
|
319
322
|
emit("SIGNED_OUT", null);
|
|
320
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Recover from a 401 on an ordinary API request.
|
|
326
|
+
*
|
|
327
|
+
* Returns `true` when the caller should retry — we minted a fresh access
|
|
328
|
+
* token. When the refresh is rejected *fatally* (the refresh token itself
|
|
329
|
+
* is invalid, expired or revoked) this client can no longer act as the
|
|
330
|
+
* user at all, so we drop the session and emit `SIGNED_OUT`. UIs gate on
|
|
331
|
+
* that event, so they show their login screen instead of leaving the user
|
|
332
|
+
* staring at "Invalid or expired token" on every view.
|
|
333
|
+
*
|
|
334
|
+
* Transient failures (offline, 5xx, backend restarting) keep the session:
|
|
335
|
+
* the scheduled refresh backs off and retries, and the token is very
|
|
336
|
+
* likely still good once the backend answers again.
|
|
337
|
+
*/
|
|
338
|
+
async function handleUnauthorized() {
|
|
339
|
+
if (!currentSession) return false;
|
|
340
|
+
if (authFlowMode !== "cookie" && !currentSession.refreshToken) {
|
|
341
|
+
abandonSessionLocally();
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
try {
|
|
345
|
+
await refreshSession();
|
|
346
|
+
return true;
|
|
347
|
+
} catch (err) {
|
|
348
|
+
if (isFatalRefreshError(err)) abandonSessionLocally();
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
321
352
|
async function attemptScheduledRefresh(attempt) {
|
|
322
353
|
try {
|
|
323
354
|
await refreshSession();
|
|
@@ -819,6 +850,7 @@ function createAuth(transport, options) {
|
|
|
819
850
|
signInWithSpotify,
|
|
820
851
|
signOut,
|
|
821
852
|
refreshSession,
|
|
853
|
+
handleUnauthorized,
|
|
822
854
|
getUser,
|
|
823
855
|
findUserByEmail,
|
|
824
856
|
updateUser,
|
|
@@ -1360,6 +1392,12 @@ function createFunctionsClient(transport) {
|
|
|
1360
1392
|
*/
|
|
1361
1393
|
function createStorage(transport, storageId) {
|
|
1362
1394
|
const urlsCache = /* @__PURE__ */ new Map();
|
|
1395
|
+
/**
|
|
1396
|
+
* Base for URLs the *browser* will fetch on its own (file downloads,
|
|
1397
|
+
* previews). API requests keep going to `baseUrl`; see
|
|
1398
|
+
* {@link RebaseClientConfig.storageUrlOrigin} for why these can differ.
|
|
1399
|
+
*/
|
|
1400
|
+
const fileUrlBase = () => `${transport.storageUrlOrigin ?? transport.baseUrl}${transport.apiPath}`;
|
|
1363
1401
|
/** Append ?storageId=... to a path when multi-backend routing is active. */
|
|
1364
1402
|
const withStorageId = (path) => {
|
|
1365
1403
|
if (!storageId) return path;
|
|
@@ -1397,7 +1435,7 @@ function createStorage(transport, storageId) {
|
|
|
1397
1435
|
fileNotFound: true
|
|
1398
1436
|
};
|
|
1399
1437
|
if (isPublicStoragePath(filePath)) {
|
|
1400
|
-
const publicConfig = { url: withStorageId(`${
|
|
1438
|
+
const publicConfig = { url: withStorageId(`${fileUrlBase()}/storage/file/${filePath}`) };
|
|
1401
1439
|
urlsCache.set(cacheKey, { config: publicConfig });
|
|
1402
1440
|
return publicConfig;
|
|
1403
1441
|
}
|
|
@@ -1405,7 +1443,7 @@ function createStorage(transport, storageId) {
|
|
|
1405
1443
|
const result = await transport.request(withStorageId(`/storage/metadata/${filePath}`));
|
|
1406
1444
|
if (result.data.public) {
|
|
1407
1445
|
const publicConfig = {
|
|
1408
|
-
url: withStorageId(`${
|
|
1446
|
+
url: withStorageId(`${fileUrlBase()}/storage/file/${filePath}`),
|
|
1409
1447
|
metadata: result.data
|
|
1410
1448
|
};
|
|
1411
1449
|
urlsCache.set(cacheKey, { config: publicConfig });
|
|
@@ -1414,7 +1452,7 @@ function createStorage(transport, storageId) {
|
|
|
1414
1452
|
const scopedToken = result.data.token;
|
|
1415
1453
|
const tokenQuery = scopedToken ? `?token=${scopedToken}` : "";
|
|
1416
1454
|
const downloadConfig = {
|
|
1417
|
-
url: withStorageId(`${
|
|
1455
|
+
url: withStorageId(`${fileUrlBase()}/storage/file/${filePath}${tokenQuery}`),
|
|
1418
1456
|
metadata: result.data
|
|
1419
1457
|
};
|
|
1420
1458
|
const expiresAt = result.data.tokenExpiresIn ? Date.now() + (result.data.tokenExpiresIn - 10) * 1e3 : void 0;
|
|
@@ -3882,6 +3920,8 @@ var OfflineManager = class {
|
|
|
3882
3920
|
for (const slug of slugs) this.collections.set(slug, {
|
|
3883
3921
|
rows: /* @__PURE__ */ new Map(),
|
|
3884
3922
|
snapshots: /* @__PURE__ */ new Map(),
|
|
3923
|
+
fresh: /* @__PURE__ */ new Set(),
|
|
3924
|
+
freshRows: /* @__PURE__ */ new Set(),
|
|
3885
3925
|
absent: /* @__PURE__ */ new Set(),
|
|
3886
3926
|
ready: true
|
|
3887
3927
|
});
|
|
@@ -3927,6 +3967,7 @@ var OfflineManager = class {
|
|
|
3927
3967
|
this.connectivity.markFailure();
|
|
3928
3968
|
}
|
|
3929
3969
|
const answer = this.localFind(slug, params);
|
|
3970
|
+
this.notifyCollection(slug, false);
|
|
3930
3971
|
return {
|
|
3931
3972
|
data: answer.data,
|
|
3932
3973
|
meta: answer.meta
|
|
@@ -4118,7 +4159,7 @@ var OfflineManager = class {
|
|
|
4118
4159
|
emit: () => {
|
|
4119
4160
|
if (closed || !this.collections.get(slug)?.ready) return;
|
|
4120
4161
|
const result = this.answer(slug, params, this.snapshotFor(slug, params));
|
|
4121
|
-
const signature = this.signature(slug, result.data, result.meta.total);
|
|
4162
|
+
const signature = `${result.fromCache ? "c" : "s"}${result.hasPendingWrites ? "p" : "-"}` + this.signature(slug, result.data, result.meta.total);
|
|
4122
4163
|
if (observer.settled && signature === observer.signature) return;
|
|
4123
4164
|
observer.signature = signature;
|
|
4124
4165
|
observer.settled = true;
|
|
@@ -4170,13 +4211,15 @@ var OfflineManager = class {
|
|
|
4170
4211
|
if (closed || !this.collections.get(slug)?.ready) return;
|
|
4171
4212
|
const row = this.localRow(slug, id);
|
|
4172
4213
|
const entry = this.collections.get(slug)?.rows.get(String(id));
|
|
4173
|
-
const
|
|
4214
|
+
const fromCache = !this.collections.get(slug)?.freshRows.has(String(id));
|
|
4215
|
+
const hasPendingWrites = this.hasPending(slug, id);
|
|
4216
|
+
const signature = `${fromCache ? "c" : "s"}${hasPendingWrites ? "p" : "-"}|` + (row === void 0 ? MISSING : `${String(id)}:${entry?.rev ?? 0}`);
|
|
4174
4217
|
if (observer.settled && signature === observer.signature) return;
|
|
4175
4218
|
observer.signature = signature;
|
|
4176
4219
|
observer.settled = true;
|
|
4177
4220
|
onResult(row, {
|
|
4178
|
-
fromCache
|
|
4179
|
-
hasPendingWrites
|
|
4221
|
+
fromCache,
|
|
4222
|
+
hasPendingWrites
|
|
4180
4223
|
});
|
|
4181
4224
|
}
|
|
4182
4225
|
};
|
|
@@ -4247,6 +4290,8 @@ var OfflineManager = class {
|
|
|
4247
4290
|
state = {
|
|
4248
4291
|
rows: /* @__PURE__ */ new Map(),
|
|
4249
4292
|
snapshots: /* @__PURE__ */ new Map(),
|
|
4293
|
+
fresh: /* @__PURE__ */ new Set(),
|
|
4294
|
+
freshRows: /* @__PURE__ */ new Set(),
|
|
4250
4295
|
absent: /* @__PURE__ */ new Set(),
|
|
4251
4296
|
ready: false
|
|
4252
4297
|
};
|
|
@@ -4306,6 +4351,7 @@ var OfflineManager = class {
|
|
|
4306
4351
|
answer(slug, params, snapshot) {
|
|
4307
4352
|
const state = this.collections.get(slug);
|
|
4308
4353
|
const exact = isExactlyEvaluable(params);
|
|
4354
|
+
const fromCache = !state?.fresh.has(buildQueryString(params));
|
|
4309
4355
|
if (!state) return {
|
|
4310
4356
|
data: [],
|
|
4311
4357
|
meta: {
|
|
@@ -4322,7 +4368,7 @@ var OfflineManager = class {
|
|
|
4322
4368
|
const local = runLocalQuery([...state.rows.values()].map((e) => e.row), params);
|
|
4323
4369
|
return {
|
|
4324
4370
|
...local,
|
|
4325
|
-
fromCache
|
|
4371
|
+
fromCache,
|
|
4326
4372
|
hasPendingWrites: local.data.some((row) => this.hasPending(slug, row.id)),
|
|
4327
4373
|
partial: true
|
|
4328
4374
|
};
|
|
@@ -4365,7 +4411,7 @@ var OfflineManager = class {
|
|
|
4365
4411
|
offset,
|
|
4366
4412
|
hasMore: snapshot.hasMore
|
|
4367
4413
|
},
|
|
4368
|
-
fromCache
|
|
4414
|
+
fromCache,
|
|
4369
4415
|
hasPendingWrites: rows.some((row) => this.hasPending(slug, row.id)),
|
|
4370
4416
|
partial: !exact
|
|
4371
4417
|
};
|
|
@@ -4373,6 +4419,7 @@ var OfflineManager = class {
|
|
|
4373
4419
|
localFind(slug, params) {
|
|
4374
4420
|
const state = this.collections.get(slug);
|
|
4375
4421
|
const snapshot = this.snapshotFor(slug, params);
|
|
4422
|
+
state?.fresh.delete(buildQueryString(params));
|
|
4376
4423
|
if (!snapshot && (!state || state.rows.size === 0)) throw offlineError(`Offline: no cached data for "${slug}".`);
|
|
4377
4424
|
const answer = this.answer(slug, params, snapshot);
|
|
4378
4425
|
return snapshot ? answer : {
|
|
@@ -4396,6 +4443,7 @@ var OfflineManager = class {
|
|
|
4396
4443
|
cachedAt,
|
|
4397
4444
|
rev: ++this.revCounter
|
|
4398
4445
|
});
|
|
4446
|
+
state.freshRows.delete(key);
|
|
4399
4447
|
this.forgetTombstone(slug, key);
|
|
4400
4448
|
this.writeCache(this.rowKey(slug, key), dehydrateRow(row), cachedAt);
|
|
4401
4449
|
this.evictRows(slug);
|
|
@@ -4412,8 +4460,9 @@ var OfflineManager = class {
|
|
|
4412
4460
|
const existed = state.rows.delete(key);
|
|
4413
4461
|
if (known) {
|
|
4414
4462
|
state.absent.add(key);
|
|
4463
|
+
state.freshRows.add(key);
|
|
4415
4464
|
this.writeCache(this.absentKey(slug, key), true);
|
|
4416
|
-
}
|
|
4465
|
+
} else state.freshRows.delete(key);
|
|
4417
4466
|
if (existed) this.deleteCache([this.rowKey(slug, key)]);
|
|
4418
4467
|
}
|
|
4419
4468
|
forgetTombstone(slug, key) {
|
|
@@ -4445,6 +4494,7 @@ var OfflineManager = class {
|
|
|
4445
4494
|
continue;
|
|
4446
4495
|
}
|
|
4447
4496
|
this.forgetTombstone(slug, key);
|
|
4497
|
+
state.freshRows.add(key);
|
|
4448
4498
|
const existing = state.rows.get(key);
|
|
4449
4499
|
if (existing && JSON.stringify(existing.row) === JSON.stringify(merged)) {
|
|
4450
4500
|
existing.cachedAt = cachedAt;
|
|
@@ -4515,6 +4565,7 @@ var OfflineManager = class {
|
|
|
4515
4565
|
const state = this.collectionState(slug);
|
|
4516
4566
|
const key = buildQueryString(params);
|
|
4517
4567
|
state.snapshots.set(key, snapshot);
|
|
4568
|
+
state.fresh.add(key);
|
|
4518
4569
|
this.writeCache(`${this.scope}|q|${slug}|${key}`, snapshot);
|
|
4519
4570
|
this.evictSnapshots(slug);
|
|
4520
4571
|
return snapshot;
|
|
@@ -4778,6 +4829,7 @@ var OfflineManager = class {
|
|
|
4778
4829
|
cachedAt,
|
|
4779
4830
|
rev: ++this.revCounter
|
|
4780
4831
|
});
|
|
4832
|
+
if (this.applyPendingToRow(slug, key, void 0, op.mutationId) === void 0) state.freshRows.add(key);
|
|
4781
4833
|
this.writeCache(this.rowKey(slug, key), dehydrateRow(merged), cachedAt);
|
|
4782
4834
|
}
|
|
4783
4835
|
/**
|
|
@@ -5017,14 +5069,7 @@ function createRebaseClient(options) {
|
|
|
5017
5069
|
} catch (e) {}
|
|
5018
5070
|
return session?.accessToken || options.token || "";
|
|
5019
5071
|
},
|
|
5020
|
-
onUnauthorized: options.onUnauthorized || (
|
|
5021
|
-
try {
|
|
5022
|
-
await auth.refreshSession();
|
|
5023
|
-
return true;
|
|
5024
|
-
} catch (e) {
|
|
5025
|
-
return false;
|
|
5026
|
-
}
|
|
5027
|
-
})
|
|
5072
|
+
onUnauthorized: options.onUnauthorized || (() => auth.handleUnauthorized())
|
|
5028
5073
|
});
|
|
5029
5074
|
auth.onAuthStateChange((event, session) => {
|
|
5030
5075
|
if (!ws) return;
|
|
@@ -5034,14 +5079,7 @@ function createRebaseClient(options) {
|
|
|
5034
5079
|
}
|
|
5035
5080
|
});
|
|
5036
5081
|
}
|
|
5037
|
-
if (!options.onUnauthorized) transport.setOnUnauthorized(
|
|
5038
|
-
try {
|
|
5039
|
-
await auth.refreshSession();
|
|
5040
|
-
return true;
|
|
5041
|
-
} catch (e) {
|
|
5042
|
-
return false;
|
|
5043
|
-
}
|
|
5044
|
-
});
|
|
5082
|
+
if (!options.onUnauthorized) transport.setOnUnauthorized(() => auth.handleUnauthorized());
|
|
5045
5083
|
/**
|
|
5046
5084
|
* Suggest the closest known collection key for a mistyped accessor.
|
|
5047
5085
|
* Uses edit-distance-1 and prefix matching — no external dependency.
|