@rdlabo/ionic-angular-kit 22.0.0-beta.pr99.sha950c83950d41 → 22.0.1-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.
@@ -941,22 +941,36 @@ class OfflineReplicaTransientWriteError extends Error {
941
941
  this.name = 'OfflineReplicaTransientWriteError';
942
942
  }
943
943
  }
944
- function normalizeOfflineReplicaTransientWriteError(error) {
945
- if (error instanceof OfflineReplicaTransientWriteError)
946
- return error;
944
+ function transientSqliteLockReason(error) {
947
945
  const code = typeof error === 'object' && error !== null && typeof error.code === 'string'
948
946
  ? error.code.toUpperCase()
949
947
  : '';
950
948
  const message = error instanceof Error ? error.message : typeof error === 'string' ? error : '';
951
949
  const normalizedMessage = message.toUpperCase();
952
950
  if (code.includes('SQLITE_BUSY') || normalizedMessage.includes('SQLITE_BUSY')) {
953
- return new OfflineReplicaTransientWriteError('sqlite_busy', message || 'SQLite is busy.', { cause: error });
951
+ return 'sqlite_busy';
954
952
  }
955
953
  if (code.includes('SQLITE_LOCKED') ||
956
954
  normalizedMessage.includes('SQLITE_LOCKED') ||
957
955
  message.includes('database is locked') ||
958
956
  message.includes('database table is locked')) {
959
- return new OfflineReplicaTransientWriteError('sqlite_locked', message || 'SQLite database is locked.', { cause: error });
957
+ return 'sqlite_locked';
958
+ }
959
+ return null;
960
+ }
961
+ /** Returns whether SQLite reported a transient busy/locked condition in any supported error shape. */
962
+ function isTransientSqliteLockError(error) {
963
+ return error instanceof OfflineReplicaTransientWriteError
964
+ ? error.reason === 'sqlite_busy' || error.reason === 'sqlite_locked'
965
+ : transientSqliteLockReason(error) !== null;
966
+ }
967
+ function normalizeOfflineReplicaTransientWriteError(error) {
968
+ if (error instanceof OfflineReplicaTransientWriteError)
969
+ return error;
970
+ const reason = transientSqliteLockReason(error);
971
+ if (reason) {
972
+ const message = error instanceof Error ? error.message : typeof error === 'string' ? error : '';
973
+ return new OfflineReplicaTransientWriteError(reason, message || (reason === 'sqlite_busy' ? 'SQLite is busy.' : 'SQLite database is locked.'), { cause: error });
960
974
  }
961
975
  return error;
962
976
  }
@@ -1198,29 +1212,21 @@ class IonicOfflineRepository {
1198
1212
  return commands;
1199
1213
  });
1200
1214
  }
1215
+ /** @deprecated Use {@link transactReplica} with `putCommands`. */
1201
1216
  replaceCommand(command) {
1202
1217
  return this.putCommand(command);
1203
1218
  }
1204
- async removeCommand(commandId) {
1205
- await this.initialize();
1206
- await this.#mutateRecord(OUTBOX_KEY, (commands) => {
1207
- delete commands[commandId];
1208
- return commands;
1209
- });
1219
+ /** @deprecated Use {@link transactReplica} with `removeCommandIds`. */
1220
+ removeCommand(commandId) {
1221
+ return this.transactReplica({ removeCommandIds: [commandId] });
1210
1222
  }
1211
- async putPullAttention(attention) {
1212
- await this.initialize();
1213
- await this.#mutateRecord(PULL_ATTENTIONS_KEY, (attentions) => {
1214
- attentions[this.#cursorKey(attention)] = attention;
1215
- return attentions;
1216
- });
1223
+ /** @deprecated Use {@link transactReplica} with `putPullAttentions`. */
1224
+ putPullAttention(attention) {
1225
+ return this.transactReplica({ putPullAttentions: [attention] });
1217
1226
  }
1218
- async removePullAttention(scope) {
1219
- await this.initialize();
1220
- await this.#mutateRecord(PULL_ATTENTIONS_KEY, (attentions) => {
1221
- delete attentions[this.#cursorKey(scope)];
1222
- return attentions;
1223
- });
1227
+ /** @deprecated Use {@link transactReplica} with `removePullAttentions`. */
1228
+ removePullAttention(scope) {
1229
+ return this.transactReplica({ removePullAttentions: [scope] });
1224
1230
  }
1225
1231
  async clearUser(userId) {
1226
1232
  await this.initialize();
@@ -1821,7 +1827,7 @@ class OfflineReplicaMutationCoordinator {
1821
1827
  return this.#enqueue(() => {
1822
1828
  if (!this.#repository)
1823
1829
  throw new Error('Offline repository is not configured.');
1824
- const atomicMutation = this.#repository?.[OFFLINE_REPOSITORY_ATOMIC_MUTATION];
1830
+ const atomicMutation = this.#repository[OFFLINE_REPOSITORY_ATOMIC_MUTATION];
1825
1831
  return atomicMutation ? atomicMutation.call(this.#repository, operation) : operation(this.#repository);
1826
1832
  });
1827
1833
  }
@@ -2255,7 +2261,7 @@ class OfflineReplicaPullService {
2255
2261
  { putRows, removeRows },
2256
2262
  { putRows: projection?.putRows ?? [], removeRows: projection?.removeRows ?? [] },
2257
2263
  ]);
2258
- const rematerialized = await this.#rematerializePendingAggregates(scope, userCommands, scopeCommands, confirmedAndProjected, putCommands, removeCommandIds, rematerializeAfter);
2264
+ const rematerialized = await this.#rematerializePendingAggregates(userCommands, scopeCommands, confirmedAndProjected, putCommands, removeCommandIds, rematerializeAfter);
2259
2265
  const finalRows = this.#mergeRowMutations([confirmedAndProjected, rematerialized]);
2260
2266
  const removedCommands = [...removeCommandIds]
2261
2267
  .map((commandId) => userCommands.find((command) => command.commandId === commandId) ??
@@ -2396,7 +2402,7 @@ class OfflineReplicaPullService {
2396
2402
  this.#assertPullChange(change, index);
2397
2403
  }
2398
2404
  }
2399
- async #rematerializePendingAggregates(scope, userCommands, scopeCommands, currentRows, putCommands, removeCommandIds, seeds) {
2405
+ async #rematerializePendingAggregates(userCommands, scopeCommands, currentRows, putCommands, removeCommandIds, seeds) {
2400
2406
  const remaining = [...userCommands, ...scopeCommands]
2401
2407
  .filter((command, index, all) => all.findIndex((candidate) => candidate.commandId === command.commandId) === index)
2402
2408
  .filter((command) => !removeCommandIds.has(command.commandId))
@@ -2460,7 +2466,6 @@ class OfflineReplicaPullService {
2460
2466
  putRows.push(...(mutations.putRows ?? []));
2461
2467
  removeRows.push(...(mutations.removeRows ?? []));
2462
2468
  }
2463
- void scope;
2464
2469
  return { putRows, removeRows };
2465
2470
  }
2466
2471
  async #overlayBaseRow(command, overlay, removed) {
@@ -3272,11 +3277,11 @@ class OfflineSyncService {
3272
3277
  const scope = { userId, scopeId: request.scopeId };
3273
3278
  this.noteScope(scope);
3274
3279
  const commandIdentity = offlineCommandLookupIdentity(request.identity);
3275
- const normalized = await this.#normalizeEnqueueRequest(scope, request, commandIdentity, repository);
3276
3280
  const commandId = request.commandId ?? crypto.randomUUID();
3277
3281
  if (typeof commandId !== 'string' || commandId.length === 0 || commandId.length > 255) {
3278
3282
  throw new Error('Offline command id must contain between 1 and 255 characters.');
3279
3283
  }
3284
+ assertOfflinePayload(request.payload);
3280
3285
  const sourceKey = this.#hooks.entityType(request);
3281
3286
  const localOnlyFootprint = this.#normalizedLocalOnlyFootprint(scope, request.localOnlyFootprint);
3282
3287
  if (replaced)
@@ -3288,9 +3293,9 @@ class OfflineSyncService {
3288
3293
  sourceKey,
3289
3294
  identity: commandIdentity,
3290
3295
  operation: request.operation,
3291
- payload: normalized.payload,
3296
+ payload: request.payload,
3292
3297
  replicaMutation: request.replicaMutation ?? 'upsert',
3293
- baseRevision: normalized.baseRevision,
3298
+ baseRevision: request.baseRevision ?? null,
3294
3299
  state: 'pending',
3295
3300
  attempts: 0,
3296
3301
  retryAt: null,
@@ -3307,21 +3312,23 @@ class OfflineSyncService {
3307
3312
  canonicalOfflineCommandIdentity(replaced.identity) !== canonicalOfflineCommandIdentity(command.identity))) {
3308
3313
  throw new Error('Offline replacement command must address the same aggregate and replica identity.');
3309
3314
  }
3310
- const entityType = command.sourceKey;
3311
- const schema = this.#entitySchema(entityType);
3315
+ const schema = this.#entitySchema(sourceKey);
3312
3316
  if (schema.identity.kind === 'localOnly') {
3313
- throw new Error(`Offline replica source "${entityType}" is local-only and cannot be added to the Outbox.`);
3317
+ throw new Error(`Offline replica source "${sourceKey}" is local-only and cannot be added to the Outbox.`);
3314
3318
  }
3315
3319
  if (schema.identity.kind === 'generated' && request.identity.kind !== 'generated') {
3316
- throw new Error(`Offline replica source "${entityType}" requires generated identity.`);
3320
+ throw new Error(`Offline replica source "${sourceKey}" requires generated identity.`);
3317
3321
  }
3318
3322
  if (schema.identity.kind === 'naturalKey' && request.identity.kind !== 'natural') {
3319
- throw new Error(`Offline replica source "${entityType}" requires natural identity.`);
3323
+ throw new Error(`Offline replica source "${sourceKey}" requires natural identity.`);
3320
3324
  }
3321
3325
  if (request.replicaMutation === 'delete' && !repository.getReplicaRowIncludingPendingDelete) {
3322
3326
  throw new Error('Offline repository does not support durable replica delete tombstones.');
3323
3327
  }
3324
- const existing = await this.#getReplicaRowForSync(scope, entityType, commandIdentity, repository);
3328
+ const existing = await this.#getReplicaRowForSync(scope, sourceKey, commandIdentity, repository);
3329
+ if (existing?.serverRevision != null && existing.serverRevision !== command.baseRevision) {
3330
+ command = { ...command, baseRevision: existing.serverRevision };
3331
+ }
3325
3332
  const generatedIdentity = request.identity.kind === 'generated' ? request.identity : null;
3326
3333
  const initialRemoteId = this.#initialRemoteId(schema, existing?.identity.kind === 'generated' ? existing.identity.remoteId : null, generatedIdentity?.remoteId);
3327
3334
  this.#assertRemoteIdHint(schema, existing?.identity.kind === 'generated' ? existing.identity.remoteId : null, generatedIdentity?.remoteIdHint);
@@ -3334,7 +3341,7 @@ class OfflineSyncService {
3334
3341
  request.identity.kind === 'natural' &&
3335
3342
  canonicalOfflineRemoteIdentity(schema, { naturalKey: request.identity.naturalKey }) !==
3336
3343
  canonicalOfflineRemoteIdentity(schema, { naturalKey: naturalKey })) {
3337
- throw new Error(`Offline command naturalKey must match replica identity for "${entityType}".`);
3344
+ throw new Error(`Offline command naturalKey must match replica identity for "${sourceKey}".`);
3338
3345
  }
3339
3346
  const remoteIdentity = schema.identity.kind === 'generated'
3340
3347
  ? initialRemoteId === null
@@ -3349,11 +3356,11 @@ class OfflineSyncService {
3349
3356
  canonicalOfflineRemoteIdentity(schema, {
3350
3357
  naturalKey: existing.identity.kind === 'natural' ? existing.identity.naturalKey : offlineNaturalKeyFromValues(schema, existing.values),
3351
3358
  }) !== canonicalValuesKey) {
3352
- throw new Error(`Offline replica naturalKey is immutable and must match command identity for "${entityType}".`);
3359
+ throw new Error(`Offline replica naturalKey is immutable and must match command identity for "${sourceKey}".`);
3353
3360
  }
3354
3361
  }
3355
3362
  if (remoteIdentity !== null) {
3356
- const mapped = await repository.getReplicaRowByRemoteIdentity(scope, entityType, remoteIdentity);
3363
+ const mapped = await repository.getReplicaRowByRemoteIdentity(scope, sourceKey, remoteIdentity);
3357
3364
  if (mapped !== null && !commandIdentityMatchesReplicaRow(schema, mapped, commandIdentity)) {
3358
3365
  if ('remoteId' in remoteIdentity) {
3359
3366
  throw new Error(`Offline replica remote id ${String(remoteIdentity.remoteId)} is already mapped to another row.`);
@@ -3361,7 +3368,6 @@ class OfflineSyncService {
3361
3368
  throw new Error(`Offline replica remote identity is already mapped to another row.`);
3362
3369
  }
3363
3370
  }
3364
- this.#canonicalJson(normalized.payload);
3365
3371
  const seedBaseRow = existing
3366
3372
  ? undefined
3367
3373
  : request.identity.kind === 'generated'
@@ -4103,15 +4109,6 @@ class OfflineSyncService {
4103
4109
  }
4104
4110
  return true;
4105
4111
  }
4106
- async #normalizeEnqueueRequest(scope, request, commandIdentity, repository) {
4107
- let baseRevision = request.baseRevision ?? null;
4108
- const sourceKey = this.#hooks.entityType(request);
4109
- const row = await this.#getReplicaRowForSync(scope, sourceKey, commandIdentity, repository);
4110
- if (row?.serverRevision != null && row.serverRevision !== baseRevision) {
4111
- baseRevision = row.serverRevision;
4112
- }
4113
- return { payload: request.payload, baseRevision };
4114
- }
4115
4112
  async #completeCommand(commands, command, result, generation) {
4116
4113
  return this.#serializeReplicaMutation((repository) => this.#completeCommandLocked(commands, command, result, generation, repository));
4117
4114
  }
@@ -4617,27 +4614,6 @@ class OfflineSyncService {
4617
4614
  async #waitForSendingTransitions() {
4618
4615
  await Promise.allSettled([...this.#sendingTransitions, ...this.#commandSendTransitions.values()]);
4619
4616
  }
4620
- #canonicalJson(value) {
4621
- if (Array.isArray(value))
4622
- return `[${value.map((item) => this.#canonicalJson(item)).join(',')}]`;
4623
- if (value !== null && typeof value === 'object') {
4624
- if (Object.getPrototypeOf(value) !== Object.prototype) {
4625
- throw new OfflinePayloadValidationError();
4626
- }
4627
- return `{${Object.entries(value)
4628
- .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
4629
- .map(([key, item]) => `${JSON.stringify(key)}:${this.#canonicalJson(item)}`)
4630
- .join(',')}}`;
4631
- }
4632
- if (typeof value === 'number' && !Number.isFinite(value)) {
4633
- throw new OfflinePayloadValidationError();
4634
- }
4635
- const serialized = JSON.stringify(value);
4636
- if (typeof serialized !== 'string') {
4637
- throw new OfflinePayloadValidationError();
4638
- }
4639
- return serialized;
4640
- }
4641
4617
  #scopeKey(scope) {
4642
4618
  return `${canonicalOfflinePrincipalId(scope.userId)}:${scope.scopeId}`;
4643
4619
  }
@@ -4648,6 +4624,26 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.3", ngImpor
4648
4624
  type: Injectable,
4649
4625
  args: [{ providedIn: 'root' }]
4650
4626
  }], ctorParameters: () => [] });
4627
+ function assertOfflinePayload(value, ancestors = new Set()) {
4628
+ if (value === null || typeof value === 'string' || typeof value === 'boolean')
4629
+ return;
4630
+ if (typeof value === 'number') {
4631
+ if (Number.isFinite(value))
4632
+ return;
4633
+ throw new OfflinePayloadValidationError();
4634
+ }
4635
+ if (typeof value !== 'object' || ancestors.has(value)) {
4636
+ throw new OfflinePayloadValidationError();
4637
+ }
4638
+ if (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype) {
4639
+ throw new OfflinePayloadValidationError();
4640
+ }
4641
+ ancestors.add(value);
4642
+ const items = Array.isArray(value) ? value : Object.values(value);
4643
+ for (const item of items)
4644
+ assertOfflinePayload(item, ancestors);
4645
+ ancestors.delete(value);
4646
+ }
4651
4647
  function compareOfflineCommands(left, right) {
4652
4648
  return left.createdAt - right.createdAt || (left.commandId < right.commandId ? -1 : left.commandId > right.commandId ? 1 : 0);
4653
4649
  }
@@ -5789,6 +5785,7 @@ async function createRandomOfflineEncryptionKey() {
5789
5785
  return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
5790
5786
  }
5791
5787
  const settledSqliteOperation = async () => undefined;
5788
+ const COMMUNITY_SQLITE_OPEN_RETRY_DELAYS_MS = [50, 150, 300];
5792
5789
  const communitySqliteDrivers = new WeakMap();
5793
5790
  function sqliteErrorMessage(error) {
5794
5791
  if (error instanceof Error)
@@ -5798,6 +5795,17 @@ function sqliteErrorMessage(error) {
5798
5795
  }
5799
5796
  return String(error);
5800
5797
  }
5798
+ async function openCommunitySqliteDatabase(database) {
5799
+ for (const delayMs of COMMUNITY_SQLITE_OPEN_RETRY_DELAYS_MS) {
5800
+ const result = await database.open().then(() => ({ ok: true }), (error) => ({ ok: false, error }));
5801
+ if (result.ok)
5802
+ return;
5803
+ if (!isTransientSqliteLockError(result.error))
5804
+ throw result.error;
5805
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
5806
+ }
5807
+ await database.open();
5808
+ }
5801
5809
  function normalizeCommunitySqliteDatabaseName(database) {
5802
5810
  return database.endsWith('.db') ? database.slice(0, -3) : database;
5803
5811
  }
@@ -5880,7 +5888,7 @@ function createCommunitySqliteDriver(connection, encrypted = COMMUNITY_SQLITE_EN
5880
5888
  const opening = (async () => {
5881
5889
  await ensureSecret(createEncryptionKey);
5882
5890
  const value = await createReloadSafeCommunitySqliteDatabase(connection, databaseName, encrypted);
5883
- await value.open();
5891
+ await openCommunitySqliteDatabase(value);
5884
5892
  databases.set(databaseKey, value);
5885
5893
  return value;
5886
5894
  })();
@@ -6106,26 +6114,21 @@ class SqliteOfflineRepository {
6106
6114
  async putCommand(command) {
6107
6115
  await this.#queueWrite((databaseId) => this.#putCommand(databaseId, command));
6108
6116
  }
6117
+ /** @deprecated Use {@link transactReplica} with `putCommands`. */
6109
6118
  replaceCommand(command) {
6110
6119
  return this.putCommand(command);
6111
6120
  }
6112
- async removeCommand(commandId) {
6113
- await this.#write('DELETE FROM offline_sync_commands WHERE command_id = ?', [commandId]);
6121
+ /** @deprecated Use {@link transactReplica} with `removeCommandIds`. */
6122
+ removeCommand(commandId) {
6123
+ return this.transactReplica({ removeCommandIds: [commandId] });
6114
6124
  }
6115
- async putPullAttention(attention) {
6116
- await this.#write(`INSERT INTO offline_pull_attentions (user_id, scope_id, reason, status) VALUES (?, ?, ?, ?)
6117
- ON CONFLICT(user_id, scope_id) DO UPDATE SET reason = excluded.reason, status = excluded.status`, [
6118
- canonicalOfflinePrincipalId(attention.userId),
6119
- attention.scopeId,
6120
- attention.reason,
6121
- attention.status === undefined ? null : attention.status,
6122
- ]);
6125
+ /** @deprecated Use {@link transactReplica} with `putPullAttentions`. */
6126
+ putPullAttention(attention) {
6127
+ return this.transactReplica({ putPullAttentions: [attention] });
6123
6128
  }
6124
- async removePullAttention(scope) {
6125
- await this.#write('DELETE FROM offline_pull_attentions WHERE user_id = ? AND scope_id = ?', [
6126
- canonicalOfflinePrincipalId(scope.userId),
6127
- scope.scopeId,
6128
- ]);
6129
+ /** @deprecated Use {@link transactReplica} with `removePullAttentions`. */
6130
+ removePullAttention(scope) {
6131
+ return this.transactReplica({ removePullAttentions: [scope] });
6129
6132
  }
6130
6133
  async clearUser(userId) {
6131
6134
  await this.#transaction((database) => this.#clearUser(database, userId));
@@ -7212,7 +7215,16 @@ function assertSupportedOfflineMode(platform, mode) {
7212
7215
  }
7213
7216
  }
7214
7217
 
7215
- /** Standard scoped local replica and outbox runtime for offline-capable Ionic applications. */
7218
+ /**
7219
+ * Experimental scoped local replica and outbox runtime for offline-capable Ionic applications.
7220
+ *
7221
+ * This entry point is not covered by the package's SemVer compatibility guarantee. Its public
7222
+ * APIs, persistence schema, and synchronization behavior may change incompatibly in a minor or
7223
+ * patch release before stabilization.
7224
+ *
7225
+ * @packageDocumentation
7226
+ * @experimental
7227
+ */
7216
7228
 
7217
7229
  /**
7218
7230
  * Generated bundle index. Do not edit.