promidas 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +179 -0
- package/dist/builder.d.ts +158 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +255 -0
- package/dist/builder.js.map +1 -0
- package/dist/factory.d.ts +154 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +243 -0
- package/dist/factory.js.map +1 -0
- package/dist/fetcher/client/config.d.ts +140 -0
- package/dist/fetcher/client/config.d.ts.map +1 -0
- package/dist/fetcher/client/config.js +2 -0
- package/dist/fetcher/client/config.js.map +1 -0
- package/dist/fetcher/client/fetch-with-progress.d.ts +156 -0
- package/dist/fetcher/client/fetch-with-progress.d.ts.map +1 -0
- package/dist/fetcher/client/fetch-with-progress.js +313 -0
- package/dist/fetcher/client/fetch-with-progress.js.map +1 -0
- package/dist/fetcher/client/fetch-with-timeout.d.ts +6 -0
- package/dist/fetcher/client/fetch-with-timeout.d.ts.map +1 -0
- package/dist/fetcher/client/fetch-with-timeout.js +48 -0
- package/dist/fetcher/client/fetch-with-timeout.js.map +1 -0
- package/dist/fetcher/client/protopedia-api-custom-client.d.ts +141 -0
- package/dist/fetcher/client/protopedia-api-custom-client.d.ts.map +1 -0
- package/dist/fetcher/client/protopedia-api-custom-client.js +268 -0
- package/dist/fetcher/client/protopedia-api-custom-client.js.map +1 -0
- package/dist/fetcher/client/select-custom-fetch.d.ts +58 -0
- package/dist/fetcher/client/select-custom-fetch.d.ts.map +1 -0
- package/dist/fetcher/client/select-custom-fetch.js +58 -0
- package/dist/fetcher/client/select-custom-fetch.js.map +1 -0
- package/dist/fetcher/errors/fetcher-error.d.ts +10 -0
- package/dist/fetcher/errors/fetcher-error.d.ts.map +1 -0
- package/dist/fetcher/errors/fetcher-error.js +15 -0
- package/dist/fetcher/errors/fetcher-error.js.map +1 -0
- package/dist/fetcher/index.d.ts +73 -0
- package/dist/fetcher/index.d.ts.map +1 -0
- package/dist/fetcher/index.js +70 -0
- package/dist/fetcher/index.js.map +1 -0
- package/dist/fetcher/types/index.d.ts +9 -0
- package/dist/fetcher/types/index.d.ts.map +1 -0
- package/dist/fetcher/types/index.js +7 -0
- package/dist/fetcher/types/index.js.map +1 -0
- package/dist/fetcher/types/progress-event.types.d.ts +221 -0
- package/dist/fetcher/types/progress-event.types.d.ts.map +1 -0
- package/dist/fetcher/types/progress-event.types.js +10 -0
- package/dist/fetcher/types/progress-event.types.js.map +1 -0
- package/dist/fetcher/types/prototype-api.types.d.ts +106 -0
- package/dist/fetcher/types/prototype-api.types.d.ts.map +1 -0
- package/dist/fetcher/types/prototype-api.types.js +2 -0
- package/dist/fetcher/types/prototype-api.types.js.map +1 -0
- package/dist/fetcher/types/result.types.d.ts +75 -0
- package/dist/fetcher/types/result.types.d.ts.map +1 -0
- package/dist/fetcher/types/result.types.js +2 -0
- package/dist/fetcher/types/result.types.js.map +1 -0
- package/dist/fetcher/utils/create-client-fetch.d.ts +63 -0
- package/dist/fetcher/utils/create-client-fetch.d.ts.map +1 -0
- package/dist/fetcher/utils/create-client-fetch.js +89 -0
- package/dist/fetcher/utils/create-client-fetch.js.map +1 -0
- package/dist/fetcher/utils/create-fetch-with-stripped-headers.d.ts +6 -0
- package/dist/fetcher/utils/create-fetch-with-stripped-headers.d.ts.map +1 -0
- package/dist/fetcher/utils/create-fetch-with-stripped-headers.js +40 -0
- package/dist/fetcher/utils/create-fetch-with-stripped-headers.js.map +1 -0
- package/dist/fetcher/utils/errors/handler.d.ts +58 -0
- package/dist/fetcher/utils/errors/handler.d.ts.map +1 -0
- package/dist/fetcher/utils/errors/handler.js +243 -0
- package/dist/fetcher/utils/errors/handler.js.map +1 -0
- package/dist/fetcher/utils/errors/messages.d.ts +75 -0
- package/dist/fetcher/utils/errors/messages.d.ts.map +1 -0
- package/dist/fetcher/utils/errors/messages.js +88 -0
- package/dist/fetcher/utils/errors/messages.js.map +1 -0
- package/dist/fetcher/utils/index.d.ts +13 -0
- package/dist/fetcher/utils/index.d.ts.map +1 -0
- package/dist/fetcher/utils/index.js +12 -0
- package/dist/fetcher/utils/index.js.map +1 -0
- package/dist/fetcher/utils/log-timestamp-normalization-warnings.d.ts +10 -0
- package/dist/fetcher/utils/log-timestamp-normalization-warnings.d.ts.map +1 -0
- package/dist/fetcher/utils/log-timestamp-normalization-warnings.js +32 -0
- package/dist/fetcher/utils/log-timestamp-normalization-warnings.js.map +1 -0
- package/dist/fetcher/utils/normalize-protopedia-timestamp.d.ts +59 -0
- package/dist/fetcher/utils/normalize-protopedia-timestamp.d.ts.map +1 -0
- package/dist/fetcher/utils/normalize-protopedia-timestamp.js +81 -0
- package/dist/fetcher/utils/normalize-protopedia-timestamp.js.map +1 -0
- package/dist/fetcher/utils/normalize-prototype.d.ts +56 -0
- package/dist/fetcher/utils/normalize-prototype.d.ts.map +1 -0
- package/dist/fetcher/utils/normalize-prototype.js +113 -0
- package/dist/fetcher/utils/normalize-prototype.js.map +1 -0
- package/dist/fetcher/utils/sanitize-options.d.ts +14 -0
- package/dist/fetcher/utils/sanitize-options.d.ts.map +1 -0
- package/dist/fetcher/utils/sanitize-options.js +16 -0
- package/dist/fetcher/utils/sanitize-options.js.map +1 -0
- package/dist/fetcher/utils/string-parsers.d.ts +45 -0
- package/dist/fetcher/utils/string-parsers.d.ts.map +1 -0
- package/dist/fetcher/utils/string-parsers.js +53 -0
- package/dist/fetcher/utils/string-parsers.js.map +1 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +70 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/console-logger.d.ts +74 -0
- package/dist/logger/console-logger.d.ts.map +1 -0
- package/dist/logger/console-logger.js +113 -0
- package/dist/logger/console-logger.js.map +1 -0
- package/dist/logger/factory.d.ts +88 -0
- package/dist/logger/factory.d.ts.map +1 -0
- package/dist/logger/factory.js +94 -0
- package/dist/logger/factory.js.map +1 -0
- package/dist/logger/index.d.ts +42 -0
- package/dist/logger/index.d.ts.map +1 -0
- package/dist/logger/index.js +41 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/logger.types.d.ts +49 -0
- package/dist/logger/logger.types.d.ts.map +1 -0
- package/dist/logger/logger.types.js +2 -0
- package/dist/logger/logger.types.js.map +1 -0
- package/dist/repository/errors/validation-error.d.ts +24 -0
- package/dist/repository/errors/validation-error.d.ts.map +1 -0
- package/dist/repository/errors/validation-error.js +26 -0
- package/dist/repository/errors/validation-error.js.map +1 -0
- package/dist/repository/index.d.ts +122 -0
- package/dist/repository/index.d.ts.map +1 -0
- package/dist/repository/index.js +44 -0
- package/dist/repository/index.js.map +1 -0
- package/dist/repository/protopedia-in-memory-repository.d.ts +560 -0
- package/dist/repository/protopedia-in-memory-repository.d.ts.map +1 -0
- package/dist/repository/protopedia-in-memory-repository.js +929 -0
- package/dist/repository/protopedia-in-memory-repository.js.map +1 -0
- package/dist/repository/schemas/index.d.ts +9 -0
- package/dist/repository/schemas/index.d.ts.map +1 -0
- package/dist/repository/schemas/index.js +11 -0
- package/dist/repository/schemas/index.js.map +1 -0
- package/dist/repository/schemas/params.d.ts +44 -0
- package/dist/repository/schemas/params.d.ts.map +1 -0
- package/dist/repository/schemas/params.js +44 -0
- package/dist/repository/schemas/params.js.map +1 -0
- package/dist/repository/schemas/serializable-snapshot.d.ts +33 -0
- package/dist/repository/schemas/serializable-snapshot.d.ts.map +1 -0
- package/dist/repository/schemas/serializable-snapshot.js +45 -0
- package/dist/repository/schemas/serializable-snapshot.js.map +1 -0
- package/dist/repository/types/analysis.types.d.ts +89 -0
- package/dist/repository/types/analysis.types.d.ts.map +1 -0
- package/dist/repository/types/analysis.types.js +2 -0
- package/dist/repository/types/analysis.types.js.map +1 -0
- package/dist/repository/types/index.d.ts +12 -0
- package/dist/repository/types/index.d.ts.map +1 -0
- package/dist/repository/types/index.js +7 -0
- package/dist/repository/types/index.js.map +1 -0
- package/dist/repository/types/repository-events.types.d.ts +110 -0
- package/dist/repository/types/repository-events.types.d.ts.map +1 -0
- package/dist/repository/types/repository-events.types.js +2 -0
- package/dist/repository/types/repository-events.types.js.map +1 -0
- package/dist/repository/types/repository.types.d.ts +330 -0
- package/dist/repository/types/repository.types.d.ts.map +1 -0
- package/dist/repository/types/repository.types.js +2 -0
- package/dist/repository/types/repository.types.js.map +1 -0
- package/dist/repository/types/result.types.d.ts +55 -0
- package/dist/repository/types/result.types.d.ts.map +1 -0
- package/dist/repository/types/result.types.js +2 -0
- package/dist/repository/types/result.types.js.map +1 -0
- package/dist/repository/types/serialization.types.d.ts +61 -0
- package/dist/repository/types/serialization.types.d.ts.map +1 -0
- package/dist/repository/types/serialization.types.js +2 -0
- package/dist/repository/types/serialization.types.js.map +1 -0
- package/dist/repository/types/snapshot-operation.types.d.ts +140 -0
- package/dist/repository/types/snapshot-operation.types.d.ts.map +1 -0
- package/dist/repository/types/snapshot-operation.types.js +2 -0
- package/dist/repository/types/snapshot-operation.types.js.map +1 -0
- package/dist/repository/utils/convert-fetch-result.d.ts +46 -0
- package/dist/repository/utils/convert-fetch-result.d.ts.map +1 -0
- package/dist/repository/utils/convert-fetch-result.js +59 -0
- package/dist/repository/utils/convert-fetch-result.js.map +1 -0
- package/dist/repository/utils/convert-store-result.d.ts +36 -0
- package/dist/repository/utils/convert-store-result.d.ts.map +1 -0
- package/dist/repository/utils/convert-store-result.js +36 -0
- package/dist/repository/utils/convert-store-result.js.map +1 -0
- package/dist/repository/utils/emit-repository-event-safely.d.ts +5 -0
- package/dist/repository/utils/emit-repository-event-safely.d.ts.map +1 -0
- package/dist/repository/utils/emit-repository-event-safely.js +17 -0
- package/dist/repository/utils/emit-repository-event-safely.js.map +1 -0
- package/dist/repository/utils/index.d.ts +3 -0
- package/dist/repository/utils/index.d.ts.map +1 -0
- package/dist/repository/utils/index.js +3 -0
- package/dist/repository/utils/index.js.map +1 -0
- package/dist/repository/validation/index.d.ts +9 -0
- package/dist/repository/validation/index.d.ts.map +1 -0
- package/dist/repository/validation/index.js +10 -0
- package/dist/repository/validation/index.js.map +1 -0
- package/dist/repository/validation/params-validators.d.ts +46 -0
- package/dist/repository/validation/params-validators.d.ts.map +1 -0
- package/dist/repository/validation/params-validators.js +68 -0
- package/dist/repository/validation/params-validators.js.map +1 -0
- package/dist/repository/validation/serializable-snapshot.d.ts +47 -0
- package/dist/repository/validation/serializable-snapshot.d.ts.map +1 -0
- package/dist/repository/validation/serializable-snapshot.js +104 -0
- package/dist/repository/validation/serializable-snapshot.js.map +1 -0
- package/dist/schemas/index.d.ts +8 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +8 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/normalized-prototype.d.ts +56 -0
- package/dist/schemas/normalized-prototype.d.ts.map +1 -0
- package/dist/schemas/normalized-prototype.js +123 -0
- package/dist/schemas/normalized-prototype.js.map +1 -0
- package/dist/store/errors/store-error.d.ts +148 -0
- package/dist/store/errors/store-error.d.ts.map +1 -0
- package/dist/store/errors/store-error.js +156 -0
- package/dist/store/errors/store-error.js.map +1 -0
- package/dist/store/index.d.ts +84 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +83 -0
- package/dist/store/index.js.map +1 -0
- package/dist/store/store.d.ts +295 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.js +411 -0
- package/dist/store/store.js.map +1 -0
- package/dist/store/types/index.d.ts +2 -0
- package/dist/store/types/index.d.ts.map +1 -0
- package/dist/store/types/index.js +2 -0
- package/dist/store/types/index.js.map +1 -0
- package/dist/store/types/result.types.d.ts +67 -0
- package/dist/store/types/result.types.d.ts.map +1 -0
- package/dist/store/types/result.types.js +2 -0
- package/dist/store/types/result.types.js.map +1 -0
- package/dist/types/codes.d.ts +44 -0
- package/dist/types/codes.d.ts.map +1 -0
- package/dist/types/codes.js +9 -0
- package/dist/types/codes.js.map +1 -0
- package/dist/types/index.d.ts +61 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +60 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/normalized-prototype.d.ts +95 -0
- package/dist/types/normalized-prototype.d.ts.map +1 -0
- package/dist/types/normalized-prototype.js +2 -0
- package/dist/types/normalized-prototype.js.map +1 -0
- package/dist/utils/converters/index.d.ts +15 -0
- package/dist/utils/converters/index.d.ts.map +1 -0
- package/dist/utils/converters/index.js +15 -0
- package/dist/utils/converters/index.js.map +1 -0
- package/dist/utils/converters/license-type.d.ts +23 -0
- package/dist/utils/converters/license-type.d.ts.map +1 -0
- package/dist/utils/converters/license-type.js +38 -0
- package/dist/utils/converters/license-type.js.map +1 -0
- package/dist/utils/converters/release-flag.d.ts +24 -0
- package/dist/utils/converters/release-flag.d.ts.map +1 -0
- package/dist/utils/converters/release-flag.js +40 -0
- package/dist/utils/converters/release-flag.js.map +1 -0
- package/dist/utils/converters/status.d.ts +23 -0
- package/dist/utils/converters/status.d.ts.map +1 -0
- package/dist/utils/converters/status.js +40 -0
- package/dist/utils/converters/status.js.map +1 -0
- package/dist/utils/converters/thanks-flag.d.ts +25 -0
- package/dist/utils/converters/thanks-flag.d.ts.map +1 -0
- package/dist/utils/converters/thanks-flag.js +41 -0
- package/dist/utils/converters/thanks-flag.js.map +1 -0
- package/dist/utils/deep-merge.d.ts +38 -0
- package/dist/utils/deep-merge.d.ts.map +1 -0
- package/dist/utils/deep-merge.js +85 -0
- package/dist/utils/deep-merge.js.map +1 -0
- package/dist/utils/index.d.ts +80 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +85 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logger-utils.d.ts +100 -0
- package/dist/utils/logger-utils.d.ts.map +1 -0
- package/dist/utils/logger-utils.js +265 -0
- package/dist/utils/logger-utils.js.map +1 -0
- package/dist/utils/time/constants.d.ts +14 -0
- package/dist/utils/time/constants.d.ts.map +1 -0
- package/dist/utils/time/constants.js +14 -0
- package/dist/utils/time/constants.js.map +1 -0
- package/dist/utils/time/index.d.ts +28 -0
- package/dist/utils/time/index.d.ts.map +1 -0
- package/dist/utils/time/index.js +28 -0
- package/dist/utils/time/index.js.map +1 -0
- package/dist/utils/time/parser.d.ts +91 -0
- package/dist/utils/time/parser.d.ts.map +1 -0
- package/dist/utils/time/parser.js +143 -0
- package/dist/utils/time/parser.js.map +1 -0
- package/dist/utils/validation/index.d.ts +8 -0
- package/dist/utils/validation/index.d.ts.map +1 -0
- package/dist/utils/validation/index.js +7 -0
- package/dist/utils/validation/index.js.map +1 -0
- package/dist/utils/validation/normalized-prototype.d.ts +64 -0
- package/dist/utils/validation/normalized-prototype.d.ts.map +1 -0
- package/dist/utils/validation/normalized-prototype.js +97 -0
- package/dist/utils/validation/normalized-prototype.js.map +1 -0
- package/dist/utils/validation/types.d.ts +62 -0
- package/dist/utils/validation/types.d.ts.map +1 -0
- package/dist/utils/validation/types.js +8 -0
- package/dist/utils/validation/types.js.map +1 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/package.json +138 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file In-memory store that keeps the most recent ProtoPedia snapshot and exposes
|
|
3
|
+
* efficient lookup helpers (O(1) by id via index, stale detection, etc.).
|
|
4
|
+
*
|
|
5
|
+
* The store sits above upstream fetch logic, allowing server actions to reuse
|
|
6
|
+
* canonical data without repeated API calls while still respecting TTL limits.
|
|
7
|
+
*/
|
|
8
|
+
import type { DeepReadonly } from 'ts-essentials';
|
|
9
|
+
import { type Logger, type LogLevel } from '../logger/index.js';
|
|
10
|
+
import type { NormalizedPrototype } from '../types/index.js';
|
|
11
|
+
/**
|
|
12
|
+
* Hard limit for data size in bytes for storing snapshots.
|
|
13
|
+
* Attempting to configure a store with a size larger than this will throw an error.
|
|
14
|
+
*/
|
|
15
|
+
export declare const LIMIT_DATA_SIZE_BYTES: number;
|
|
16
|
+
/**
|
|
17
|
+
* Configuration options for the PrototypeInMemoryStore.
|
|
18
|
+
*/
|
|
19
|
+
export type PrototypeInMemoryStoreConfig = {
|
|
20
|
+
/**
|
|
21
|
+
* TTL in milliseconds after which the cached snapshot is considered expired.
|
|
22
|
+
* @default 1800000 (30 minutes)
|
|
23
|
+
*/
|
|
24
|
+
ttlMs?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Maximum allowed data size in bytes for storing snapshots.
|
|
27
|
+
* @default 10485760 (10 MiB)
|
|
28
|
+
*/
|
|
29
|
+
maxDataSizeBytes?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Custom logger instance.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* - If provided, the logger will be used as-is
|
|
35
|
+
* - If provided with logLevel, the level will be updated if logger is mutable
|
|
36
|
+
* - If not provided, creates a ConsoleLogger with the specified logLevel
|
|
37
|
+
*
|
|
38
|
+
* @default undefined (creates ConsoleLogger with 'info' level)
|
|
39
|
+
*/
|
|
40
|
+
logger?: Logger;
|
|
41
|
+
/**
|
|
42
|
+
* Log level for creating a default ConsoleLogger.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* - Only used when `logger` is NOT provided
|
|
46
|
+
* - Creates a new ConsoleLogger with this level
|
|
47
|
+
* - If logger is provided and mutable, updates its level property
|
|
48
|
+
*
|
|
49
|
+
* @default 'info'
|
|
50
|
+
*/
|
|
51
|
+
logLevel?: LogLevel;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Statistics and metadata about the current state of the PrototypeInMemoryStore.
|
|
55
|
+
*
|
|
56
|
+
* Provides information about cache health, configuration, and runtime state.
|
|
57
|
+
*/
|
|
58
|
+
export type PrototypeInMemoryStats = {
|
|
59
|
+
/** Number of prototypes currently stored in the cache. */
|
|
60
|
+
size: number;
|
|
61
|
+
/** Timestamp when the snapshot was last cached, or null if never cached. */
|
|
62
|
+
cachedAt: Date | null;
|
|
63
|
+
/** Whether the cached snapshot has exceeded its TTL. */
|
|
64
|
+
isExpired: boolean;
|
|
65
|
+
/** Remaining time in milliseconds until expiration. 0 if expired or no data cached. */
|
|
66
|
+
remainingTtlMs: number;
|
|
67
|
+
/** Exact size of the cached snapshot in bytes (JSON serialized). */
|
|
68
|
+
dataSizeBytes: number;
|
|
69
|
+
/** Whether a background refresh operation is currently in progress. */
|
|
70
|
+
refreshInFlight: boolean;
|
|
71
|
+
};
|
|
72
|
+
type RefreshTask = () => Promise<void>;
|
|
73
|
+
export type Snapshot = {
|
|
74
|
+
data: readonly DeepReadonly<NormalizedPrototype>[];
|
|
75
|
+
cachedAt: Date | null;
|
|
76
|
+
isExpired: boolean;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* In-memory store that keeps the full set of normalized prototypes with an ID-based index.
|
|
80
|
+
*
|
|
81
|
+
* The store accepts full snapshots (`setAll`) and exposes O(1) lookups by prototype id
|
|
82
|
+
* via an internal index. When TTL expires, data remains readable while callers kick off
|
|
83
|
+
* background refresh tasks using {@link runExclusive} to avoid redundant upstream calls.
|
|
84
|
+
*/
|
|
85
|
+
export declare class PrototypeInMemoryStore {
|
|
86
|
+
private readonly logger;
|
|
87
|
+
private readonly logLevel;
|
|
88
|
+
private readonly ttlMs;
|
|
89
|
+
private readonly maxDataSizeBytes;
|
|
90
|
+
private prototypeIdIndex;
|
|
91
|
+
private prototypes;
|
|
92
|
+
private cachedAt;
|
|
93
|
+
private dataSizeBytes;
|
|
94
|
+
private refreshPromise;
|
|
95
|
+
/**
|
|
96
|
+
* Create a new PrototypeInMemoryStore instance.
|
|
97
|
+
*
|
|
98
|
+
* Initializes an in-memory cache for normalized prototypes with configurable
|
|
99
|
+
* TTL and size limits. The store manages snapshot expiration, refresh state,
|
|
100
|
+
* and provides type-safe read-only access to cached data.
|
|
101
|
+
*
|
|
102
|
+
* @param config - Configuration options for the store
|
|
103
|
+
* @param config.ttlMs - Time-to-live in milliseconds for cached snapshots.
|
|
104
|
+
* Defaults to 30 minutes (1,800,000ms). After this duration, the snapshot
|
|
105
|
+
* is considered expired and should be refreshed.
|
|
106
|
+
* @param config.maxDataSizeBytes - Maximum allowed size for cached data in bytes.
|
|
107
|
+
* Defaults to 10 MiB (10,485,760 bytes). Must not exceed 30 MiB.
|
|
108
|
+
* If a snapshot exceeds this limit, setAll() will reject it.
|
|
109
|
+
* @param config.logger - Optional custom logger instance. If not provided,
|
|
110
|
+
* a default ConsoleLogger is created.
|
|
111
|
+
* @param config.logLevel - Log level for the logger. When `logger` is not provided,
|
|
112
|
+
* creates a ConsoleLogger with this level. When `logger` is provided and mutable,
|
|
113
|
+
* updates the logger's level property.
|
|
114
|
+
*
|
|
115
|
+
* @throws {ConfigurationError} When maxDataSizeBytes exceeds LIMIT_DATA_SIZE_BYTES (30 MiB)
|
|
116
|
+
*/
|
|
117
|
+
constructor({ ttlMs, maxDataSizeBytes, logger, logLevel, }?: PrototypeInMemoryStoreConfig);
|
|
118
|
+
/**
|
|
119
|
+
* Retrieve the configuration used to initialize this store.
|
|
120
|
+
*
|
|
121
|
+
* Returns the resolved configuration values (TTL and max payload size) that were
|
|
122
|
+
* set during instantiation. These values are immutable after construction.
|
|
123
|
+
*/
|
|
124
|
+
getConfig(): Omit<Required<PrototypeInMemoryStoreConfig>, 'logger'>;
|
|
125
|
+
/** Count of prototypes currently kept in the in-memory store. */
|
|
126
|
+
get size(): number;
|
|
127
|
+
/** Timestamp representing when the snapshot was last refreshed. */
|
|
128
|
+
getCachedAt(): Date | null;
|
|
129
|
+
/**
|
|
130
|
+
* Calculate elapsed time in milliseconds since the snapshot was cached.
|
|
131
|
+
* Returns 0 if no data is cached.
|
|
132
|
+
*/
|
|
133
|
+
private getElapsedTime;
|
|
134
|
+
/**
|
|
135
|
+
* Calculate remaining time in milliseconds until expiration.
|
|
136
|
+
* Returns 0 if already expired or no data is cached.
|
|
137
|
+
*/
|
|
138
|
+
private getRemainingTtl;
|
|
139
|
+
/** Determine whether the snapshot is stale based on the configured TTL. */
|
|
140
|
+
isExpired(): boolean;
|
|
141
|
+
/** Report whether a background refresh is currently in flight. */
|
|
142
|
+
isRefreshInFlight(): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Provide statistics describing cache health and runtime state.
|
|
145
|
+
*
|
|
146
|
+
* Returns metadata about the current snapshot including size, expiration status,
|
|
147
|
+
* and refresh state. For configuration values like TTL, use {@link getConfig}.
|
|
148
|
+
*/
|
|
149
|
+
getStats(): PrototypeInMemoryStats;
|
|
150
|
+
/**
|
|
151
|
+
* Estimates the JSON payload size of an array of NormalizedPrototypes in bytes.
|
|
152
|
+
*
|
|
153
|
+
* This method calculates the size by iteratively serializing each item and summing their byte lengths,
|
|
154
|
+
* along with the overhead for array brackets and commas. This approach minimizes memory usage
|
|
155
|
+
* by avoiding the creation of a single large JSON string for the entire array, thus reducing
|
|
156
|
+
* the risk of out-of-memory errors, especially with large datasets.
|
|
157
|
+
*
|
|
158
|
+
* @param data - The array of NormalizedPrototypes to estimate the size for.
|
|
159
|
+
* @returns The estimated size in bytes of the JSON-serialized data.
|
|
160
|
+
* @throws {SizeEstimationError} When JSON serialization fails (e.g., circular references).
|
|
161
|
+
*/
|
|
162
|
+
private estimateSize;
|
|
163
|
+
/**
|
|
164
|
+
* Execute a refresh task while preventing concurrent execution.
|
|
165
|
+
*
|
|
166
|
+
* @returns Promise resolved when the task completes; callers may ignore it for background refreshes.
|
|
167
|
+
*/
|
|
168
|
+
runExclusive(task: RefreshTask): Promise<void>;
|
|
169
|
+
/** Reset the store to an empty state and clear all metadata. */
|
|
170
|
+
clear(): void;
|
|
171
|
+
/**
|
|
172
|
+
* Store the provided snapshot if it fits within the configured payload limit.
|
|
173
|
+
* Creates a shallow copy of the input array to prevent external mutations.
|
|
174
|
+
*
|
|
175
|
+
* @param prototypes - Array of normalized prototypes to store (array will be copied)
|
|
176
|
+
* @returns Metadata about the stored snapshot including the exact data size in bytes
|
|
177
|
+
* @throws {SizeEstimationError} When data size estimation fails
|
|
178
|
+
* @throws {DataSizeExceededError} When the payload exceeds the configured maximum size limit
|
|
179
|
+
*
|
|
180
|
+
* @remarks
|
|
181
|
+
* **Error Handling**: When an error is thrown, the store is NOT modified.
|
|
182
|
+
* Any previously stored snapshot remains intact and accessible. This ensures
|
|
183
|
+
* that applications can continue serving data from the last successful snapshot
|
|
184
|
+
* even when new data cannot be stored.
|
|
185
|
+
*
|
|
186
|
+
* The method creates a shallow copy of the input array to ensure the store's
|
|
187
|
+
* internal state cannot be corrupted by external mutations of the array.
|
|
188
|
+
* However, the prototype objects themselves are not cloned. Callers must not
|
|
189
|
+
* mutate the prototype objects after passing them to this method.
|
|
190
|
+
*
|
|
191
|
+
* Size calculation is performed AFTER deduplication to ensure accurate size checking.
|
|
192
|
+
* If duplicate IDs are present in the input array, only the last occurrence is kept.
|
|
193
|
+
*/
|
|
194
|
+
setAll(prototypes: NormalizedPrototype[]): {
|
|
195
|
+
dataSizeBytes: number;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Retrieve the latest fetched prototypes in their original order.
|
|
199
|
+
*
|
|
200
|
+
* Returns type-level readonly reference to the internal prototypes array.
|
|
201
|
+
* The readonly type provides compile-time safety but not runtime protection.
|
|
202
|
+
*
|
|
203
|
+
* @returns Type-level readonly array of prototypes
|
|
204
|
+
*
|
|
205
|
+
* @remarks
|
|
206
|
+
* **Type Safety**: This method returns a readonly-typed reference without
|
|
207
|
+
* runtime immutability enforcement (no Object.freeze or defensive copying).
|
|
208
|
+
* Callers must honor the readonly contract and not cast it away.
|
|
209
|
+
*
|
|
210
|
+
* **Performance**: Direct reference with zero overhead - suitable for
|
|
211
|
+
* high-frequency reads of large datasets.
|
|
212
|
+
*/
|
|
213
|
+
getAll(): readonly DeepReadonly<NormalizedPrototype>[];
|
|
214
|
+
/**
|
|
215
|
+
* Return a lightweight structure containing the cached data and metadata.
|
|
216
|
+
*
|
|
217
|
+
* Useful for callers that want to inspect expiry state without mutating the store.
|
|
218
|
+
*/
|
|
219
|
+
getSnapshot(): Snapshot;
|
|
220
|
+
/**
|
|
221
|
+
* Retrieve a single prototype by its numeric identifier.
|
|
222
|
+
*
|
|
223
|
+
* Uses the internal prototypeIdIndex for O(1) constant-time lookup, providing
|
|
224
|
+
* exceptional performance even with thousands of cached prototypes. This is
|
|
225
|
+
* significantly faster than linear search alternatives (approximately 12,500x
|
|
226
|
+
* faster for 5,000 items).
|
|
227
|
+
*
|
|
228
|
+
* The index-based implementation adds minimal memory overhead (~230KB for 5,000
|
|
229
|
+
* items, or ~0.8% of total cache size) while delivering constant-time access
|
|
230
|
+
* regardless of cache size.
|
|
231
|
+
*
|
|
232
|
+
* @param prototypeId - The numeric ID of the prototype to retrieve
|
|
233
|
+
* @returns The prototype with type-level immutability, or null if not found
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* const proto = store.getByPrototypeId(123);
|
|
238
|
+
* if (proto) {
|
|
239
|
+
* console.log(proto.prototypeNm);
|
|
240
|
+
* }
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* @performance
|
|
244
|
+
* - Time complexity: O(1) - constant time regardless of cache size
|
|
245
|
+
* - Measured: ~0.0002ms per lookup (10,000 items)
|
|
246
|
+
* - Memory overhead: ~40 bytes per entry (including index metadata and hash table)
|
|
247
|
+
*/
|
|
248
|
+
getByPrototypeId(prototypeId: number): DeepReadonly<NormalizedPrototype> | null;
|
|
249
|
+
/**
|
|
250
|
+
* Return an array of all cached prototype IDs.
|
|
251
|
+
*
|
|
252
|
+
* This method provides efficient access to prototype IDs without copying the
|
|
253
|
+
* entire prototype objects. Useful for operations that only need IDs, such as
|
|
254
|
+
* ID-based filtering, statistics, or exporting ID lists.
|
|
255
|
+
*
|
|
256
|
+
* @returns Read-only array of prototype IDs in insertion order
|
|
257
|
+
*
|
|
258
|
+
* @performance
|
|
259
|
+
* - Time complexity: O(n) - must iterate through all Map keys
|
|
260
|
+
* - Memory: Creates a new array of numbers (~40 bytes per ID)
|
|
261
|
+
* - Lighter than getAll() which copies full objects (~300+ bytes each)
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* // ✅ Good: Call once and reuse
|
|
266
|
+
* const ids = store.getPrototypeIds();
|
|
267
|
+
* const count = ids.length;
|
|
268
|
+
* const maxId = Math.max(...ids);
|
|
269
|
+
*
|
|
270
|
+
* // ✅ Good: Single-use cases
|
|
271
|
+
* return { availableIds: store.getPrototypeIds() };
|
|
272
|
+
*
|
|
273
|
+
* // ❌ Bad: Repeated calls in loops
|
|
274
|
+
* for (let i = 0; i < 1000; i++) {
|
|
275
|
+
* const ids = store.getPrototypeIds(); // O(n) × 1000 = very slow!
|
|
276
|
+
* const id = ids[Math.floor(Math.random() * ids.length)];
|
|
277
|
+
* }
|
|
278
|
+
*
|
|
279
|
+
* // ✅ Better: Use getAll() once for repeated access
|
|
280
|
+
* const all = store.getAll();
|
|
281
|
+
* for (let i = 0; i < 1000; i++) {
|
|
282
|
+
* const item = all[Math.floor(Math.random() * all.length)];
|
|
283
|
+
* }
|
|
284
|
+
* ```
|
|
285
|
+
*
|
|
286
|
+
* @remarks
|
|
287
|
+
* **Performance Warning**: This method creates a new array on every call.
|
|
288
|
+
* For high-frequency operations (loops, repeated random access), prefer
|
|
289
|
+
* calling {@link getAll} once and reusing the result. The O(n) cost per
|
|
290
|
+
* call makes this unsuitable for tight loops.
|
|
291
|
+
*/
|
|
292
|
+
getPrototypeIds(): readonly number[];
|
|
293
|
+
}
|
|
294
|
+
export {};
|
|
295
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../lib/store/store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,QAAQ,EAEd,MAAM,oBAAoB,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