@rdlabo/ionic-angular-kit 21.4.2 → 21.4.3
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
CHANGED
|
@@ -338,56 +338,69 @@ session boundary, cursor-based delta pull, aggregate-ordered replay, optimistic
|
|
|
338
338
|
read-only request-policy interceptor. Applications provide URL/DTO read policies, a replica puller, and a command
|
|
339
339
|
executor through `provideOffline(...)`.
|
|
340
340
|
Mutations are queued explicitly with `OfflineSyncService.enqueue`, not through HTTP interceptor policy.
|
|
341
|
-
Web storage uses Ionic Storage; iOS and Android use encrypted
|
|
342
|
-
primary entry point or `/offline` does not pull the
|
|
341
|
+
Web storage uses Ionic Storage; iOS and Android use encrypted `@capacitor-community/sqlite`. Importing either the
|
|
342
|
+
primary entry point or `/offline` does not pull the optional native SQLite plugin into web-only applications.
|
|
343
343
|
|
|
344
344
|
The offline interceptor observes real transport responses to update API reachability. For matched `GET`
|
|
345
345
|
requests only, a transport failure with `status=0` may return a local replica response tagged
|
|
346
346
|
`X-Offline-Response: local`. `POST` and other write methods always go to transport unchanged; outbox replay
|
|
347
347
|
requests bypass policy with `OFFLINE_BYPASS` while still using the same transport observation.
|
|
348
348
|
|
|
349
|
-
The native offline runtime
|
|
350
|
-
|
|
351
|
-
before installing the SQLite and Secure Preferences packages plus the SQLite WASM runtime. Supply the license key
|
|
352
|
-
through a local/CI secret; never commit it to `.npmrc`.
|
|
349
|
+
The native offline runtime uses `@capacitor-community/sqlite` on iOS and Android. Install the plugin in the app and
|
|
350
|
+
sync native projects:
|
|
353
351
|
|
|
354
352
|
```bash
|
|
355
|
-
|
|
356
|
-
npm
|
|
357
|
-
npm install @capawesome-team/capacitor-sqlite@^0.3.0 \
|
|
358
|
-
@capawesome-team/capacitor-secure-preferences@^0.2.0 \
|
|
359
|
-
@sqlite.org/sqlite-wasm
|
|
353
|
+
# Capacitor 8
|
|
354
|
+
npm install @capacitor-community/sqlite@^8.1.0
|
|
360
355
|
npx cap sync
|
|
361
356
|
```
|
|
362
357
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
`CAPAWESOME_SECURE_PREFERENCES.xml` from Android 11-and-lower `fullBackupContent` and Android 12+ cloud backup rules,
|
|
366
|
-
so the database key is not restored independently of its device keystore material.
|
|
358
|
+
Use the plugin major matching the application's Capacitor major (`^6` for Capacitor 6, `^7` for Capacitor 7,
|
|
359
|
+
`^8.1.0` for Capacitor 8).
|
|
367
360
|
|
|
368
|
-
|
|
369
|
-
|
|
361
|
+
Add the required `CapacitorSQLite` plugin block to `capacitor.config.ts`. Encryption must be enabled — the kit opens
|
|
362
|
+
databases in encrypted mode and relies on the plugin's built-in secure secret storage (`isSecretStored` /
|
|
363
|
+
`setEncryptionSecret` in the device keychain / Android keystore):
|
|
370
364
|
|
|
371
365
|
```ts
|
|
372
|
-
|
|
373
|
-
|
|
366
|
+
// capacitor.config.ts
|
|
367
|
+
plugins: {
|
|
368
|
+
CapacitorSQLite: {
|
|
369
|
+
iosDatabaseLocation: 'Library/CapacitorDatabase',
|
|
370
|
+
iosIsEncryption: true,
|
|
371
|
+
iosKeychainPrefix: '<your-app-prefix>',
|
|
372
|
+
androidIsEncryption: true,
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
```
|
|
374
376
|
|
|
375
|
-
|
|
377
|
+
Follow the [@capacitor-community/sqlite installation guide](https://github.com/capacitor-community/sqlite#installation)
|
|
378
|
+
for platform-specific steps and SQLCipher export-compliance notes.
|
|
376
379
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
+
Android must not back up or transfer the encrypted database independently from its keystore secret. Set
|
|
381
|
+
`android:allowBackup="false"`, `android:fullBackupContent="false"`, and
|
|
382
|
+
`android:dataExtractionRules="@xml/data_extraction_rules"` on `<application>`. The referenced Android 12+ rules must
|
|
383
|
+
exclude at least the `database`, `sharedpref`, `root`, and `external` domains from both `cloud-backup` and
|
|
384
|
+
`device-transfer`, as shown in the plugin installation guide.
|
|
380
385
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
+
Create the community plugin connection in the application, then pass it with a stable `databaseName` and a
|
|
387
|
+
`createEncryptionKey` generator to `provideOffline`. Keeping the runtime object application-supplied prevents the optional
|
|
388
|
+
native plugin from entering web-only `/offline` bundles. The kit invokes the generator only
|
|
389
|
+
when the plugin has no secret yet, then stores the result in the plugin's Keychain / Android keystore. Later opens use
|
|
390
|
+
that stored secret without invoking the generator. Never hard-code or derive the key from a user identifier, device
|
|
391
|
+
identifier, or access token; generate a cryptographically random value for the first installation.
|
|
392
|
+
|
|
393
|
+
```ts
|
|
394
|
+
import { CapacitorSQLite, SQLiteConnection } from '@capacitor-community/sqlite';
|
|
395
|
+
|
|
396
|
+
const createRandomOfflineEncryptionKey = async () =>
|
|
397
|
+
Array.from(crypto.getRandomValues(new Uint8Array(32)), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
386
398
|
|
|
387
399
|
provideOffline({
|
|
400
|
+
databaseName: 'product-offline',
|
|
401
|
+
sqliteConnection: new SQLiteConnection(CapacitorSQLite),
|
|
402
|
+
createEncryptionKey: createRandomOfflineEncryptionKey,
|
|
388
403
|
// ...product policies, puller, and executor
|
|
389
|
-
sqlitePlugin: Sqlite,
|
|
390
|
-
encryptionKey: offlineDatabaseKey,
|
|
391
404
|
});
|
|
392
405
|
```
|
|
393
406
|
|
|
@@ -397,10 +410,10 @@ Immediately before each send, the executor receives the latest `{ localId, serve
|
|
|
397
410
|
SQLite; a successful create adds `serverId` without replacing `localId`. Entity projection and outbox
|
|
398
411
|
append/removal are committed in one local transaction.
|
|
399
412
|
|
|
400
|
-
| Identity
|
|
401
|
-
|
|
|
402
|
-
| `localId`
|
|
403
|
-
| `serverId` | `server_id`
|
|
413
|
+
| Identity | SQLite column | Before synchronization | After server acknowledgement |
|
|
414
|
+
| ---------- | ------------- | ----------------------- | ----------------------------------- |
|
|
415
|
+
| `localId` | `local_id` | client-generated UUID | unchanged UUID |
|
|
416
|
+
| `serverId` | `server_id` | `NULL` for a new entity | positive server `AUTO_INCREMENT` id |
|
|
404
417
|
|
|
405
418
|
The write lifecycle is: update the replica immediately → append an outbox command in the same transaction → render
|
|
406
419
|
the optimistic value → replay in the background → validate the server revision → store the confirmed value and
|
|
@@ -455,13 +468,7 @@ shape or `null` to delete a row. Identity and sync metadata (`localId`, `serverI
|
|
|
455
468
|
migration `fromVersion`/`statements` — never function bodies.
|
|
456
469
|
|
|
457
470
|
```typescript
|
|
458
|
-
import {
|
|
459
|
-
defineOfflineReplicaSchema,
|
|
460
|
-
defineReplicaEntity,
|
|
461
|
-
provideOffline,
|
|
462
|
-
serverId,
|
|
463
|
-
text,
|
|
464
|
-
} from '@rdlabo/ionic-angular-kit/offline';
|
|
471
|
+
import { defineOfflineReplicaSchema, defineReplicaEntity, provideOffline, serverId, text } from '@rdlabo/ionic-angular-kit/offline';
|
|
465
472
|
|
|
466
473
|
// This is the Hono package's existing `typeof items.$inferSelect` export.
|
|
467
474
|
import type { Items as ItemSelect } from '@product/hono/db/schema';
|
|
@@ -487,8 +494,7 @@ const replicaSchema = defineOfflineReplicaSchema({
|
|
|
487
494
|
migrateWebRow: (row) => ({
|
|
488
495
|
sourceKey: row.sourceKey,
|
|
489
496
|
values: { ...row.values, subtitle: '' },
|
|
490
|
-
confirmedValues:
|
|
491
|
-
row.confirmedValues === null ? null : { ...row.confirmedValues, subtitle: '' },
|
|
497
|
+
confirmedValues: row.confirmedValues === null ? null : { ...row.confirmedValues, subtitle: '' },
|
|
492
498
|
}),
|
|
493
499
|
},
|
|
494
500
|
],
|
|
@@ -498,7 +504,7 @@ provideOffline({
|
|
|
498
504
|
replicaSchema,
|
|
499
505
|
replicaPuller: ProductReplicaPuller,
|
|
500
506
|
commandExecutor: ProductCommandExecutor,
|
|
501
|
-
// ...request policies,
|
|
507
|
+
// ...request policies, databaseName, createEncryptionKey
|
|
502
508
|
});
|
|
503
509
|
```
|
|
504
510
|
|
|
@@ -513,14 +519,6 @@ removing, or changing nullability of a Drizzle column breaks the app build until
|
|
|
513
519
|
At runtime, `values` contains only the mapped column projection; `localId` and `serverId` remain dedicated replica
|
|
514
520
|
fields and ignored server fields are never persisted.
|
|
515
521
|
|
|
516
|
-
Encrypted native builds also require the plugin's SQLCipher platform setup: enable
|
|
517
|
-
`capawesomeCapacitorSqliteIncludeSqlcipher = true` on Android; select the `SQLCipher` pod when using
|
|
518
|
-
CocoaPods, or enable the `SQLCipher` package trait when using Swift Package Manager on iOS. Follow
|
|
519
|
-
the [Capawesome SQLite installation guide](https://capawesome.io/docs/plugins/sqlite/#installation)
|
|
520
|
-
for the exact native configuration and export-compliance notes.
|
|
521
|
-
Also follow the [Capawesome Secure Preferences installation guide](https://capawesome.io/docs/plugins/secure-preferences/#installation),
|
|
522
|
-
including its Android backup exclusion rules.
|
|
523
|
-
|
|
524
522
|
- **Status classification**: `0`→`onNetworkError` (connected only), `429`→`onRateLimited`, `502/503/504`→`onServerBusy`, `400/422/500`+message→`onServerError`, `401`→`onUnauthorized`, `403`→`onForbidden`. Other statuses (e.g. `404`) are left to the caller.
|
|
525
523
|
- **Universal 60s timeout** — every request fails with a synthetic (retryable) `408` if it hangs for 60s. Deliberately generous (catches a dead server without cutting off a large upload / AI generation; `timeout({ each })` resets per emission, so streaming is unaffected). Not configurable — one fleet-wide behavior.
|
|
526
524
|
- **Optional `treatAsError(response)`** — reject a 2xx (e.g. `204`/`206`) as an error when a backend uses it to signal a condition. The one genuinely app-specific bit (some apps receive a normal `204`), kept optional so class interceptors with a 2xx-as-error convention can migrate to `provideKitHttp`.
|
|
@@ -2030,10 +2030,51 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImpo
|
|
|
2030
2030
|
args: [{ providedIn: 'root' }]
|
|
2031
2031
|
}] });
|
|
2032
2032
|
|
|
2033
|
-
/** DI token for the
|
|
2034
|
-
const
|
|
2033
|
+
/** DI token for the native community SQLite driver. */
|
|
2034
|
+
const COMMUNITY_SQLITE = new InjectionToken('COMMUNITY_SQLITE', {
|
|
2035
2035
|
factory: () => null,
|
|
2036
2036
|
});
|
|
2037
|
+
/** Create the standard encrypted `@capacitor-community/sqlite` driver. */
|
|
2038
|
+
function createCommunitySqliteDriver(connection) {
|
|
2039
|
+
const databases = new Map();
|
|
2040
|
+
const database = (databaseId) => {
|
|
2041
|
+
const value = databases.get(databaseId);
|
|
2042
|
+
if (!value)
|
|
2043
|
+
throw new Error(`Offline SQLite database "${databaseId}" is not open`);
|
|
2044
|
+
return value;
|
|
2045
|
+
};
|
|
2046
|
+
return {
|
|
2047
|
+
async open({ databaseName, createEncryptionKey }) {
|
|
2048
|
+
const stored = await connection.isSecretStored();
|
|
2049
|
+
if (!stored.result) {
|
|
2050
|
+
const encryptionKey = await createEncryptionKey?.();
|
|
2051
|
+
if (!encryptionKey)
|
|
2052
|
+
throw new Error('Native offline storage requires a non-empty encryption key on first open');
|
|
2053
|
+
await connection.setEncryptionSecret(encryptionKey);
|
|
2054
|
+
}
|
|
2055
|
+
const value = await connection.createConnection(databaseName, true, 'secret', 1, false);
|
|
2056
|
+
await value.open();
|
|
2057
|
+
databases.set(databaseName, value);
|
|
2058
|
+
return { databaseId: databaseName };
|
|
2059
|
+
},
|
|
2060
|
+
async execute({ databaseId, statement, values = [] }) {
|
|
2061
|
+
await database(databaseId).run(statement, values, false);
|
|
2062
|
+
},
|
|
2063
|
+
async query({ databaseId, statement, values = [] }) {
|
|
2064
|
+
const result = await database(databaseId).query(statement, values);
|
|
2065
|
+
return { rows: result.values ?? [] };
|
|
2066
|
+
},
|
|
2067
|
+
async beginTransaction({ databaseId }) {
|
|
2068
|
+
await database(databaseId).beginTransaction();
|
|
2069
|
+
},
|
|
2070
|
+
async commitTransaction({ databaseId }) {
|
|
2071
|
+
await database(databaseId).commitTransaction();
|
|
2072
|
+
},
|
|
2073
|
+
async rollbackTransaction({ databaseId }) {
|
|
2074
|
+
await database(databaseId).rollbackTransaction();
|
|
2075
|
+
},
|
|
2076
|
+
};
|
|
2077
|
+
}
|
|
2037
2078
|
const SCHEMA = [
|
|
2038
2079
|
`CREATE TABLE IF NOT EXISTS offline_metadata (
|
|
2039
2080
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
@@ -2075,9 +2116,9 @@ const SCHEMA = [
|
|
|
2075
2116
|
PRIMARY KEY (user_id, group_id)
|
|
2076
2117
|
)`,
|
|
2077
2118
|
];
|
|
2078
|
-
/** Native iOS/Android
|
|
2119
|
+
/** Native iOS/Android repository backed by encrypted `@capacitor-community/sqlite`. */
|
|
2079
2120
|
class SqliteOfflineRepository {
|
|
2080
|
-
#sqlite = inject(
|
|
2121
|
+
#sqlite = inject(COMMUNITY_SQLITE);
|
|
2081
2122
|
#options = inject(OFFLINE_KIT_OPTIONS);
|
|
2082
2123
|
#databaseId = null;
|
|
2083
2124
|
#initialization = null;
|
|
@@ -2213,14 +2254,10 @@ class SqliteOfflineRepository {
|
|
|
2213
2254
|
}
|
|
2214
2255
|
async #open() {
|
|
2215
2256
|
if (!this.#sqlite)
|
|
2216
|
-
throw new Error('Native offline storage requires
|
|
2217
|
-
const encryptionKey = await this.#options.encryptionKey?.();
|
|
2218
|
-
if (!encryptionKey)
|
|
2219
|
-
throw new Error('Native offline storage requires a non-empty encryption key');
|
|
2257
|
+
throw new Error('Native offline storage requires a community SQLite connection');
|
|
2220
2258
|
const { databaseId } = await this.#sqlite.open({
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
readOnly: false,
|
|
2259
|
+
databaseName: this.#options.databaseName,
|
|
2260
|
+
createEncryptionKey: this.#options.createEncryptionKey,
|
|
2224
2261
|
});
|
|
2225
2262
|
this.#databaseId = databaseId;
|
|
2226
2263
|
for (const statement of SCHEMA)
|
|
@@ -2510,7 +2547,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImpo
|
|
|
2510
2547
|
* Provide the standard scoped offline runtime.
|
|
2511
2548
|
*
|
|
2512
2549
|
* @remarks
|
|
2513
|
-
* Web uses Ionic Storage. Native iOS/Android uses encrypted
|
|
2550
|
+
* Web uses Ionic Storage. Native iOS/Android uses encrypted `@capacitor-community/sqlite`. The application owns
|
|
2514
2551
|
* URL/DTO policy and command execution; the kit owns persistence, ordering, retries, and session
|
|
2515
2552
|
* isolation.
|
|
2516
2553
|
*/
|
|
@@ -2522,11 +2559,14 @@ function provideOffline(options) {
|
|
|
2522
2559
|
provide: OFFLINE_KIT_OPTIONS,
|
|
2523
2560
|
useValue: {
|
|
2524
2561
|
databaseName: options.databaseName,
|
|
2525
|
-
|
|
2562
|
+
createEncryptionKey: options.createEncryptionKey,
|
|
2526
2563
|
replicaSchema: options.replicaSchema,
|
|
2527
2564
|
},
|
|
2528
2565
|
},
|
|
2529
|
-
{
|
|
2566
|
+
{
|
|
2567
|
+
provide: COMMUNITY_SQLITE,
|
|
2568
|
+
useValue: options.sqliteConnection ? createCommunitySqliteDriver(options.sqliteConnection) : null,
|
|
2569
|
+
},
|
|
2530
2570
|
{
|
|
2531
2571
|
provide: OFFLINE_REPOSITORY,
|
|
2532
2572
|
useFactory: () => selectOfflineRepository(Capacitor.getPlatform(), inject(IonicOfflineRepository), inject(SqliteOfflineRepository)),
|
|
@@ -2597,5 +2637,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.17", ngImpo
|
|
|
2597
2637
|
* Generated bundle index. Do not edit.
|
|
2598
2638
|
*/
|
|
2599
2639
|
|
|
2600
|
-
export {
|
|
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 };
|
|
2601
2641
|
//# sourceMappingURL=rdlabo-ionic-angular-kit-offline.mjs.map
|