@klime/node 1.0.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,545 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ KlimeClient: () => KlimeClient,
34
+ SendError: () => SendError
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+ var https = __toESM(require("https"), 1);
38
+ var http = __toESM(require("http"), 1);
39
+ var url = __toESM(require("url"), 1);
40
+
41
+ // src/types.ts
42
+ var SendError = class extends Error {
43
+ constructor(message, events) {
44
+ super(message);
45
+ this.name = "SendError";
46
+ this.events = events;
47
+ }
48
+ };
49
+
50
+ // src/index.ts
51
+ var DEFAULT_ENDPOINT = "https://i.klime.com";
52
+ var DEFAULT_FLUSH_INTERVAL = 2e3;
53
+ var DEFAULT_MAX_BATCH_SIZE = 20;
54
+ var DEFAULT_MAX_QUEUE_SIZE = 1e3;
55
+ var DEFAULT_RETRY_MAX_ATTEMPTS = 5;
56
+ var DEFAULT_RETRY_INITIAL_DELAY = 1e3;
57
+ var MAX_BATCH_SIZE = 100;
58
+ var MAX_EVENT_SIZE_BYTES = 200 * 1024;
59
+ var MAX_BATCH_SIZE_BYTES = 10 * 1024 * 1024;
60
+ var SDK_VERSION = "1.1.0";
61
+ var hasNativeFetch = typeof fetch !== "undefined";
62
+ var createDefaultLogger = () => ({
63
+ debug: (message, ...args) => console.debug(`[Klime] ${message}`, ...args),
64
+ info: (message, ...args) => console.info(`[Klime] ${message}`, ...args),
65
+ warn: (message, ...args) => console.warn(`[Klime] ${message}`, ...args),
66
+ error: (message, ...args) => console.error(`[Klime] ${message}`, ...args)
67
+ });
68
+ var KlimeClient = class {
69
+ constructor(config) {
70
+ this.queue = [];
71
+ this.flushTimer = null;
72
+ this.isShutdown = false;
73
+ this.flushPromise = null;
74
+ this.shutdownHandlers = [];
75
+ if (!config.writeKey) {
76
+ throw new Error("writeKey is required");
77
+ }
78
+ this.config = {
79
+ writeKey: config.writeKey,
80
+ endpoint: config.endpoint || DEFAULT_ENDPOINT,
81
+ flushInterval: config.flushInterval ?? DEFAULT_FLUSH_INTERVAL,
82
+ maxBatchSize: Math.min(
83
+ config.maxBatchSize ?? DEFAULT_MAX_BATCH_SIZE,
84
+ MAX_BATCH_SIZE
85
+ ),
86
+ maxQueueSize: config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE,
87
+ retryMaxAttempts: config.retryMaxAttempts ?? DEFAULT_RETRY_MAX_ATTEMPTS,
88
+ retryInitialDelay: config.retryInitialDelay ?? DEFAULT_RETRY_INITIAL_DELAY,
89
+ flushOnShutdown: config.flushOnShutdown ?? true,
90
+ logger: config.logger ?? createDefaultLogger(),
91
+ onError: config.onError,
92
+ onSuccess: config.onSuccess
93
+ };
94
+ if (this.config.flushOnShutdown) {
95
+ const shutdownHandler = async () => {
96
+ await this.shutdown();
97
+ };
98
+ process.on("SIGTERM", shutdownHandler);
99
+ process.on("SIGINT", shutdownHandler);
100
+ this.shutdownHandlers.push(() => {
101
+ process.removeListener("SIGTERM", shutdownHandler);
102
+ process.removeListener("SIGINT", shutdownHandler);
103
+ });
104
+ }
105
+ this.scheduleFlush();
106
+ }
107
+ track(event, properties, options) {
108
+ if (this.isShutdown) {
109
+ return;
110
+ }
111
+ const eventObj = {
112
+ type: "track",
113
+ messageId: this.generateUUID(),
114
+ event,
115
+ timestamp: this.generateTimestamp(),
116
+ properties: properties || {},
117
+ context: this.getContext()
118
+ };
119
+ if (options?.userId) {
120
+ eventObj.userId = options.userId;
121
+ }
122
+ if (options?.groupId) {
123
+ eventObj.groupId = options.groupId;
124
+ }
125
+ this.enqueue(eventObj);
126
+ }
127
+ identify(userId, traits, options) {
128
+ if (this.isShutdown) {
129
+ return;
130
+ }
131
+ const eventObj = {
132
+ type: "identify",
133
+ messageId: this.generateUUID(),
134
+ userId,
135
+ timestamp: this.generateTimestamp(),
136
+ traits: traits || {},
137
+ context: this.getContext()
138
+ };
139
+ this.enqueue(eventObj);
140
+ }
141
+ group(groupId, traits, options) {
142
+ if (this.isShutdown) {
143
+ return;
144
+ }
145
+ const eventObj = {
146
+ type: "group",
147
+ messageId: this.generateUUID(),
148
+ groupId,
149
+ timestamp: this.generateTimestamp(),
150
+ traits: traits || {},
151
+ context: this.getContext()
152
+ };
153
+ if (options?.userId) {
154
+ eventObj.userId = options.userId;
155
+ }
156
+ this.enqueue(eventObj);
157
+ }
158
+ /**
159
+ * Track an event synchronously. Returns BatchResponse or throws SendError.
160
+ */
161
+ async trackSync(event, properties, options) {
162
+ if (this.isShutdown) {
163
+ throw new SendError("Client is shutdown", []);
164
+ }
165
+ const eventObj = {
166
+ type: "track",
167
+ messageId: this.generateUUID(),
168
+ event,
169
+ timestamp: this.generateTimestamp(),
170
+ properties: properties || {},
171
+ context: this.getContext()
172
+ };
173
+ if (options?.userId) {
174
+ eventObj.userId = options.userId;
175
+ }
176
+ if (options?.groupId) {
177
+ eventObj.groupId = options.groupId;
178
+ }
179
+ return this.sendSync([eventObj]);
180
+ }
181
+ /**
182
+ * Identify a user synchronously. Returns BatchResponse or throws SendError.
183
+ */
184
+ async identifySync(userId, traits, options) {
185
+ if (this.isShutdown) {
186
+ throw new SendError("Client is shutdown", []);
187
+ }
188
+ const eventObj = {
189
+ type: "identify",
190
+ messageId: this.generateUUID(),
191
+ userId,
192
+ timestamp: this.generateTimestamp(),
193
+ traits: traits || {},
194
+ context: this.getContext()
195
+ };
196
+ return this.sendSync([eventObj]);
197
+ }
198
+ /**
199
+ * Associate a user with a group synchronously. Returns BatchResponse or throws SendError.
200
+ */
201
+ async groupSync(groupId, traits, options) {
202
+ if (this.isShutdown) {
203
+ throw new SendError("Client is shutdown", []);
204
+ }
205
+ const eventObj = {
206
+ type: "group",
207
+ messageId: this.generateUUID(),
208
+ groupId,
209
+ timestamp: this.generateTimestamp(),
210
+ traits: traits || {},
211
+ context: this.getContext()
212
+ };
213
+ if (options?.userId) {
214
+ eventObj.userId = options.userId;
215
+ }
216
+ return this.sendSync([eventObj]);
217
+ }
218
+ /**
219
+ * Return the number of events currently in the queue.
220
+ */
221
+ getQueueSize() {
222
+ return this.queue.length;
223
+ }
224
+ async flush() {
225
+ if (this.flushPromise) {
226
+ return this.flushPromise;
227
+ }
228
+ this.flushPromise = this.doFlush();
229
+ try {
230
+ await this.flushPromise;
231
+ } finally {
232
+ this.flushPromise = null;
233
+ }
234
+ }
235
+ async shutdown() {
236
+ if (this.isShutdown) {
237
+ return;
238
+ }
239
+ this.isShutdown = true;
240
+ this.shutdownHandlers.forEach((handler) => handler());
241
+ this.shutdownHandlers = [];
242
+ if (this.flushTimer) {
243
+ clearTimeout(this.flushTimer);
244
+ this.flushTimer = null;
245
+ }
246
+ await this.flush();
247
+ }
248
+ enqueue(event) {
249
+ const eventSize = this.estimateEventSize(event);
250
+ if (eventSize > MAX_EVENT_SIZE_BYTES) {
251
+ this.config.logger.warn(
252
+ `Event size (${eventSize} bytes) exceeds ${MAX_EVENT_SIZE_BYTES} bytes limit`
253
+ );
254
+ return;
255
+ }
256
+ if (this.queue.length >= this.config.maxQueueSize) {
257
+ const dropped = this.queue.shift();
258
+ this.config.logger.warn(`Queue full, dropping oldest event: ${dropped?.type}`);
259
+ }
260
+ this.queue.push(event);
261
+ this.config.logger.debug(`Enqueued ${event.type} event, queue size: ${this.queue.length}`);
262
+ if (this.queue.length >= this.config.maxBatchSize) {
263
+ this.flush();
264
+ }
265
+ }
266
+ async doFlush() {
267
+ if (this.queue.length === 0) {
268
+ return;
269
+ }
270
+ if (this.flushTimer) {
271
+ clearTimeout(this.flushTimer);
272
+ this.flushTimer = null;
273
+ }
274
+ while (this.queue.length > 0) {
275
+ const batch = this.extractBatch();
276
+ if (batch.length === 0) {
277
+ break;
278
+ }
279
+ await this.sendBatch(batch);
280
+ }
281
+ this.scheduleFlush();
282
+ }
283
+ extractBatch() {
284
+ const batch = [];
285
+ let batchSize = 0;
286
+ while (this.queue.length > 0 && batch.length < MAX_BATCH_SIZE) {
287
+ const event = this.queue[0];
288
+ const eventSize = this.estimateEventSize(event);
289
+ if (batchSize + eventSize > MAX_BATCH_SIZE_BYTES) {
290
+ break;
291
+ }
292
+ batch.push(this.queue.shift());
293
+ batchSize += eventSize;
294
+ }
295
+ return batch;
296
+ }
297
+ async sendBatch(batch) {
298
+ if (batch.length === 0) {
299
+ return;
300
+ }
301
+ this.config.logger.debug(`Sending batch of ${batch.length} events`);
302
+ const result = await this.doSend(batch);
303
+ if (result === null) {
304
+ const error = new Error(`Failed to send batch of ${batch.length} events after retries`);
305
+ this.invokeOnError(error, batch);
306
+ } else if (result.failed > 0 && result.errors) {
307
+ this.config.logger.warn(
308
+ `Batch partially failed. Accepted: ${result.accepted}, Failed: ${result.failed}`,
309
+ result.errors
310
+ );
311
+ this.invokeOnSuccess(result);
312
+ } else {
313
+ this.config.logger.debug(`Batch sent successfully. Accepted: ${result.accepted}`);
314
+ this.invokeOnSuccess(result);
315
+ }
316
+ }
317
+ async sendSync(events) {
318
+ this.config.logger.debug(`Sending ${events.length} events synchronously`);
319
+ const result = await this.doSend(events);
320
+ if (result === null) {
321
+ throw new SendError(`Failed to send ${events.length} events after retries`, events);
322
+ }
323
+ return result;
324
+ }
325
+ async doSend(batch) {
326
+ const request = { batch };
327
+ const requestBody = JSON.stringify(request);
328
+ if (hasNativeFetch) {
329
+ return this.doSendWithFetch(batch, requestBody);
330
+ } else {
331
+ return this.doSendWithHttps(batch, requestBody);
332
+ }
333
+ }
334
+ async doSendWithFetch(batch, requestBody) {
335
+ const requestUrl = `${this.config.endpoint}/v1/batch`;
336
+ let attempt = 0;
337
+ let delay = this.config.retryInitialDelay;
338
+ while (attempt < this.config.retryMaxAttempts) {
339
+ try {
340
+ const response = await fetch(requestUrl, {
341
+ method: "POST",
342
+ headers: {
343
+ "Content-Type": "application/json",
344
+ Authorization: `Bearer ${this.config.writeKey}`
345
+ },
346
+ body: requestBody
347
+ });
348
+ const data = await response.json();
349
+ if (response.ok) {
350
+ return data;
351
+ }
352
+ if (response.status === 400 || response.status === 401) {
353
+ this.config.logger.error(`Permanent error (${response.status}):`, data);
354
+ return null;
355
+ }
356
+ if (response.status === 429 || response.status === 503) {
357
+ const retryAfter = response.headers.get("Retry-After");
358
+ if (retryAfter) {
359
+ delay = parseInt(retryAfter, 10) * 1e3;
360
+ }
361
+ attempt++;
362
+ if (attempt < this.config.retryMaxAttempts) {
363
+ this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);
364
+ await this.sleep(delay);
365
+ delay = Math.min(delay * 2, 16e3);
366
+ continue;
367
+ }
368
+ }
369
+ attempt++;
370
+ if (attempt < this.config.retryMaxAttempts) {
371
+ this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);
372
+ await this.sleep(delay);
373
+ delay = Math.min(delay * 2, 16e3);
374
+ }
375
+ } catch (error) {
376
+ attempt++;
377
+ if (attempt < this.config.retryMaxAttempts) {
378
+ this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);
379
+ await this.sleep(delay);
380
+ delay = Math.min(delay * 2, 16e3);
381
+ } else {
382
+ this.config.logger.error("Failed to send batch after retries:", error);
383
+ }
384
+ }
385
+ }
386
+ return null;
387
+ }
388
+ async doSendWithHttps(batch, requestBody) {
389
+ const parsedUrl = url.parse(this.config.endpoint);
390
+ const isHttps = parsedUrl.protocol === "https:";
391
+ const client = isHttps ? https : http;
392
+ const port = parsedUrl.port ? parseInt(parsedUrl.port, 10) : isHttps ? 443 : 80;
393
+ const hostname = parsedUrl.hostname || "";
394
+ let attempt = 0;
395
+ let delay = this.config.retryInitialDelay;
396
+ while (attempt < this.config.retryMaxAttempts) {
397
+ try {
398
+ const result = await this.makeRequest(
399
+ client,
400
+ hostname,
401
+ port,
402
+ "/v1/batch",
403
+ requestBody
404
+ );
405
+ if (result.statusCode === 200) {
406
+ const data = JSON.parse(result.body);
407
+ return data;
408
+ }
409
+ if (result.statusCode === 400 || result.statusCode === 401) {
410
+ const data = JSON.parse(result.body);
411
+ this.config.logger.error(`Permanent error (${result.statusCode}):`, data);
412
+ return null;
413
+ }
414
+ if (result.statusCode === 429 || result.statusCode === 503) {
415
+ const retryAfter = result.headers["retry-after"];
416
+ if (retryAfter) {
417
+ delay = parseInt(
418
+ Array.isArray(retryAfter) ? retryAfter[0] : retryAfter,
419
+ 10
420
+ ) * 1e3;
421
+ }
422
+ attempt++;
423
+ if (attempt < this.config.retryMaxAttempts) {
424
+ this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);
425
+ await this.sleep(delay);
426
+ delay = Math.min(delay * 2, 16e3);
427
+ continue;
428
+ }
429
+ }
430
+ attempt++;
431
+ if (attempt < this.config.retryMaxAttempts) {
432
+ this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);
433
+ await this.sleep(delay);
434
+ delay = Math.min(delay * 2, 16e3);
435
+ }
436
+ } catch (error) {
437
+ attempt++;
438
+ if (attempt < this.config.retryMaxAttempts) {
439
+ this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);
440
+ await this.sleep(delay);
441
+ delay = Math.min(delay * 2, 16e3);
442
+ } else {
443
+ this.config.logger.error("Failed to send batch after retries:", error);
444
+ }
445
+ }
446
+ }
447
+ return null;
448
+ }
449
+ invokeOnError(error, batch) {
450
+ if (this.config.onError) {
451
+ try {
452
+ this.config.onError(error, batch);
453
+ } catch (e) {
454
+ this.config.logger.error("Error in onError callback:", e);
455
+ }
456
+ }
457
+ }
458
+ invokeOnSuccess(response) {
459
+ if (this.config.onSuccess) {
460
+ try {
461
+ this.config.onSuccess(response);
462
+ } catch (e) {
463
+ this.config.logger.error("Error in onSuccess callback:", e);
464
+ }
465
+ }
466
+ }
467
+ makeRequest(client, hostname, port, path, body) {
468
+ return new Promise((resolve, reject) => {
469
+ const options = {
470
+ hostname,
471
+ port,
472
+ path,
473
+ method: "POST",
474
+ headers: {
475
+ "Content-Type": "application/json",
476
+ Authorization: `Bearer ${this.config.writeKey}`,
477
+ "Content-Length": Buffer.byteLength(body)
478
+ }
479
+ };
480
+ const req = client.request(options, (res) => {
481
+ let data = "";
482
+ res.on("data", (chunk) => {
483
+ data += chunk;
484
+ });
485
+ res.on("end", () => {
486
+ resolve({
487
+ statusCode: res.statusCode || 500,
488
+ body: data,
489
+ headers: res.headers
490
+ });
491
+ });
492
+ });
493
+ req.on("error", (error) => {
494
+ reject(error);
495
+ });
496
+ req.write(body);
497
+ req.end();
498
+ });
499
+ }
500
+ scheduleFlush() {
501
+ if (this.isShutdown || this.flushTimer) {
502
+ return;
503
+ }
504
+ this.flushTimer = setTimeout(() => {
505
+ this.flush();
506
+ }, this.config.flushInterval);
507
+ }
508
+ generateUUID() {
509
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
510
+ return crypto.randomUUID();
511
+ }
512
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
513
+ const r = Math.random() * 16 | 0;
514
+ const v = c === "x" ? r : r & 3 | 8;
515
+ return v.toString(16);
516
+ });
517
+ }
518
+ generateTimestamp() {
519
+ return (/* @__PURE__ */ new Date()).toISOString();
520
+ }
521
+ getContext() {
522
+ return {
523
+ library: {
524
+ name: "node-sdk",
525
+ version: SDK_VERSION
526
+ }
527
+ };
528
+ }
529
+ estimateEventSize(event) {
530
+ try {
531
+ return JSON.stringify(event).length;
532
+ } catch {
533
+ return 500;
534
+ }
535
+ }
536
+ sleep(ms) {
537
+ return new Promise((resolve) => setTimeout(resolve, ms));
538
+ }
539
+ };
540
+ // Annotate the CommonJS export names for ESM import in node:
541
+ 0 && (module.exports = {
542
+ KlimeClient,
543
+ SendError
544
+ });
545
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/types.ts"],"sourcesContent":["import * as https from \"https\";\nimport * as http from \"http\";\nimport * as url from \"url\";\nimport {\n KlimeConfig,\n TrackOptions,\n IdentifyOptions,\n GroupOptions,\n Event,\n BatchRequest,\n BatchResponse,\n EventContext,\n Logger,\n SendError,\n} from \"./types\";\n\n// Re-export types for users\nexport { SendError, BatchResponse, Logger } from \"./types\";\nexport type { KlimeConfig, TrackOptions, IdentifyOptions, GroupOptions, Event } from \"./types\";\n\nconst DEFAULT_ENDPOINT = \"https://i.klime.com\";\nconst DEFAULT_FLUSH_INTERVAL = 2000;\nconst DEFAULT_MAX_BATCH_SIZE = 20;\nconst DEFAULT_MAX_QUEUE_SIZE = 1000;\nconst DEFAULT_RETRY_MAX_ATTEMPTS = 5;\nconst DEFAULT_RETRY_INITIAL_DELAY = 1000;\nconst MAX_BATCH_SIZE = 100;\nconst MAX_EVENT_SIZE_BYTES = 200 * 1024; // 200KB\nconst MAX_BATCH_SIZE_BYTES = 10 * 1024 * 1024; // 10MB\nconst SDK_VERSION = \"1.1.0\";\n\n// Check if fetch is available (Node 18+)\nconst hasNativeFetch = typeof fetch !== \"undefined\";\n\n// Default logger that wraps console with [Klime] prefix\nconst createDefaultLogger = (): Logger => ({\n debug: (message: string, ...args: any[]) =>\n console.debug(`[Klime] ${message}`, ...args),\n info: (message: string, ...args: any[]) =>\n console.info(`[Klime] ${message}`, ...args),\n warn: (message: string, ...args: any[]) =>\n console.warn(`[Klime] ${message}`, ...args),\n error: (message: string, ...args: any[]) =>\n console.error(`[Klime] ${message}`, ...args),\n});\n\n// Internal config type with required fields and optional callbacks/logger\ninterface InternalConfig {\n writeKey: string;\n endpoint: string;\n flushInterval: number;\n maxBatchSize: number;\n maxQueueSize: number;\n retryMaxAttempts: number;\n retryInitialDelay: number;\n flushOnShutdown: boolean;\n logger: Logger;\n onError?: (error: Error, events: Event[]) => void;\n onSuccess?: (response: BatchResponse) => void;\n}\n\nexport class KlimeClient {\n private config: InternalConfig;\n private queue: Event[] = [];\n private flushTimer: NodeJS.Timeout | null = null;\n private isShutdown = false;\n private flushPromise: Promise<void> | null = null;\n private shutdownHandlers: (() => void)[] = [];\n\n constructor(config: KlimeConfig) {\n if (!config.writeKey) {\n throw new Error(\"writeKey is required\");\n }\n\n this.config = {\n writeKey: config.writeKey,\n endpoint: config.endpoint || DEFAULT_ENDPOINT,\n flushInterval: config.flushInterval ?? DEFAULT_FLUSH_INTERVAL,\n maxBatchSize: Math.min(\n config.maxBatchSize ?? DEFAULT_MAX_BATCH_SIZE,\n MAX_BATCH_SIZE\n ),\n maxQueueSize: config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE,\n retryMaxAttempts: config.retryMaxAttempts ?? DEFAULT_RETRY_MAX_ATTEMPTS,\n retryInitialDelay:\n config.retryInitialDelay ?? DEFAULT_RETRY_INITIAL_DELAY,\n flushOnShutdown: config.flushOnShutdown ?? true,\n logger: config.logger ?? createDefaultLogger(),\n onError: config.onError,\n onSuccess: config.onSuccess,\n };\n\n if (this.config.flushOnShutdown) {\n const shutdownHandler = async () => {\n await this.shutdown();\n };\n process.on(\"SIGTERM\", shutdownHandler);\n process.on(\"SIGINT\", shutdownHandler);\n this.shutdownHandlers.push(() => {\n process.removeListener(\"SIGTERM\", shutdownHandler);\n process.removeListener(\"SIGINT\", shutdownHandler);\n });\n }\n\n this.scheduleFlush();\n }\n\n track(\n event: string,\n properties?: Record<string, any>,\n options?: TrackOptions\n ): void {\n if (this.isShutdown) {\n return;\n }\n\n const eventObj: Event = {\n type: \"track\",\n messageId: this.generateUUID(),\n event,\n timestamp: this.generateTimestamp(),\n properties: properties || {},\n context: this.getContext(),\n };\n\n if (options?.userId) {\n eventObj.userId = options.userId;\n }\n if (options?.groupId) {\n eventObj.groupId = options.groupId;\n }\n\n this.enqueue(eventObj);\n }\n\n identify(\n userId: string,\n traits?: Record<string, any>,\n options?: IdentifyOptions\n ): void {\n if (this.isShutdown) {\n return;\n }\n\n const eventObj: Event = {\n type: \"identify\",\n messageId: this.generateUUID(),\n userId,\n timestamp: this.generateTimestamp(),\n traits: traits || {},\n context: this.getContext(),\n };\n\n this.enqueue(eventObj);\n }\n\n group(\n groupId: string,\n traits?: Record<string, any>,\n options?: GroupOptions\n ): void {\n if (this.isShutdown) {\n return;\n }\n\n const eventObj: Event = {\n type: \"group\",\n messageId: this.generateUUID(),\n groupId,\n timestamp: this.generateTimestamp(),\n traits: traits || {},\n context: this.getContext(),\n };\n\n if (options?.userId) {\n eventObj.userId = options.userId;\n }\n\n this.enqueue(eventObj);\n }\n\n /**\n * Track an event synchronously. Returns BatchResponse or throws SendError.\n */\n async trackSync(\n event: string,\n properties?: Record<string, any>,\n options?: TrackOptions\n ): Promise<BatchResponse> {\n if (this.isShutdown) {\n throw new SendError(\"Client is shutdown\", []);\n }\n\n const eventObj: Event = {\n type: \"track\",\n messageId: this.generateUUID(),\n event,\n timestamp: this.generateTimestamp(),\n properties: properties || {},\n context: this.getContext(),\n };\n\n if (options?.userId) {\n eventObj.userId = options.userId;\n }\n if (options?.groupId) {\n eventObj.groupId = options.groupId;\n }\n\n return this.sendSync([eventObj]);\n }\n\n /**\n * Identify a user synchronously. Returns BatchResponse or throws SendError.\n */\n async identifySync(\n userId: string,\n traits?: Record<string, any>,\n options?: IdentifyOptions\n ): Promise<BatchResponse> {\n if (this.isShutdown) {\n throw new SendError(\"Client is shutdown\", []);\n }\n\n const eventObj: Event = {\n type: \"identify\",\n messageId: this.generateUUID(),\n userId,\n timestamp: this.generateTimestamp(),\n traits: traits || {},\n context: this.getContext(),\n };\n\n return this.sendSync([eventObj]);\n }\n\n /**\n * Associate a user with a group synchronously. Returns BatchResponse or throws SendError.\n */\n async groupSync(\n groupId: string,\n traits?: Record<string, any>,\n options?: GroupOptions\n ): Promise<BatchResponse> {\n if (this.isShutdown) {\n throw new SendError(\"Client is shutdown\", []);\n }\n\n const eventObj: Event = {\n type: \"group\",\n messageId: this.generateUUID(),\n groupId,\n timestamp: this.generateTimestamp(),\n traits: traits || {},\n context: this.getContext(),\n };\n\n if (options?.userId) {\n eventObj.userId = options.userId;\n }\n\n return this.sendSync([eventObj]);\n }\n\n /**\n * Return the number of events currently in the queue.\n */\n getQueueSize(): number {\n return this.queue.length;\n }\n\n async flush(): Promise<void> {\n if (this.flushPromise) {\n return this.flushPromise;\n }\n\n this.flushPromise = this.doFlush();\n try {\n await this.flushPromise;\n } finally {\n this.flushPromise = null;\n }\n }\n\n async shutdown(): Promise<void> {\n if (this.isShutdown) {\n return;\n }\n\n this.isShutdown = true;\n\n // Remove shutdown handlers\n this.shutdownHandlers.forEach((handler) => handler());\n this.shutdownHandlers = [];\n\n if (this.flushTimer) {\n clearTimeout(this.flushTimer);\n this.flushTimer = null;\n }\n\n await this.flush();\n }\n\n private enqueue(event: Event): void {\n // Check event size\n const eventSize = this.estimateEventSize(event);\n if (eventSize > MAX_EVENT_SIZE_BYTES) {\n this.config.logger.warn(\n `Event size (${eventSize} bytes) exceeds ${MAX_EVENT_SIZE_BYTES} bytes limit`\n );\n return;\n }\n\n // Drop oldest if queue is full\n if (this.queue.length >= this.config.maxQueueSize) {\n const dropped = this.queue.shift();\n this.config.logger.warn(`Queue full, dropping oldest event: ${dropped?.type}`);\n }\n\n this.queue.push(event);\n this.config.logger.debug(`Enqueued ${event.type} event, queue size: ${this.queue.length}`);\n\n // Check if we should flush immediately\n if (this.queue.length >= this.config.maxBatchSize) {\n this.flush();\n }\n }\n\n private async doFlush(): Promise<void> {\n if (this.queue.length === 0) {\n return;\n }\n\n // Clear the flush timer\n if (this.flushTimer) {\n clearTimeout(this.flushTimer);\n this.flushTimer = null;\n }\n\n // Process batches\n while (this.queue.length > 0) {\n const batch = this.extractBatch();\n if (batch.length === 0) {\n break;\n }\n\n await this.sendBatch(batch);\n }\n\n // Schedule next flush\n this.scheduleFlush();\n }\n\n private extractBatch(): Event[] {\n const batch: Event[] = [];\n let batchSize = 0;\n\n while (this.queue.length > 0 && batch.length < MAX_BATCH_SIZE) {\n const event = this.queue[0];\n const eventSize = this.estimateEventSize(event);\n\n // Check if adding this event would exceed batch size limit\n if (batchSize + eventSize > MAX_BATCH_SIZE_BYTES) {\n break;\n }\n\n batch.push(this.queue.shift()!);\n batchSize += eventSize;\n }\n\n return batch;\n }\n\n private async sendBatch(batch: Event[]): Promise<void> {\n if (batch.length === 0) {\n return;\n }\n\n this.config.logger.debug(`Sending batch of ${batch.length} events`);\n const result = await this.doSend(batch);\n\n if (result === null) {\n // Send failed after all retries\n const error = new Error(`Failed to send batch of ${batch.length} events after retries`);\n this.invokeOnError(error, batch);\n } else if (result.failed > 0 && result.errors) {\n this.config.logger.warn(\n `Batch partially failed. Accepted: ${result.accepted}, Failed: ${result.failed}`,\n result.errors\n );\n // Still invoke success callback since some events were accepted\n this.invokeOnSuccess(result);\n } else {\n this.config.logger.debug(`Batch sent successfully. Accepted: ${result.accepted}`);\n this.invokeOnSuccess(result);\n }\n }\n\n private async sendSync(events: Event[]): Promise<BatchResponse> {\n this.config.logger.debug(`Sending ${events.length} events synchronously`);\n const result = await this.doSend(events);\n\n if (result === null) {\n throw new SendError(`Failed to send ${events.length} events after retries`, events);\n }\n\n return result;\n }\n\n private async doSend(batch: Event[]): Promise<BatchResponse | null> {\n const request: BatchRequest = { batch };\n const requestBody = JSON.stringify(request);\n\n if (hasNativeFetch) {\n return this.doSendWithFetch(batch, requestBody);\n } else {\n return this.doSendWithHttps(batch, requestBody);\n }\n }\n\n private async doSendWithFetch(\n batch: Event[],\n requestBody: string\n ): Promise<BatchResponse | null> {\n const requestUrl = `${this.config.endpoint}/v1/batch`;\n let attempt = 0;\n let delay = this.config.retryInitialDelay;\n\n while (attempt < this.config.retryMaxAttempts) {\n try {\n const response = await fetch(requestUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.config.writeKey}`,\n },\n body: requestBody,\n });\n\n const data = (await response.json()) as BatchResponse;\n\n if (response.ok) {\n return data;\n }\n\n if (response.status === 400 || response.status === 401) {\n this.config.logger.error(`Permanent error (${response.status}):`, data);\n return null;\n }\n\n if (response.status === 429 || response.status === 503) {\n const retryAfter = response.headers.get(\"Retry-After\");\n if (retryAfter) {\n delay = parseInt(retryAfter, 10) * 1000;\n }\n\n attempt++;\n if (attempt < this.config.retryMaxAttempts) {\n this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);\n await this.sleep(delay);\n delay = Math.min(delay * 2, 16000);\n continue;\n }\n }\n\n attempt++;\n if (attempt < this.config.retryMaxAttempts) {\n this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);\n await this.sleep(delay);\n delay = Math.min(delay * 2, 16000);\n }\n } catch (error) {\n attempt++;\n if (attempt < this.config.retryMaxAttempts) {\n this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);\n await this.sleep(delay);\n delay = Math.min(delay * 2, 16000);\n } else {\n this.config.logger.error(\"Failed to send batch after retries:\", error);\n }\n }\n }\n\n return null;\n }\n\n private async doSendWithHttps(\n batch: Event[],\n requestBody: string\n ): Promise<BatchResponse | null> {\n const parsedUrl = url.parse(this.config.endpoint);\n const isHttps = parsedUrl.protocol === \"https:\";\n const client = isHttps ? https : http;\n const port = parsedUrl.port\n ? parseInt(parsedUrl.port, 10)\n : isHttps\n ? 443\n : 80;\n const hostname = parsedUrl.hostname || \"\";\n\n let attempt = 0;\n let delay = this.config.retryInitialDelay;\n\n while (attempt < this.config.retryMaxAttempts) {\n try {\n const result = await this.makeRequest(\n client,\n hostname,\n port,\n \"/v1/batch\",\n requestBody\n );\n\n if (result.statusCode === 200) {\n const data: BatchResponse = JSON.parse(result.body);\n return data;\n }\n\n if (result.statusCode === 400 || result.statusCode === 401) {\n const data = JSON.parse(result.body);\n this.config.logger.error(`Permanent error (${result.statusCode}):`, data);\n return null;\n }\n\n if (result.statusCode === 429 || result.statusCode === 503) {\n const retryAfter = result.headers[\"retry-after\"];\n if (retryAfter) {\n delay =\n parseInt(\n Array.isArray(retryAfter) ? retryAfter[0] : retryAfter,\n 10\n ) * 1000;\n }\n\n attempt++;\n if (attempt < this.config.retryMaxAttempts) {\n this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);\n await this.sleep(delay);\n delay = Math.min(delay * 2, 16000);\n continue;\n }\n }\n\n attempt++;\n if (attempt < this.config.retryMaxAttempts) {\n this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);\n await this.sleep(delay);\n delay = Math.min(delay * 2, 16000);\n }\n } catch (error) {\n attempt++;\n if (attempt < this.config.retryMaxAttempts) {\n this.config.logger.debug(`Retrying after ${delay}ms (attempt ${attempt})`);\n await this.sleep(delay);\n delay = Math.min(delay * 2, 16000);\n } else {\n this.config.logger.error(\"Failed to send batch after retries:\", error);\n }\n }\n }\n\n return null;\n }\n\n private invokeOnError(error: Error, batch: Event[]): void {\n if (this.config.onError) {\n try {\n this.config.onError(error, batch);\n } catch (e) {\n this.config.logger.error(\"Error in onError callback:\", e);\n }\n }\n }\n\n private invokeOnSuccess(response: BatchResponse): void {\n if (this.config.onSuccess) {\n try {\n this.config.onSuccess(response);\n } catch (e) {\n this.config.logger.error(\"Error in onSuccess callback:\", e);\n }\n }\n }\n\n private makeRequest(\n client: typeof https | typeof http,\n hostname: string,\n port: number,\n path: string,\n body: string\n ): Promise<{\n statusCode: number;\n body: string;\n headers: Record<string, string | string[] | undefined>;\n }> {\n return new Promise((resolve, reject) => {\n const options = {\n hostname,\n port,\n path,\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.config.writeKey}`,\n \"Content-Length\": Buffer.byteLength(body),\n },\n };\n\n const req = client.request(options, (res) => {\n let data = \"\";\n\n res.on(\"data\", (chunk) => {\n data += chunk;\n });\n\n res.on(\"end\", () => {\n resolve({\n statusCode: res.statusCode || 500,\n body: data,\n headers: res.headers as Record<string, string | string[] | undefined>,\n });\n });\n });\n\n req.on(\"error\", (error) => {\n reject(error);\n });\n\n req.write(body);\n req.end();\n });\n }\n\n private scheduleFlush(): void {\n if (this.isShutdown || this.flushTimer) {\n return;\n }\n\n this.flushTimer = setTimeout(() => {\n this.flush();\n }, this.config.flushInterval);\n }\n\n private generateUUID(): string {\n if (typeof crypto !== \"undefined\" && crypto.randomUUID) {\n return crypto.randomUUID();\n }\n // Fallback for Node < 15\n return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === \"x\" ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n }\n\n private generateTimestamp(): string {\n return new Date().toISOString();\n }\n\n private getContext(): EventContext {\n return {\n library: {\n name: \"node-sdk\",\n version: SDK_VERSION,\n },\n };\n }\n\n private estimateEventSize(event: Event): number {\n try {\n return JSON.stringify(event).length;\n } catch {\n return 500;\n }\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n","export interface Logger {\n debug(message: string, ...args: any[]): void;\n info(message: string, ...args: any[]): void;\n warn(message: string, ...args: any[]): void;\n error(message: string, ...args: any[]): void;\n}\n\nexport interface KlimeConfig {\n writeKey: string;\n endpoint?: string;\n flushInterval?: number; // milliseconds, default 2000\n maxBatchSize?: number; // default 20, max 100\n maxQueueSize?: number; // default 1000\n retryMaxAttempts?: number; // default 5\n retryInitialDelay?: number; // milliseconds, default 1000\n flushOnShutdown?: boolean; // default true\n logger?: Logger; // optional custom logger\n onError?: (error: Error, events: Event[]) => void; // callback for batch failures\n onSuccess?: (response: BatchResponse) => void; // callback for successful sends\n}\n\nexport interface TrackOptions {\n userId?: string;\n groupId?: string;\n}\n\nexport interface IdentifyOptions {}\n\nexport interface GroupOptions {\n userId?: string;\n}\n\nexport interface Event {\n type: 'track' | 'identify' | 'group';\n messageId: string;\n event?: string; // required for track\n userId?: string; // required for identify\n groupId?: string; // required for group\n timestamp: string; // ISO 8601\n properties?: Record<string, any>; // for track\n traits?: Record<string, any>; // for identify/group\n context?: EventContext;\n}\n\nexport interface EventContext {\n library?: {\n name: string;\n version: string;\n };\n}\n\nexport interface BatchRequest {\n batch: Event[];\n}\n\nexport interface BatchResponse {\n status: string;\n accepted: number;\n failed: number;\n errors?: ValidationError[];\n}\n\nexport interface ValidationError {\n index: number;\n message: string;\n code: string;\n}\n\nexport class SendError extends Error {\n events: Event[];\n\n constructor(message: string, events: Event[]) {\n super(message);\n this.name = 'SendError';\n this.events = events;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,YAAuB;AACvB,WAAsB;AACtB,UAAqB;;;ACkEd,IAAM,YAAN,cAAwB,MAAM;AAAA,EAGnC,YAAY,SAAiB,QAAiB;AAC5C,UAAM,OAAO;AACb,SAAK,OAAO;AACZ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