@splitlab/node 0.1.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 +1 -0
- package/dist/index.d.cts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +1 -0
- package/package.json +42 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var f=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var C=(n,e)=>{for(var t in e)f(n,t,{get:e[t],enumerable:!0})},b=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of m(e))!y.call(n,i)&&i!==t&&f(n,i,{get:()=>e[i],enumerable:!(r=d(e,i))||r.enumerable});return n};var R=n=>b(f({},"__esModule",{value:!0}),n);var T={};C(T,{SplitLabServer:()=>u,hashToFloat:()=>o.hashToFloat,murmurhash3:()=>o.murmurhash3});module.exports=R(T);var o=require("@splitlab/core"),s=require("@splitlab/core"),u=class{constructor(e){this.serverConfig=null;this.eventQueue=[];this.flushTimer=null;this.configRefreshTimer=null;this.lastEtag=null;this.ready=!1;this.apiKey=e.apiKey,this.baseUrl=e.baseUrl.replace(/\/$/,""),this.ingestUrl=(e.ingestUrl||e.baseUrl).replace(/\/$/,""),this.configRefreshInterval=e.configRefreshInterval??3e4,this.flushInterval=e.flushInterval??1e4,this.flushSize=e.flushSize??100,this.onConfigUpdate=e.onConfigUpdate??null}async initialize(){let{config:e,etag:t}=await this.fetchConfig();this.serverConfig=e,this.lastEtag=t,this.configRefreshTimer=setInterval(()=>{this.refresh().catch(()=>{})},this.configRefreshInterval),this.flushTimer=setInterval(()=>{this.flush().catch(()=>{})},this.flushInterval),this.ready=!0}isReady(){return this.ready}async destroy(){this.configRefreshTimer!==null&&(clearInterval(this.configRefreshTimer),this.configRefreshTimer=null),this.flushTimer!==null&&(clearInterval(this.flushTimer),this.flushTimer=null),await this.flush(),this.ready=!1}getVariant(e,t,r){if(!this.serverConfig)return null;let i=this.serverConfig.experiments.find(a=>a.key===e);if(!i||i.targeting_rules&&!(0,s.evaluateRules)(i.targeting_rules,r||{}))return null;let l=(0,s.murmurhash3)(i.key+":"+t);if(l%1e4/100>=i.traffic_percentage)return null;let h=i.variants.reduce((a,p)=>a+p.weight,0),v=l%h,g=0;for(let a of i.variants)if(g+=a.weight,v<g)return a.key;return i.variants[i.variants.length-1].key}isFeatureEnabled(e,t,r){if(!this.serverConfig)return!1;let i=this.serverConfig.flags.find(h=>h.key===e);return!i||i.rules&&!(0,s.evaluateRules)(i.rules,r||{})?!1:(0,s.murmurhash3)(i.key+":"+t)%100<i.rollout_percentage}evaluateAll(e,t){return this.serverConfig?(0,s.localEvaluate)(this.serverConfig,e,t||{}):{experiments:{},flags:{}}}track(e,t,r){this.eventQueue.push({distinct_id:e,event_name:t,properties:r,timestamp:new Date().toISOString()}),this.eventQueue.length>=this.flushSize&&this.flush().catch(()=>{})}async flush(){if(this.eventQueue.length===0)return;let e=this.eventQueue;this.eventQueue=[];try{(await fetch(`${this.ingestUrl}/ingest/batch`,{method:"POST",headers:{"Content-Type":"application/json","X-API-Key":this.apiKey},body:JSON.stringify({events:e})})).ok||(this.eventQueue=e.concat(this.eventQueue))}catch{this.eventQueue=e.concat(this.eventQueue)}}async refresh(){try{let{config:e,etag:t,notModified:r}=await this.fetchConfig();if(r)return;this.serverConfig=e,this.lastEtag=t,this.onConfigUpdate&&this.onConfigUpdate()}catch{}}async fetchConfig(){let e={"Content-Type":"application/json"};this.lastEtag&&(e["If-None-Match"]=this.lastEtag);let t=await fetch(`${this.baseUrl}/api/sdk/config?key=${encodeURIComponent(this.apiKey)}`,{method:"GET",headers:e});if(t.status===304)return{config:this.serverConfig,etag:this.lastEtag,notModified:!0};if(!t.ok){let l=await t.text().catch(()=>"");throw new Error(`SplitLab API error ${t.status}: ${l}`)}let r=t.headers.get("etag");return{config:await t.json(),etag:r}}};0&&(module.exports={SplitLabServer,hashToFloat,murmurhash3});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { EvalResult } from '@splitlab/core';
|
|
2
|
+
export { EvalResult, ExperimentConfig, FlagConfig, ServerConfig, TargetingCondition, TargetingGroup, TargetingRules, TrackEvent, Variant, hashToFloat, murmurhash3 } from '@splitlab/core';
|
|
3
|
+
|
|
4
|
+
interface SplitLabServerConfig {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
ingestUrl?: string;
|
|
8
|
+
/** Config polling interval in ms. Default: 30000 (30s). */
|
|
9
|
+
configRefreshInterval?: number;
|
|
10
|
+
/** Event flush interval in ms. Default: 10000 (10s). */
|
|
11
|
+
flushInterval?: number;
|
|
12
|
+
/** Max events to buffer before auto-flush. Default: 100. */
|
|
13
|
+
flushSize?: number;
|
|
14
|
+
/** Callback when config changes (after refresh). */
|
|
15
|
+
onConfigUpdate?: () => void;
|
|
16
|
+
}
|
|
17
|
+
declare class SplitLabServer {
|
|
18
|
+
private apiKey;
|
|
19
|
+
private baseUrl;
|
|
20
|
+
private ingestUrl;
|
|
21
|
+
private configRefreshInterval;
|
|
22
|
+
private flushInterval;
|
|
23
|
+
private flushSize;
|
|
24
|
+
private onConfigUpdate;
|
|
25
|
+
private serverConfig;
|
|
26
|
+
private eventQueue;
|
|
27
|
+
private flushTimer;
|
|
28
|
+
private configRefreshTimer;
|
|
29
|
+
private lastEtag;
|
|
30
|
+
private ready;
|
|
31
|
+
constructor(config: SplitLabServerConfig);
|
|
32
|
+
initialize(): Promise<void>;
|
|
33
|
+
isReady(): boolean;
|
|
34
|
+
destroy(): Promise<void>;
|
|
35
|
+
getVariant(experimentKey: string, distinctId: string, attributes?: Record<string, any>): string | null;
|
|
36
|
+
isFeatureEnabled(flagKey: string, distinctId: string, attributes?: Record<string, any>): boolean;
|
|
37
|
+
evaluateAll(distinctId: string, attributes?: Record<string, any>): EvalResult;
|
|
38
|
+
track(distinctId: string, eventName: string, properties?: Record<string, any>): void;
|
|
39
|
+
flush(): Promise<void>;
|
|
40
|
+
refresh(): Promise<void>;
|
|
41
|
+
private fetchConfig;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { SplitLabServer, type SplitLabServerConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { EvalResult } from '@splitlab/core';
|
|
2
|
+
export { EvalResult, ExperimentConfig, FlagConfig, ServerConfig, TargetingCondition, TargetingGroup, TargetingRules, TrackEvent, Variant, hashToFloat, murmurhash3 } from '@splitlab/core';
|
|
3
|
+
|
|
4
|
+
interface SplitLabServerConfig {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
ingestUrl?: string;
|
|
8
|
+
/** Config polling interval in ms. Default: 30000 (30s). */
|
|
9
|
+
configRefreshInterval?: number;
|
|
10
|
+
/** Event flush interval in ms. Default: 10000 (10s). */
|
|
11
|
+
flushInterval?: number;
|
|
12
|
+
/** Max events to buffer before auto-flush. Default: 100. */
|
|
13
|
+
flushSize?: number;
|
|
14
|
+
/** Callback when config changes (after refresh). */
|
|
15
|
+
onConfigUpdate?: () => void;
|
|
16
|
+
}
|
|
17
|
+
declare class SplitLabServer {
|
|
18
|
+
private apiKey;
|
|
19
|
+
private baseUrl;
|
|
20
|
+
private ingestUrl;
|
|
21
|
+
private configRefreshInterval;
|
|
22
|
+
private flushInterval;
|
|
23
|
+
private flushSize;
|
|
24
|
+
private onConfigUpdate;
|
|
25
|
+
private serverConfig;
|
|
26
|
+
private eventQueue;
|
|
27
|
+
private flushTimer;
|
|
28
|
+
private configRefreshTimer;
|
|
29
|
+
private lastEtag;
|
|
30
|
+
private ready;
|
|
31
|
+
constructor(config: SplitLabServerConfig);
|
|
32
|
+
initialize(): Promise<void>;
|
|
33
|
+
isReady(): boolean;
|
|
34
|
+
destroy(): Promise<void>;
|
|
35
|
+
getVariant(experimentKey: string, distinctId: string, attributes?: Record<string, any>): string | null;
|
|
36
|
+
isFeatureEnabled(flagKey: string, distinctId: string, attributes?: Record<string, any>): boolean;
|
|
37
|
+
evaluateAll(distinctId: string, attributes?: Record<string, any>): EvalResult;
|
|
38
|
+
track(distinctId: string, eventName: string, properties?: Record<string, any>): void;
|
|
39
|
+
flush(): Promise<void>;
|
|
40
|
+
refresh(): Promise<void>;
|
|
41
|
+
private fetchConfig;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { SplitLabServer, type SplitLabServerConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{murmurhash3 as C,hashToFloat as b}from"@splitlab/core";import{murmurhash3 as o,localEvaluate as v,evaluateRules as h}from"@splitlab/core";var f=class{constructor(e){this.serverConfig=null;this.eventQueue=[];this.flushTimer=null;this.configRefreshTimer=null;this.lastEtag=null;this.ready=!1;this.apiKey=e.apiKey,this.baseUrl=e.baseUrl.replace(/\/$/,""),this.ingestUrl=(e.ingestUrl||e.baseUrl).replace(/\/$/,""),this.configRefreshInterval=e.configRefreshInterval??3e4,this.flushInterval=e.flushInterval??1e4,this.flushSize=e.flushSize??100,this.onConfigUpdate=e.onConfigUpdate??null}async initialize(){let{config:e,etag:t}=await this.fetchConfig();this.serverConfig=e,this.lastEtag=t,this.configRefreshTimer=setInterval(()=>{this.refresh().catch(()=>{})},this.configRefreshInterval),this.flushTimer=setInterval(()=>{this.flush().catch(()=>{})},this.flushInterval),this.ready=!0}isReady(){return this.ready}async destroy(){this.configRefreshTimer!==null&&(clearInterval(this.configRefreshTimer),this.configRefreshTimer=null),this.flushTimer!==null&&(clearInterval(this.flushTimer),this.flushTimer=null),await this.flush(),this.ready=!1}getVariant(e,t,r){if(!this.serverConfig)return null;let i=this.serverConfig.experiments.find(n=>n.key===e);if(!i||i.targeting_rules&&!h(i.targeting_rules,r||{}))return null;let s=o(i.key+":"+t);if(s%1e4/100>=i.traffic_percentage)return null;let a=i.variants.reduce((n,c)=>n+c.weight,0),g=s%a,l=0;for(let n of i.variants)if(l+=n.weight,g<l)return n.key;return i.variants[i.variants.length-1].key}isFeatureEnabled(e,t,r){if(!this.serverConfig)return!1;let i=this.serverConfig.flags.find(a=>a.key===e);return!i||i.rules&&!h(i.rules,r||{})?!1:o(i.key+":"+t)%100<i.rollout_percentage}evaluateAll(e,t){return this.serverConfig?v(this.serverConfig,e,t||{}):{experiments:{},flags:{}}}track(e,t,r){this.eventQueue.push({distinct_id:e,event_name:t,properties:r,timestamp:new Date().toISOString()}),this.eventQueue.length>=this.flushSize&&this.flush().catch(()=>{})}async flush(){if(this.eventQueue.length===0)return;let e=this.eventQueue;this.eventQueue=[];try{(await fetch(`${this.ingestUrl}/ingest/batch`,{method:"POST",headers:{"Content-Type":"application/json","X-API-Key":this.apiKey},body:JSON.stringify({events:e})})).ok||(this.eventQueue=e.concat(this.eventQueue))}catch{this.eventQueue=e.concat(this.eventQueue)}}async refresh(){try{let{config:e,etag:t,notModified:r}=await this.fetchConfig();if(r)return;this.serverConfig=e,this.lastEtag=t,this.onConfigUpdate&&this.onConfigUpdate()}catch{}}async fetchConfig(){let e={"Content-Type":"application/json"};this.lastEtag&&(e["If-None-Match"]=this.lastEtag);let t=await fetch(`${this.baseUrl}/api/sdk/config?key=${encodeURIComponent(this.apiKey)}`,{method:"GET",headers:e});if(t.status===304)return{config:this.serverConfig,etag:this.lastEtag,notModified:!0};if(!t.ok){let s=await t.text().catch(()=>"");throw new Error(`SplitLab API error ${t.status}: ${s}`)}let r=t.headers.get("etag");return{config:await t.json(),etag:r}}};export{f as SplitLabServer,b as hashToFloat,C as murmurhash3};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@splitlab/node",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js server SDK for SplitLab A/B testing and feature flags",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"require": {
|
|
20
|
+
"types": "./dist/index.d.cts",
|
|
21
|
+
"default": "./dist/index.cjs"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup",
|
|
33
|
+
"dev": "tsup --watch"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@splitlab/core": "^0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"tsup": "^8.0.0",
|
|
40
|
+
"typescript": "^5.4.0"
|
|
41
|
+
}
|
|
42
|
+
}
|