@klime/browser 1.0.3 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/types.ts","../src/index.ts"],"sourcesContent":["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 autoFlushOnUnload?: 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 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 userAgent?: string;\n locale?: string;\n timezone?: string;\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","import {\n KlimeConfig,\n TrackOptions,\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, 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\n/** Max body size for fetch keepalive (spec limit). Requests larger than this omit keepalive. */\nconst KEEPALIVE_MAX_BODY_BYTES = 64 * 1024; // 64KB\nconst SDK_VERSION = \"1.1.0\";\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 autoFlushOnUnload: 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: ReturnType<typeof setTimeout> | null = null;\n private isShutdown = false;\n private flushPromise: Promise<void> | null = null;\n private unloadHandler: (() => void) | null = null;\n private visibilityChangeHandler: (() => void) | null = null;\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 autoFlushOnUnload: config.autoFlushOnUnload ?? true,\n logger: config.logger ?? createDefaultLogger(),\n onError: config.onError,\n onSuccess: config.onSuccess,\n };\n\n if (this.config.autoFlushOnUnload && typeof window !== \"undefined\") {\n this.unloadHandler = () => {\n this.flush();\n };\n window.addEventListener(\"beforeunload\", this.unloadHandler);\n window.addEventListener(\"pagehide\", this.unloadHandler);\n this.visibilityChangeHandler = () => {\n if (typeof document !== \"undefined\" && document.visibilityState === \"hidden\") {\n this.flush();\n }\n };\n document.addEventListener(\"visibilitychange\", this.visibilityChangeHandler);\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(userId: string, traits?: Record<string, any>): 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?: TrackOptions\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 ): 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?: TrackOptions\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 if (this.unloadHandler && typeof window !== \"undefined\") {\n window.removeEventListener(\"beforeunload\", this.unloadHandler);\n window.removeEventListener(\"pagehide\", this.unloadHandler);\n }\n if (this.visibilityChangeHandler && typeof document !== \"undefined\") {\n document.removeEventListener(\"visibilitychange\", this.visibilityChangeHandler);\n }\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 requestUrl = `${this.config.endpoint}/v1/batch`;\n const body = JSON.stringify(request);\n const useKeepalive = body.length <= KEEPALIVE_MAX_BODY_BYTES;\n\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,\n keepalive: useKeepalive,\n });\n\n const data: BatchResponse = await response.json();\n\n if (response.ok) {\n return data;\n }\n\n // Handle error responses\n if (response.status === 400 || response.status === 401) {\n // Permanent errors - don't retry\n this.config.logger.error(`Permanent error (${response.status}):`, data);\n return null;\n }\n\n // Transient errors - retry with backoff\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); // Cap at 16s\n continue;\n }\n }\n\n // Other errors - retry\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 // Network errors - retry\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 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 older browsers\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 const context: EventContext = {\n library: {\n name: \"js-sdk\",\n version: SDK_VERSION,\n },\n };\n\n if (typeof navigator !== \"undefined\") {\n if (navigator.userAgent) {\n context.userAgent = navigator.userAgent;\n }\n if (navigator.language) {\n context.locale = navigator.language;\n }\n }\n\n if (typeof Intl !== \"undefined\" && Intl.DateTimeFormat) {\n try {\n const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;\n if (timezone) {\n context.timezone = timezone;\n }\n } catch (e) {\n // Ignore timezone errors\n }\n }\n\n return context;\n }\n\n private estimateEventSize(event: Event): number {\n // Rough estimate: JSON stringified size\n try {\n return JSON.stringify(event).length;\n } catch {\n // Fallback: rough estimate based on structure\n return 500; // Conservative estimate\n }\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n}\n"],"mappings":";AAiEO,IAAM,YAAN,cAAwB,MAAM;AAAA,EAGnC,YAAY,SAAiB,QAAiB;AAC5C,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;;;AC1DA,IAAM,mBAAmB;AACzB,IAAM,yBAAyB;AAC/B,IAAM,yBAAyB;AAC/B,IAAM,yBAAyB;AAC/B,IAAM,6BAA6B;AACnC,IAAM,8BAA8B;AACpC,IAAM,iBAAiB;AACvB,IAAM,uBAAuB,MAAM;AACnC,IAAM,uBAAuB,KAAK,OAAO;AAEzC,IAAM,2BAA2B,KAAK;AACtC,IAAM,cAAc;AAGpB,IAAM,sBAAsB,OAAe;AAAA,EACzC,OAAO,CAAC,YAAoB,SAC1B,QAAQ,MAAM,WAAW,OAAO,IAAI,GAAG,IAAI;AAAA,EAC7C,MAAM,CAAC,YAAoB,SACzB,QAAQ,KAAK,WAAW,OAAO,IAAI,GAAG,IAAI;AAAA,EAC5C,MAAM,CAAC,YAAoB,SACzB,QAAQ,KAAK,WAAW,OAAO,IAAI,GAAG,IAAI;AAAA,EAC5C,OAAO,CAAC,YAAoB,SAC1B,QAAQ,MAAM,WAAW,OAAO,IAAI,GAAG,IAAI;AAC/C;AAiBO,IAAM,cAA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AAM,MAAM,IAAK,IAAI,IAAO;AACtC,aAAO,EAAE,SAAS,EAAE;AAAA,IACtB,CAAC;AAAA,EACH;AAAA,EAEQ,oBAA4B;AAClC,YAAO,oBAAI,KAAK,GAAE,YAAY;AAAA,EAChC;AAAA,EAEQ,aAA2B;AACjC,UAAM,UAAwB;AAAA,MAC5B,SAAS;AAAA,QACP,MAAM;AAAA,QACN,SAAS;AAAA,MACX;AAAA,IACF;AAEA,QAAI,OAAO,cAAc,aAAa;AACpC,UAAI,UAAU,WAAW;AACvB,gBAAQ,YAAY,UAAU;AAAA,MAChC;AACA,UAAI,UAAU,UAAU;AACtB,gBAAQ,SAAS,UAAU;AAAA,MAC7B;AAAA,IACF;AAEA,QAAI,OAAO,SAAS,eAAe,KAAK,gBAAgB;AACtD,UAAI;AACF,cAAM,WAAW,KAAK,eAAe,EAAE,gBAAgB,EAAE;AACzD,YAAI,UAAU;AACZ,kBAAQ,WAAW;AAAA,QACrB;AAAA,MACF,SAAS,GAAG;AAAA,MAEZ;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,kBAAkB,OAAsB;AAE9C,QAAI;AACF,aAAO,KAAK,UAAU,KAAK,EAAE;AAAA,IAC/B,QAAQ;AAEN,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEQ,MAAM,IAA2B;AACvC,WAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,EACzD;AACF;","names":[]}
package/package.json CHANGED
@@ -1,12 +1,25 @@
1
1
  {
2
2
  "name": "@klime/browser",
3
- "version": "1.0.3",
3
+ "version": "1.2.1",
4
+ "type": "module",
4
5
  "description": "Klime SDK for browsers",
5
- "main": "dist/index.js",
6
+ "main": "dist/index.cjs",
6
7
  "module": "dist/index.js",
7
8
  "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
8
21
  "scripts": {
9
- "build": "tsc",
22
+ "build": "tsup",
10
23
  "clean": "rm -rf dist",
11
24
  "test": "vitest run",
12
25
  "test:watch": "vitest",
@@ -23,6 +36,7 @@
23
36
  "license": "MIT",
24
37
  "devDependencies": {
25
38
  "jsdom": "^25.0.0",
39
+ "tsup": "^8.0.0",
26
40
  "typescript": "^5.0.0",
27
41
  "vitest": "^2.0.0"
28
42
  },
package/dist/types.d.ts DELETED
@@ -1,49 +0,0 @@
1
- export interface KlimeConfig {
2
- writeKey: string;
3
- endpoint?: string;
4
- flushInterval?: number;
5
- maxBatchSize?: number;
6
- maxQueueSize?: number;
7
- retryMaxAttempts?: number;
8
- retryInitialDelay?: number;
9
- autoFlushOnUnload?: boolean;
10
- }
11
- export interface TrackOptions {
12
- userId?: string;
13
- groupId?: string;
14
- }
15
- export interface Event {
16
- type: 'track' | 'identify' | 'group';
17
- messageId: string;
18
- event?: string;
19
- userId?: string;
20
- groupId?: string;
21
- timestamp: string;
22
- properties?: Record<string, any>;
23
- traits?: Record<string, any>;
24
- context?: EventContext;
25
- }
26
- export interface EventContext {
27
- library?: {
28
- name: string;
29
- version: string;
30
- };
31
- userAgent?: string;
32
- locale?: string;
33
- timezone?: string;
34
- ip?: string;
35
- }
36
- export interface BatchRequest {
37
- batch: Event[];
38
- }
39
- export interface BatchResponse {
40
- status: string;
41
- accepted: number;
42
- failed: number;
43
- errors?: ValidationError[];
44
- }
45
- export interface ValidationError {
46
- index: number;
47
- message: string;
48
- code: string;
49
- }
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};