@icp-sdk/core 5.1.0 → 5.2.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/lib/cjs/agent/agent/http/index.d.ts +17 -0
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +43 -20
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +18 -1
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +42 -5
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/index.d.ts +1 -0
- package/lib/cjs/agent/index.d.ts.map +1 -1
- package/lib/cjs/agent/index.js.map +1 -1
- package/lib/cjs/agent/utils/createExpirableStore.d.ts +23 -0
- package/lib/cjs/agent/utils/createExpirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/createExpirableStore.js +18 -0
- package/lib/cjs/agent/utils/createExpirableStore.js.map +1 -0
- package/lib/cjs/agent/utils/expirableStore.d.ts +39 -0
- package/lib/cjs/agent/utils/expirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/expirableStore.js +14 -0
- package/lib/cjs/agent/utils/expirableStore.js.map +1 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts +14 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.js +46 -0
- package/lib/cjs/agent/utils/inMemoryExpirableStore.js.map +1 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts +28 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.js +139 -0
- package/lib/cjs/agent/utils/indexedDBExpirableStore.js.map +1 -0
- package/lib/esm/agent/agent/http/index.d.ts +17 -0
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +44 -21
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +18 -1
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +39 -4
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/index.d.ts +1 -0
- package/lib/esm/agent/index.d.ts.map +1 -1
- package/lib/esm/agent/index.js.map +1 -1
- package/lib/esm/agent/utils/createExpirableStore.d.ts +23 -0
- package/lib/esm/agent/utils/createExpirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/createExpirableStore.js +15 -0
- package/lib/esm/agent/utils/createExpirableStore.js.map +1 -0
- package/lib/esm/agent/utils/expirableStore.d.ts +39 -0
- package/lib/esm/agent/utils/expirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/expirableStore.js +11 -0
- package/lib/esm/agent/utils/expirableStore.js.map +1 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts +14 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.js +42 -0
- package/lib/esm/agent/utils/inMemoryExpirableStore.js.map +1 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts +28 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.js +135 -0
- package/lib/esm/agent/utils/indexedDBExpirableStore.js.map +1 -0
- package/package.json +2 -1
- package/src/agent/agent/http/index.ts +48 -17
- package/src/agent/errors.ts +45 -4
- package/src/agent/index.ts +1 -0
- package/src/agent/utils/createExpirableStore.ts +31 -0
- package/src/agent/utils/expirableStore.ts +47 -0
- package/src/agent/utils/inMemoryExpirableStore.ts +49 -0
- package/src/agent/utils/indexedDBExpirableStore.ts +191 -0
- package/lib/cjs/agent/utils/expirableMap.d.ts +0 -80
- package/lib/cjs/agent/utils/expirableMap.d.ts.map +0 -1
- package/lib/cjs/agent/utils/expirableMap.js +0 -148
- package/lib/cjs/agent/utils/expirableMap.js.map +0 -1
- package/lib/esm/agent/utils/expirableMap.d.ts +0 -80
- package/lib/esm/agent/utils/expirableMap.d.ts.map +0 -1
- package/lib/esm/agent/utils/expirableMap.js +0 -144
- package/lib/esm/agent/utils/expirableMap.js.map +0 -1
- package/src/agent/utils/expirableMap.ts +0 -163
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ExpirableStore } from './expirableStore.ts';
|
|
2
|
+
export interface CreateExpirableStoreOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Name of the IndexedDB database (used when IndexedDB is available).
|
|
5
|
+
*/
|
|
6
|
+
dbName: string;
|
|
7
|
+
/**
|
|
8
|
+
* Name of the IndexedDB object store (used when IndexedDB is available).
|
|
9
|
+
*/
|
|
10
|
+
storeName: string;
|
|
11
|
+
/**
|
|
12
|
+
* Time in milliseconds after which entries expire.
|
|
13
|
+
*/
|
|
14
|
+
expirationTime: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates an {@link ExpirableStore} with automatic runtime detection.
|
|
18
|
+
* Uses {@link IndexedDBExpirableStore} in browser environments (when IndexedDB is available),
|
|
19
|
+
* falls back to {@link InMemoryExpirableStore} otherwise.
|
|
20
|
+
* @param options - Configuration for the store (dbName, storeName, and expirationTime).
|
|
21
|
+
*/
|
|
22
|
+
export declare function createExpirableStore<V>(options: CreateExpirableStoreOptions): ExpirableStore<V>;
|
|
23
|
+
//# sourceMappingURL=createExpirableStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createExpirableStore.d.ts","sourceRoot":"","sources":["../../../../src/agent/utils/createExpirableStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAI1D,MAAM,WAAW,2BAA2B;IAC1C;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,OAAO,EAAE,2BAA2B,GAAG,cAAc,CAAC,CAAC,CAAC,CAK/F"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { IndexedDBExpirableStore } from "./indexedDBExpirableStore.js";
|
|
2
|
+
import { InMemoryExpirableStore } from "./inMemoryExpirableStore.js";
|
|
3
|
+
/**
|
|
4
|
+
* Creates an {@link ExpirableStore} with automatic runtime detection.
|
|
5
|
+
* Uses {@link IndexedDBExpirableStore} in browser environments (when IndexedDB is available),
|
|
6
|
+
* falls back to {@link InMemoryExpirableStore} otherwise.
|
|
7
|
+
* @param options - Configuration for the store (dbName, storeName, and expirationTime).
|
|
8
|
+
*/
|
|
9
|
+
export function createExpirableStore(options) {
|
|
10
|
+
if (typeof globalThis.indexedDB !== 'undefined') {
|
|
11
|
+
return new IndexedDBExpirableStore(options);
|
|
12
|
+
}
|
|
13
|
+
return new InMemoryExpirableStore({ expirationTime: options.expirationTime });
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=createExpirableStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createExpirableStore.js","sourceRoot":"","sources":["../../../../src/agent/utils/createExpirableStore.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAiBrE;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAI,OAAoC;IAC1E,IAAI,OAAO,UAAU,CAAC,SAAS,KAAK,WAAW,EAAE,CAAC;QAChD,OAAO,IAAI,uBAAuB,CAAI,OAAO,CAAC,CAAC;IACjD,CAAC;IACD,OAAO,IAAI,sBAAsB,CAAI,EAAE,cAAc,EAAE,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;AACnF,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic interface for a key-value store with time-based expiration.
|
|
3
|
+
* Keys are strings, values are of type V.
|
|
4
|
+
* Implementations must handle expiration internally.
|
|
5
|
+
*/
|
|
6
|
+
export interface ExpirableStore<V> {
|
|
7
|
+
/**
|
|
8
|
+
* Time in milliseconds after which entries expire.
|
|
9
|
+
*/
|
|
10
|
+
readonly expirationTime: number;
|
|
11
|
+
/**
|
|
12
|
+
* Get the value for a key.
|
|
13
|
+
* Returns undefined if the key is not present or has expired.
|
|
14
|
+
*/
|
|
15
|
+
get(key: string): Promise<V | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Store a value for a key.
|
|
18
|
+
* Prunes expired entries before inserting.
|
|
19
|
+
*/
|
|
20
|
+
set(key: string, value: V): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Delete the entry for a key.
|
|
23
|
+
*/
|
|
24
|
+
delete(key: string): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export interface ExpirableStoreOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Time in milliseconds after which entries expire.
|
|
29
|
+
* Must be a positive finite number.
|
|
30
|
+
*/
|
|
31
|
+
expirationTime: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Validates that expirationTime is a positive finite number.
|
|
35
|
+
* @param expirationTime - Time in milliseconds after which entries expire.
|
|
36
|
+
* @throws {Error} if expirationTime is not valid.
|
|
37
|
+
*/
|
|
38
|
+
export declare function validateExpirationTime(expirationTime: number): void;
|
|
39
|
+
//# sourceMappingURL=expirableStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expirableStore.d.ts","sourceRoot":"","sources":["../../../../src/agent/utils/expirableStore.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC;IAC/B;;OAEG;IACH,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAEhC;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAEzC;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1C;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI,CAInE"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates that expirationTime is a positive finite number.
|
|
3
|
+
* @param expirationTime - Time in milliseconds after which entries expire.
|
|
4
|
+
* @throws {Error} if expirationTime is not valid.
|
|
5
|
+
*/
|
|
6
|
+
export function validateExpirationTime(expirationTime) {
|
|
7
|
+
if (!Number.isFinite(expirationTime) || expirationTime <= 0) {
|
|
8
|
+
throw new Error(`expirationTime must be a positive finite number, got ${expirationTime}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=expirableStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expirableStore.js","sourceRoot":"","sources":["../../../../src/agent/utils/expirableStore.ts"],"names":[],"mappings":"AAqCA;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CAAC,cAAsB;IAC3D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,cAAc,IAAI,CAAC,EAAE,CAAC;QAC5D,MAAM,IAAI,KAAK,CAAC,wDAAwD,cAAc,EAAE,CAAC,CAAC;IAC5F,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ExpirableStore, ExpirableStoreOptions } from './expirableStore.ts';
|
|
2
|
+
/**
|
|
3
|
+
* In-memory key-value store with timestamp-based expiration.
|
|
4
|
+
* Prunes all expired entries on every {@link set} call.
|
|
5
|
+
*/
|
|
6
|
+
export declare class InMemoryExpirableStore<V> implements ExpirableStore<V> {
|
|
7
|
+
#private;
|
|
8
|
+
readonly expirationTime: number;
|
|
9
|
+
constructor(options: ExpirableStoreOptions);
|
|
10
|
+
get(key: string): Promise<V | undefined>;
|
|
11
|
+
set(key: string, value: V): Promise<void>;
|
|
12
|
+
delete(key: string): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=inMemoryExpirableStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inMemoryExpirableStore.d.ts","sourceRoot":"","sources":["../../../../src/agent/utils/inMemoryExpirableStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAEjF;;;GAGG;AACH,qBAAa,sBAAsB,CAAC,CAAC,CAAE,YAAW,cAAc,CAAC,CAAC,CAAC;;IACjE,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;gBAGpB,OAAO,EAAE,qBAAqB;IAM1C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAYxC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAMzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAanC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { validateExpirationTime } from "./expirableStore.js";
|
|
2
|
+
/**
|
|
3
|
+
* In-memory key-value store with timestamp-based expiration.
|
|
4
|
+
* Prunes all expired entries on every {@link set} call.
|
|
5
|
+
*/
|
|
6
|
+
export class InMemoryExpirableStore {
|
|
7
|
+
#entries;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
validateExpirationTime(options.expirationTime);
|
|
10
|
+
this.expirationTime = options.expirationTime;
|
|
11
|
+
this.#entries = new Map();
|
|
12
|
+
}
|
|
13
|
+
get(key) {
|
|
14
|
+
const entry = this.#entries.get(key);
|
|
15
|
+
if (!entry) {
|
|
16
|
+
return Promise.resolve(undefined);
|
|
17
|
+
}
|
|
18
|
+
if (Date.now() - entry.timestamp > this.expirationTime) {
|
|
19
|
+
this.#entries.delete(key);
|
|
20
|
+
return Promise.resolve(undefined);
|
|
21
|
+
}
|
|
22
|
+
return Promise.resolve(entry.value);
|
|
23
|
+
}
|
|
24
|
+
set(key, value) {
|
|
25
|
+
this.#prune();
|
|
26
|
+
this.#entries.set(key, { value, timestamp: Date.now() });
|
|
27
|
+
return Promise.resolve();
|
|
28
|
+
}
|
|
29
|
+
delete(key) {
|
|
30
|
+
this.#entries.delete(key);
|
|
31
|
+
return Promise.resolve();
|
|
32
|
+
}
|
|
33
|
+
#prune() {
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
for (const [key, entry] of this.#entries) {
|
|
36
|
+
if (now - entry.timestamp > this.expirationTime) {
|
|
37
|
+
this.#entries.delete(key);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=inMemoryExpirableStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inMemoryExpirableStore.js","sourceRoot":"","sources":["../../../../src/agent/utils/inMemoryExpirableStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAG7D;;;GAGG;AACH,MAAM,OAAO,sBAAsB;IAExB,QAAQ,CAA+C;IAEhE,YAAY,OAA8B;QACxC,sBAAsB,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;QAC/C,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAA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AAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ExpirableStore } from './expirableStore.ts';
|
|
2
|
+
export interface IndexedDBExpirableStoreOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Name of the IndexedDB database.
|
|
5
|
+
*/
|
|
6
|
+
dbName: string;
|
|
7
|
+
/**
|
|
8
|
+
* Name of the IndexedDB object store.
|
|
9
|
+
*/
|
|
10
|
+
storeName: string;
|
|
11
|
+
/**
|
|
12
|
+
* Time in milliseconds after which entries expire.
|
|
13
|
+
*/
|
|
14
|
+
expirationTime: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Key-value store backed by IndexedDB with time-based expiration.
|
|
18
|
+
* Enables sharing cached data across multiple instances and page reloads.
|
|
19
|
+
*/
|
|
20
|
+
export declare class IndexedDBExpirableStore<V> implements ExpirableStore<V> {
|
|
21
|
+
#private;
|
|
22
|
+
readonly expirationTime: number;
|
|
23
|
+
constructor(options: IndexedDBExpirableStoreOptions);
|
|
24
|
+
get(key: string): Promise<V | undefined>;
|
|
25
|
+
set(key: string, value: V): Promise<void>;
|
|
26
|
+
delete(key: string): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=indexedDBExpirableStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexedDBExpirableStore.d.ts","sourceRoot":"","sources":["../../../../src/agent/utils/indexedDBExpirableStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAQ1D,MAAM,WAAW,8BAA8B;IAC7C;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IACf;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;GAGG;AACH,qBAAa,uBAAuB,CAAC,CAAC,CAAE,YAAW,cAAc,CAAC,CAAC,CAAC;;IAClE,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;gBAKpB,OAAO,EAAE,8BAA8B;IAoE7C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IA+BxC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAqCzC"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { validateExpirationTime } from "./expirableStore.js";
|
|
2
|
+
/**
|
|
3
|
+
* Key-value store backed by IndexedDB with time-based expiration.
|
|
4
|
+
* Enables sharing cached data across multiple instances and page reloads.
|
|
5
|
+
*/
|
|
6
|
+
export class IndexedDBExpirableStore {
|
|
7
|
+
#dbName;
|
|
8
|
+
#storeName;
|
|
9
|
+
#dbPromise = null;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
if (typeof globalThis.indexedDB === 'undefined') {
|
|
12
|
+
throw new Error('IndexedDBExpirableStore requires IndexedDB, which is not available in this environment');
|
|
13
|
+
}
|
|
14
|
+
validateExpirationTime(options.expirationTime);
|
|
15
|
+
this.#dbName = options.dbName;
|
|
16
|
+
this.#storeName = options.storeName;
|
|
17
|
+
this.expirationTime = options.expirationTime;
|
|
18
|
+
}
|
|
19
|
+
#getDb() {
|
|
20
|
+
if (!this.#dbPromise) {
|
|
21
|
+
this.#dbPromise = this.#openDb().catch((error) => {
|
|
22
|
+
// Reset the cached promise so future calls can retry opening the DB
|
|
23
|
+
this.#dbPromise = null;
|
|
24
|
+
throw error;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return this.#dbPromise;
|
|
28
|
+
}
|
|
29
|
+
async #openDb() {
|
|
30
|
+
const db = await this.#openRequest();
|
|
31
|
+
if (db.objectStoreNames.contains(this.#storeName)) {
|
|
32
|
+
return db;
|
|
33
|
+
}
|
|
34
|
+
// Object store is missing — close and reopen with a bumped version
|
|
35
|
+
// to trigger onupgradeneeded.
|
|
36
|
+
const nextVersion = db.version + 1;
|
|
37
|
+
db.close();
|
|
38
|
+
return this.#openRequest(nextVersion);
|
|
39
|
+
}
|
|
40
|
+
#openRequest(version) {
|
|
41
|
+
const storeName = this.#storeName;
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const request = globalThis.indexedDB.open(this.#dbName, version);
|
|
44
|
+
request.onupgradeneeded = () => {
|
|
45
|
+
const db = request.result;
|
|
46
|
+
if (!db.objectStoreNames.contains(storeName)) {
|
|
47
|
+
db.createObjectStore(storeName, { keyPath: 'key' });
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
request.onsuccess = () => {
|
|
51
|
+
const db = request.result;
|
|
52
|
+
// Cooperatively close when another instance (or this store with a
|
|
53
|
+
// different storeName) requests a version upgrade. The next
|
|
54
|
+
// operation will re-acquire the connection via #getDb().
|
|
55
|
+
db.onversionchange = () => {
|
|
56
|
+
db.close();
|
|
57
|
+
this.#dbPromise = null;
|
|
58
|
+
};
|
|
59
|
+
resolve(db);
|
|
60
|
+
};
|
|
61
|
+
request.onerror = () => reject(request.error);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async get(key) {
|
|
65
|
+
const db = await this.#getDb();
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const tx = db.transaction(this.#storeName, 'readonly');
|
|
68
|
+
const store = tx.objectStore(this.#storeName);
|
|
69
|
+
const request = store.get(key);
|
|
70
|
+
request.onsuccess = () => {
|
|
71
|
+
const record = request.result;
|
|
72
|
+
if (!record) {
|
|
73
|
+
resolve(undefined);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (Date.now() - record.timestamp > this.expirationTime) {
|
|
77
|
+
// Lazy expiration: delete expired entry and return undefined
|
|
78
|
+
this.delete(key).then(() => resolve(undefined), () => resolve(undefined));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
resolve(record.value);
|
|
82
|
+
};
|
|
83
|
+
request.onerror = () => reject(request.error);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
async set(key, value) {
|
|
87
|
+
await this.#prune();
|
|
88
|
+
const db = await this.#getDb();
|
|
89
|
+
return new Promise((resolve, reject) => {
|
|
90
|
+
const tx = db.transaction(this.#storeName, 'readwrite');
|
|
91
|
+
const store = tx.objectStore(this.#storeName);
|
|
92
|
+
const record = {
|
|
93
|
+
key,
|
|
94
|
+
value,
|
|
95
|
+
timestamp: Date.now(),
|
|
96
|
+
};
|
|
97
|
+
store.put(record);
|
|
98
|
+
tx.oncomplete = () => resolve();
|
|
99
|
+
tx.onerror = () => reject(tx.error);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async delete(key) {
|
|
103
|
+
const db = await this.#getDb();
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
const tx = db.transaction(this.#storeName, 'readwrite');
|
|
106
|
+
const store = tx.objectStore(this.#storeName);
|
|
107
|
+
store.delete(key);
|
|
108
|
+
tx.oncomplete = () => resolve();
|
|
109
|
+
tx.onerror = () => reject(tx.error);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async #prune() {
|
|
113
|
+
const db = await this.#getDb();
|
|
114
|
+
const now = Date.now();
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
const tx = db.transaction(this.#storeName, 'readwrite');
|
|
117
|
+
const store = tx.objectStore(this.#storeName);
|
|
118
|
+
const request = store.openCursor();
|
|
119
|
+
request.onsuccess = () => {
|
|
120
|
+
const cursor = request.result;
|
|
121
|
+
if (!cursor) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const record = cursor.value;
|
|
125
|
+
if (now - record.timestamp > this.expirationTime) {
|
|
126
|
+
cursor.delete();
|
|
127
|
+
}
|
|
128
|
+
cursor.continue();
|
|
129
|
+
};
|
|
130
|
+
tx.oncomplete = () => resolve();
|
|
131
|
+
tx.onerror = () => reject(tx.error);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=indexedDBExpirableStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indexedDBExpirableStore.js","sourceRoot":"","sources":["../../../../src/agent/utils/indexedDBExpirableStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAwB7D;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IAEzB,OAAO,CAAS;IAChB,UAAU,CAAS;IAC5B,UAAU,GAAgC,IAAI,CAAC;IAE/C,YAAY,OAAuC;QACjD,IAAI,OAAO,UAAU,CAAC,SAAS,KAAK,WAAW,EAAE,CAAC;YAChD,MAAM,IAAI,KAAK,CACb,wFAAwF,CACzF,CAAC;QACJ,CAAC;QACD,sBAAsB,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;QAC/C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;QAC9B,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;QACpC,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;IAC/C,CAAC;IAED,MAAM;QACJ,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YACrB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBAC/C,oEAAoE;gBACpE,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;gBACvB,MAAM,KAAK,CAAC;YACd,CAAC,CAAC,CAAC;QACL,CAAC;QACD,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,OAAO;QACX,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,YAAY,EAAE,C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"Apache-2.0",
|
|
6
6
|
"description": "JavaScript and TypeScript library to interact with the Internet Computer",
|
|
@@ -100,6 +100,7 @@
|
|
|
100
100
|
"asn1js": "^3.0.7"
|
|
101
101
|
},
|
|
102
102
|
"devDependencies": {
|
|
103
|
+
"fake-indexeddb": "^6.2.5",
|
|
103
104
|
"tsc-alias": "^1.8.16"
|
|
104
105
|
}
|
|
105
106
|
}
|
|
@@ -3,6 +3,7 @@ import { Principal } from '#principal';
|
|
|
3
3
|
import {
|
|
4
4
|
HashTreeDecodeErrorCode,
|
|
5
5
|
CreateHttpAgentErrorCode,
|
|
6
|
+
MissingFetchErrorCode,
|
|
6
7
|
ExcessiveSignaturesErrorCode,
|
|
7
8
|
ExternalError,
|
|
8
9
|
IdentityInvalidErrorCode,
|
|
@@ -79,7 +80,8 @@ import {
|
|
|
79
80
|
} from '../../certificate.ts';
|
|
80
81
|
import { readCertifiedReject } from '../../utils/certificateReject.ts';
|
|
81
82
|
import { ed25519 } from '@noble/curves/ed25519';
|
|
82
|
-
import {
|
|
83
|
+
import type { ExpirableStore } from '../../utils/expirableStore.ts';
|
|
84
|
+
import { createExpirableStore } from '../../utils/createExpirableStore.ts';
|
|
83
85
|
import { Ed25519PublicKey } from '../../public_key.ts';
|
|
84
86
|
import { ObservableLog } from '../../observable.ts';
|
|
85
87
|
import {
|
|
@@ -93,12 +95,22 @@ import { concatBytes, hexToBytes, utf8ToBytes } from '@noble/hashes/utils';
|
|
|
93
95
|
import { uint8Equals, uint8FromBufLike } from '../../utils/buffer.ts';
|
|
94
96
|
import { IC_RESPONSE_DOMAIN_SEPARATOR } from '../../constants.ts';
|
|
95
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Possible values for the request status in the IC state tree.
|
|
100
|
+
* @see https://internetcomputer.org/docs/references/ic-interface-spec#state-tree-request-status
|
|
101
|
+
*/
|
|
96
102
|
export enum RequestStatusResponseStatus {
|
|
103
|
+
/** The call has made it past the endpoint into the IC's state. */
|
|
97
104
|
Received = 'received',
|
|
105
|
+
/** The initial effect of the call has happened or will happen. */
|
|
98
106
|
Processing = 'processing',
|
|
107
|
+
/** The call completed successfully; the reply is available at `/request_status/<id>/reply`. */
|
|
99
108
|
Replied = 'replied',
|
|
109
|
+
/** The call failed; reject code and message are available at `/request_status/<id>/reject_code` and `/request_status/<id>/reject_message`. */
|
|
100
110
|
Rejected = 'rejected',
|
|
111
|
+
/** The request status path is absent from the state tree, the request is unknown to the IC (never received or pruned after expiry). */
|
|
101
112
|
Unknown = 'unknown',
|
|
113
|
+
/** The IC has pruned the response data but remembers the request to prevent replay attacks. */
|
|
102
114
|
Done = 'done',
|
|
103
115
|
}
|
|
104
116
|
|
|
@@ -184,6 +196,12 @@ export interface HttpAgentOptions {
|
|
|
184
196
|
* @default true
|
|
185
197
|
*/
|
|
186
198
|
verifyQuerySignatures?: boolean;
|
|
199
|
+
/**
|
|
200
|
+
* Custom store for caching subnet node keys.
|
|
201
|
+
* Allows sharing the cache across multiple HttpAgent instances.
|
|
202
|
+
* Defaults to IndexedDB in browser environments, in-memory otherwise.
|
|
203
|
+
*/
|
|
204
|
+
subnetNodeKeyExpirableStore?: ExpirableStore<SubnetNodeKeys>;
|
|
187
205
|
/**
|
|
188
206
|
* Whether to log to the console. Defaults to false.
|
|
189
207
|
*/
|
|
@@ -281,6 +299,7 @@ export class HttpAgent implements Agent {
|
|
|
281
299
|
readonly #retryTimes; // Retry requests N times before erroring by default
|
|
282
300
|
#backoffStrategy: BackoffStrategyFactory;
|
|
283
301
|
readonly #maxIngressExpiryInMinutes: number;
|
|
302
|
+
readonly #subnetNodeKeyExpirableStore: ExpirableStore<SubnetNodeKeys>;
|
|
284
303
|
get #maxIngressExpiryInMs(): number {
|
|
285
304
|
return this.#maxIngressExpiryInMinutes * MINUTE_TO_MSECS;
|
|
286
305
|
}
|
|
@@ -294,9 +313,6 @@ export class HttpAgent implements Agent {
|
|
|
294
313
|
#queryPipeline: HttpAgentRequestTransformFn[] = [];
|
|
295
314
|
#updatePipeline: HttpAgentRequestTransformFn[] = [];
|
|
296
315
|
|
|
297
|
-
#subnetKeys: ExpirableMap<string, SubnetNodeKeys> = new ExpirableMap({
|
|
298
|
-
expirationTime: 5 * MINUTE_TO_MSECS,
|
|
299
|
-
});
|
|
300
316
|
// Tracks in-flight fetchSubnetKeys promises to deduplicate parallel requests
|
|
301
317
|
// for the same canister, preventing redundant read_state round-trips.
|
|
302
318
|
#subnetKeysFetching: Map<string, Promise<SubnetNodeKeys>> = new Map();
|
|
@@ -308,7 +324,15 @@ export class HttpAgent implements Agent {
|
|
|
308
324
|
*/
|
|
309
325
|
constructor(options: HttpAgentOptions = {}) {
|
|
310
326
|
this.config = options;
|
|
311
|
-
|
|
327
|
+
if (options.fetch) {
|
|
328
|
+
this.#fetch = options.fetch;
|
|
329
|
+
} else {
|
|
330
|
+
const globalFetch = (globalThis as Record<string, unknown>).fetch;
|
|
331
|
+
if (typeof globalFetch !== 'function') {
|
|
332
|
+
throw InputError.fromCode(new MissingFetchErrorCode());
|
|
333
|
+
}
|
|
334
|
+
this.#fetch = (globalFetch as typeof fetch).bind(globalThis);
|
|
335
|
+
}
|
|
312
336
|
this.#fetchOptions = options.fetchOptions;
|
|
313
337
|
this.#callOptions = options.callOptions;
|
|
314
338
|
this.#shouldFetchRootKey = options.shouldFetchRootKey ?? false;
|
|
@@ -325,10 +349,25 @@ export class HttpAgent implements Agent {
|
|
|
325
349
|
|
|
326
350
|
const host = determineHost(options.host);
|
|
327
351
|
this.host = new URL(host);
|
|
352
|
+
// Rewrite to avoid redirects and normalize the host before using it for namespacing caches
|
|
353
|
+
if (this.host.hostname.endsWith(IC0_SUB_DOMAIN)) {
|
|
354
|
+
this.host.hostname = IC0_DOMAIN;
|
|
355
|
+
} else if (this.host.hostname.endsWith(ICP0_SUB_DOMAIN)) {
|
|
356
|
+
this.host.hostname = ICP0_DOMAIN;
|
|
357
|
+
} else if (this.host.hostname.endsWith(ICP_API_SUB_DOMAIN)) {
|
|
358
|
+
this.host.hostname = ICP_API_DOMAIN;
|
|
359
|
+
}
|
|
328
360
|
|
|
329
361
|
if (options.verifyQuerySignatures !== undefined) {
|
|
330
362
|
this.#verifyQuerySignatures = options.verifyQuerySignatures;
|
|
331
363
|
}
|
|
364
|
+
this.#subnetNodeKeyExpirableStore =
|
|
365
|
+
options.subnetNodeKeyExpirableStore ??
|
|
366
|
+
createExpirableStore<SubnetNodeKeys>({
|
|
367
|
+
dbName: `icp-sdk-${this.host.host}`,
|
|
368
|
+
storeName: 'subnetNodeKeys',
|
|
369
|
+
expirationTime: 5 * MINUTE_TO_MSECS,
|
|
370
|
+
});
|
|
332
371
|
// Default is 3
|
|
333
372
|
this.#retryTimes = options.retryTimes ?? 3;
|
|
334
373
|
// Delay strategy for retries. Default is exponential backoff
|
|
@@ -337,14 +376,6 @@ export class HttpAgent implements Agent {
|
|
|
337
376
|
maxIterations: this.#retryTimes,
|
|
338
377
|
});
|
|
339
378
|
this.#backoffStrategy = options.backoffStrategy || defaultBackoffFactory;
|
|
340
|
-
// Rewrite to avoid redirects
|
|
341
|
-
if (this.host.hostname.endsWith(IC0_SUB_DOMAIN)) {
|
|
342
|
-
this.host.hostname = IC0_DOMAIN;
|
|
343
|
-
} else if (this.host.hostname.endsWith(ICP0_SUB_DOMAIN)) {
|
|
344
|
-
this.host.hostname = ICP0_DOMAIN;
|
|
345
|
-
} else if (this.host.hostname.endsWith(ICP_API_SUB_DOMAIN)) {
|
|
346
|
-
this.host.hostname = ICP_API_DOMAIN;
|
|
347
|
-
}
|
|
348
379
|
|
|
349
380
|
if (options.credentials) {
|
|
350
381
|
const { name, password } = options.credentials;
|
|
@@ -664,7 +695,7 @@ export class HttpAgent implements Agent {
|
|
|
664
695
|
this.#handleV2Rejection(body, requestId, effectiveCanisterId, fields.methodName, httpDetails);
|
|
665
696
|
}
|
|
666
697
|
|
|
667
|
-
if (response.status ===
|
|
698
|
+
if (response.status === HTTP_STATUS_ACCEPTED) {
|
|
668
699
|
const pollResult = await pollForResponse(
|
|
669
700
|
this,
|
|
670
701
|
effectiveCanisterId,
|
|
@@ -1075,7 +1106,7 @@ export class HttpAgent implements Agent {
|
|
|
1075
1106
|
} catch {
|
|
1076
1107
|
// In case the node signatures have changed, refresh the subnet keys and try again
|
|
1077
1108
|
this.log.warn('Query response verification failed. Retrying with fresh subnet keys.');
|
|
1078
|
-
this.#
|
|
1109
|
+
await this.#subnetNodeKeyExpirableStore.delete(ecid.toString());
|
|
1079
1110
|
const updatedSubnetNodeKeys = await this.fetchSubnetKeys(ecid);
|
|
1080
1111
|
return this.#verifyQueryResponse(queryWithDetails, updatedSubnetNodeKeys);
|
|
1081
1112
|
}
|
|
@@ -1558,7 +1589,7 @@ export class HttpAgent implements Agent {
|
|
|
1558
1589
|
const effectiveCanisterId: Principal = Principal.from(canisterId);
|
|
1559
1590
|
|
|
1560
1591
|
// Return cached result if available within the TTL.
|
|
1561
|
-
const cached = this.#
|
|
1592
|
+
const cached = await this.#subnetNodeKeyExpirableStore.get(effectiveCanisterId.toText());
|
|
1562
1593
|
if (cached) {
|
|
1563
1594
|
return cached;
|
|
1564
1595
|
}
|
|
@@ -1607,7 +1638,7 @@ export class HttpAgent implements Agent {
|
|
|
1607
1638
|
|
|
1608
1639
|
const subnetId = getSubnetIdFromCertificate(canisterCertificate.cert, rootKey);
|
|
1609
1640
|
const nodeKeys = lookupNodeKeysFromCertificate(canisterCertificate.cert, subnetId);
|
|
1610
|
-
this.#
|
|
1641
|
+
await this.#subnetNodeKeyExpirableStore.set(effectiveCanisterId.toText(), nodeKeys);
|
|
1611
1642
|
|
|
1612
1643
|
return nodeKeys;
|
|
1613
1644
|
}
|
package/src/agent/errors.ts
CHANGED
|
@@ -40,7 +40,35 @@ export interface CallContext extends PollingCallContext {
|
|
|
40
40
|
httpDetails: HttpDetailsResponse;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Controls the level of detail included in error messages.
|
|
45
|
+
* - `Normal` (default): includes error message, call context, and HTTP details, but omits large binary fields (arg, certificate, nonce).
|
|
46
|
+
* - `Verbose`: same as Normal, but includes hex-encoded binary fields.
|
|
47
|
+
*
|
|
48
|
+
* To enable verbose mode: `ErrorCode.verbosity = ErrorVerbosity.Verbose`
|
|
49
|
+
*/
|
|
50
|
+
export enum ErrorVerbosity {
|
|
51
|
+
Normal = 'normal',
|
|
52
|
+
Verbose = 'verbose',
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const VERBOSE_ONLY_KEYS = new Set(['arg', 'certificate', 'nonce']);
|
|
56
|
+
|
|
57
|
+
function bytesReplacer(verbosity: ErrorVerbosity): (key: string, value: unknown) => unknown {
|
|
58
|
+
return (key: string, value: unknown): unknown => {
|
|
59
|
+
if (value instanceof Uint8Array) {
|
|
60
|
+
if (verbosity === ErrorVerbosity.Normal && VERBOSE_ONLY_KEYS.has(key)) {
|
|
61
|
+
return `<${value.length} bytes, set ErrorCode.verbosity = ErrorVerbosity.Verbose to display>`;
|
|
62
|
+
}
|
|
63
|
+
return `hex(${value.length}):${bytesToHex(value)}`;
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export abstract class ErrorCode {
|
|
70
|
+
static verbosity: ErrorVerbosity = ErrorVerbosity.Normal;
|
|
71
|
+
|
|
44
72
|
public requestContext?: RequestContext;
|
|
45
73
|
public callContext?: CallContext | PollingCallContext;
|
|
46
74
|
|
|
@@ -64,7 +92,7 @@ abstract class ErrorCode {
|
|
|
64
92
|
` Canister ID: ${this.callContext.canisterId.toText()}\n` +
|
|
65
93
|
` Method name: ${this.callContext.methodName}`;
|
|
66
94
|
if ('httpDetails' in this.callContext) {
|
|
67
|
-
errorMessage += `\n HTTP details: ${JSON.stringify(this.callContext.httpDetails,
|
|
95
|
+
errorMessage += `\n HTTP details: ${JSON.stringify(this.callContext.httpDetails, bytesReplacer(ErrorCode.verbosity), 2)}`;
|
|
68
96
|
}
|
|
69
97
|
}
|
|
70
98
|
return errorMessage;
|
|
@@ -634,6 +662,19 @@ export class IngressExpiryInvalidErrorCode extends ErrorCode {
|
|
|
634
662
|
}
|
|
635
663
|
}
|
|
636
664
|
|
|
665
|
+
export class MissingFetchErrorCode extends ErrorCode {
|
|
666
|
+
public name = 'MissingFetchErrorCode';
|
|
667
|
+
|
|
668
|
+
constructor() {
|
|
669
|
+
super();
|
|
670
|
+
Object.setPrototypeOf(this, MissingFetchErrorCode.prototype);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
public toErrorMessage(): string {
|
|
674
|
+
return 'No fetch implementation available. Provide a `fetch` function in HttpAgentOptions or ensure `globalThis.fetch` is defined.';
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
637
678
|
export class CreateHttpAgentErrorCode extends ErrorCode {
|
|
638
679
|
public name = 'CreateHttpAgentErrorCode';
|
|
639
680
|
|
|
@@ -758,7 +799,7 @@ export class HttpErrorCode extends ErrorCode {
|
|
|
758
799
|
let errorMessage =
|
|
759
800
|
'HTTP request failed:\n' +
|
|
760
801
|
` Status: ${this.status} (${this.statusText})\n` +
|
|
761
|
-
` Headers: ${JSON.stringify(this.headers)}\n`;
|
|
802
|
+
` Headers: ${JSON.stringify(this.headers, bytesReplacer(ErrorCode.verbosity))}\n`;
|
|
762
803
|
if (this.bodyText) {
|
|
763
804
|
errorMessage += ` Body: ${this.bodyText}\n`;
|
|
764
805
|
}
|
|
@@ -878,7 +919,7 @@ function formatUnknownError(error: unknown): string {
|
|
|
878
919
|
return error.stack ?? error.message;
|
|
879
920
|
}
|
|
880
921
|
try {
|
|
881
|
-
return JSON.stringify(error);
|
|
922
|
+
return JSON.stringify(error, bytesReplacer(ErrorCode.verbosity));
|
|
882
923
|
} catch {
|
|
883
924
|
return String(error);
|
|
884
925
|
}
|
package/src/agent/index.ts
CHANGED
|
@@ -108,6 +108,7 @@ export * from './request_id.ts';
|
|
|
108
108
|
export * from './utils/bls.ts';
|
|
109
109
|
export * from './utils/buffer.ts';
|
|
110
110
|
export * from './utils/random.ts';
|
|
111
|
+
export { type ExpirableStore } from './utils/expirableStore.ts';
|
|
111
112
|
export * as polling from './polling/index.ts';
|
|
112
113
|
import * as CanisterStatus from './canisterStatus/index.ts';
|
|
113
114
|
import * as SubnetStatus from './subnetStatus/index.ts';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ExpirableStore } from './expirableStore.ts';
|
|
2
|
+
import { IndexedDBExpirableStore } from './indexedDBExpirableStore.ts';
|
|
3
|
+
import { InMemoryExpirableStore } from './inMemoryExpirableStore.ts';
|
|
4
|
+
|
|
5
|
+
export interface CreateExpirableStoreOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Name of the IndexedDB database (used when IndexedDB is available).
|
|
8
|
+
*/
|
|
9
|
+
dbName: string;
|
|
10
|
+
/**
|
|
11
|
+
* Name of the IndexedDB object store (used when IndexedDB is available).
|
|
12
|
+
*/
|
|
13
|
+
storeName: string;
|
|
14
|
+
/**
|
|
15
|
+
* Time in milliseconds after which entries expire.
|
|
16
|
+
*/
|
|
17
|
+
expirationTime: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates an {@link ExpirableStore} with automatic runtime detection.
|
|
22
|
+
* Uses {@link IndexedDBExpirableStore} in browser environments (when IndexedDB is available),
|
|
23
|
+
* falls back to {@link InMemoryExpirableStore} otherwise.
|
|
24
|
+
* @param options - Configuration for the store (dbName, storeName, and expirationTime).
|
|
25
|
+
*/
|
|
26
|
+
export function createExpirableStore<V>(options: CreateExpirableStoreOptions): ExpirableStore<V> {
|
|
27
|
+
if (typeof globalThis.indexedDB !== 'undefined') {
|
|
28
|
+
return new IndexedDBExpirableStore<V>(options);
|
|
29
|
+
}
|
|
30
|
+
return new InMemoryExpirableStore<V>({ expirationTime: options.expirationTime });
|
|
31
|
+
}
|