lightswitch-js-sdk
Version:
light switch javascript sdk
252 lines (251 loc) • 10.1 kB
JavaScript
import { LogLevel, } from './types';
import { LSLogger } from './LSLogger';
import { postRequest, getHashedPercentageForObjectIds, compareObjectsAndMaps, } from './utils';
import ReconnectingEventSource from 'reconnecting-eventsource';
import { LSServerError, LSTypeCastError } from './error';
const logger = LSLogger(LogLevel.DEBUG);
const FLAG_NOT_FOUND = 1000;
const VARIATION_NOT_FOUND = 1001;
const MEMBER_NOT_FOUND = 2000;
const SDK_KEY_ALREADY_EXISTS = 3000;
const SDK_KEY_NOT_FOUND = 3001;
class LightSwitch {
static instance = null;
static isInitialized = false;
constructor() { }
static getInstance() {
if (!LightSwitch.instance) {
LightSwitch.instance = new LightSwitch();
}
return LightSwitch.instance;
}
sdkKey = '';
logLevel = LogLevel.DEBUG; // 로그 레벨
// private lastUpdated = ''; // feature flag 데이터 갱신 날짜
flags = new Map();
onError = null;
eventSource = null;
onFlagChanged = null;
userKey = '';
reconnectTime = 3000;
SERVER_URL = 'http://localhost:8000';
INIT_REQUEST_PATH = this.SERVER_URL + '/api/v1/sdk/init';
SSE_CONNECT_PATH = this.SERVER_URL + '/api/v1/sse/subscribe';
async init(config) {
if (LightSwitch.instance != null && LightSwitch.isInitialized) {
logger.info('lightswitch is already initialized, skip init process');
return;
}
const { sdkKey, onError, onFlagChanged, reconnectTime, endpoint } = config;
this.sdkKey = sdkKey;
if (reconnectTime) {
this.reconnectTime = reconnectTime;
}
if (onError) {
this.onError = onError;
}
if (endpoint) {
this.SERVER_URL = endpoint;
this.INIT_REQUEST_PATH = this.SERVER_URL + '/api/v1/sdk/init';
this.SSE_CONNECT_PATH = this.SERVER_URL + '/api/v1/sse/subscribe';
}
this.onFlagChanged = onFlagChanged;
await this.getInitData();
logger.debug('success to getInitData');
await this.getUserKey();
logger.debug('success to getUserKey, start connecting SSE');
this.eventSource = this.getEventSource(this.userKey, this.reconnectTime);
this.addSseListener();
LightSwitch.isInitialized = true;
// logger.info('success to initialize client sdk');
}
getVariationValue(flag, LSUser) {
let percentage = getHashedPercentageForObjectIds([LSUser.getUserId(), flag.title], 1);
logger.info(percentage);
if (flag.variations) {
for (let i = 0; i < flag.variations.length; i++) {
const variation = flag.variations[i];
percentage -= variation.portion;
logger.info(`percentage : ${percentage}, portion : ${variation.portion}`);
if (percentage < 0) {
// logger.info(`value : ${variation.value}`);
return variation.value;
}
}
}
return flag.defaultValue;
}
getTypedValue(flag, value) {
if (flag.type == 'STRING') {
return value;
}
else if (flag.type == 'BOOLEAN') {
if (value == 'TRUE')
return true;
else
return false;
}
else if (flag.type == 'INTEGER') {
return parseInt(value);
}
}
getFlag(name, LSUser, defaultVal) {
const flag = this.flags.get(name);
if (!flag) {
logger.warning(`${name} 플래그가 존재하지 않습니다. 기본값을 이용합니다.`);
return defaultVal;
}
if (!flag.active) {
logger.info(`id : ${flag?.flagId} title : ${flag?.title} 플래그가 활성화 되어있지 않습니다. 기본값을 이용합니다.`);
return defaultVal;
// return this.getTypedValue(flag, flag.defaultValue) as T;
}
else {
if (flag.keywords.length > 0 && LSUser.properties?.size > 0) {
logger.info(`id : ${flag?.flagId} title : ${flag.title} 플래그가 활성화 되어 있습니다. 키워드를 확인합니다.`);
for (let i = 0; i < flag.keywords.length; i++) {
const keyword = flag.keywords[i];
const isEqual = compareObjectsAndMaps(keyword.properties, LSUser.properties);
if (isEqual) {
return this.getTypedValue(flag, keyword.value);
}
else {
logger.info(`키워드 대상이 아닙니다. portion을 계산합니다.`);
}
}
}
logger.info(`id : ${flag?.flagId} title : ${flag.title} 플래그가 활성화 되어 있습니다. portion을 계산합니다.`);
const evaluatedValue = this.getVariationValue(flag, LSUser);
return this.getTypedValue(flag, evaluatedValue);
}
}
handleError(error) {
if (this.onError) {
this.onError(error);
}
else {
throw error;
}
}
getBooleanFlag(name, LSUser, defaultVal) {
const booleanFlag = this.getFlag(name, LSUser, defaultVal);
if (typeof booleanFlag != 'boolean') {
this.handleError(new LSTypeCastError(`${name} 플래그를 BOOLEAN 타입으로 캐스팅하는데 실패했습니다.`));
}
return booleanFlag;
}
getIntegerFlag(name, LSUser, defaultVal) {
const integerFlag = this.getFlag(name, LSUser, defaultVal);
if (typeof integerFlag != 'number') {
this.handleError(new LSTypeCastError(`${name} 플래그를 INTEGER 타입으로 캐스팅하는데 실패했습니다.`));
}
return integerFlag;
}
getStringFlag(name, LSUser, defaultVal) {
const stringFlag = this.getFlag(name, LSUser, defaultVal);
if (typeof stringFlag != 'string') {
this.handleError(new LSTypeCastError(`${name} 플래그를 STRING 타입으로 캐스팅하는데 실패했습니다.`));
}
return stringFlag;
}
getEventSource(userKey, reconnectTime) {
return new ReconnectingEventSource(this.SSE_CONNECT_PATH + '/' + userKey, {
// indicating if CORS should be set to include credentials, default `false`
withCredentials: true,
// the maximum time to wait before attempting to reconnect in ms, default `3000`
// note: wait time is randomised to prevent all clients from attempting to reconnect simultaneously
max_retry_time: reconnectTime,
// underlying EventSource class, default `EventSource`
eventSourceClass: EventSource,
});
}
async getInitData() {
const response = await postRequest(this.INIT_REQUEST_PATH, {
sdkKey: this.sdkKey,
});
if (response?.code == SDK_KEY_NOT_FOUND) {
this.handleError(new LSServerError(response.message));
}
const newFlags = response?.data;
newFlags.forEach((flag) => {
this.flags.set(flag.title, flag);
});
this.onFlagChanged?.();
// logger.info(`receive init data : ${JSON.stringify(response)}`);
}
async getUserKey() {
try {
logger.info(this.sdkKey);
const response = await postRequest(`${this.SSE_CONNECT_PATH}`, {
sdkKey: this.sdkKey,
});
this.userKey = response.data.userKey;
// logger.info(`receive userKey data : ${JSON.stringify(response)}`);
}
catch (error) {
if (error instanceof Error) {
this.handleError(new LSServerError(error.message));
}
}
}
getAllFlags() {
return this.flags;
}
addSseListener() {
this.eventSource?.addEventListener('sse', (event) => {
if (event.data === 'SSE connected') {
return;
}
const data = JSON.parse(event.data);
logger.info(data.type);
switch (data.type) {
case 'CREATE':
const createData = data.data;
this.addFlag(createData);
break;
case 'UPDATE':
const updateData = data.data;
this.updateFlag(updateData);
break;
case 'DELETE':
const deleteData = data.data;
this.deleteFlag(deleteData);
break;
case 'SWITCH': // not used
const switchData = data.data;
this.switchFlag(switchData);
break;
}
this.onFlagChanged?.();
});
}
addFlag(flag) {
logger.info('addFlag call');
this.flags.set(flag.title, flag);
}
updateFlag(newFlag) {
logger.info('updateFlag call');
this.flags.delete(newFlag.title);
this.flags.set(newFlag.title, newFlag);
}
deleteFlag(title) {
logger.info('deleteFlag call');
this.flags.delete(title.title);
}
switchFlag(sw) {
logger.info('switchFlag call');
const flag = this.flags.get(sw.title);
const newFlag = JSON.parse(JSON.stringify(flag));
newFlag.active = sw.active;
this.flags.set(sw.title, newFlag);
}
destroy() {
if (!LightSwitch.isInitialized) {
throw new Error('LightSwitch is not initialized.');
}
this.eventSource?.close();
LightSwitch.isInitialized = false;
logger.debug('call destroy');
}
}
export default LightSwitch;