Skip to content

Commit

Permalink
decode obfuscated bandit
Browse files Browse the repository at this point in the history
  • Loading branch information
leoromanovsky committed Jan 16, 2025
1 parent 8ce8737 commit 5bd5fd2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eppo/js-client-sdk-common",
"version": "4.8.2",
"version": "4.8.3-alpha.1",
"description": "Common library for Eppo JavaScript SDKs (web, react native, and node)",
"main": "dist/index.js",
"files": [
Expand Down
9 changes: 6 additions & 3 deletions src/client/eppo-precomputed-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
MAX_EVENT_QUEUE_SIZE,
PRECOMPUTED_BASE_URL,
} from '../constants';
import { decodePrecomputedFlag } from '../decoding';
import { decodePrecomputedBandit, decodePrecomputedFlag } from '../decoding';
import { FlagEvaluationWithoutDetails } from '../evaluator';
import FetchHttpClient from '../http-client';
import {
Expand Down Expand Up @@ -361,10 +361,13 @@ export default class EppoPrecomputedClient {
return this.getObfuscatedPrecomputedBandit(banditKey);
}

private getObfuscatedPrecomputedBandit(banditKey: string): IObfuscatedPrecomputedBandit | null {
private getObfuscatedPrecomputedBandit(banditKey: string): IPrecomputedBandit | null {
const salt = this.precomputedBanditStore?.salt;
const saltedAndHashedBanditKey = getMD5Hash(banditKey, salt);
return this.precomputedBanditStore?.get(saltedAndHashedBanditKey) ?? null;
const precomputedBandit: IObfuscatedPrecomputedBandit | null = this.precomputedBanditStore?.get(
saltedAndHashedBanditKey,
) as IObfuscatedPrecomputedBandit;
return precomputedBandit ? decodePrecomputedBandit(precomputedBandit) : null;
}

public isInitialized() {
Expand Down
15 changes: 15 additions & 0 deletions src/decoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
ObfuscatedSplit,
PrecomputedFlag,
DecodedPrecomputedFlag,
IPrecomputedBandit,
IObfuscatedPrecomputedBandit,
} from './interfaces';
import { decodeBase64 } from './obfuscation';

Expand Down Expand Up @@ -88,3 +90,16 @@ export function decodePrecomputedFlag(precomputedFlag: PrecomputedFlag): Decoded
extraLogging: decodeObject(precomputedFlag.extraLogging ?? {}),
};
}

export function decodePrecomputedBandit(
precomputedBandit: IObfuscatedPrecomputedBandit,
): IPrecomputedBandit {
return {
...precomputedBandit,
banditKey: decodeBase64(precomputedBandit.banditKey),
action: decodeBase64(precomputedBandit.action),
modelVersion: decodeBase64(precomputedBandit.modelVersion),
actionNumericAttributes: decodeObject(precomputedBandit.actionNumericAttributes ?? {}),
actionCategoricalAttributes: decodeObject(precomputedBandit.actionCategoricalAttributes ?? {}),
};
}

0 comments on commit 5bd5fd2

Please sign in to comment.