@rdlabo/ionic-angular-kit 21.4.4 → 21.4.6
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/README.md +132 -18
- package/fesm2022/rdlabo-ionic-angular-kit-offline.mjs +167 -21
- package/fesm2022/rdlabo-ionic-angular-kit-offline.mjs.map +1 -1
- package/fesm2022/rdlabo-ionic-angular-kit.mjs +371 -39
- package/fesm2022/rdlabo-ionic-angular-kit.mjs.map +1 -1
- package/package.json +1 -1
- package/types/rdlabo-ionic-angular-kit-offline.d.ts +78 -26
- package/types/rdlabo-ionic-angular-kit.d.ts +147 -17
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ A small ergonomic kit for Ionic Angular applications. It provides:
|
|
|
4
4
|
|
|
5
5
|
- **KitStorageService** — a typed, write-loss-safe wrapper around `@ionic/storage-angular`
|
|
6
6
|
- **KitOverlayController** — a unified presenter for Ionic Modal, Toast, and Alert
|
|
7
|
-
- **Auth guards** — functional `
|
|
7
|
+
- **Auth guards** — functional guards plus shared `none` / `local` / `remote` runtime access
|
|
8
8
|
- **HTTP interceptor** — a fleet-canonical auth + retry + error-hook interceptor
|
|
9
9
|
- **KitRealtimeConnection** — foreground/network-aware Hibernation WebSocket reconnect and resync
|
|
10
10
|
- **KitAuthInputDirective** — sign-in email remember/prefill + iOS autofill workaround for `ion-input`
|
|
@@ -59,6 +59,8 @@ authentication and the stable `KIT_REALTIME_CLIENT_ID` through WebSocket subprot
|
|
|
59
59
|
putting credentials in the URL.
|
|
60
60
|
|
|
61
61
|
Domain event types, authorization, room selection, and REST resync behavior remain in the app.
|
|
62
|
+
Offline-capable authenticated clients set `requireRemoteAccess: true` in `realtimeOptions`; sockets then close on
|
|
63
|
+
`local` / `none` and reopen only after `KitAuthAccessService` publishes `remote`.
|
|
62
64
|
|
|
63
65
|
---
|
|
64
66
|
|
|
@@ -227,16 +229,42 @@ This centralizes presentation options, keeps component props and dismiss data ty
|
|
|
227
229
|
|
|
228
230
|
### Auth guards + provideKitAuth
|
|
229
231
|
|
|
230
|
-
Functional `CanActivateFn` guards for a
|
|
231
|
-
|
|
232
|
-
| State
|
|
233
|
-
|
|
|
234
|
-
| `'user'`
|
|
235
|
-
| `'confirm'`
|
|
236
|
-
| `'required'`
|
|
237
|
-
| `'anonymous'`
|
|
238
|
-
|
|
239
|
-
|
|
232
|
+
Functional `CanActivateFn` guards for a five-state auth model:
|
|
233
|
+
|
|
234
|
+
| State | Meaning |
|
|
235
|
+
| --------------- | -------------------------------------------------------- |
|
|
236
|
+
| `'user'` | Fully authenticated |
|
|
237
|
+
| `'confirm'` | Authenticated but email confirmation pending |
|
|
238
|
+
| `'required'` | Not authenticated |
|
|
239
|
+
| `'anonymous'` | Anonymous login active (can be prompted to register) |
|
|
240
|
+
| `'unavailable'` | The authentication authority cannot currently be reached |
|
|
241
|
+
|
|
242
|
+
**Convention:** every redirect path is supplied via `provideKitAuth`; the kit does not hard-code any routes.
|
|
243
|
+
`authState` and `redirects` are required. The app-specific hooks `onAuthorized`, `onUnauthenticated`, and
|
|
244
|
+
`onUnavailable` are optional. An authenticated user is allowed by default; unauthenticated and unavailable states
|
|
245
|
+
redirect by default.
|
|
246
|
+
|
|
247
|
+
`'required'` is an authoritative signed-out result. It must never be converted into offline access.
|
|
248
|
+
`'unavailable'` means the authentication authority could not produce a result. Likewise,
|
|
249
|
+
`isUnavailableError` must classify transport failures only; HTTP 401/403 are explicit denials and must return
|
|
250
|
+
`false`. `onUnavailable` authorizes the route for local-replica use only—it does not create an HTTP or realtime
|
|
251
|
+
credential.
|
|
252
|
+
|
|
253
|
+
`KitAuthAccessService` is the authoritative capability state for the rest of the application:
|
|
254
|
+
|
|
255
|
+
| Access mode | Local replica / outbox | Authenticated HTTP / realtime / sync |
|
|
256
|
+
| ----------- | ---------------------- | ------------------------------------ |
|
|
257
|
+
| `none` | blocked | blocked |
|
|
258
|
+
| `local` | allowed | blocked |
|
|
259
|
+
| `remote` | allowed | allowed |
|
|
260
|
+
|
|
261
|
+
Remote activation has two ordered phases. `activate()` installs the remotely verified identity without starting
|
|
262
|
+
transport. The guard then publishes `remote`, and only then calls `resume()` to start pull, outbox replay, and
|
|
263
|
+
realtime work. Returning plain `true` remains supported for applications that do not need phased activation.
|
|
264
|
+
When a protected guard starts a new asynchronous decision, any previously published `remote` capability is
|
|
265
|
+
immediately suspended to `none`; it is granted again only after the current lease completes successfully.
|
|
266
|
+
Once the authority returns `required` or `confirm`, an existing `local` capability is also suspended before any
|
|
267
|
+
anonymous-sign-in fallback runs. Only the `unavailable` path may retain or re-grant verified local access.
|
|
240
268
|
|
|
241
269
|
**Setup**
|
|
242
270
|
|
|
@@ -257,10 +285,38 @@ export const appConfig: ApplicationConfig = {
|
|
|
257
285
|
whenUnauthorized: '/auth', // kitRequireAuthorizedGuard
|
|
258
286
|
},
|
|
259
287
|
// onAuthorized / onUnauthenticated omitted → defaults (allow / redirect).
|
|
260
|
-
// Supply onAuthorized only when 'user' needs extra work
|
|
261
|
-
//
|
|
288
|
+
// Supply onAuthorized only when 'user' needs extra work. A phased result is preferred when
|
|
289
|
+
// activating the offline runtime:
|
|
290
|
+
// onAuthorized: async () => {
|
|
291
|
+
// const session = await auth.exchangeCredential();
|
|
292
|
+
// return {
|
|
293
|
+
// activate: (lease) => offline.prepareRemoteSession(
|
|
294
|
+
// session.userId, session.groupIds, session.subject, lease,
|
|
295
|
+
// ),
|
|
296
|
+
// resume: () => offline.resumeRemoteSession(),
|
|
297
|
+
// };
|
|
298
|
+
// },
|
|
262
299
|
// Supply onUnauthenticated only for a fallback such as anonymous sign-in:
|
|
263
300
|
// onUnauthenticated: async () => { await auth.signInAnonymously(); return true; },
|
|
301
|
+
// Supply onUnavailable only for a previously verified local replica:
|
|
302
|
+
// onUnavailable: async (_state, _error, lease) =>
|
|
303
|
+
// (await offline.activateOfflineSession(auth.currentSubject(), lease)) !== null,
|
|
304
|
+
// isUnavailableError: (error) => isOfflineFallbackError(error),
|
|
305
|
+
// Optionally recover automatically after the authority is reachable again:
|
|
306
|
+
// remoteRecovery: {
|
|
307
|
+
// availability: () => auth.authorityAvailable$,
|
|
308
|
+
// reauthenticate: async () => {
|
|
309
|
+
// const session = await auth.tryExchangeCredential();
|
|
310
|
+
// return session
|
|
311
|
+
// ? {
|
|
312
|
+
// activate: (lease) => offline.prepareRemoteSession(
|
|
313
|
+
// session.userId, session.groupIds, session.subject, lease,
|
|
314
|
+
// ),
|
|
315
|
+
// resume: () => offline.resumeRemoteSession(),
|
|
316
|
+
// }
|
|
317
|
+
// : false;
|
|
318
|
+
// },
|
|
319
|
+
// },
|
|
264
320
|
};
|
|
265
321
|
}),
|
|
266
322
|
],
|
|
@@ -341,6 +397,54 @@ Mutations are queued explicitly with `OfflineSyncService.enqueue`, not through H
|
|
|
341
397
|
Web storage uses Ionic Storage; iOS and Android use encrypted `@capacitor-community/sqlite`. Importing either the
|
|
342
398
|
primary entry point or `/offline` does not pull the optional native SQLite plugin into web-only applications.
|
|
343
399
|
|
|
400
|
+
For cold-start offline route access, `OfflineCoordinatorService.activateOfflineSession()` restores only a manifest
|
|
401
|
+
that is bound to a non-null authentication-provider subject. Supplying a currently known subject also rejects a
|
|
402
|
+
different user on a shared device. It activates local replica writes and durable outbox enqueue, but remote pull and
|
|
403
|
+
command replay remain disabled until online authentication completes the ordered
|
|
404
|
+
`prepareRemoteSession(...)` → publish `remote` → `resumeRemoteSession()` transition. `activateSession(...)` remains
|
|
405
|
+
available as a backward-compatible one-step API for callers that do not enforce shared access mode.
|
|
406
|
+
Explicit sign-out must first call `KitAuthAccessService.clear()` and then await `clearActiveSession()`. The first
|
|
407
|
+
step immediately invalidates every in-flight auth lease; the second serializes cleanup after local persistence
|
|
408
|
+
already in progress and removes the manifest and user replica.
|
|
409
|
+
|
|
410
|
+
```ts
|
|
411
|
+
provideKitAuth(() => {
|
|
412
|
+
const offline = inject(OfflineCoordinatorService);
|
|
413
|
+
return {
|
|
414
|
+
authState: () => auth.state$,
|
|
415
|
+
onAuthorized: async () => {
|
|
416
|
+
const session = await auth.exchangeCredential();
|
|
417
|
+
return {
|
|
418
|
+
activate: (lease) => offline.prepareRemoteSession(session.userId, session.groupIds, session.subject, lease),
|
|
419
|
+
resume: () => offline.resumeRemoteSession(),
|
|
420
|
+
};
|
|
421
|
+
},
|
|
422
|
+
onUnavailable: async (_state, _error, lease) =>
|
|
423
|
+
(await offline.activateOfflineSession(auth.currentSubject(), lease)) !== null,
|
|
424
|
+
isUnavailableError: isOfflineFallbackError,
|
|
425
|
+
remoteRecovery: {
|
|
426
|
+
availability: () => auth.authorityAvailable$,
|
|
427
|
+
reauthenticate: async () => {
|
|
428
|
+
const session = await auth.tryExchangeCredential();
|
|
429
|
+
return session
|
|
430
|
+
? {
|
|
431
|
+
activate: (lease) =>
|
|
432
|
+
offline.prepareRemoteSession(session.userId, session.groupIds, session.subject, lease),
|
|
433
|
+
resume: () => offline.resumeRemoteSession(),
|
|
434
|
+
}
|
|
435
|
+
: false;
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
redirects,
|
|
439
|
+
};
|
|
440
|
+
});
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
Register `offlineInterceptor` before `kitAuthInterceptor`. In local mode the auth interceptor synthesizes a
|
|
444
|
+
transport-unavailable error before generating credentials or touching the network; the outer offline interceptor
|
|
445
|
+
may then serve a matched `GET` from the replica. In `none` mode the same request is rejected and no local data is
|
|
446
|
+
returned.
|
|
447
|
+
|
|
344
448
|
The offline interceptor observes real transport responses to update API reachability. For matched `GET`
|
|
345
449
|
requests only, a transport failure with `status=0` may return a local replica response tagged
|
|
346
450
|
`X-Offline-Response: local`. `POST` and other write methods always go to transport unchanged; outbox replay
|
|
@@ -541,6 +645,9 @@ export const appConfig: ApplicationConfig = {
|
|
|
541
645
|
const auth = inject(AuthService);
|
|
542
646
|
const reload = inject(KitReloadAlertController);
|
|
543
647
|
return {
|
|
648
|
+
// Required for the new offline auth boundary. Kept opt-in so existing applications retain
|
|
649
|
+
// their current interceptor behavior until they wire KitAuthAccessService.
|
|
650
|
+
enforceAuthAccessMode: true,
|
|
544
651
|
getAuthHeaders: async (req) => ({
|
|
545
652
|
Authorization: `Bearer ${await auth.getToken()}`,
|
|
546
653
|
}),
|
|
@@ -561,13 +668,20 @@ export const appConfig: ApplicationConfig = {
|
|
|
561
668
|
};
|
|
562
669
|
```
|
|
563
670
|
|
|
671
|
+
For an offline replica, use
|
|
672
|
+
`withInterceptors([offlineInterceptor, kitAuthInterceptor])` in that order. Authentication/bootstrap endpoints that
|
|
673
|
+
must run before `remote` is granted must be explicitly covered by `bypass`; do not globally relax
|
|
674
|
+
`enforceAuthAccessMode`.
|
|
675
|
+
|
|
564
676
|
**Error dispatch** (after retries, in `catchError`):
|
|
565
677
|
|
|
566
|
-
1. `
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
678
|
+
1. With `enforceAuthAccessMode`, `401` / `403` → revoke access, notify the matching hook, and reject
|
|
679
|
+
without consulting `offlineFallback`
|
|
680
|
+
2. Otherwise, `offlineFallback` non-null → return fallback observable (no further hooks called)
|
|
681
|
+
3. `401` → `onUnauthorized` · `403` → `onForbidden`
|
|
682
|
+
4. `0` (connected) → `onNetworkError` · `429` → `onRateLimited(retryAfter?)` · `502/503/504` → `onServerBusy(status, retryAfter?)`
|
|
683
|
+
5. `400/422/500` with `error.message` → `onServerError`
|
|
684
|
+
6. anything else (`404`, …) → not handled here; the caller decides
|
|
571
685
|
|
|
572
686
|
Plus: a `getAuthHeaders` rejection → `onAuthError(request, error)` (the request is never sent).
|
|
573
687
|
|
|
@@ -22,7 +22,8 @@ const RESERVED_COLUMN_NAMES = new Set([
|
|
|
22
22
|
* Begins a type-safe replica entity schema definition for the given select shape.
|
|
23
23
|
*
|
|
24
24
|
* Every key in `TSelect` must appear exactly once in `fields`, and column nullability
|
|
25
|
-
* must match the select property nullability.
|
|
25
|
+
* must match the select property nullability. An entity may map at most one field with
|
|
26
|
+
* {@link serverId}; omit it for local-only projections that have no remote row identity.
|
|
26
27
|
*/
|
|
27
28
|
function defineReplicaEntity() {
|
|
28
29
|
return function defineReplicaEntityConfig(definition) {
|
|
@@ -75,6 +76,13 @@ function nullable(column) {
|
|
|
75
76
|
function serverId() {
|
|
76
77
|
return { kind: 'serverId' };
|
|
77
78
|
}
|
|
79
|
+
/** Rejects a remote identity on a local-only projection before it reaches platform storage. */
|
|
80
|
+
function assertOfflineReplicaServerId(schema, value) {
|
|
81
|
+
const hasServerId = schema.fields.some((field) => field.policy === 'serverId');
|
|
82
|
+
if (!hasServerId && value !== null) {
|
|
83
|
+
throw new Error(`Offline replica source "${schema.sourceKey}" does not define a serverId field.`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
78
86
|
/** Excludes a source property from SQLite while retaining it in the select shape. */
|
|
79
87
|
function ignored(reason) {
|
|
80
88
|
return { kind: 'ignored', reason };
|
|
@@ -84,9 +92,9 @@ function buildOfflineReplicaEntitySchema(definition) {
|
|
|
84
92
|
validateIdentifier(definition.sourceKey, 'source key');
|
|
85
93
|
const sourceKeys = Object.keys(definition.fields).sort();
|
|
86
94
|
const serverIdCount = sourceKeys.filter((sourceKey) => definition.fields[sourceKey]?.kind === 'serverId').length;
|
|
87
|
-
if (serverIdCount
|
|
88
|
-
throw new Error('Replica entity must define
|
|
89
|
-
const hasServerId =
|
|
95
|
+
if (serverIdCount > 1)
|
|
96
|
+
throw new Error('Replica entity must define at most one serverId field.');
|
|
97
|
+
const hasServerId = serverIdCount === 1;
|
|
90
98
|
const fields = sourceKeys.map((sourceKey) => {
|
|
91
99
|
const fieldDef = definition.fields[sourceKey];
|
|
92
100
|
return materializeFieldDescriptor(sourceKey, fieldDef);
|
|
@@ -957,6 +965,7 @@ class IonicOfflineRepository {
|
|
|
957
965
|
}
|
|
958
966
|
#validateReplicaRow(row) {
|
|
959
967
|
const schema = this.#resolveReplicaEntitySchema(row.sourceKey);
|
|
968
|
+
assertOfflineReplicaServerId(schema, row.serverId);
|
|
960
969
|
encodeOfflineReplicaValues(schema, row.values);
|
|
961
970
|
if (row.confirmedValues !== null)
|
|
962
971
|
encodeOfflineReplicaValues(schema, row.confirmedValues);
|
|
@@ -1319,13 +1328,15 @@ class OfflineSessionService {
|
|
|
1319
1328
|
this.#repository = inject(OFFLINE_REPOSITORY);
|
|
1320
1329
|
this.#activeManifest = signal(null, ...(ngDevMode ? [{ debugName: "#activeManifest" }] : /* istanbul ignore next */ []));
|
|
1321
1330
|
this.#initialized = false;
|
|
1322
|
-
this.#
|
|
1331
|
+
this.#localAccessThisRun = false;
|
|
1332
|
+
this.#remoteActivatedThisRun = false;
|
|
1323
1333
|
this.activeManifest = this.#activeManifest.asReadonly();
|
|
1324
1334
|
}
|
|
1325
1335
|
#repository;
|
|
1326
1336
|
#activeManifest;
|
|
1327
1337
|
#initialized;
|
|
1328
|
-
#
|
|
1338
|
+
#localAccessThisRun;
|
|
1339
|
+
#remoteActivatedThisRun;
|
|
1329
1340
|
async initialize() {
|
|
1330
1341
|
if (this.#initialized)
|
|
1331
1342
|
return;
|
|
@@ -1337,23 +1348,31 @@ class OfflineSessionService {
|
|
|
1337
1348
|
}
|
|
1338
1349
|
this.#initialized = true;
|
|
1339
1350
|
}
|
|
1340
|
-
async activateSession(userId, scopeIds, authSubject) {
|
|
1351
|
+
async activateSession(userId, scopeIds, authSubject, lease) {
|
|
1341
1352
|
await this.initialize();
|
|
1353
|
+
if (lease && !lease.isCurrent())
|
|
1354
|
+
return false;
|
|
1342
1355
|
const normalizedScopeIds = [...new Set(scopeIds)].filter((id) => id !== 0).sort((a, b) => a - b);
|
|
1343
1356
|
const previousUserId = await this.#repository.getLastUserId();
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1357
|
+
if (lease && !lease.isCurrent())
|
|
1358
|
+
return false;
|
|
1359
|
+
let previous = previousUserId === userId ? ((await this.#repository.getSessionManifest(userId)) ?? null) : null;
|
|
1360
|
+
if (lease && !lease.isCurrent())
|
|
1361
|
+
return false;
|
|
1347
1362
|
// A changed provider subject is a different person even when the product reuses its numeric id.
|
|
1348
1363
|
// This deliberately also clears legacy null -> known subject and known subject -> null transitions.
|
|
1349
1364
|
if (previousUserId !== null && (previousUserId !== userId || previous?.authSubject !== authSubject)) {
|
|
1350
1365
|
await this.#repository.clearUser(previousUserId);
|
|
1366
|
+
if (lease && !lease.isCurrent())
|
|
1367
|
+
return false;
|
|
1351
1368
|
previous = null;
|
|
1352
1369
|
}
|
|
1353
1370
|
const active = new Set(normalizedScopeIds);
|
|
1354
1371
|
await Promise.all((previous?.scopeIds ?? [])
|
|
1355
1372
|
.filter((groupId) => !active.has(groupId))
|
|
1356
1373
|
.map((groupId) => this.#repository.clearGroup({ userId, groupId })));
|
|
1374
|
+
if (lease && !lease.isCurrent())
|
|
1375
|
+
return false;
|
|
1357
1376
|
const manifest = {
|
|
1358
1377
|
userId,
|
|
1359
1378
|
scopeIds: normalizedScopeIds,
|
|
@@ -1361,9 +1380,15 @@ class OfflineSessionService {
|
|
|
1361
1380
|
updatedAt: Date.now(),
|
|
1362
1381
|
};
|
|
1363
1382
|
await this.#repository.setLastUserId(userId);
|
|
1383
|
+
if (lease && !lease.isCurrent())
|
|
1384
|
+
return false;
|
|
1364
1385
|
await this.#repository.putSessionManifest(userId, manifest);
|
|
1386
|
+
if (lease && !lease.isCurrent())
|
|
1387
|
+
return false;
|
|
1365
1388
|
this.#activeManifest.set(manifest);
|
|
1366
|
-
this.#
|
|
1389
|
+
this.#localAccessThisRun = true;
|
|
1390
|
+
this.#remoteActivatedThisRun = true;
|
|
1391
|
+
return lease ? true : undefined;
|
|
1367
1392
|
}
|
|
1368
1393
|
async clearActiveSession() {
|
|
1369
1394
|
await this.initialize();
|
|
@@ -1371,12 +1396,62 @@ class OfflineSessionService {
|
|
|
1371
1396
|
if (userId !== null)
|
|
1372
1397
|
await this.#repository.clearUser(userId);
|
|
1373
1398
|
this.#activeManifest.set(null);
|
|
1374
|
-
this.#
|
|
1399
|
+
this.#localAccessThisRun = false;
|
|
1400
|
+
this.#remoteActivatedThisRun = false;
|
|
1375
1401
|
}
|
|
1376
|
-
|
|
1402
|
+
/** Disable remote pull/replay eligibility while retaining the verified local manifest. */
|
|
1403
|
+
async suspendRemoteSession() {
|
|
1404
|
+
await this.initialize();
|
|
1405
|
+
this.#remoteActivatedThisRun = false;
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Returns the persisted identity boundary for local-only route access.
|
|
1409
|
+
*
|
|
1410
|
+
* @remarks
|
|
1411
|
+
* This does not activate the sync context. Call it only after the authentication authority has
|
|
1412
|
+
* been classified as unavailable, never after explicit sign-out or HTTP 401/403. Legacy
|
|
1413
|
+
* manifests without an authentication-provider subject are rejected.
|
|
1414
|
+
*
|
|
1415
|
+
* @param authSubject - A currently known provider subject. When supplied, it must match the
|
|
1416
|
+
* persisted subject.
|
|
1417
|
+
*/
|
|
1418
|
+
async getOfflineAccessManifest(authSubject) {
|
|
1377
1419
|
await this.initialize();
|
|
1378
|
-
|
|
1420
|
+
const manifest = this.#activeManifest();
|
|
1421
|
+
if (!manifest?.authSubject || (authSubject !== undefined && manifest.authSubject !== authSubject)) {
|
|
1422
|
+
return null;
|
|
1423
|
+
}
|
|
1424
|
+
return { ...manifest, scopeIds: [...manifest.scopeIds] };
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Activates a previously verified manifest for local replica and outbox access only.
|
|
1428
|
+
*
|
|
1429
|
+
* @remarks
|
|
1430
|
+
* This never enables pull or command replay. A later successful remote authentication must call
|
|
1431
|
+
* {@link activateSession} before synchronization can use transport.
|
|
1432
|
+
*
|
|
1433
|
+
* @param authSubject - A currently known provider subject. When supplied, it must match the
|
|
1434
|
+
* persisted subject.
|
|
1435
|
+
*/
|
|
1436
|
+
async activateOfflineSession(authSubject, lease) {
|
|
1437
|
+
const manifest = await this.getOfflineAccessManifest(authSubject);
|
|
1438
|
+
if (lease && !lease.isCurrent())
|
|
1379
1439
|
return null;
|
|
1440
|
+
this.#localAccessThisRun = manifest !== null;
|
|
1441
|
+
this.#remoteActivatedThisRun = false;
|
|
1442
|
+
return manifest;
|
|
1443
|
+
}
|
|
1444
|
+
/** Returns the session allowed to use the local replica and append outbox commands. */
|
|
1445
|
+
async getLocalSession() {
|
|
1446
|
+
await this.initialize();
|
|
1447
|
+
return this.#localAccessThisRun ? this.#sessionFromManifest() : null;
|
|
1448
|
+
}
|
|
1449
|
+
/** Returns the remotely authenticated session eligible for pull and command replay. */
|
|
1450
|
+
async getSession() {
|
|
1451
|
+
await this.initialize();
|
|
1452
|
+
return this.#remoteActivatedThisRun ? this.#sessionFromManifest() : null;
|
|
1453
|
+
}
|
|
1454
|
+
#sessionFromManifest() {
|
|
1380
1455
|
const manifest = this.#activeManifest();
|
|
1381
1456
|
return manifest
|
|
1382
1457
|
? { userId: manifest.userId, scopes: manifest.scopeIds.map((groupId) => ({ userId: manifest.userId, groupId })) }
|
|
@@ -1483,6 +1558,13 @@ class OfflineSyncService {
|
|
|
1483
1558
|
if (this.#network.connected())
|
|
1484
1559
|
this.#flushInBackground();
|
|
1485
1560
|
}
|
|
1561
|
+
/** Restores local outbox visibility without enabling pull or replay transport. */
|
|
1562
|
+
async refreshLocalSession() {
|
|
1563
|
+
await this.initialize();
|
|
1564
|
+
await this.#discoverLocalScopes();
|
|
1565
|
+
await this.#restoreInterruptedCommands();
|
|
1566
|
+
await this.#refreshState();
|
|
1567
|
+
}
|
|
1486
1568
|
async resetSession() {
|
|
1487
1569
|
this.#invalidateFlush();
|
|
1488
1570
|
await this.#waitForSendingTransitions();
|
|
@@ -1499,7 +1581,7 @@ class OfflineSyncService {
|
|
|
1499
1581
|
}
|
|
1500
1582
|
async #enqueue(request, options) {
|
|
1501
1583
|
await this.initialize();
|
|
1502
|
-
const session = await this.#
|
|
1584
|
+
const session = await this.#getLocalSession();
|
|
1503
1585
|
if (!session)
|
|
1504
1586
|
throw new Error('Cannot enqueue an offline command without an authenticated user');
|
|
1505
1587
|
const userId = session.userId;
|
|
@@ -1824,6 +1906,19 @@ class OfflineSyncService {
|
|
|
1824
1906
|
}
|
|
1825
1907
|
async #discoverScopes(generation = this.#generation) {
|
|
1826
1908
|
const session = await this.#context.getSession();
|
|
1909
|
+
if (!this.#isCurrent(generation))
|
|
1910
|
+
return false;
|
|
1911
|
+
if (!session) {
|
|
1912
|
+
return false;
|
|
1913
|
+
}
|
|
1914
|
+
this.#setActiveUser(session.userId);
|
|
1915
|
+
this.#knownScopes.clear();
|
|
1916
|
+
for (const scope of session.scopes)
|
|
1917
|
+
this.#knownScopes.set(this.#scopeKey(scope), scope);
|
|
1918
|
+
return true;
|
|
1919
|
+
}
|
|
1920
|
+
async #discoverLocalScopes(generation = this.#generation) {
|
|
1921
|
+
const session = await this.#getLocalSession();
|
|
1827
1922
|
if (!this.#isCurrent(generation))
|
|
1828
1923
|
return false;
|
|
1829
1924
|
if (!session) {
|
|
@@ -1837,6 +1932,9 @@ class OfflineSyncService {
|
|
|
1837
1932
|
this.#knownScopes.set(this.#scopeKey(scope), scope);
|
|
1838
1933
|
return true;
|
|
1839
1934
|
}
|
|
1935
|
+
#getLocalSession() {
|
|
1936
|
+
return this.#context.getLocalSession?.() ?? this.#context.getSession();
|
|
1937
|
+
}
|
|
1840
1938
|
#setActiveUser(userId) {
|
|
1841
1939
|
if (this.#activeUserId === userId)
|
|
1842
1940
|
return;
|
|
@@ -1955,6 +2053,8 @@ class OfflineCoordinatorService {
|
|
|
1955
2053
|
this.#network = inject(OfflineNetworkService);
|
|
1956
2054
|
this.#sync = inject(OfflineSyncService);
|
|
1957
2055
|
this.#session = inject(OfflineSessionService);
|
|
2056
|
+
this.#transitionRevision = 0;
|
|
2057
|
+
this.#transitionTail = Promise.resolve();
|
|
1958
2058
|
this.networkState = this.#network.state;
|
|
1959
2059
|
this.syncState = this.#sync.syncState;
|
|
1960
2060
|
this.pendingCount = this.#sync.pendingCount;
|
|
@@ -1964,19 +2064,56 @@ class OfflineCoordinatorService {
|
|
|
1964
2064
|
#network;
|
|
1965
2065
|
#sync;
|
|
1966
2066
|
#session;
|
|
2067
|
+
#transitionRevision;
|
|
2068
|
+
#transitionTail;
|
|
1967
2069
|
async initialize() {
|
|
1968
2070
|
await Promise.all([this.#repository.initialize(), this.#network.initialize()]);
|
|
1969
2071
|
await this.#session.initialize();
|
|
1970
2072
|
await this.#sync.initialize();
|
|
1971
2073
|
}
|
|
1972
2074
|
async activateSession(userId, scopeIds, authSubject) {
|
|
1973
|
-
await this
|
|
1974
|
-
|
|
2075
|
+
if (!(await this.prepareRemoteSession(userId, scopeIds, authSubject)))
|
|
2076
|
+
return;
|
|
2077
|
+
await this.resumeRemoteSession();
|
|
2078
|
+
}
|
|
2079
|
+
/** Installs a remotely verified identity without starting pull or outbox replay. */
|
|
2080
|
+
prepareRemoteSession(userId, scopeIds, authSubject, authLease) {
|
|
2081
|
+
const revision = ++this.#transitionRevision;
|
|
2082
|
+
const lease = this.#lease(revision, authLease);
|
|
2083
|
+
return this.#enqueueTransition(async () => {
|
|
2084
|
+
await this.#sync.resetSession();
|
|
2085
|
+
await this.#session.suspendRemoteSession();
|
|
2086
|
+
if (!lease.isCurrent())
|
|
2087
|
+
return false;
|
|
2088
|
+
return this.#session.activateSession(userId, scopeIds, authSubject, lease);
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
2091
|
+
/** Starts pull and outbox replay after the caller has published remote access. */
|
|
2092
|
+
async resumeRemoteSession() {
|
|
1975
2093
|
await this.#sync.refreshSession();
|
|
1976
2094
|
}
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
2095
|
+
/**
|
|
2096
|
+
* Activates a restored identity for local replica/outbox use without enabling transport sync.
|
|
2097
|
+
*/
|
|
2098
|
+
activateOfflineSession(authSubject, authLease) {
|
|
2099
|
+
const revision = ++this.#transitionRevision;
|
|
2100
|
+
const lease = this.#lease(revision, authLease);
|
|
2101
|
+
return this.#enqueueTransition(async () => {
|
|
2102
|
+
await this.#sync.resetSession();
|
|
2103
|
+
if (!lease.isCurrent())
|
|
2104
|
+
return null;
|
|
2105
|
+
const manifest = await this.#session.activateOfflineSession(authSubject, lease);
|
|
2106
|
+
if (manifest && lease.isCurrent())
|
|
2107
|
+
await this.#sync.refreshLocalSession();
|
|
2108
|
+
return lease.isCurrent() ? manifest : null;
|
|
2109
|
+
});
|
|
2110
|
+
}
|
|
2111
|
+
clearActiveSession() {
|
|
2112
|
+
++this.#transitionRevision;
|
|
2113
|
+
return this.#enqueueTransition(async () => {
|
|
2114
|
+
await this.#sync.resetSession();
|
|
2115
|
+
await this.#session.clearActiveSession();
|
|
2116
|
+
});
|
|
1980
2117
|
}
|
|
1981
2118
|
async prepareLogout(action) {
|
|
1982
2119
|
if (action === 'cancel')
|
|
@@ -1991,6 +2128,14 @@ class OfflineCoordinatorService {
|
|
|
1991
2128
|
flush() {
|
|
1992
2129
|
return this.#sync.flush();
|
|
1993
2130
|
}
|
|
2131
|
+
#lease(revision, authLease) {
|
|
2132
|
+
return { isCurrent: () => revision === this.#transitionRevision && (authLease?.isCurrent() ?? true) };
|
|
2133
|
+
}
|
|
2134
|
+
#enqueueTransition(operation) {
|
|
2135
|
+
const transition = this.#transitionTail.then(operation, operation);
|
|
2136
|
+
this.#transitionTail = transition.then(() => undefined, () => undefined);
|
|
2137
|
+
return transition;
|
|
2138
|
+
}
|
|
1994
2139
|
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: OfflineCoordinatorService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
1995
2140
|
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.2.17", ngImport: i0, type: OfflineCoordinatorService, providedIn: 'root' }); }
|
|
1996
2141
|
}
|
|
@@ -2416,6 +2561,7 @@ class SqliteOfflineRepository {
|
|
|
2416
2561
|
}
|
|
2417
2562
|
#putReplicaRow(databaseId, row) {
|
|
2418
2563
|
const schema = this.#resolveReplicaEntitySchema(row.sourceKey);
|
|
2564
|
+
assertOfflineReplicaServerId(schema, row.serverId);
|
|
2419
2565
|
const encoded = encodeOfflineReplicaValues(schema, row.values);
|
|
2420
2566
|
const confirmedValues = row.confirmedValues === null ? null : projectOfflineReplicaValues(schema, row.confirmedValues);
|
|
2421
2567
|
const { sql, domainColumns } = this.#buildReplicaUpsertStatement(schema);
|
|
@@ -2637,5 +2783,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImpo
|
|
|
2637
2783
|
* Generated bundle index. Do not edit.
|
|
2638
2784
|
*/
|
|
2639
2785
|
|
|
2640
|
-
export { COMMUNITY_SQLITE, DEFAULT_OFFLINE_COMMAND_HOOKS, IonicOfflineRepository, OFFLINE_BYPASS, OFFLINE_COMMAND_EXECUTOR, OFFLINE_COMMAND_HOOKS, OFFLINE_KIT_OPTIONS, OFFLINE_REPLICA_PULLER, OFFLINE_REPOSITORY, OFFLINE_REQUEST_POLICIES, OFFLINE_RESPONSE_HEADER, OFFLINE_SCHEMA_VERSION, OFFLINE_SYNC_CONTEXT, OfflineCoordinatorService, OfflineNetworkService, OfflinePayloadValidationError, OfflineReplicaPullService, OfflineRequestFallbackService, OfflineRequestPolicyRegistry, OfflineSessionService, OfflineSyncService, SqliteOfflineRepository, booleanColumn, createCommunitySqliteDriver, datetime, decodeOfflineReplicaValues, defineOfflineReplicaSchema, defineReplicaEntity, encodeOfflineReplicaValues, ignored, integer, isOfflineFallbackError, json, nullable, offlineInterceptor, projectOfflineReplicaValues, provideOffline, provideOfflineRequestPolicy, real, selectOfflineRepository, serverId, sha256OfflineReplicaSchema, text };
|
|
2786
|
+
export { COMMUNITY_SQLITE, DEFAULT_OFFLINE_COMMAND_HOOKS, IonicOfflineRepository, OFFLINE_BYPASS, OFFLINE_COMMAND_EXECUTOR, OFFLINE_COMMAND_HOOKS, OFFLINE_KIT_OPTIONS, OFFLINE_REPLICA_PULLER, OFFLINE_REPOSITORY, OFFLINE_REQUEST_POLICIES, OFFLINE_RESPONSE_HEADER, OFFLINE_SCHEMA_VERSION, OFFLINE_SYNC_CONTEXT, OfflineCoordinatorService, OfflineNetworkService, OfflinePayloadValidationError, OfflineReplicaPullService, OfflineRequestFallbackService, OfflineRequestPolicyRegistry, OfflineSessionService, OfflineSyncService, SqliteOfflineRepository, assertOfflineReplicaServerId, booleanColumn, createCommunitySqliteDriver, datetime, decodeOfflineReplicaValues, defineOfflineReplicaSchema, defineReplicaEntity, encodeOfflineReplicaValues, ignored, integer, isOfflineFallbackError, json, nullable, offlineInterceptor, projectOfflineReplicaValues, provideOffline, provideOfflineRequestPolicy, real, selectOfflineRepository, serverId, sha256OfflineReplicaSchema, text };
|
|
2641
2787
|
//# sourceMappingURL=rdlabo-ionic-angular-kit-offline.mjs.map
|