ml-cache 1.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.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +599 -0
  3. package/dist/client.d.ts +175 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +411 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/constants.d.ts +184 -0
  8. package/dist/constants.d.ts.map +1 -0
  9. package/dist/constants.js +236 -0
  10. package/dist/constants.js.map +1 -0
  11. package/dist/esm/client.d.ts +175 -0
  12. package/dist/esm/client.d.ts.map +1 -0
  13. package/dist/esm/client.js +407 -0
  14. package/dist/esm/client.js.map +1 -0
  15. package/dist/esm/constants.d.ts +184 -0
  16. package/dist/esm/constants.d.ts.map +1 -0
  17. package/dist/esm/constants.js +233 -0
  18. package/dist/esm/constants.js.map +1 -0
  19. package/dist/esm/index.d.ts +58 -0
  20. package/dist/esm/index.d.ts.map +1 -0
  21. package/dist/esm/index.js.map +1 -0
  22. package/dist/esm/queue.d.ts +106 -0
  23. package/dist/esm/queue.d.ts.map +1 -0
  24. package/dist/esm/queue.js +207 -0
  25. package/dist/esm/queue.js.map +1 -0
  26. package/dist/esm/storage/glacier-storage.d.ts +77 -0
  27. package/dist/esm/storage/glacier-storage.d.ts.map +1 -0
  28. package/dist/esm/storage/glacier-storage.js +133 -0
  29. package/dist/esm/storage/glacier-storage.js.map +1 -0
  30. package/dist/esm/storage/index.d.ts +7 -0
  31. package/dist/esm/storage/index.d.ts.map +1 -0
  32. package/dist/esm/storage/index.js +7 -0
  33. package/dist/esm/storage/index.js.map +1 -0
  34. package/dist/esm/storage/s3-storage.d.ts +89 -0
  35. package/dist/esm/storage/s3-storage.d.ts.map +1 -0
  36. package/dist/esm/storage/s3-storage.js +161 -0
  37. package/dist/esm/storage/s3-storage.js.map +1 -0
  38. package/dist/esm/types/index.d.ts +337 -0
  39. package/dist/esm/types/index.d.ts.map +1 -0
  40. package/dist/esm/types/index.js +6 -0
  41. package/dist/esm/types/index.js.map +1 -0
  42. package/dist/esm/utils/helpers.d.ts +132 -0
  43. package/dist/esm/utils/helpers.d.ts.map +1 -0
  44. package/dist/esm/utils/helpers.js +198 -0
  45. package/dist/esm/utils/helpers.js.map +1 -0
  46. package/dist/esm/utils/index.d.ts +8 -0
  47. package/dist/esm/utils/index.d.ts.map +1 -0
  48. package/dist/esm/utils/index.js +8 -0
  49. package/dist/esm/utils/index.js.map +1 -0
  50. package/dist/esm/utils/logger.d.ts +110 -0
  51. package/dist/esm/utils/logger.d.ts.map +1 -0
  52. package/dist/esm/utils/logger.js +177 -0
  53. package/dist/esm/utils/logger.js.map +1 -0
  54. package/dist/esm/utils/validator.d.ts +77 -0
  55. package/dist/esm/utils/validator.d.ts.map +1 -0
  56. package/dist/esm/utils/validator.js +208 -0
  57. package/dist/esm/utils/validator.js.map +1 -0
  58. package/dist/index.d.ts +58 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +100 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/index.mjs +72 -0
  63. package/dist/queue.d.ts +106 -0
  64. package/dist/queue.d.ts.map +1 -0
  65. package/dist/queue.js +211 -0
  66. package/dist/queue.js.map +1 -0
  67. package/dist/storage/glacier-storage.d.ts +77 -0
  68. package/dist/storage/glacier-storage.d.ts.map +1 -0
  69. package/dist/storage/glacier-storage.js +137 -0
  70. package/dist/storage/glacier-storage.js.map +1 -0
  71. package/dist/storage/index.d.ts +7 -0
  72. package/dist/storage/index.d.ts.map +1 -0
  73. package/dist/storage/index.js +12 -0
  74. package/dist/storage/index.js.map +1 -0
  75. package/dist/storage/s3-storage.d.ts +89 -0
  76. package/dist/storage/s3-storage.d.ts.map +1 -0
  77. package/dist/storage/s3-storage.js +165 -0
  78. package/dist/storage/s3-storage.js.map +1 -0
  79. package/dist/types/index.d.ts +337 -0
  80. package/dist/types/index.d.ts.map +1 -0
  81. package/dist/types/index.js +7 -0
  82. package/dist/types/index.js.map +1 -0
  83. package/dist/utils/helpers.d.ts +132 -0
  84. package/dist/utils/helpers.d.ts.map +1 -0
  85. package/dist/utils/helpers.js +215 -0
  86. package/dist/utils/helpers.js.map +1 -0
  87. package/dist/utils/index.d.ts +8 -0
  88. package/dist/utils/index.d.ts.map +1 -0
  89. package/dist/utils/index.js +35 -0
  90. package/dist/utils/index.js.map +1 -0
  91. package/dist/utils/logger.d.ts +110 -0
  92. package/dist/utils/logger.d.ts.map +1 -0
  93. package/dist/utils/logger.js +181 -0
  94. package/dist/utils/logger.js.map +1 -0
  95. package/dist/utils/validator.d.ts +77 -0
  96. package/dist/utils/validator.d.ts.map +1 -0
  97. package/dist/utils/validator.js +221 -0
  98. package/dist/utils/validator.js.map +1 -0
  99. package/package.json +64 -0
@@ -0,0 +1,337 @@
1
+ /**
2
+ * @fileoverview Type definitions and interfaces for ml-cache SDK
3
+ * @module ml-cache/types
4
+ */
5
+ /**
6
+ * AWS credentials configuration
7
+ * @interface AWSCredentials
8
+ */
9
+ export interface AWSCredentials {
10
+ /** AWS access key ID */
11
+ accessKeyId: string;
12
+ /** AWS secret access key */
13
+ secretAccessKey: string;
14
+ /** Optional AWS session token for temporary credentials */
15
+ sessionToken?: string;
16
+ }
17
+ /**
18
+ * S3 storage configuration
19
+ * @interface S3Config
20
+ */
21
+ export interface S3Config {
22
+ /** S3 bucket name for event storage */
23
+ bucket: string;
24
+ /** AWS region (e.g., 'us-east-1', 'eu-west-1') */
25
+ region: string;
26
+ /** Optional prefix/folder path for stored objects */
27
+ prefix?: string;
28
+ /** Optional S3 storage class (defaults to GLACIER) */
29
+ storageClass?: S3StorageClass;
30
+ }
31
+ /**
32
+ * Glacier storage configuration
33
+ * @interface GlacierConfig
34
+ */
35
+ export interface GlacierConfig {
36
+ /** Glacier vault name */
37
+ vaultName: string;
38
+ /** AWS region for Glacier vault */
39
+ region: string;
40
+ /** Account ID (use '-' for current account) */
41
+ accountId?: string;
42
+ }
43
+ /**
44
+ * S3 storage class options
45
+ * @typedef S3StorageClass
46
+ */
47
+ export type S3StorageClass = 'STANDARD' | 'REDUCED_REDUNDANCY' | 'STANDARD_IA' | 'ONEZONE_IA' | 'INTELLIGENT_TIERING' | 'GLACIER' | 'DEEP_ARCHIVE' | 'GLACIER_IR';
48
+ /**
49
+ * Base event properties that all events must have
50
+ * @interface BaseEvent
51
+ */
52
+ export interface BaseEvent {
53
+ /** Unique event identifier (auto-generated if not provided) */
54
+ eventId?: string;
55
+ /** Event timestamp (auto-generated if not provided) */
56
+ timestamp?: string;
57
+ /** Event type/name (e.g., 'page_view', 'purchase', 'signup') */
58
+ eventType: string;
59
+ }
60
+ /**
61
+ * User context information
62
+ * @interface UserContext
63
+ */
64
+ export interface UserContext {
65
+ /** Unique user identifier */
66
+ userId?: string;
67
+ /** Anonymous/session identifier */
68
+ anonymousId?: string;
69
+ /** User traits/attributes */
70
+ traits?: Record<string, unknown>;
71
+ }
72
+ /**
73
+ * Device and browser context
74
+ * @interface DeviceContext
75
+ */
76
+ export interface DeviceContext {
77
+ /** User agent string */
78
+ userAgent?: string;
79
+ /** IP address */
80
+ ip?: string;
81
+ /** Device type (mobile, desktop, tablet) */
82
+ deviceType?: string;
83
+ /** Operating system */
84
+ os?: string;
85
+ /** Browser name */
86
+ browser?: string;
87
+ /** Screen resolution */
88
+ screenResolution?: string;
89
+ /** Viewport size */
90
+ viewport?: string;
91
+ /** Device locale/language */
92
+ locale?: string;
93
+ /** Timezone */
94
+ timezone?: string;
95
+ }
96
+ /**
97
+ * Page/screen context
98
+ * @interface PageContext
99
+ */
100
+ export interface PageContext {
101
+ /** Current page URL */
102
+ url?: string;
103
+ /** Page path */
104
+ path?: string;
105
+ /** Page title */
106
+ title?: string;
107
+ /** Referrer URL */
108
+ referrer?: string;
109
+ /** Search/query string */
110
+ search?: string;
111
+ }
112
+ /**
113
+ * Campaign/UTM tracking context
114
+ * @interface CampaignContext
115
+ */
116
+ export interface CampaignContext {
117
+ /** Campaign name (utm_campaign) */
118
+ name?: string;
119
+ /** Campaign source (utm_source) */
120
+ source?: string;
121
+ /** Campaign medium (utm_medium) */
122
+ medium?: string;
123
+ /** Campaign term (utm_term) */
124
+ term?: string;
125
+ /** Campaign content (utm_content) */
126
+ content?: string;
127
+ }
128
+ /**
129
+ * Complete event context
130
+ * @interface EventContext
131
+ */
132
+ export interface EventContext {
133
+ /** User information */
134
+ user?: UserContext;
135
+ /** Device/browser information */
136
+ device?: DeviceContext;
137
+ /** Page/screen information */
138
+ page?: PageContext;
139
+ /** Campaign/marketing information */
140
+ campaign?: CampaignContext;
141
+ /** Application information */
142
+ app?: {
143
+ /** Application name */
144
+ name?: string;
145
+ /** Application version */
146
+ version?: string;
147
+ /** Build number */
148
+ build?: string;
149
+ };
150
+ /** Custom context properties */
151
+ custom?: Record<string, unknown>;
152
+ }
153
+ /**
154
+ * Complete ML Cache event
155
+ * @interface MLCacheEvent
156
+ */
157
+ export interface MLCacheEvent extends BaseEvent {
158
+ /** Event context (user, device, page, etc.) */
159
+ context?: EventContext;
160
+ /** Event properties/payload (business data) */
161
+ properties?: Record<string, unknown>;
162
+ /** Event metadata */
163
+ metadata?: EventMetadata;
164
+ }
165
+ /**
166
+ * Event metadata for internal tracking
167
+ * @interface EventMetadata
168
+ */
169
+ export interface EventMetadata {
170
+ /** SDK version that generated the event */
171
+ sdkVersion: string;
172
+ /** Event batch ID (if batched) */
173
+ batchId?: string;
174
+ /** Number of retry attempts */
175
+ retryCount?: number;
176
+ /** Source application identifier */
177
+ sourceApp?: string;
178
+ /** Environment (production, staging, development) */
179
+ environment?: string;
180
+ }
181
+ /**
182
+ * Stored event with additional storage metadata
183
+ * @interface StoredEvent
184
+ */
185
+ export interface StoredEvent extends MLCacheEvent {
186
+ /** Storage location (S3 key or Glacier archive ID) */
187
+ storageLocation: string;
188
+ /** Storage timestamp */
189
+ storedAt: string;
190
+ /** Storage type used */
191
+ storageType: 'S3' | 'GLACIER';
192
+ }
193
+ /**
194
+ * Batching configuration for event collection
195
+ * @interface BatchConfig
196
+ */
197
+ export interface BatchConfig {
198
+ /** Maximum number of events per batch */
199
+ maxSize: number;
200
+ /** Maximum time (ms) to wait before flushing batch */
201
+ maxWaitMs: number;
202
+ /** Enable/disable batching */
203
+ enabled: boolean;
204
+ }
205
+ /**
206
+ * Retry configuration for failed requests
207
+ * @interface RetryConfig
208
+ */
209
+ export interface RetryConfig {
210
+ /** Maximum number of retry attempts */
211
+ maxRetries: number;
212
+ /** Initial delay between retries (ms) */
213
+ initialDelayMs: number;
214
+ /** Maximum delay between retries (ms) */
215
+ maxDelayMs: number;
216
+ /** Enable exponential backoff */
217
+ exponentialBackoff: boolean;
218
+ }
219
+ /**
220
+ * Logging configuration
221
+ * @interface LogConfig
222
+ */
223
+ export interface LogConfig {
224
+ /** Log level */
225
+ level: LogLevel;
226
+ /** Enable/disable logging */
227
+ enabled: boolean;
228
+ /** Custom logger function */
229
+ customLogger?: LoggerFunction;
230
+ }
231
+ /**
232
+ * Log levels
233
+ * @typedef LogLevel
234
+ */
235
+ export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
236
+ /**
237
+ * Custom logger function type
238
+ * @typedef LoggerFunction
239
+ */
240
+ export type LoggerFunction = (level: LogLevel, message: string, data?: Record<string, unknown>) => void;
241
+ /**
242
+ * Storage mode configuration
243
+ * @typedef StorageMode
244
+ */
245
+ export type StorageMode = 'S3' | 'GLACIER' | 'S3_TO_GLACIER';
246
+ /**
247
+ * Complete SDK configuration
248
+ * @interface MLCacheConfig
249
+ */
250
+ export interface MLCacheConfig {
251
+ /** AWS credentials */
252
+ credentials: AWSCredentials;
253
+ /** S3 configuration (required if using S3 or S3_TO_GLACIER mode) */
254
+ s3?: S3Config;
255
+ /** Glacier configuration (required if using GLACIER mode) */
256
+ glacier?: GlacierConfig;
257
+ /** Storage mode */
258
+ storageMode: StorageMode;
259
+ /** Batching configuration */
260
+ batch?: Partial<BatchConfig>;
261
+ /** Retry configuration */
262
+ retry?: Partial<RetryConfig>;
263
+ /** Logging configuration */
264
+ log?: Partial<LogConfig>;
265
+ /** Source application identifier */
266
+ sourceApp?: string;
267
+ /** Environment name */
268
+ environment?: string;
269
+ /** Enable debug mode */
270
+ debug?: boolean;
271
+ }
272
+ /**
273
+ * Result of a track operation
274
+ * @interface TrackResult
275
+ */
276
+ export interface TrackResult {
277
+ /** Whether the operation was successful */
278
+ success: boolean;
279
+ /** Event ID */
280
+ eventId: string;
281
+ /** Storage location (if immediately stored) */
282
+ storageLocation?: string;
283
+ /** Whether event was queued for batching */
284
+ queued: boolean;
285
+ /** Error message if failed */
286
+ error?: string;
287
+ }
288
+ /**
289
+ * Result of a flush operation
290
+ * @interface FlushResult
291
+ */
292
+ export interface FlushResult {
293
+ /** Whether the flush was successful */
294
+ success: boolean;
295
+ /** Number of events flushed */
296
+ eventCount: number;
297
+ /** Storage locations of flushed events */
298
+ storageLocations: string[];
299
+ /** Failed event IDs */
300
+ failedEventIds: string[];
301
+ /** Errors encountered */
302
+ errors: string[];
303
+ }
304
+ /**
305
+ * SDK health status
306
+ * @interface HealthStatus
307
+ */
308
+ export interface HealthStatus {
309
+ /** Overall health status */
310
+ healthy: boolean;
311
+ /** S3 connection status */
312
+ s3Connected: boolean;
313
+ /** Glacier connection status */
314
+ glacierConnected: boolean;
315
+ /** Number of events in queue */
316
+ queueSize: number;
317
+ /** Last successful flush timestamp */
318
+ lastFlush?: string;
319
+ /** Last error message */
320
+ lastError?: string;
321
+ }
322
+ /**
323
+ * Event callback function type
324
+ * @typedef EventCallback
325
+ */
326
+ export type EventCallback = (event: MLCacheEvent) => void;
327
+ /**
328
+ * Error callback function type
329
+ * @typedef ErrorCallback
330
+ */
331
+ export type ErrorCallback = (error: Error, event?: MLCacheEvent) => void;
332
+ /**
333
+ * Flush callback function type
334
+ * @typedef FlushCallback
335
+ */
336
+ export type FlushCallback = (result: FlushResult) => void;
337
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.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AAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,YAAY,KAAK,IAAI,CAAC;AAEzE;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,WAAW,KAAK,IAAI,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @fileoverview Type definitions and interfaces for ml-cache SDK
3
+ * @module ml-cache/types
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
@@ -0,0 +1,132 @@
1
+ /**
2
+ * @fileoverview Helper utilities for ml-cache SDK
3
+ * @module ml-cache/utils/helpers
4
+ */
5
+ import type { MLCacheEvent, EventMetadata } from '../types';
6
+ /**
7
+ * Generates a unique event ID using UUID v4
8
+ * @returns {string} Unique event ID
9
+ */
10
+ export declare function generateEventId(): string;
11
+ /**
12
+ * Generates an ISO 8601 timestamp for the current time
13
+ * @returns {string} ISO 8601 timestamp
14
+ */
15
+ export declare function generateTimestamp(): string;
16
+ /**
17
+ * Generates a unique batch ID
18
+ * @returns {string} Unique batch ID
19
+ */
20
+ export declare function generateBatchId(): string;
21
+ /**
22
+ * Creates event metadata
23
+ * @param {Object} options - Metadata options
24
+ * @param {string} [options.sourceApp] - Source application name
25
+ * @param {string} [options.environment] - Environment name
26
+ * @param {string} [options.batchId] - Batch ID
27
+ * @param {number} [options.retryCount] - Retry attempt count
28
+ * @returns {EventMetadata} Event metadata object
29
+ */
30
+ export declare function createEventMetadata(options: {
31
+ sourceApp?: string;
32
+ environment?: string;
33
+ batchId?: string;
34
+ retryCount?: number;
35
+ }): EventMetadata;
36
+ /**
37
+ * Enriches an event with auto-generated fields
38
+ * @param {MLCacheEvent} event - Event to enrich
39
+ * @param {Object} options - Enrichment options
40
+ * @param {string} [options.sourceApp] - Source application name
41
+ * @param {string} [options.environment] - Environment name
42
+ * @returns {MLCacheEvent} Enriched event
43
+ */
44
+ export declare function enrichEvent(event: MLCacheEvent, options?: {
45
+ sourceApp?: string;
46
+ environment?: string;
47
+ }): MLCacheEvent;
48
+ /**
49
+ * Generates an S3 object key for an event or batch
50
+ * @param {Object} options - Key generation options
51
+ * @param {string} [options.prefix] - S3 prefix
52
+ * @param {Date} [options.date] - Date for partitioning
53
+ * @param {string} [options.eventId] - Event ID
54
+ * @param {string} [options.batchId] - Batch ID
55
+ * @param {string} [options.extension] - File extension
56
+ * @returns {string} S3 object key
57
+ */
58
+ export declare function generateS3Key(options: {
59
+ prefix?: string;
60
+ date?: Date;
61
+ eventId?: string;
62
+ batchId?: string;
63
+ extension?: string;
64
+ }): string;
65
+ /**
66
+ * Generates a Glacier archive description
67
+ * @param {Object} options - Description options
68
+ * @param {string} [options.batchId] - Batch ID
69
+ * @param {number} [options.eventCount] - Number of events in archive
70
+ * @param {Date} [options.date] - Archive date
71
+ * @returns {string} Archive description
72
+ */
73
+ export declare function generateGlacierDescription(options: {
74
+ batchId?: string;
75
+ eventCount?: number;
76
+ date?: Date;
77
+ }): string;
78
+ /**
79
+ * Serializes events to NDJSON format
80
+ * @param {MLCacheEvent[]} events - Events to serialize
81
+ * @returns {string} NDJSON string
82
+ */
83
+ export declare function serializeEventsToNDJSON(events: MLCacheEvent[]): string;
84
+ /**
85
+ * Deserializes events from NDJSON format
86
+ * @param {string} ndjson - NDJSON string
87
+ * @returns {MLCacheEvent[]} Parsed events
88
+ */
89
+ export declare function deserializeEventsFromNDJSON(ndjson: string): MLCacheEvent[];
90
+ /**
91
+ * Calculates the size of serialized events in bytes
92
+ * @param {MLCacheEvent[]} events - Events to measure
93
+ * @returns {number} Size in bytes
94
+ */
95
+ export declare function calculateEventsSize(events: MLCacheEvent[]): number;
96
+ /**
97
+ * Calculates retry delay with optional exponential backoff
98
+ * @param {number} attempt - Current attempt number (0-indexed)
99
+ * @param {number} initialDelayMs - Initial delay in milliseconds
100
+ * @param {number} maxDelayMs - Maximum delay in milliseconds
101
+ * @param {boolean} exponential - Use exponential backoff
102
+ * @returns {number} Delay in milliseconds
103
+ */
104
+ export declare function calculateRetryDelay(attempt: number, initialDelayMs: number, maxDelayMs: number, exponential: boolean): number;
105
+ /**
106
+ * Sleeps for a specified duration
107
+ * @param {number} ms - Duration in milliseconds
108
+ * @returns {Promise<void>} Promise that resolves after the delay
109
+ */
110
+ export declare function sleep(ms: number): Promise<void>;
111
+ /**
112
+ * Chunks an array into smaller arrays
113
+ * @template T
114
+ * @param {T[]} array - Array to chunk
115
+ * @param {number} size - Chunk size
116
+ * @returns {T[][]} Array of chunks
117
+ */
118
+ export declare function chunk<T>(array: T[], size: number): T[][];
119
+ /**
120
+ * Deep clones an object
121
+ * @template T
122
+ * @param {T} obj - Object to clone
123
+ * @returns {T} Cloned object
124
+ */
125
+ export declare function deepClone<T>(obj: T): T;
126
+ /**
127
+ * Safely stringifies an object, handling circular references
128
+ * @param {unknown} obj - Object to stringify
129
+ * @returns {string} JSON string
130
+ */
131
+ export declare function safeStringify(obj: unknown): string;
132
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/helpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG5D;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAE1C;AAED;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE;IAC3C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB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IAAI,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,CAMxD;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAEtC;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAWlD"}
@@ -0,0 +1,198 @@
1
+ /**
2
+ * @fileoverview Helper utilities for ml-cache SDK
3
+ * @module ml-cache/utils/helpers
4
+ */
5
+ import { randomUUID } from 'crypto';
6
+ import { SDK_VERSION, DEFAULT_S3_PREFIX } from '../constants';
7
+ /**
8
+ * Generates a unique event ID using UUID v4
9
+ * @returns {string} Unique event ID
10
+ */
11
+ export function generateEventId() {
12
+ return randomUUID();
13
+ }
14
+ /**
15
+ * Generates an ISO 8601 timestamp for the current time
16
+ * @returns {string} ISO 8601 timestamp
17
+ */
18
+ export function generateTimestamp() {
19
+ return new Date().toISOString();
20
+ }
21
+ /**
22
+ * Generates a unique batch ID
23
+ * @returns {string} Unique batch ID
24
+ */
25
+ export function generateBatchId() {
26
+ return `batch_${Date.now()}_${randomUUID().slice(0, 8)}`;
27
+ }
28
+ /**
29
+ * Creates event metadata
30
+ * @param {Object} options - Metadata options
31
+ * @param {string} [options.sourceApp] - Source application name
32
+ * @param {string} [options.environment] - Environment name
33
+ * @param {string} [options.batchId] - Batch ID
34
+ * @param {number} [options.retryCount] - Retry attempt count
35
+ * @returns {EventMetadata} Event metadata object
36
+ */
37
+ export function createEventMetadata(options) {
38
+ return {
39
+ sdkVersion: SDK_VERSION,
40
+ sourceApp: options.sourceApp,
41
+ environment: options.environment,
42
+ batchId: options.batchId,
43
+ retryCount: options.retryCount,
44
+ };
45
+ }
46
+ /**
47
+ * Enriches an event with auto-generated fields
48
+ * @param {MLCacheEvent} event - Event to enrich
49
+ * @param {Object} options - Enrichment options
50
+ * @param {string} [options.sourceApp] - Source application name
51
+ * @param {string} [options.environment] - Environment name
52
+ * @returns {MLCacheEvent} Enriched event
53
+ */
54
+ export function enrichEvent(event, options = {}) {
55
+ return {
56
+ ...event,
57
+ eventId: event.eventId || generateEventId(),
58
+ timestamp: event.timestamp || generateTimestamp(),
59
+ metadata: {
60
+ ...createEventMetadata(options),
61
+ ...event.metadata,
62
+ },
63
+ };
64
+ }
65
+ /**
66
+ * Generates an S3 object key for an event or batch
67
+ * @param {Object} options - Key generation options
68
+ * @param {string} [options.prefix] - S3 prefix
69
+ * @param {Date} [options.date] - Date for partitioning
70
+ * @param {string} [options.eventId] - Event ID
71
+ * @param {string} [options.batchId] - Batch ID
72
+ * @param {string} [options.extension] - File extension
73
+ * @returns {string} S3 object key
74
+ */
75
+ export function generateS3Key(options) {
76
+ const prefix = options.prefix || DEFAULT_S3_PREFIX;
77
+ const date = options.date || new Date();
78
+ const extension = options.extension || 'ndjson';
79
+ const year = date.getUTCFullYear();
80
+ const month = String(date.getUTCMonth() + 1).padStart(2, '0');
81
+ const day = String(date.getUTCDate()).padStart(2, '0');
82
+ const hour = String(date.getUTCHours()).padStart(2, '0');
83
+ const identifier = options.batchId || options.eventId || generateEventId();
84
+ return `${prefix}/${year}/${month}/${day}/${hour}/${identifier}.${extension}`;
85
+ }
86
+ /**
87
+ * Generates a Glacier archive description
88
+ * @param {Object} options - Description options
89
+ * @param {string} [options.batchId] - Batch ID
90
+ * @param {number} [options.eventCount] - Number of events in archive
91
+ * @param {Date} [options.date] - Archive date
92
+ * @returns {string} Archive description
93
+ */
94
+ export function generateGlacierDescription(options) {
95
+ const date = options.date || new Date();
96
+ const parts = [
97
+ 'ml-cache-events',
98
+ date.toISOString().slice(0, 10),
99
+ options.batchId || generateBatchId(),
100
+ ];
101
+ if (options.eventCount !== undefined) {
102
+ parts.push(`count:${options.eventCount}`);
103
+ }
104
+ return parts.join('_');
105
+ }
106
+ /**
107
+ * Serializes events to NDJSON format
108
+ * @param {MLCacheEvent[]} events - Events to serialize
109
+ * @returns {string} NDJSON string
110
+ */
111
+ export function serializeEventsToNDJSON(events) {
112
+ return events.map((event) => JSON.stringify(event)).join('\n');
113
+ }
114
+ /**
115
+ * Deserializes events from NDJSON format
116
+ * @param {string} ndjson - NDJSON string
117
+ * @returns {MLCacheEvent[]} Parsed events
118
+ */
119
+ export function deserializeEventsFromNDJSON(ndjson) {
120
+ return ndjson
121
+ .split('\n')
122
+ .filter((line) => line.trim())
123
+ .map((line) => JSON.parse(line));
124
+ }
125
+ /**
126
+ * Calculates the size of serialized events in bytes
127
+ * @param {MLCacheEvent[]} events - Events to measure
128
+ * @returns {number} Size in bytes
129
+ */
130
+ export function calculateEventsSize(events) {
131
+ const serialized = serializeEventsToNDJSON(events);
132
+ return Buffer.byteLength(serialized, 'utf8');
133
+ }
134
+ /**
135
+ * Calculates retry delay with optional exponential backoff
136
+ * @param {number} attempt - Current attempt number (0-indexed)
137
+ * @param {number} initialDelayMs - Initial delay in milliseconds
138
+ * @param {number} maxDelayMs - Maximum delay in milliseconds
139
+ * @param {boolean} exponential - Use exponential backoff
140
+ * @returns {number} Delay in milliseconds
141
+ */
142
+ export function calculateRetryDelay(attempt, initialDelayMs, maxDelayMs, exponential) {
143
+ if (!exponential) {
144
+ return Math.min(initialDelayMs, maxDelayMs);
145
+ }
146
+ const delay = initialDelayMs * Math.pow(2, attempt);
147
+ const jitter = Math.random() * 0.1 * delay;
148
+ return Math.min(delay + jitter, maxDelayMs);
149
+ }
150
+ /**
151
+ * Sleeps for a specified duration
152
+ * @param {number} ms - Duration in milliseconds
153
+ * @returns {Promise<void>} Promise that resolves after the delay
154
+ */
155
+ export function sleep(ms) {
156
+ return new Promise((resolve) => setTimeout(resolve, ms));
157
+ }
158
+ /**
159
+ * Chunks an array into smaller arrays
160
+ * @template T
161
+ * @param {T[]} array - Array to chunk
162
+ * @param {number} size - Chunk size
163
+ * @returns {T[][]} Array of chunks
164
+ */
165
+ export function chunk(array, size) {
166
+ const chunks = [];
167
+ for (let i = 0; i < array.length; i += size) {
168
+ chunks.push(array.slice(i, i + size));
169
+ }
170
+ return chunks;
171
+ }
172
+ /**
173
+ * Deep clones an object
174
+ * @template T
175
+ * @param {T} obj - Object to clone
176
+ * @returns {T} Cloned object
177
+ */
178
+ export function deepClone(obj) {
179
+ return JSON.parse(JSON.stringify(obj));
180
+ }
181
+ /**
182
+ * Safely stringifies an object, handling circular references
183
+ * @param {unknown} obj - Object to stringify
184
+ * @returns {string} JSON string
185
+ */
186
+ export function safeStringify(obj) {
187
+ const seen = new WeakSet();
188
+ return JSON.stringify(obj, (_key, value) => {
189
+ if (typeof value === 'object' && value !== null) {
190
+ if (seen.has(value)) {
191
+ return '[Circular]';
192
+ }
193
+ seen.add(value);
194
+ }
195
+ return value;
196
+ });
197
+ }
198
+ //# sourceMappingURL=helpers.js.map