@choksheak/ts-utils 0.3.0 → 0.3.1

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.
Files changed (60) hide show
  1. package/arrayBuffer.d.ts +4 -2
  2. package/assert.d.ts +3 -1
  3. package/average.d.ts +3 -1
  4. package/base64Url.d.ts +4 -2
  5. package/dateTimeStr.d.ts +15 -13
  6. package/duration.d.ts +15 -13
  7. package/isEmpty.d.ts +3 -1
  8. package/iterators.cjs +34 -0
  9. package/iterators.d.mts +4 -0
  10. package/iterators.d.ts +4 -0
  11. package/iterators.min.cjs +2 -0
  12. package/iterators.min.cjs.map +1 -0
  13. package/iterators.min.mjs +2 -0
  14. package/iterators.min.mjs.map +1 -0
  15. package/iterators.mjs +9 -0
  16. package/kvStore.cjs +103 -71
  17. package/kvStore.d.mts +91 -49
  18. package/kvStore.d.ts +95 -50
  19. package/kvStore.min.cjs +1 -1
  20. package/kvStore.min.cjs.map +1 -1
  21. package/kvStore.min.mjs +1 -1
  22. package/kvStore.min.mjs.map +1 -1
  23. package/kvStore.mjs +99 -66
  24. package/localStore.cjs +267 -0
  25. package/localStore.d.mts +119 -0
  26. package/localStore.d.ts +119 -0
  27. package/localStore.min.cjs +2 -0
  28. package/localStore.min.cjs.map +1 -0
  29. package/localStore.min.mjs +2 -0
  30. package/localStore.min.mjs.map +1 -0
  31. package/localStore.mjs +235 -0
  32. package/logging.d.ts +4 -2
  33. package/nonEmpty.d.ts +3 -1
  34. package/nonNil.d.ts +3 -1
  35. package/package.json +48 -15
  36. package/round.d.ts +4 -2
  37. package/safeBtoa.d.ts +3 -1
  38. package/safeParseFloat.d.ts +3 -1
  39. package/safeParseInt.d.ts +3 -1
  40. package/sha256.d.ts +3 -1
  41. package/sleep.d.ts +3 -1
  42. package/storageAdapter.cjs +18 -0
  43. package/storageAdapter.d.mts +33 -0
  44. package/storageAdapter.d.ts +33 -0
  45. package/storageAdapter.min.cjs +2 -0
  46. package/storageAdapter.min.cjs.map +1 -0
  47. package/storageAdapter.min.mjs +1 -0
  48. package/storageAdapter.min.mjs.map +1 -0
  49. package/storageAdapter.mjs +0 -0
  50. package/sum.d.ts +3 -1
  51. package/timeConstants.d.ts +16 -14
  52. package/timer.d.ts +4 -2
  53. package/localStorageCache.cjs +0 -119
  54. package/localStorageCache.d.mts +0 -57
  55. package/localStorageCache.d.ts +0 -55
  56. package/localStorageCache.min.cjs +0 -2
  57. package/localStorageCache.min.cjs.map +0 -1
  58. package/localStorageCache.min.mjs +0 -2
  59. package/localStorageCache.min.mjs.map +0 -1
  60. package/localStorageCache.mjs +0 -92
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/duration.ts","../src/kvStore.ts"],"sourcesContent":["/**\n * Bunch of miscellaneous constants and utility functions related to handling\n * date and time durations.\n *\n * Note that month and year do not have fixed durations, and hence are excluded\n * from this file. Weeks have fixed durations, but are excluded because we\n * use days as the max duration supported.\n */\n\nimport {\n HOURS_PER_DAY,\n MINUTES_PER_HOUR,\n MS_PER_DAY,\n MS_PER_HOUR,\n MS_PER_MINUTE,\n MS_PER_SECOND,\n SECONDS_PER_MINUTE,\n} from \"./timeConstants\";\n\nexport type Duration = {\n days?: number;\n hours?: number;\n minutes?: number;\n seconds?: number;\n milliseconds?: number;\n};\n\n/**\n * One of: days, hours, minutes, seconds, milliseconds\n */\nexport type DurationType = keyof Duration;\n\n/**\n * Order in which the duration type appears in the duration string.\n */\nexport const DURATION_TYPE_SEQUENCE: DurationType[] = [\n \"days\",\n \"hours\",\n \"minutes\",\n \"seconds\",\n \"milliseconds\",\n];\n\n/**\n * Follows the same format as Intl.DurationFormat.prototype.format().\n *\n * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns\n * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,\n * 7 milliseconds, 8 microseconds, 9 nanoseconds\n * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns\n */\nexport type DurationStyle = \"short\" | \"long\" | \"narrow\";\n\nexport type DurationSuffixMap = {\n short: string;\n shorts: string;\n long: string;\n longs: string;\n narrow: string;\n};\n\nexport type DurationSuffixType = keyof DurationSuffixMap;\n\nexport const DURATION_STYLE_SUFFIX_MAP: Record<\n DurationType,\n DurationSuffixMap\n> = {\n days: {\n short: \"day\",\n shorts: \"days\",\n long: \"day\",\n longs: \"days\",\n narrow: \"d\",\n },\n hours: {\n short: \"hr\",\n shorts: \"hrs\",\n long: \"hour\",\n longs: \"hours\",\n narrow: \"h\",\n },\n minutes: {\n short: \"min\",\n shorts: \"mins\",\n long: \"minute\",\n longs: \"minutes\",\n narrow: \"m\",\n },\n seconds: {\n short: \"sec\",\n shorts: \"secs\",\n long: \"second\",\n longs: \"seconds\",\n narrow: \"s\",\n },\n milliseconds: {\n short: \"ms\",\n shorts: \"ms\",\n long: \"millisecond\",\n longs: \"milliseconds\",\n narrow: \"ms\",\n },\n};\n\nfunction getDurationStyleForPlural(style: DurationStyle): DurationSuffixType {\n return style == \"short\" ? \"shorts\" : style === \"long\" ? \"longs\" : style;\n}\n\nfunction getValueAndUnitSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \"\" : \" \";\n}\n\nfunction getDurationTypeSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \" \" : \", \";\n}\n\n/**\n * Convert a milliseconds duration into a Duration object. If the given ms is\n * zero, then return an object with a single field of zero with duration type\n * of durationTypeForZero.\n *\n * @param durationTypeForZero Defaults to 'milliseconds'\n */\nexport function msToDuration(\n ms: number,\n durationTypeForZero?: DurationType,\n): Duration {\n if (ms === 0) {\n durationTypeForZero = durationTypeForZero ?? \"milliseconds\";\n return { [durationTypeForZero]: 0 };\n }\n\n const duration: Duration = {};\n\n for (let i = 0; i < 1; i++) {\n let seconds = Math.floor(ms / MS_PER_SECOND);\n const millis = ms - seconds * MS_PER_SECOND;\n\n if (millis > 0) {\n duration[\"milliseconds\"] = millis;\n }\n\n if (seconds === 0) {\n break;\n }\n\n let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);\n seconds -= minutes * SECONDS_PER_MINUTE;\n\n if (seconds > 0) {\n duration[\"seconds\"] = seconds;\n }\n\n if (minutes === 0) {\n break;\n }\n\n let hours = Math.floor(minutes / MINUTES_PER_HOUR);\n minutes -= hours * MINUTES_PER_HOUR;\n\n if (minutes > 0) {\n duration[\"minutes\"] = minutes;\n }\n\n if (hours === 0) {\n break;\n }\n\n const days = Math.floor(hours / HOURS_PER_DAY);\n hours -= days * HOURS_PER_DAY;\n\n if (hours > 0) {\n duration[\"hours\"] = hours;\n }\n\n if (days > 0) {\n duration[\"days\"] = days;\n }\n }\n\n return duration;\n}\n\n/**\n * Returns the number of milliseconds for the given duration.\n */\nexport function durationToMs(duration: Duration): number {\n const daysMs = (duration.days ?? 0) * MS_PER_DAY;\n const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;\n const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;\n const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;\n const msMs = duration.milliseconds ?? 0;\n\n return daysMs + hoursMs + minsMs + secsMs + msMs;\n}\n\n/**\n * Convenience function to return a duration given an ms or Duration.\n */\nexport function durationOrMsToMs(duration: number | Duration): number {\n return typeof duration === \"number\" ? duration : durationToMs(duration);\n}\n\n/**\n * Format a given Duration object into a string. If the object has no fields,\n * then returns an empty string.\n *\n * @param style Defaults to 'short'\n */\nexport function formatDuration(duration: Duration, style?: DurationStyle) {\n style = style ?? \"short\";\n const stylePlural = getDurationStyleForPlural(style);\n\n const space = getValueAndUnitSeparator(style);\n\n const a: string[] = [];\n\n for (const unit of DURATION_TYPE_SEQUENCE) {\n const value = duration[unit];\n if (value === undefined) continue;\n\n const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];\n const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];\n a.push(value + space + suffix);\n }\n\n const separator = getDurationTypeSeparator(style);\n return a.join(separator);\n}\n\n/**\n * Convert a millisecond duration into a human-readable duration string.\n *\n * @param options.durationTypeForZero - Defaults to 'milliseconds'\n * @param options.style - Defaults to 'short'\n */\nexport function readableDuration(\n ms: number,\n options?: { durationTypeForZero?: DurationType; style?: DurationStyle },\n): string {\n const duration = msToDuration(ms, options?.durationTypeForZero);\n\n return formatDuration(duration, options?.style);\n}\n\n/** A shortened duration string useful for logging timings. */\nexport function elapsed(ms: number): string {\n // Use long format for 1 minute or over.\n if (ms > MS_PER_MINUTE) {\n return readableDuration(ms);\n }\n\n // Use seconds format for over 100ms.\n if (ms > 100) {\n return `${(ms / 1000).toFixed(3)}s`;\n }\n\n // Use milliseconds format.\n return ms + \"ms\";\n}\n","/**\n * Indexed DB key-value store with support for auto-expirations.\n *\n * Why use this?\n * 1. No need to worry about running out of storage.\n * 2. Extremely simple interface to use indexed DBs.\n * 3. Auto-expirations frees you from worrying about data clean-up.\n * 4. Any serializable data type can be stored (except undefined).\n *\n * How to use?\n * Just use the `kvStore` global constant like the local storage.\n */\n\nimport { Duration, durationOrMsToMs } from \"./duration\";\n\n// Updating the DB name will cause all old entries to be gone.\nconst DEFAULT_DB_NAME = \"KVStore\";\n\n// Updating the version will cause all old entries to be gone.\nconst DEFAULT_DB_VERSION = 1;\n\n// Use a constant store name to keep things simple.\nconst STORE_NAME = \"kvStore\";\n\n/** One day in milliseconds. */\nexport const MILLIS_PER_DAY = 86_400_000;\n\n/** 30 days in ms. */\nexport const DEFAULT_EXPIRY_DELTA_MS = MILLIS_PER_DAY * 30;\n\n/** Do GC once per day. */\nexport const GC_INTERVAL_MS = MILLIS_PER_DAY;\n\ntype StoredObject<T> = {\n key: string;\n value: T;\n storedMs: number;\n expiryMs: number;\n};\n\n/**\n * Parse a stored value string. Returns undefined if invalid or expired.\n * Throws an error if the string cannot be parsed as JSON.\n */\nfunction validateStoredObject<T>(\n obj: StoredObject<T>,\n): StoredObject<T> | undefined {\n if (\n !obj ||\n typeof obj !== \"object\" ||\n !(\"key\" in obj) ||\n typeof obj.key !== \"string\" ||\n !(\"value\" in obj) ||\n !(\"storedMs\" in obj) ||\n typeof obj.storedMs !== \"number\" ||\n obj.value === undefined ||\n !(\"expiryMs\" in obj) ||\n typeof obj.expiryMs !== \"number\" ||\n Date.now() >= obj.expiryMs\n ) {\n return undefined;\n }\n\n return obj;\n}\n\n/** Add an `onerror` handler to the request. */\nfunction withOnError<T extends IDBRequest | IDBTransaction>(\n request: T,\n reject: (reason?: unknown) => void,\n): T {\n request.onerror = (event) => {\n reject(event);\n };\n\n return request;\n}\n\nexport class KVStoreField<T> {\n public constructor(\n public readonly store: KVStore,\n public readonly key: string,\n ) {}\n\n public get(): Promise<T | undefined> {\n return this.store.get(this.key);\n }\n\n public set(t: T): Promise<T> {\n return this.store.set(this.key, t);\n }\n\n public delete(): Promise<void> {\n return this.store.delete(this.key);\n }\n}\n\n/**\n * You can create multiple KVStores if you want, but most likely you will only\n * need to use the default `kvStore` instance.\n */\nexport class KVStore {\n // We'll init the DB only on first use.\n private db: IDBDatabase | undefined;\n\n // Local storage key name for the last GC completed timestamp.\n public readonly gcMsStorageKey: string;\n\n public constructor(\n public readonly dbName: string,\n public readonly dbVersion: number,\n public readonly defaultExpiryDeltaMs: number,\n ) {\n this.gcMsStorageKey = `__kvStore:lastGcMs:${dbName}:v${dbVersion}:${STORE_NAME}`;\n }\n\n private async getOrCreateDb() {\n if (!this.db) {\n this.db = await new Promise<IDBDatabase>((resolve, reject) => {\n const request = withOnError(\n globalThis.indexedDB.open(this.dbName, this.dbVersion),\n reject,\n );\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n\n // Create the store on DB init.\n const objectStore = db.createObjectStore(STORE_NAME, {\n keyPath: \"key\",\n });\n\n objectStore.createIndex(\"key\", \"key\", {\n unique: true,\n });\n };\n\n request.onsuccess = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n resolve(db);\n };\n });\n }\n\n return this.db;\n }\n\n private async transact<T>(\n mode: IDBTransactionMode,\n callback: (\n objectStore: IDBObjectStore,\n resolve: (t: T) => void,\n reject: (reason?: unknown) => void,\n ) => void,\n ): Promise<T> {\n const db = await this.getOrCreateDb();\n\n return await new Promise<T>((resolve, reject) => {\n const transaction = withOnError(db.transaction(STORE_NAME, mode), reject);\n\n transaction.onabort = (event) => {\n reject(event);\n };\n\n const objectStore = transaction.objectStore(STORE_NAME);\n\n callback(objectStore, resolve, reject);\n });\n }\n\n public async set<T>(\n key: string,\n value: T,\n expiryDeltaMs: number | Duration = this.defaultExpiryDeltaMs,\n ): Promise<T> {\n const nowMs = Date.now();\n const obj: StoredObject<T> = {\n key,\n value,\n storedMs: nowMs,\n expiryMs: nowMs + durationOrMsToMs(expiryDeltaMs),\n };\n\n return await this.transact<T>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.put(obj), reject);\n\n request.onsuccess = () => {\n resolve(value);\n\n this.gc(); // check GC on every write\n };\n },\n );\n }\n\n /** Delete one or multiple keys. */\n public async delete(key: string | string[]): Promise<void> {\n return await this.transact<void>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n objectStore.transaction.oncomplete = () => {\n resolve();\n };\n\n if (typeof key === \"string\") {\n withOnError(objectStore.delete(key), reject);\n } else {\n for (const k of key) {\n withOnError(objectStore.delete(k), reject);\n }\n }\n },\n );\n }\n\n public async getStoredObject<T>(\n key: string,\n ): Promise<StoredObject<T> | undefined> {\n const stored = await this.transact<StoredObject<T> | undefined>(\n \"readonly\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.get(key), reject);\n\n request.onsuccess = () => {\n resolve(request.result);\n };\n },\n );\n\n if (!stored) {\n return undefined;\n }\n\n try {\n const obj = validateStoredObject(stored);\n if (!obj) {\n await this.delete(key);\n\n this.gc(); // check GC on every read of an expired key\n\n return undefined;\n }\n\n return obj;\n } catch (e) {\n console.error(`Invalid kv value: ${key}=${JSON.stringify(stored)}:`, e);\n await this.delete(key);\n\n this.gc(); // check GC on every read of an invalid key\n\n return undefined;\n }\n }\n\n public async get<T>(key: string): Promise<T | undefined> {\n const obj = await this.getStoredObject<T>(key);\n\n return obj?.value;\n }\n\n public async forEach(\n callback: (\n key: string,\n value: unknown,\n expiryMs: number,\n ) => void | Promise<void>,\n ): Promise<void> {\n await this.transact<void>(\"readonly\", (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.openCursor(), reject);\n\n request.onsuccess = async (event) => {\n const cursor = (\n event.target as unknown as { result: IDBCursorWithValue }\n ).result;\n\n if (cursor) {\n if (cursor.key) {\n const obj = validateStoredObject(cursor.value);\n if (obj) {\n await callback(String(cursor.key), obj.value, obj.expiryMs);\n } else {\n await callback(String(cursor.key), undefined, 0);\n }\n }\n cursor.continue();\n } else {\n resolve();\n }\n };\n });\n }\n\n /** Cannot be a getter because this needs to be async. */\n public async size() {\n let count = 0;\n await this.forEach(() => {\n count++;\n });\n return count;\n }\n\n public async clear() {\n const keys: string[] = [];\n await this.forEach((key) => {\n keys.push(key);\n });\n\n await this.delete(keys);\n }\n\n /** Mainly for debugging dumps. */\n public async asMap(): Promise<Map<string, unknown>> {\n const map = new Map<string, unknown>();\n await this.forEach((key, value, expiryMs) => {\n map.set(key, { value, expiryMs });\n });\n return map;\n }\n\n public get lastGcMs(): number {\n const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);\n if (!lastGcMsStr) return 0;\n\n const ms = Number(lastGcMsStr);\n return isNaN(ms) ? 0 : ms;\n }\n\n public set lastGcMs(ms: number) {\n globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));\n }\n\n /** Perform garbage-collection if due. */\n public async gc() {\n const lastGcMs = this.lastGcMs;\n\n // Set initial timestamp - no need GC now.\n if (!lastGcMs) {\n this.lastGcMs = Date.now();\n return;\n }\n\n if (Date.now() < lastGcMs + GC_INTERVAL_MS) {\n return; // not due for next GC yet\n }\n\n // GC is due now, so run it.\n await this.gcNow();\n }\n\n /** Perform garbage-collection immediately without checking. */\n public async gcNow() {\n console.log(`Starting kvStore GC on ${this.dbName} v${this.dbVersion}...`);\n\n // Prevent concurrent GC runs.\n this.lastGcMs = Date.now();\n\n const keysToDelete: string[] = [];\n await this.forEach(\n async (key: string, value: unknown, expiryMs: number) => {\n if (value === undefined || Date.now() >= expiryMs) {\n keysToDelete.push(key);\n }\n },\n );\n\n if (keysToDelete.length) {\n await this.delete(keysToDelete);\n }\n\n console.log(\n `Finished kvStore GC on ${this.dbName} v${this.dbVersion} ` +\n `- deleted ${keysToDelete.length} keys`,\n );\n\n // Mark the end time as last GC time.\n this.lastGcMs = Date.now();\n }\n\n /** Get an independent store item with a locked key and value type. */\n public field<T>(key: string) {\n return new KVStoreField<T>(this, key);\n }\n}\n\n/**\n * Default KV store ready for immediate use. You can create new instances if\n * you want, but most likely you will only need one store instance.\n */\nexport const kvStore = new KVStore(\n DEFAULT_DB_NAME,\n DEFAULT_DB_VERSION,\n DEFAULT_EXPIRY_DELTA_MS,\n);\n\n/**\n * Class to represent one key in the store with a default expiration.\n */\nclass KvStoreItem<T> {\n public constructor(\n public readonly key: string,\n public readonly defaultExpiryDeltaMs: number,\n public readonly store = kvStore,\n ) {}\n\n /**\n * Example usage:\n *\n * const { value, storedMs, expiryMs } = await myKvItem.getStoredObject();\n */\n public async getStoredObject(): Promise<StoredObject<T> | undefined> {\n return await this.store.getStoredObject(this.key);\n }\n\n public async get(): Promise<T | undefined> {\n return await this.store.get(this.key);\n }\n\n public async set(\n value: T,\n expiryDeltaMs: number = this.defaultExpiryDeltaMs,\n ): Promise<void> {\n await this.store.set(this.key, value, expiryDeltaMs);\n }\n\n public async delete(): Promise<void> {\n await this.store.delete(this.key);\n }\n}\n\n/**\n * Create a KV store item with a key and a default expiration.\n */\nexport function kvStoreItem<T>(\n key: string,\n defaultExpiration: number | Duration,\n): KvStoreItem<T> {\n const defaultExpiryDeltaMs = durationOrMsToMs(defaultExpiration);\n\n return new KvStoreItem<T>(key, defaultExpiryDeltaMs);\n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,EAAaW,EAAgBW,IAAqB,EACnDX,IAAU,QAAa,KAAK,IAAI,GAAKW,IACvCI,EAAa,KAAK1B,CAAG,CAEzB,CACF,EAEI0B,EAAa,QACf,MAAM,KAAK,OAAOA,CAAY,EAGhC,QAAQ,IACN,0BAA0B,KAAK,MAAM,KAAK,KAAK,SAAS,cACzCA,EAAa,MAAM,OACpC,EAGA,KAAK,SAAW,KAAK,IAAI,CAC3B,CAGO,MAAS1B,EAAa,CAC3B,OAAO,IAAIF,EAAgB,KAAME,CAAG,CACtC,CACF,EAMa2B,EAAU,IAAI1B,EACzBf,EACAC,EACAG,CACF,EAKMsC,EAAN,KAAqB,CACZ,YACW5B,EACAI,EACAL,EAAQ4B,EACxB,CAHgB,SAAA3B,EACA,0BAAAI,EACA,WAAAL,CACf,CAOH,MAAa,iBAAwD,CACnE,OAAO,MAAM,KAAK,MAAM,gBAAgB,KAAK,GAAG,CAClD,CAEA,MAAa,KAA8B,CACzC,OAAO,MAAM,KAAK,MAAM,IAAI,KAAK,GAAG,CACtC,CAEA,MAAa,IACXY,EACAC,EAAwB,KAAK,qBACd,CACf,MAAM,KAAK,MAAM,IAAI,KAAK,IAAKD,EAAOC,CAAa,CACrD,CAEA,MAAa,QAAwB,CACnC,MAAM,KAAK,MAAM,OAAO,KAAK,GAAG,CAClC,CACF,EAKO,SAASiB,EACd7B,EACA8B,EACgB,CAChB,IAAM1B,EAAuBU,EAAiBgB,CAAiB,EAE/D,OAAO,IAAIF,EAAe5B,EAAKI,CAAoB,CACrD","names":["durationToMs","duration","daysMs","hoursMs","minsMs","secsMs","msMs","durationOrMsToMs","DEFAULT_DB_NAME","DEFAULT_DB_VERSION","STORE_NAME","MILLIS_PER_DAY","DEFAULT_EXPIRY_DELTA_MS","GC_INTERVAL_MS","validateStoredObject","obj","withOnError","request","reject","event","KVStoreField","store","key","KVStore","dbName","dbVersion","defaultExpiryDeltaMs","resolve","db","mode","callback","transaction","objectStore","value","expiryDeltaMs","nowMs","durationOrMsToMs","k","stored","e","cursor","count","keys","map","expiryMs","lastGcMsStr","ms","lastGcMs","keysToDelete","kvStore","KvStoreItem","kvStoreItem","defaultExpiration"]}
1
+ {"version":3,"sources":["../src/duration.ts","../src/kvStore.ts"],"sourcesContent":["/**\n * Bunch of miscellaneous constants and utility functions related to handling\n * date and time durations.\n *\n * Note that month and year do not have fixed durations, and hence are excluded\n * from this file. Weeks have fixed durations, but are excluded because we\n * use days as the max duration supported.\n */\n\nimport {\n HOURS_PER_DAY,\n MINUTES_PER_HOUR,\n MS_PER_DAY,\n MS_PER_HOUR,\n MS_PER_MINUTE,\n MS_PER_SECOND,\n SECONDS_PER_MINUTE,\n} from \"./timeConstants\";\n\nexport type Duration = {\n days?: number;\n hours?: number;\n minutes?: number;\n seconds?: number;\n milliseconds?: number;\n};\n\n/**\n * One of: days, hours, minutes, seconds, milliseconds\n */\nexport type DurationType = keyof Duration;\n\n/**\n * Order in which the duration type appears in the duration string.\n */\nexport const DURATION_TYPE_SEQUENCE: DurationType[] = [\n \"days\",\n \"hours\",\n \"minutes\",\n \"seconds\",\n \"milliseconds\",\n];\n\n/**\n * Follows the same format as Intl.DurationFormat.prototype.format().\n *\n * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns\n * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds,\n * 7 milliseconds, 8 microseconds, 9 nanoseconds\n * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns\n */\nexport type DurationStyle = \"short\" | \"long\" | \"narrow\";\n\nexport type DurationSuffixMap = {\n short: string;\n shorts: string;\n long: string;\n longs: string;\n narrow: string;\n};\n\nexport type DurationSuffixType = keyof DurationSuffixMap;\n\nexport const DURATION_STYLE_SUFFIX_MAP: Record<\n DurationType,\n DurationSuffixMap\n> = {\n days: {\n short: \"day\",\n shorts: \"days\",\n long: \"day\",\n longs: \"days\",\n narrow: \"d\",\n },\n hours: {\n short: \"hr\",\n shorts: \"hrs\",\n long: \"hour\",\n longs: \"hours\",\n narrow: \"h\",\n },\n minutes: {\n short: \"min\",\n shorts: \"mins\",\n long: \"minute\",\n longs: \"minutes\",\n narrow: \"m\",\n },\n seconds: {\n short: \"sec\",\n shorts: \"secs\",\n long: \"second\",\n longs: \"seconds\",\n narrow: \"s\",\n },\n milliseconds: {\n short: \"ms\",\n shorts: \"ms\",\n long: \"millisecond\",\n longs: \"milliseconds\",\n narrow: \"ms\",\n },\n};\n\nfunction getDurationStyleForPlural(style: DurationStyle): DurationSuffixType {\n return style == \"short\" ? \"shorts\" : style === \"long\" ? \"longs\" : style;\n}\n\nfunction getValueAndUnitSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \"\" : \" \";\n}\n\nfunction getDurationTypeSeparator(style: DurationStyle): string {\n return style === \"narrow\" ? \" \" : \", \";\n}\n\n/**\n * Convert a milliseconds duration into a Duration object. If the given ms is\n * zero, then return an object with a single field of zero with duration type\n * of durationTypeForZero.\n *\n * @param durationTypeForZero Defaults to 'milliseconds'\n */\nexport function msToDuration(\n ms: number,\n durationTypeForZero?: DurationType,\n): Duration {\n if (ms === 0) {\n durationTypeForZero = durationTypeForZero ?? \"milliseconds\";\n return { [durationTypeForZero]: 0 };\n }\n\n const duration: Duration = {};\n\n for (let i = 0; i < 1; i++) {\n let seconds = Math.floor(ms / MS_PER_SECOND);\n const millis = ms - seconds * MS_PER_SECOND;\n\n if (millis > 0) {\n duration[\"milliseconds\"] = millis;\n }\n\n if (seconds === 0) {\n break;\n }\n\n let minutes = Math.floor(seconds / SECONDS_PER_MINUTE);\n seconds -= minutes * SECONDS_PER_MINUTE;\n\n if (seconds > 0) {\n duration[\"seconds\"] = seconds;\n }\n\n if (minutes === 0) {\n break;\n }\n\n let hours = Math.floor(minutes / MINUTES_PER_HOUR);\n minutes -= hours * MINUTES_PER_HOUR;\n\n if (minutes > 0) {\n duration[\"minutes\"] = minutes;\n }\n\n if (hours === 0) {\n break;\n }\n\n const days = Math.floor(hours / HOURS_PER_DAY);\n hours -= days * HOURS_PER_DAY;\n\n if (hours > 0) {\n duration[\"hours\"] = hours;\n }\n\n if (days > 0) {\n duration[\"days\"] = days;\n }\n }\n\n return duration;\n}\n\n/**\n * Returns the number of milliseconds for the given duration.\n */\nexport function durationToMs(duration: Duration): number {\n const daysMs = (duration.days ?? 0) * MS_PER_DAY;\n const hoursMs = (duration.hours ?? 0) * MS_PER_HOUR;\n const minsMs = (duration.minutes ?? 0) * MS_PER_MINUTE;\n const secsMs = (duration.seconds ?? 0) * MS_PER_SECOND;\n const msMs = duration.milliseconds ?? 0;\n\n return daysMs + hoursMs + minsMs + secsMs + msMs;\n}\n\n/**\n * Convenience function to return a duration given an ms or Duration.\n */\nexport function durationOrMsToMs(duration: number | Duration): number {\n return typeof duration === \"number\" ? duration : durationToMs(duration);\n}\n\n/**\n * Format a given Duration object into a string. If the object has no fields,\n * then returns an empty string.\n *\n * @param style Defaults to 'short'\n */\nexport function formatDuration(duration: Duration, style?: DurationStyle) {\n style = style ?? \"short\";\n const stylePlural = getDurationStyleForPlural(style);\n\n const space = getValueAndUnitSeparator(style);\n\n const a: string[] = [];\n\n for (const unit of DURATION_TYPE_SEQUENCE) {\n const value = duration[unit];\n if (value === undefined) continue;\n\n const suffixMap = DURATION_STYLE_SUFFIX_MAP[unit];\n const suffix = value === 1 ? suffixMap[style] : suffixMap[stylePlural];\n a.push(value + space + suffix);\n }\n\n const separator = getDurationTypeSeparator(style);\n return a.join(separator);\n}\n\n/**\n * Convert a millisecond duration into a human-readable duration string.\n *\n * @param options.durationTypeForZero - Defaults to 'milliseconds'\n * @param options.style - Defaults to 'short'\n */\nexport function readableDuration(\n ms: number,\n options?: { durationTypeForZero?: DurationType; style?: DurationStyle },\n): string {\n const duration = msToDuration(ms, options?.durationTypeForZero);\n\n return formatDuration(duration, options?.style);\n}\n\n/** A shortened duration string useful for logging timings. */\nexport function elapsed(ms: number): string {\n // Use long format for 1 minute or over.\n if (ms > MS_PER_MINUTE) {\n return readableDuration(ms);\n }\n\n // Use seconds format for over 100ms.\n if (ms > 100) {\n return `${(ms / 1000).toFixed(3)}s`;\n }\n\n // Use milliseconds format.\n return ms + \"ms\";\n}\n","/**\n * Indexed DB key-value store with support for auto-expirations.\n *\n * Why use this?\n * 1. Extremely simple interface to use indexed DBs.\n * 2. Auto-expirations with GC frees you from worrying about data clean-up.\n * 3. Any serializable data type can be stored (except undefined).\n *\n * How to use?\n * Just use the `kvStore` global constant like the local storage, but with\n * async interface functions as required by indexed DB.\n *\n * Why not use the indexed DB directly?\n * It will require you to write a lot of code to reinvent the wheel.\n */\n\nimport { Duration, durationOrMsToMs } from \"./duration\";\nimport {\n FullStorageAdapter,\n StorageAdapter,\n StoredObject,\n} from \"./storageAdapter\";\nimport { MS_PER_DAY } from \"./timeConstants\";\n\n/** Global defaults can be updated directly. */\nexport const KvStoreConfig = {\n /**\n * Name of the DB in the indexed DB.\n * Updating the DB name will cause all old entries to be gone.\n */\n dbName: \"KVStore\",\n\n /**\n * Version of the DB schema. Most likely you will never want to change this.\n * Updating the version will cause all old entries to be gone.\n */\n dbVersion: 1,\n\n /**\n * Name of the store within the indexed DB. Each DB can have multiple stores.\n * In practice, it doesn't matter what you name this to be.\n */\n storeName: \"kvStore\",\n\n /** 30 days in ms. */\n expiryMs: MS_PER_DAY * 30,\n\n /** Do GC once per day. */\n gcIntervalMs: MS_PER_DAY,\n};\n\nexport type KvStoreConfig = typeof KvStoreConfig;\n\n/** Convenience function to update global defaults. */\nexport function configureKvStore(config: Partial<KvStoreConfig>) {\n Object.assign(KvStoreConfig, config);\n}\n\n/** Type to represent a full object with metadata stored in the store. */\nexport type KvStoredObject<T> = StoredObject<T> & {\n // The key is required by the ObjectStore.\n key: string;\n};\n\n/**\n * Parse a stored value string. Returns undefined if invalid or expired.\n * Throws an error if the string cannot be parsed as JSON.\n */\nfunction validateStoredObject<T>(\n obj: KvStoredObject<T>,\n): KvStoredObject<T> | undefined {\n if (\n !obj ||\n typeof obj !== \"object\" ||\n typeof obj.key !== \"string\" ||\n obj.value === undefined ||\n typeof obj.storedMs !== \"number\" ||\n typeof obj.expiryMs !== \"number\" ||\n Date.now() >= obj.expiryMs\n ) {\n return undefined;\n }\n\n return obj;\n}\n\n/** Add an `onerror` handler to the request. */\nfunction withOnError<T extends IDBRequest | IDBTransaction>(\n request: T,\n reject: (reason?: unknown) => void,\n): T {\n request.onerror = (event) => {\n reject(event);\n };\n\n return request;\n}\n\n/**\n * You can create multiple KvStores if you want, but most likely you will only\n * need to use the default `kvStore` instance.\n */\n// Using `any` because the store could store any type of data for each key,\n// but the caller can specify a more specific type when calling each of the\n// methods.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport class KvStore implements FullStorageAdapter<any> {\n /** We'll init the DB only on first use. */\n private db: IDBDatabase | undefined;\n\n /** Local storage key name for the last GC completed timestamp. */\n public readonly gcMsStorageKey: string;\n\n public readonly dbVersion: number;\n public readonly storeName: string;\n public readonly defaultExpiryMs: number;\n public readonly gcIntervalMs: number;\n\n public constructor(\n public readonly dbName: string,\n options?: {\n dbVersion?: number;\n storeName?: string;\n defaultExpiryMs?: number | Duration;\n gcIntervalMs?: number | Duration;\n },\n ) {\n this.dbVersion = options?.dbVersion ?? KvStoreConfig.dbVersion;\n this.storeName = options?.storeName ?? KvStoreConfig.storeName;\n\n this.defaultExpiryMs = options?.defaultExpiryMs\n ? durationOrMsToMs(options.defaultExpiryMs)\n : KvStoreConfig.expiryMs;\n\n this.gcIntervalMs = options?.gcIntervalMs\n ? durationOrMsToMs(options.gcIntervalMs)\n : KvStoreConfig.gcIntervalMs;\n\n this.gcMsStorageKey = `__kvStore:lastGcMs:${dbName}:v${this.dbVersion}:${this.storeName}`;\n }\n\n private async getOrCreateDb() {\n if (!this.db) {\n this.db = await new Promise<IDBDatabase>((resolve, reject) => {\n const request = withOnError(\n globalThis.indexedDB.open(this.dbName, this.dbVersion),\n reject,\n );\n\n request.onupgradeneeded = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n\n // Create the store on DB init.\n const objectStore = db.createObjectStore(this.storeName, {\n keyPath: \"key\",\n });\n\n objectStore.createIndex(\"key\", \"key\", {\n unique: true,\n });\n };\n\n request.onsuccess = (event) => {\n const db = (event.target as unknown as { result: IDBDatabase })\n .result;\n resolve(db);\n };\n });\n }\n\n return this.db;\n }\n\n private async transact<T>(\n mode: IDBTransactionMode,\n callback: (\n objectStore: IDBObjectStore,\n resolve: (t: T) => void,\n reject: (reason?: unknown) => void,\n ) => void,\n ): Promise<T> {\n const db = await this.getOrCreateDb();\n\n return await new Promise<T>((resolve, reject) => {\n const transaction = withOnError(\n db.transaction(this.storeName, mode),\n reject,\n );\n\n transaction.onabort = (event) => {\n reject(event);\n };\n\n const objectStore = transaction.objectStore(this.storeName);\n\n callback(objectStore, resolve, reject);\n });\n }\n\n /** Set a value in the store. */\n public async set<T>(\n key: string,\n value: T,\n expiryDeltaMs: number | Duration = this.defaultExpiryMs,\n ): Promise<T> {\n const nowMs = Date.now();\n const obj: KvStoredObject<T> = {\n key,\n value,\n storedMs: nowMs,\n expiryMs: nowMs + durationOrMsToMs(expiryDeltaMs),\n };\n\n return await this.transact<T>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.put(obj), reject);\n\n request.onsuccess = () => {\n resolve(value);\n\n this.gc(); // check GC on every write\n };\n },\n );\n }\n\n /** Delete one or multiple keys. */\n public async delete(key: string | string[]): Promise<void> {\n return await this.transact<void>(\n \"readwrite\",\n (objectStore, resolve, reject) => {\n objectStore.transaction.oncomplete = () => {\n resolve();\n };\n\n if (typeof key === \"string\") {\n withOnError(objectStore.delete(key), reject);\n } else {\n for (const k of key) {\n withOnError(objectStore.delete(k), reject);\n }\n }\n },\n );\n }\n\n /** Mainly used to get the expiration timestamp of an object. */\n public async getStoredObject<T>(\n key: string,\n ): Promise<KvStoredObject<T> | undefined> {\n const stored = await this.transact<KvStoredObject<T> | undefined>(\n \"readonly\",\n (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.get(key), reject);\n\n request.onsuccess = () => {\n resolve(request.result);\n };\n },\n );\n\n if (!stored) {\n return undefined;\n }\n\n try {\n const obj = validateStoredObject(stored);\n if (!obj) {\n await this.delete(key);\n\n this.gc(); // check GC on every read of an expired key\n\n return undefined;\n }\n\n return obj;\n } catch (e) {\n console.error(`Invalid kv value: ${key}=${JSON.stringify(stored)}:`, e);\n await this.delete(key);\n\n this.gc(); // check GC on every read of an invalid key\n\n return undefined;\n }\n }\n\n /** Get a value by key, or undefined if it does not exist. */\n public async get<T>(key: string): Promise<T | undefined> {\n const obj = await this.getStoredObject<T>(key);\n\n return obj?.value;\n }\n\n /** Generic way to iterate through all entries. */\n public async forEach<T>(\n callback: (\n key: string,\n value: T,\n expiryMs: number,\n storedMs: number,\n ) => void | Promise<void>,\n ): Promise<void> {\n await this.transact<void>(\"readonly\", (objectStore, resolve, reject) => {\n const request = withOnError(objectStore.openCursor(), reject);\n\n request.onsuccess = async (event) => {\n const cursor = (\n event.target as unknown as { result: IDBCursorWithValue }\n ).result;\n\n if (cursor) {\n if (cursor.key) {\n const obj = validateStoredObject(cursor.value);\n if (obj !== undefined) {\n await callback(\n String(cursor.key),\n obj.value as T,\n obj.expiryMs,\n obj.storedMs,\n );\n }\n }\n cursor.continue();\n } else {\n resolve();\n }\n };\n });\n }\n\n /**\n * Returns the number of items in the store. Note that getting the size\n * requires iterating through the entire store because the items could expire\n * at any time, and hence the size is a dynamic number.\n */\n public async size(): Promise<number> {\n let count = 0;\n await this.forEach(() => {\n count++;\n });\n return count;\n }\n\n /** Remove all items from the store. */\n public async clear(): Promise<void> {\n const keys: string[] = [];\n await this.forEach((key) => {\n keys.push(key);\n });\n\n await this.delete(keys);\n }\n\n /**\n * Returns all items as map of key to value, mainly used for debugging dumps.\n * The type T is applied to all values, even though they might not be of type\n * T (in the case when you store different data types in the same store).\n */\n public async asMap<T>(): Promise<Map<string, StoredObject<T>>> {\n const map = new Map<string, StoredObject<T>>();\n await this.forEach((key, value, expiryMs, storedMs) => {\n map.set(key, { value: value as T, expiryMs, storedMs });\n });\n return map;\n }\n\n /** Returns the ms timestamp for the last GC (garbage collection). */\n public get lastGcMs(): number {\n const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);\n if (!lastGcMsStr) return 0;\n\n const ms = Number(lastGcMsStr);\n return isNaN(ms) ? 0 : ms;\n }\n\n /** Set the ms timestamp for the last GC (garbage collection). */\n public set lastGcMs(ms: number) {\n globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));\n }\n\n /** Perform garbage-collection if due, else do nothing. */\n public async gc(): Promise<void> {\n const lastGcMs = this.lastGcMs;\n\n // Set initial timestamp - no need GC now.\n if (!lastGcMs) {\n this.lastGcMs = Date.now();\n return;\n }\n\n if (Date.now() < lastGcMs + this.gcIntervalMs) {\n return; // not due for next GC yet\n }\n\n // GC is due now, so run it.\n await this.gcNow();\n }\n\n /**\n * Perform garbage collection immediately without checking whether we are\n * due for the next GC or not.\n */\n public async gcNow(): Promise<void> {\n console.log(`Starting kvStore GC on ${this.dbName} v${this.dbVersion}...`);\n\n // Prevent concurrent GC runs.\n this.lastGcMs = Date.now();\n\n const keysToDelete: string[] = [];\n await this.forEach(\n async (key: string, value: unknown, expiryMs: number) => {\n if (value === undefined || Date.now() >= expiryMs) {\n keysToDelete.push(key);\n }\n },\n );\n\n if (keysToDelete.length) {\n await this.delete(keysToDelete);\n }\n\n console.log(\n `Finished kvStore GC on ${this.dbName} v${this.dbVersion} ` +\n `- deleted ${keysToDelete.length} keys`,\n );\n\n // Mark the end time as last GC time.\n this.lastGcMs = Date.now();\n }\n\n /** Returns `this` casted into a StorageAdapter<T>. */\n public asStorageAdapter<T>(): StorageAdapter<T> {\n return this as StorageAdapter<T>;\n }\n}\n\n/**\n * Default KV store ready for immediate use. You can create new instances if\n * you want, but most likely you will only need one store instance.\n */\nexport const kvStore = new KvStore(KvStoreConfig.dbName);\n\n/**\n * Class to represent one key in the store with a default expiration.\n */\nexport class KvStoreItem<T> {\n public readonly defaultExpiryMs: number;\n\n public constructor(\n public readonly key: string,\n defaultExpiryMs: number | Duration = KvStoreConfig.expiryMs,\n public readonly store = kvStore,\n ) {\n this.defaultExpiryMs = defaultExpiryMs && durationOrMsToMs(defaultExpiryMs);\n }\n\n /** Set a value in the store. */\n public async set(\n value: T,\n expiryDeltaMs: number | undefined = this.defaultExpiryMs,\n ): Promise<void> {\n await this.store.set(this.key, value, expiryDeltaMs);\n }\n\n /**\n * Example usage:\n *\n * const { value, storedMs, expiryMs, storedMs } =\n * await myKvItem.getStoredObject();\n */\n public async getStoredObject(): Promise<KvStoredObject<T> | undefined> {\n return await this.store.getStoredObject(this.key);\n }\n\n /** Get a value by key, or undefined if it does not exist. */\n public async get(): Promise<T | undefined> {\n return await this.store.get(this.key);\n }\n\n /** Delete this key from the store. */\n public async delete(): Promise<void> {\n await this.store.delete(this.key);\n }\n}\n\n/** Create a KV store item with a key and a default expiration. */\nexport function kvStoreItem<T>(\n key: string,\n expiryMs?: number | Duration,\n store = kvStore,\n): KvStoreItem<T> {\n expiryMs = expiryMs && durationOrMsToMs(expiryMs);\n\n return new KvStoreItem<T>(key, expiryMs, store);\n}\n"],"mappings":"AA0LO,SAASA,EAAaC,EAA4B,CACvD,IAAMC,GAAUD,EAAS,MAAQ,GAAK,MAChCE,GAAWF,EAAS,OAAS,GAAK,KAClCG,GAAUH,EAAS,SAAW,GAAK,IACnCI,GAAUJ,EAAS,SAAW,GAAK,IACnCK,EAAOL,EAAS,cAAgB,EAEtC,OAAOC,EAASC,EAAUC,EAASC,EAASC,CAC9C,CAKO,SAASC,EAAiBN,EAAqC,CACpE,OAAO,OAAOA,GAAa,SAAWA,EAAWD,EAAaC,CAAQ,CACxE,CChLO,IAAMO,EAAgB,CAK3B,OAAQ,UAMR,UAAW,EAMX,UAAW,UAGX,SAAU,MAAa,GAGvB,aAAc,KAChB,EAKO,SAASC,EAAiBC,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AC1B,CACf,MAAM,KAAK,MAAM,IAAI,KAAK,IAAKD,EAAOC,CAAa,CACrD,CAQA,MAAa,iBAA0D,CACrE,OAAO,MAAM,KAAK,MAAM,gBAAgB,KAAK,GAAG,CAClD,CAGA,MAAa,KAA8B,CACzC,OAAO,MAAM,KAAK,MAAM,IAAI,KAAK,GAAG,CACtC,CAGA,MAAa,QAAwB,CACnC,MAAM,KAAK,MAAM,OAAO,KAAK,GAAG,CAClC,CACF,EAGO,SAASmB,EACdrB,EACAW,EACAS,EAAQH,EACQ,CAChB,OAAAN,EAAWA,GAAYlB,EAAiBkB,CAAQ,EAEzC,IAAIO,EAAelB,EAAKW,EAAUS,CAAK,CAChD","names":["durationToMs","duration","daysMs","hoursMs","minsMs","secsMs","msMs","durationOrMsToMs","KvStoreConfig","configureKvStore","config","validateStoredObject","obj","withOnError","request","reject","event","KvStore","dbName","options","durationOrMsToMs","resolve","db","mode","callback","transaction","objectStore","key","value","expiryDeltaMs","nowMs","k","stored","e","cursor","count","keys","map","expiryMs","storedMs","lastGcMsStr","ms","lastGcMs","keysToDelete","kvStore","KvStoreItem","defaultExpiryMs","store","kvStoreItem"]}
package/kvStore.mjs CHANGED
@@ -18,14 +18,32 @@ function durationOrMsToMs(duration) {
18
18
  }
19
19
 
20
20
  // src/kvStore.ts
21
- var DEFAULT_DB_NAME = "KVStore";
22
- var DEFAULT_DB_VERSION = 1;
23
- var STORE_NAME = "kvStore";
24
- var MILLIS_PER_DAY = 864e5;
25
- var DEFAULT_EXPIRY_DELTA_MS = MILLIS_PER_DAY * 30;
26
- var GC_INTERVAL_MS = MILLIS_PER_DAY;
21
+ var KvStoreConfig = {
22
+ /**
23
+ * Name of the DB in the indexed DB.
24
+ * Updating the DB name will cause all old entries to be gone.
25
+ */
26
+ dbName: "KVStore",
27
+ /**
28
+ * Version of the DB schema. Most likely you will never want to change this.
29
+ * Updating the version will cause all old entries to be gone.
30
+ */
31
+ dbVersion: 1,
32
+ /**
33
+ * Name of the store within the indexed DB. Each DB can have multiple stores.
34
+ * In practice, it doesn't matter what you name this to be.
35
+ */
36
+ storeName: "kvStore",
37
+ /** 30 days in ms. */
38
+ expiryMs: MS_PER_DAY * 30,
39
+ /** Do GC once per day. */
40
+ gcIntervalMs: MS_PER_DAY
41
+ };
42
+ function configureKvStore(config) {
43
+ Object.assign(KvStoreConfig, config);
44
+ }
27
45
  function validateStoredObject(obj) {
28
- if (!obj || typeof obj !== "object" || !("key" in obj) || typeof obj.key !== "string" || !("value" in obj) || !("storedMs" in obj) || typeof obj.storedMs !== "number" || obj.value === void 0 || !("expiryMs" in obj) || typeof obj.expiryMs !== "number" || Date.now() >= obj.expiryMs) {
46
+ if (!obj || typeof obj !== "object" || typeof obj.key !== "string" || obj.value === void 0 || typeof obj.storedMs !== "number" || typeof obj.expiryMs !== "number" || Date.now() >= obj.expiryMs) {
29
47
  return void 0;
30
48
  }
31
49
  return obj;
@@ -36,32 +54,23 @@ function withOnError(request, reject) {
36
54
  };
37
55
  return request;
38
56
  }
39
- var KVStoreField = class {
40
- constructor(store, key) {
41
- this.store = store;
42
- this.key = key;
43
- }
44
- get() {
45
- return this.store.get(this.key);
46
- }
47
- set(t) {
48
- return this.store.set(this.key, t);
49
- }
50
- delete() {
51
- return this.store.delete(this.key);
52
- }
53
- };
54
- var KVStore = class {
55
- constructor(dbName, dbVersion, defaultExpiryDeltaMs) {
57
+ var KvStore = class {
58
+ constructor(dbName, options) {
56
59
  this.dbName = dbName;
57
- this.dbVersion = dbVersion;
58
- this.defaultExpiryDeltaMs = defaultExpiryDeltaMs;
59
- this.gcMsStorageKey = `__kvStore:lastGcMs:${dbName}:v${dbVersion}:${STORE_NAME}`;
60
+ this.dbVersion = options?.dbVersion ?? KvStoreConfig.dbVersion;
61
+ this.storeName = options?.storeName ?? KvStoreConfig.storeName;
62
+ this.defaultExpiryMs = options?.defaultExpiryMs ? durationOrMsToMs(options.defaultExpiryMs) : KvStoreConfig.expiryMs;
63
+ this.gcIntervalMs = options?.gcIntervalMs ? durationOrMsToMs(options.gcIntervalMs) : KvStoreConfig.gcIntervalMs;
64
+ this.gcMsStorageKey = `__kvStore:lastGcMs:${dbName}:v${this.dbVersion}:${this.storeName}`;
60
65
  }
61
- // We'll init the DB only on first use.
66
+ /** We'll init the DB only on first use. */
62
67
  db;
63
- // Local storage key name for the last GC completed timestamp.
68
+ /** Local storage key name for the last GC completed timestamp. */
64
69
  gcMsStorageKey;
70
+ dbVersion;
71
+ storeName;
72
+ defaultExpiryMs;
73
+ gcIntervalMs;
65
74
  async getOrCreateDb() {
66
75
  if (!this.db) {
67
76
  this.db = await new Promise((resolve, reject) => {
@@ -71,7 +80,7 @@ var KVStore = class {
71
80
  );
72
81
  request.onupgradeneeded = (event) => {
73
82
  const db = event.target.result;
74
- const objectStore = db.createObjectStore(STORE_NAME, {
83
+ const objectStore = db.createObjectStore(this.storeName, {
75
84
  keyPath: "key"
76
85
  });
77
86
  objectStore.createIndex("key", "key", {
@@ -89,15 +98,19 @@ var KVStore = class {
89
98
  async transact(mode, callback) {
90
99
  const db = await this.getOrCreateDb();
91
100
  return await new Promise((resolve, reject) => {
92
- const transaction = withOnError(db.transaction(STORE_NAME, mode), reject);
101
+ const transaction = withOnError(
102
+ db.transaction(this.storeName, mode),
103
+ reject
104
+ );
93
105
  transaction.onabort = (event) => {
94
106
  reject(event);
95
107
  };
96
- const objectStore = transaction.objectStore(STORE_NAME);
108
+ const objectStore = transaction.objectStore(this.storeName);
97
109
  callback(objectStore, resolve, reject);
98
110
  });
99
111
  }
100
- async set(key, value, expiryDeltaMs = this.defaultExpiryDeltaMs) {
112
+ /** Set a value in the store. */
113
+ async set(key, value, expiryDeltaMs = this.defaultExpiryMs) {
101
114
  const nowMs = Date.now();
102
115
  const obj = {
103
116
  key,
@@ -134,6 +147,7 @@ var KVStore = class {
134
147
  }
135
148
  );
136
149
  }
150
+ /** Mainly used to get the expiration timestamp of an object. */
137
151
  async getStoredObject(key) {
138
152
  const stored = await this.transact(
139
153
  "readonly",
@@ -162,10 +176,12 @@ var KVStore = class {
162
176
  return void 0;
163
177
  }
164
178
  }
179
+ /** Get a value by key, or undefined if it does not exist. */
165
180
  async get(key) {
166
181
  const obj = await this.getStoredObject(key);
167
182
  return obj?.value;
168
183
  }
184
+ /** Generic way to iterate through all entries. */
169
185
  async forEach(callback) {
170
186
  await this.transact("readonly", (objectStore, resolve, reject) => {
171
187
  const request = withOnError(objectStore.openCursor(), reject);
@@ -174,10 +190,13 @@ var KVStore = class {
174
190
  if (cursor) {
175
191
  if (cursor.key) {
176
192
  const obj = validateStoredObject(cursor.value);
177
- if (obj) {
178
- await callback(String(cursor.key), obj.value, obj.expiryMs);
179
- } else {
180
- await callback(String(cursor.key), void 0, 0);
193
+ if (obj !== void 0) {
194
+ await callback(
195
+ String(cursor.key),
196
+ obj.value,
197
+ obj.expiryMs,
198
+ obj.storedMs
199
+ );
181
200
  }
182
201
  }
183
202
  cursor.continue();
@@ -187,7 +206,11 @@ var KVStore = class {
187
206
  };
188
207
  });
189
208
  }
190
- /** Cannot be a getter because this needs to be async. */
209
+ /**
210
+ * Returns the number of items in the store. Note that getting the size
211
+ * requires iterating through the entire store because the items could expire
212
+ * at any time, and hence the size is a dynamic number.
213
+ */
191
214
  async size() {
192
215
  let count = 0;
193
216
  await this.forEach(() => {
@@ -195,6 +218,7 @@ var KVStore = class {
195
218
  });
196
219
  return count;
197
220
  }
221
+ /** Remove all items from the store. */
198
222
  async clear() {
199
223
  const keys = [];
200
224
  await this.forEach((key) => {
@@ -202,36 +226,45 @@ var KVStore = class {
202
226
  });
203
227
  await this.delete(keys);
204
228
  }
205
- /** Mainly for debugging dumps. */
229
+ /**
230
+ * Returns all items as map of key to value, mainly used for debugging dumps.
231
+ * The type T is applied to all values, even though they might not be of type
232
+ * T (in the case when you store different data types in the same store).
233
+ */
206
234
  async asMap() {
207
235
  const map = /* @__PURE__ */ new Map();
208
- await this.forEach((key, value, expiryMs) => {
209
- map.set(key, { value, expiryMs });
236
+ await this.forEach((key, value, expiryMs, storedMs) => {
237
+ map.set(key, { value, expiryMs, storedMs });
210
238
  });
211
239
  return map;
212
240
  }
241
+ /** Returns the ms timestamp for the last GC (garbage collection). */
213
242
  get lastGcMs() {
214
243
  const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);
215
244
  if (!lastGcMsStr) return 0;
216
245
  const ms = Number(lastGcMsStr);
217
246
  return isNaN(ms) ? 0 : ms;
218
247
  }
248
+ /** Set the ms timestamp for the last GC (garbage collection). */
219
249
  set lastGcMs(ms) {
220
250
  globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));
221
251
  }
222
- /** Perform garbage-collection if due. */
252
+ /** Perform garbage-collection if due, else do nothing. */
223
253
  async gc() {
224
254
  const lastGcMs = this.lastGcMs;
225
255
  if (!lastGcMs) {
226
256
  this.lastGcMs = Date.now();
227
257
  return;
228
258
  }
229
- if (Date.now() < lastGcMs + GC_INTERVAL_MS) {
259
+ if (Date.now() < lastGcMs + this.gcIntervalMs) {
230
260
  return;
231
261
  }
232
262
  await this.gcNow();
233
263
  }
234
- /** Perform garbage-collection immediately without checking. */
264
+ /**
265
+ * Perform garbage collection immediately without checking whether we are
266
+ * due for the next GC or not.
267
+ */
235
268
  async gcNow() {
236
269
  console.log(`Starting kvStore GC on ${this.dbName} v${this.dbVersion}...`);
237
270
  this.lastGcMs = Date.now();
@@ -251,50 +284,50 @@ var KVStore = class {
251
284
  );
252
285
  this.lastGcMs = Date.now();
253
286
  }
254
- /** Get an independent store item with a locked key and value type. */
255
- field(key) {
256
- return new KVStoreField(this, key);
287
+ /** Returns `this` casted into a StorageAdapter<T>. */
288
+ asStorageAdapter() {
289
+ return this;
257
290
  }
258
291
  };
259
- var kvStore = new KVStore(
260
- DEFAULT_DB_NAME,
261
- DEFAULT_DB_VERSION,
262
- DEFAULT_EXPIRY_DELTA_MS
263
- );
292
+ var kvStore = new KvStore(KvStoreConfig.dbName);
264
293
  var KvStoreItem = class {
265
- constructor(key, defaultExpiryDeltaMs, store = kvStore) {
294
+ constructor(key, defaultExpiryMs = KvStoreConfig.expiryMs, store = kvStore) {
266
295
  this.key = key;
267
- this.defaultExpiryDeltaMs = defaultExpiryDeltaMs;
268
296
  this.store = store;
297
+ this.defaultExpiryMs = defaultExpiryMs && durationOrMsToMs(defaultExpiryMs);
298
+ }
299
+ defaultExpiryMs;
300
+ /** Set a value in the store. */
301
+ async set(value, expiryDeltaMs = this.defaultExpiryMs) {
302
+ await this.store.set(this.key, value, expiryDeltaMs);
269
303
  }
270
304
  /**
271
305
  * Example usage:
272
306
  *
273
- * const { value, storedMs, expiryMs } = await myKvItem.getStoredObject();
307
+ * const { value, storedMs, expiryMs, storedMs } =
308
+ * await myKvItem.getStoredObject();
274
309
  */
275
310
  async getStoredObject() {
276
311
  return await this.store.getStoredObject(this.key);
277
312
  }
313
+ /** Get a value by key, or undefined if it does not exist. */
278
314
  async get() {
279
315
  return await this.store.get(this.key);
280
316
  }
281
- async set(value, expiryDeltaMs = this.defaultExpiryDeltaMs) {
282
- await this.store.set(this.key, value, expiryDeltaMs);
283
- }
317
+ /** Delete this key from the store. */
284
318
  async delete() {
285
319
  await this.store.delete(this.key);
286
320
  }
287
321
  };
288
- function kvStoreItem(key, defaultExpiration) {
289
- const defaultExpiryDeltaMs = durationOrMsToMs(defaultExpiration);
290
- return new KvStoreItem(key, defaultExpiryDeltaMs);
322
+ function kvStoreItem(key, expiryMs, store = kvStore) {
323
+ expiryMs = expiryMs && durationOrMsToMs(expiryMs);
324
+ return new KvStoreItem(key, expiryMs, store);
291
325
  }
292
326
  export {
293
- DEFAULT_EXPIRY_DELTA_MS,
294
- GC_INTERVAL_MS,
295
- KVStore,
296
- KVStoreField,
297
- MILLIS_PER_DAY,
327
+ KvStore,
328
+ KvStoreConfig,
329
+ KvStoreItem,
330
+ configureKvStore,
298
331
  kvStore,
299
332
  kvStoreItem
300
333
  };