@hkdigital/lib-sveltekit 0.1.70 → 0.1.72
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/dist/classes/cache/IndexedDbCache.d.ts +212 -0
- package/dist/classes/cache/IndexedDbCache.js +673 -0
- package/dist/classes/cache/MemoryResponseCache.d.ts +101 -14
- package/dist/classes/cache/MemoryResponseCache.js +97 -12
- package/dist/classes/cache/index.d.ts +1 -1
- package/dist/classes/cache/index.js +2 -1
- package/dist/classes/events/EventEmitter.d.ts +142 -0
- package/dist/classes/events/EventEmitter.js +275 -0
- package/dist/classes/events/index.d.ts +1 -0
- package/dist/classes/events/index.js +2 -0
- package/dist/classes/logging/Logger.d.ts +74 -0
- package/dist/classes/logging/Logger.js +158 -0
- package/dist/classes/logging/constants.d.ts +14 -0
- package/dist/classes/logging/constants.js +18 -0
- package/dist/classes/logging/index.d.ts +2 -0
- package/dist/classes/logging/index.js +4 -0
- package/dist/classes/services/ServiceBase.d.ts +153 -0
- package/dist/classes/services/ServiceBase.js +409 -0
- package/dist/classes/services/ServiceManager.d.ts +350 -0
- package/dist/classes/services/ServiceManager.js +1114 -0
- package/dist/classes/services/constants.d.ts +11 -0
- package/dist/classes/services/constants.js +12 -0
- package/dist/classes/services/index.d.ts +3 -0
- package/dist/classes/services/index.js +5 -0
- package/dist/util/env/index.d.ts +1 -0
- package/dist/util/env/index.js +9 -0
- package/dist/util/http/caching.js +24 -12
- package/dist/util/http/http-request.js +12 -7
- package/package.json +2 -1
- package/dist/classes/cache/PersistentResponseCache.d.ts +0 -46
- /package/dist/classes/cache/{PersistentResponseCache.js → PersistentResponseCache.js__} +0 -0
@@ -0,0 +1,212 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview IndexedDbCache provides efficient persistent caching with
|
3
|
+
* automatic, non-blocking background cleanup.
|
4
|
+
*
|
5
|
+
* This cache automatically manages storage limits and entry expiration
|
6
|
+
* in the background using requestIdleCallback to avoid impacting application
|
7
|
+
* performance. It supports incremental cleanup, storage monitoring, and
|
8
|
+
* graceful degradation on older browsers.
|
9
|
+
*
|
10
|
+
* @example
|
11
|
+
* // Create a cache instance
|
12
|
+
* const cache = new IndexedDbCache({
|
13
|
+
* dbName: 'app-cache',
|
14
|
+
* storeName: 'http-responses',
|
15
|
+
* maxSize: 100 * 1024 * 1024, // 100MB
|
16
|
+
* maxAge: 30 * 24 * 60 * 60 * 1000 // 30 days
|
17
|
+
* });
|
18
|
+
*
|
19
|
+
* // Store a response
|
20
|
+
* const response = await fetch('https://api.example.com/data');
|
21
|
+
* await cache.set('api-data', response, {
|
22
|
+
* expiresIn: 3600000 // 1 hour
|
23
|
+
* });
|
24
|
+
*
|
25
|
+
* // Retrieve cached response
|
26
|
+
* const cached = await cache.get('api-data');
|
27
|
+
* if (cached) {
|
28
|
+
* console.log('Cache hit', cached.response);
|
29
|
+
* } else {
|
30
|
+
* console.log('Cache miss');
|
31
|
+
* }
|
32
|
+
*/
|
33
|
+
/**
|
34
|
+
* @typedef {Object} CacheEntry
|
35
|
+
* @property {Response} response - Cached Response object
|
36
|
+
* @property {Object} metadata - Cache entry metadata
|
37
|
+
* @property {string} url - Original URL
|
38
|
+
* @property {number} timestamp - When the entry was cached
|
39
|
+
* @property {number|null} expires - Expiration timestamp (null if no expiration)
|
40
|
+
* @property {string|null} etag - ETag header if present
|
41
|
+
* @property {string|null} lastModified - Last-Modified header if present
|
42
|
+
*/
|
43
|
+
/**
|
44
|
+
* IndexedDbCache with automatic background cleanup
|
45
|
+
*/
|
46
|
+
export default class IndexedDbCache {
|
47
|
+
/**
|
48
|
+
* Create a new IndexedDB cache storage
|
49
|
+
*
|
50
|
+
* @param {Object} [options] - Cache options
|
51
|
+
* @param {string} [options.dbName='http-cache'] - Database name
|
52
|
+
* @param {string} [options.storeName='responses'] - Store name
|
53
|
+
* @param {number} [options.maxSize=50000000] - Max cache size in bytes (50MB)
|
54
|
+
* @param {number} [options.maxAge=604800000] - Max age in ms (7 days)
|
55
|
+
* @param {number} [options.cleanupBatchSize=100] - Items per cleanup batch
|
56
|
+
* @param {number} [options.cleanupInterval=300000] - Time between cleanup attempts (5min)
|
57
|
+
*/
|
58
|
+
constructor(options?: {
|
59
|
+
dbName?: string;
|
60
|
+
storeName?: string;
|
61
|
+
maxSize?: number;
|
62
|
+
maxAge?: number;
|
63
|
+
cleanupBatchSize?: number;
|
64
|
+
cleanupInterval?: number;
|
65
|
+
});
|
66
|
+
dbName: string;
|
67
|
+
storeName: string;
|
68
|
+
maxSize: number;
|
69
|
+
maxAge: number;
|
70
|
+
cleanupBatchSize: number;
|
71
|
+
cleanupInterval: number;
|
72
|
+
/**
|
73
|
+
* Database connection promise
|
74
|
+
* @type {Promise<IDBDatabase>}
|
75
|
+
* @private
|
76
|
+
*/
|
77
|
+
private dbPromise;
|
78
|
+
/**
|
79
|
+
* Cleanup state tracker
|
80
|
+
* @type {Object}
|
81
|
+
* @private
|
82
|
+
*/
|
83
|
+
private cleanupState;
|
84
|
+
/**
|
85
|
+
* Open the IndexedDB database
|
86
|
+
*
|
87
|
+
* @private
|
88
|
+
* @returns {Promise<IDBDatabase>}
|
89
|
+
*/
|
90
|
+
private _openDatabase;
|
91
|
+
/**
|
92
|
+
* Get a cached response
|
93
|
+
*
|
94
|
+
* @param {string} key - Cache key
|
95
|
+
* @returns {Promise<CacheEntry|null>} Cache entry or null if not found/expired
|
96
|
+
*/
|
97
|
+
get(key: string): Promise<CacheEntry | null>;
|
98
|
+
/**
|
99
|
+
* Store a response in the cache
|
100
|
+
*
|
101
|
+
* @param {string} key - Cache key
|
102
|
+
* @param {Response} response - Response to cache
|
103
|
+
* @param {Object} [metadata={}] - Cache metadata
|
104
|
+
* @returns {Promise<void>}
|
105
|
+
*/
|
106
|
+
set(key: string, response: Response, metadata?: any): Promise<void>;
|
107
|
+
/**
|
108
|
+
* Update last accessed timestamp (without blocking)
|
109
|
+
*
|
110
|
+
* @private
|
111
|
+
* @param {string} key - Cache key
|
112
|
+
* @returns {Promise<void>}
|
113
|
+
*/
|
114
|
+
private _updateAccessTime;
|
115
|
+
/**
|
116
|
+
* Delete a cached entry
|
117
|
+
*
|
118
|
+
* @param {string} key - Cache key
|
119
|
+
* @returns {Promise<boolean>}
|
120
|
+
*/
|
121
|
+
delete(key: string): Promise<boolean>;
|
122
|
+
/**
|
123
|
+
* Delete a cached entry (internal implementation)
|
124
|
+
*
|
125
|
+
* @private
|
126
|
+
* @param {string} key - Cache key
|
127
|
+
* @returns {Promise<boolean>}
|
128
|
+
*/
|
129
|
+
private _deleteEntry;
|
130
|
+
/**
|
131
|
+
* Clear all cached responses
|
132
|
+
*
|
133
|
+
* @returns {Promise<void>}
|
134
|
+
*/
|
135
|
+
clear(): Promise<void>;
|
136
|
+
/**
|
137
|
+
* Check storage usage and schedule cleanup if needed
|
138
|
+
*
|
139
|
+
* @private
|
140
|
+
*/
|
141
|
+
private _checkAndScheduleCleanup;
|
142
|
+
/**
|
143
|
+
* Schedule a cleanup to run during idle time
|
144
|
+
*
|
145
|
+
* @private
|
146
|
+
* @param {boolean} [urgent=false] - If true, clean up sooner
|
147
|
+
*/
|
148
|
+
private _scheduleCleanup;
|
149
|
+
/**
|
150
|
+
* Perform a single cleanup step
|
151
|
+
*
|
152
|
+
* @private
|
153
|
+
*/
|
154
|
+
private _performCleanupStep;
|
155
|
+
/**
|
156
|
+
* Remove expired entries
|
157
|
+
*
|
158
|
+
* @private
|
159
|
+
* @param {number} limit - Maximum number of entries to remove
|
160
|
+
* @returns {Promise<number>} Number of entries removed
|
161
|
+
*/
|
162
|
+
private _removeExpiredEntries;
|
163
|
+
/**
|
164
|
+
* Remove old entries based on age and size constraints
|
165
|
+
*
|
166
|
+
* @private
|
167
|
+
* @param {number} limit - Maximum number of entries to remove
|
168
|
+
* @returns {Promise<number>} Number of entries removed
|
169
|
+
*/
|
170
|
+
private _removeOldEntries;
|
171
|
+
/**
|
172
|
+
* Get an estimate of the total cache size
|
173
|
+
*
|
174
|
+
* @private
|
175
|
+
* @returns {Promise<number>} Size estimate in bytes
|
176
|
+
*/
|
177
|
+
private _getCacheSizeEstimate;
|
178
|
+
/**
|
179
|
+
* Close the database connection
|
180
|
+
*/
|
181
|
+
close(): void;
|
182
|
+
}
|
183
|
+
export type CacheEntry = {
|
184
|
+
/**
|
185
|
+
* - Cached Response object
|
186
|
+
*/
|
187
|
+
response: Response;
|
188
|
+
/**
|
189
|
+
* - Cache entry metadata
|
190
|
+
*/
|
191
|
+
metadata: any;
|
192
|
+
/**
|
193
|
+
* - Original URL
|
194
|
+
*/
|
195
|
+
url: string;
|
196
|
+
/**
|
197
|
+
* - When the entry was cached
|
198
|
+
*/
|
199
|
+
timestamp: number;
|
200
|
+
/**
|
201
|
+
* - Expiration timestamp (null if no expiration)
|
202
|
+
*/
|
203
|
+
expires: number | null;
|
204
|
+
/**
|
205
|
+
* - ETag header if present
|
206
|
+
*/
|
207
|
+
etag: string | null;
|
208
|
+
/**
|
209
|
+
* - Last-Modified header if present
|
210
|
+
*/
|
211
|
+
lastModified: string | null;
|
212
|
+
};
|