@saplings/offline 0.0.0-stage → 0.3.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.
- package/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/index.d.ts +26 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/indexeddb.d.ts +10 -0
- package/dist/indexeddb.js +103 -0
- package/dist/indexeddb.js.map +1 -0
- package/dist/secure-store.d.ts +16 -0
- package/dist/secure-store.js +60 -0
- package/dist/secure-store.js.map +1 -0
- package/package.json +29 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sapling Unified SDK contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @saplings/offline
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ordered, dependency-free offline operation replay with a pluggable persistence contract.
|
|
4
|
+
|
|
5
|
+
## Queue operations
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createMemoryOfflineStore, createOfflineQueue } from '@saplings/offline';
|
|
9
|
+
|
|
10
|
+
const queue = createOfflineQueue(createMemoryOfflineStore<{ title: string }>(), async (operation, signal) => {
|
|
11
|
+
const response = await fetch('/api/tasks', {
|
|
12
|
+
method: 'POST',
|
|
13
|
+
headers: { 'Content-Type': 'application/json', 'Idempotency-Key': operation.id },
|
|
14
|
+
body: JSON.stringify(operation.payload),
|
|
15
|
+
signal,
|
|
16
|
+
});
|
|
17
|
+
if (!response.ok) throw new Error('Delivery failed');
|
|
18
|
+
});
|
|
19
|
+
await queue.enqueue({ id: crypto.randomUUID(), payload: { title: 'Draft' } });
|
|
20
|
+
await queue.flush();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The queue replays a snapshot in FIFO order, stops on failure, and removes only acknowledged operations. Concurrent flush calls on one queue share a promise. Server-side idempotency is mandatory because delivery can succeed before removal fails. The memory store is bounded but not persistent. Use the IndexedDB adapter for reload recovery. Coordinate multiple tabs externally; conflict resolution remains application-owned. Do not store secrets, and clear or isolate data on logout.
|
|
24
|
+
|
|
25
|
+
## Durable browser storage
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createIndexedDbOfflineStore, createOfflineQueue } from '@saplings/offline';
|
|
29
|
+
|
|
30
|
+
const store = await createIndexedDbOfflineStore<{ title: string }>('opaque-account-scope');
|
|
31
|
+
const queue = createOfflineQueue(store, deliver);
|
|
32
|
+
await queue.enqueue({ id: crypto.randomUUID(), payload: { title: 'Draft' } });
|
|
33
|
+
// On logout, stop active replay before clearing the account queue.
|
|
34
|
+
await store.clear();
|
|
35
|
+
store.close();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The adapter preserves FIFO order across reloads and atomically checks a pending-count limit and unique operation IDs. Use a different opaque account scope for each authenticated tenant and user. Browser quota and eviction policies still apply; this storage is not a security boundary. Cross-tab replay locking and server-side conflict resolution are not provided. Use server-side idempotency for all queued mutations.
|
|
39
|
+
|
|
40
|
+
## Recover queued work after reconnect
|
|
41
|
+
|
|
42
|
+
1. Use stable operation IDs as server idempotency keys. Enqueue only the minimum data needed and flush after connectivity is restored.
|
|
43
|
+
2. Persist operations in an appropriate storage adapter when reload recovery is required. Isolate or clear queues when the tenant or identity changes; coordinate multiple tabs independently.
|
|
44
|
+
3. Test successful delivery followed by failed local deletion, network failure, repeated flush, reload and conflict responses. Never store secrets in queued payloads.
|
|
45
|
+
|
|
46
|
+
## Secure storage adapter
|
|
47
|
+
|
|
48
|
+
`createSecureOfflineStore` validates application payloads, bounds serialized records and
|
|
49
|
+
serializes operations for a single queue instance. Supply a platform-encrypted storage adapter
|
|
50
|
+
and a distinct key per authenticated scope. This does not provide encryption itself or
|
|
51
|
+
coordinate multiple writers to the same storage key.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface OfflineOperation<T> {
|
|
2
|
+
id: string;
|
|
3
|
+
payload: T;
|
|
4
|
+
}
|
|
5
|
+
export interface OfflineStore<T> {
|
|
6
|
+
/** Return operations in insertion order, scoped to one authenticated account. */
|
|
7
|
+
list(): Promise<readonly OfflineOperation<T>[]>;
|
|
8
|
+
/** Atomically add a unique ID; duplicate IDs must reject. */
|
|
9
|
+
add(operation: OfflineOperation<T>): Promise<void>;
|
|
10
|
+
remove(id: string): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export interface FlushResult {
|
|
13
|
+
delivered: number;
|
|
14
|
+
stopped: 'empty' | 'failed' | 'aborted';
|
|
15
|
+
error?: unknown;
|
|
16
|
+
}
|
|
17
|
+
/** In-memory adapter for ephemeral queues and tests. Not durable across reloads. */
|
|
18
|
+
export declare function createMemoryOfflineStore<T>(maxPending?: number): OfflineStore<T>;
|
|
19
|
+
/** Ordered at-least-once replay. The server must deduplicate by operation ID. */
|
|
20
|
+
export declare function createOfflineQueue<T>(store: OfflineStore<T>, deliver: (operation: OfflineOperation<T>, signal?: AbortSignal) => Promise<void>): {
|
|
21
|
+
enqueue(operation: OfflineOperation<T>): Promise<void>;
|
|
22
|
+
flush(signal?: AbortSignal): Promise<FlushResult>;
|
|
23
|
+
};
|
|
24
|
+
export { createIndexedDbOfflineStore } from './indexeddb.js';
|
|
25
|
+
export type { IndexedDbOfflineStore } from './indexeddb.js';
|
|
26
|
+
export { createSecureOfflineStore, type AsyncSecureStorage } from './secure-store.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** In-memory adapter for ephemeral queues and tests. Not durable across reloads. */
|
|
2
|
+
export function createMemoryOfflineStore(maxPending = 1000) {
|
|
3
|
+
if (!Number.isSafeInteger(maxPending) || maxPending < 1)
|
|
4
|
+
throw new Error('Invalid pending limit');
|
|
5
|
+
const operations = new Map();
|
|
6
|
+
return {
|
|
7
|
+
async list() {
|
|
8
|
+
return [...operations.values()].map((operation) => structuredClone(operation));
|
|
9
|
+
},
|
|
10
|
+
async add(operation) {
|
|
11
|
+
if (operations.has(operation.id))
|
|
12
|
+
throw new Error('Duplicate operation ID');
|
|
13
|
+
if (operations.size >= maxPending)
|
|
14
|
+
throw new Error('Offline queue full');
|
|
15
|
+
operations.set(operation.id, structuredClone(operation));
|
|
16
|
+
},
|
|
17
|
+
async remove(id) {
|
|
18
|
+
operations.delete(id);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Ordered at-least-once replay. The server must deduplicate by operation ID. */
|
|
23
|
+
export function createOfflineQueue(store, deliver) {
|
|
24
|
+
let running;
|
|
25
|
+
async function replay(signal) {
|
|
26
|
+
let delivered = 0;
|
|
27
|
+
try {
|
|
28
|
+
for (const operation of await store.list()) {
|
|
29
|
+
if (signal?.aborted)
|
|
30
|
+
return { delivered, stopped: 'aborted' };
|
|
31
|
+
await deliver(operation, signal);
|
|
32
|
+
// Acknowledged operations are removed even if cancellation races with delivery.
|
|
33
|
+
await store.remove(operation.id);
|
|
34
|
+
delivered++;
|
|
35
|
+
}
|
|
36
|
+
return { delivered, stopped: signal?.aborted ? 'aborted' : 'empty' };
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
return { delivered, stopped: signal?.aborted ? 'aborted' : 'failed', error };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
async enqueue(operation) {
|
|
44
|
+
if (typeof operation.id !== 'string' || !operation.id.trim() || operation.id.length > 256)
|
|
45
|
+
throw new Error('Invalid operation ID');
|
|
46
|
+
await store.add(operation);
|
|
47
|
+
},
|
|
48
|
+
flush(signal) {
|
|
49
|
+
if (running)
|
|
50
|
+
return running;
|
|
51
|
+
running = replay(signal).finally(() => {
|
|
52
|
+
running = undefined;
|
|
53
|
+
});
|
|
54
|
+
return running;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export { createIndexedDbOfflineStore } from './indexeddb.js';
|
|
59
|
+
export { createSecureOfflineStore } from './secure-store.js';
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmBA,oFAAoF;AACpF,MAAM,UAAU,wBAAwB,CAAI,UAAU,GAAG,IAAI;IAC3D,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAClG,MAAM,UAAU,GAAG,IAAI,GAAG,EAA+B,CAAC;IAC1D,OAAO;QACL,KA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AS,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;QACvE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC;QAC/E,CAAC;IACH,CAAC;IACD,OAAO;QACL,KAAK,CAAC,OAAO,CAAC,SAA8B;YAC1C,IAAI,OAAO,SAAS,CAAC,EAAE,KAAK,QAAQ,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,SAAS,CAAC,EAAE,CAAC,MAAM,GAAG,GAAG;gBACvF,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;YAC1C,MAAM,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAC7B,CAAC;QACD,KAAK,CAAC,MAAoB;YACxB,IAAI,OAAO;gBAAE,OAAO,OAAO,CAAC;YAC5B,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,EAAE;gBACpC,OAAO,GAAG,SAAS,CAAC;YACtB,CAAC,CAAC,CAAC;YACH,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,OAAO,EAAE,2BAA2B,EAAE,MAAM,gBAAgB,CAAC;AAG7D,OAAO,EAAE,wBAAwB,EAA2B,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { OfflineStore } from './index.js';
|
|
2
|
+
export interface IndexedDbOfflineStore<T> extends OfflineStore<T> {
|
|
3
|
+
clear(): Promise<void>;
|
|
4
|
+
close(): void;
|
|
5
|
+
}
|
|
6
|
+
/** One durable browser queue per opaque account scope. Never store credentials. */
|
|
7
|
+
export declare function createIndexedDbOfflineStore<T>(scope: string, options?: {
|
|
8
|
+
maxPending?: number;
|
|
9
|
+
factory?: IDBFactory;
|
|
10
|
+
}): Promise<IndexedDbOfflineStore<T>>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
function completed(tx) {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
tx.oncomplete = () => resolve();
|
|
4
|
+
tx.onabort = () => reject(tx.error ?? new Error('Offline transaction aborted'));
|
|
5
|
+
tx.onerror = () => reject(tx.error ?? new Error('Offline transaction failed'));
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
function openDatabase(name, factory) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const request = factory.open(name, 1);
|
|
11
|
+
let blocked = false;
|
|
12
|
+
request.onupgradeneeded = () => {
|
|
13
|
+
const store = request.result.createObjectStore('operations', {
|
|
14
|
+
keyPath: 'sequence',
|
|
15
|
+
autoIncrement: true,
|
|
16
|
+
});
|
|
17
|
+
store.createIndex('operationId', 'id', { unique: true });
|
|
18
|
+
};
|
|
19
|
+
request.onblocked = () => {
|
|
20
|
+
blocked = true;
|
|
21
|
+
reject(new Error('Offline database upgrade blocked'));
|
|
22
|
+
};
|
|
23
|
+
request.onerror = () => reject(request.error ?? new Error('Offline database unavailable'));
|
|
24
|
+
request.onsuccess = () => {
|
|
25
|
+
const db = request.result;
|
|
26
|
+
db.onversionchange = () => db.close();
|
|
27
|
+
if (blocked)
|
|
28
|
+
db.close();
|
|
29
|
+
else
|
|
30
|
+
resolve(db);
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** One durable browser queue per opaque account scope. Never store credentials. */
|
|
35
|
+
export async function createIndexedDbOfflineStore(scope, options = {}) {
|
|
36
|
+
const maxPending = options.maxPending ?? 1000;
|
|
37
|
+
if (typeof scope !== 'string' ||
|
|
38
|
+
!/^[A-Za-z0-9_.-]{1,128}$/.test(scope) ||
|
|
39
|
+
!Number.isSafeInteger(maxPending) ||
|
|
40
|
+
maxPending < 1 ||
|
|
41
|
+
maxPending > 10000)
|
|
42
|
+
throw new Error('Invalid offline database settings');
|
|
43
|
+
const factory = options.factory ?? globalThis.indexedDB;
|
|
44
|
+
if (!factory)
|
|
45
|
+
throw new Error('IndexedDB unavailable');
|
|
46
|
+
const db = await openDatabase('saplings-offline:' + scope, factory);
|
|
47
|
+
return {
|
|
48
|
+
async list() {
|
|
49
|
+
const tx = db.transaction('operations', 'readonly');
|
|
50
|
+
const done = completed(tx);
|
|
51
|
+
const request = tx.objectStore('operations').getAll();
|
|
52
|
+
await done;
|
|
53
|
+
return request.result.map(({ id, payload }) => ({ id, payload }));
|
|
54
|
+
},
|
|
55
|
+
async add(operation) {
|
|
56
|
+
if (typeof operation.id !== 'string' || !operation.id.trim() || operation.id.length > 256)
|
|
57
|
+
throw new Error('Invalid operation ID');
|
|
58
|
+
const value = structuredClone({ id: operation.id, payload: operation.payload });
|
|
59
|
+
const tx = db.transaction('operations', 'readwrite');
|
|
60
|
+
const done = completed(tx);
|
|
61
|
+
const store = tx.objectStore('operations');
|
|
62
|
+
let full = false;
|
|
63
|
+
const count = store.count();
|
|
64
|
+
count.onsuccess = () => {
|
|
65
|
+
if (count.result >= maxPending) {
|
|
66
|
+
full = true;
|
|
67
|
+
tx.abort();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
store.add(value);
|
|
71
|
+
};
|
|
72
|
+
try {
|
|
73
|
+
await done;
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (full)
|
|
77
|
+
throw new Error('Offline queue full');
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
async remove(id) {
|
|
82
|
+
const tx = db.transaction('operations', 'readwrite');
|
|
83
|
+
const done = completed(tx);
|
|
84
|
+
const store = tx.objectStore('operations');
|
|
85
|
+
const request = store.index('operationId').getKey(id);
|
|
86
|
+
request.onsuccess = () => {
|
|
87
|
+
if (request.result !== undefined)
|
|
88
|
+
store.delete(request.result);
|
|
89
|
+
};
|
|
90
|
+
await done;
|
|
91
|
+
},
|
|
92
|
+
async clear() {
|
|
93
|
+
const tx = db.transaction('operations', 'readwrite');
|
|
94
|
+
const done = completed(tx);
|
|
95
|
+
tx.objectStore('operations').clear();
|
|
96
|
+
await done;
|
|
97
|
+
},
|
|
98
|
+
close() {
|
|
99
|
+
db.close();
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=indexeddb.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexeddb.js","sourceRoot":"","sources":["../src/indexeddb.ts"],"names":[],"mappings":"AAOA,SAAS,SAAS,CAAC,EAAkB;IACnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,EAAE,CAAC,UAAU,GAAG,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;QAChC,EAAE,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC,CAAC;QAChF,EAAE,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC,CAAC;IACjF,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,YAAY,CAAC,IAAY,EAAE,OAAmB;IACrD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;QACtC,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,OAAO,CAAC,eAAe,GAAG,GAAG,EAAE;YAC7B,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,YAAY,EAAE;gBAC3D,OAAO,EAAE,UAAU;gBACnB,aAAa,EAAE,IAAI;aACpB,CAAC,CAAC;YACH,KAAK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QACb,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { OfflineStore } from './index.js';
|
|
2
|
+
export interface AsyncSecureStorage {
|
|
3
|
+
getItemAsync(key: string): Promise<string | null>;
|
|
4
|
+
setItemAsync(key: string, value: string): Promise<void>;
|
|
5
|
+
deleteItemAsync(key: string): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/** Storage must itself provide encryption; this adapter only manages bounded JSON records. */
|
|
8
|
+
export declare function createSecureOfflineStore<T>(options: {
|
|
9
|
+
storage: AsyncSecureStorage;
|
|
10
|
+
key: string;
|
|
11
|
+
validatePayload: (value: unknown) => value is T;
|
|
12
|
+
maxPending?: number;
|
|
13
|
+
maxEncodedLength?: number;
|
|
14
|
+
}): OfflineStore<T> & {
|
|
15
|
+
clear(): Promise<void>;
|
|
16
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** Storage must itself provide encryption; this adapter only manages bounded JSON records. */
|
|
2
|
+
export function createSecureOfflineStore(options) {
|
|
3
|
+
const { storage, key, validatePayload, maxPending = 5, maxEncodedLength = 1500 } = options;
|
|
4
|
+
if (!key ||
|
|
5
|
+
!Number.isSafeInteger(maxPending) ||
|
|
6
|
+
maxPending < 1 ||
|
|
7
|
+
!Number.isSafeInteger(maxEncodedLength) ||
|
|
8
|
+
maxEncodedLength < 1)
|
|
9
|
+
throw new Error('Invalid secure queue configuration');
|
|
10
|
+
let chain = Promise.resolve();
|
|
11
|
+
function serialize(operation) {
|
|
12
|
+
const next = chain.then(operation);
|
|
13
|
+
chain = next.catch(() => undefined);
|
|
14
|
+
return next;
|
|
15
|
+
}
|
|
16
|
+
function valid(value) {
|
|
17
|
+
if (!value || typeof value !== 'object')
|
|
18
|
+
return false;
|
|
19
|
+
const row = value;
|
|
20
|
+
return (typeof row.id === 'string' &&
|
|
21
|
+
/^[A-Za-z0-9_-]{1,128}$/.test(row.id) &&
|
|
22
|
+
validatePayload(row.payload));
|
|
23
|
+
}
|
|
24
|
+
async function read() {
|
|
25
|
+
const stored = await storage.getItemAsync(key);
|
|
26
|
+
if (stored === null)
|
|
27
|
+
return [];
|
|
28
|
+
if (encodeURIComponent(stored).length > maxEncodedLength)
|
|
29
|
+
throw new Error('Stored queue exceeds capacity');
|
|
30
|
+
const rows = JSON.parse(stored);
|
|
31
|
+
if (!Array.isArray(rows) ||
|
|
32
|
+
rows.length > maxPending ||
|
|
33
|
+
!rows.every(valid) ||
|
|
34
|
+
new Set(rows.map((row) => row.id)).size !== rows.length)
|
|
35
|
+
throw new Error('Stored queue is invalid');
|
|
36
|
+
return rows;
|
|
37
|
+
}
|
|
38
|
+
async function write(rows) {
|
|
39
|
+
if (!rows.length)
|
|
40
|
+
return storage.deleteItemAsync(key);
|
|
41
|
+
const value = JSON.stringify(rows);
|
|
42
|
+
if (rows.length > maxPending || encodeURIComponent(value).length > maxEncodedLength)
|
|
43
|
+
throw new Error('Secure queue full');
|
|
44
|
+
await storage.setItemAsync(key, value);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
list: () => serialize(read),
|
|
48
|
+
add: (operation) => serialize(async () => {
|
|
49
|
+
if (!valid(operation))
|
|
50
|
+
throw new Error('Invalid operation');
|
|
51
|
+
const rows = await read();
|
|
52
|
+
if (rows.some((row) => row.id === operation.id))
|
|
53
|
+
throw new Error('Duplicate operation');
|
|
54
|
+
await write([...rows, operation]);
|
|
55
|
+
}),
|
|
56
|
+
remove: (id) => serialize(async () => write((await read()).filter((row) => row.id !== id))),
|
|
57
|
+
clear: () => serialize(() => storage.deleteItemAsync(key)),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=secure-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secure-store.js","sourceRoot":"","sources":["../src/secure-store.ts"],"names":[],"mappings":"AAQA,8FAA8F;AAC9F,MAAM,UAAU,wBAAwB,CAAI,OAM3C;IACC,MAAM,E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AE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;YACxF,MAAM,KAAK,CAAC,CAAC,GAAG,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;QACpC,CAAC,CAAC;QACJ,MAAM,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QAC3F,KAAK,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;KAC3D,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saplings/offline",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Offline operation replay primitives",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/snrakshith/saplings.git",
|
|
26
|
+
"directory": "packages/offline"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"registry": "https://registry.npmjs.org/"
|
|
31
|
+
}
|
|
6
32
|
}
|