Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom hash function plugin #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/StatsigOnPrem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import EntityFeatureGate from "./entities/EntityFeatureGate";
import { IDUtils } from "./utils/IDUtils";
import { SpecsCacheInterface } from "./interfaces/SpecsCacheInterface";
import StorageHandler from "./utils/StorageHandler";
import HashUtils from "./utils/HashUtils";
import HashUtils, { HashFn } from "./utils/HashUtils";
import { TargetAppNames } from "./types/TargetAppNames";
import CacheHandler from "./utils/CacheHandler";
import { SDKKeysCacheInterface } from "./interfaces/SDKKeyCacheInterface";

type Plugins = Partial<{
specsCache: SpecsCacheInterface;
sdkKeysCache: SDKKeysCacheInterface;
hash: HashFn;
}>;

export default class StatsigOnPrem implements StatsigInterface {
Expand All @@ -36,6 +37,9 @@ export default class StatsigOnPrem implements StatsigInterface {
specs: plugins?.specsCache,
keys: plugins?.sdkKeysCache,
});
if (plugins?.hash !== undefined) {
HashUtils.setHashFn(plugins.hash);
}
}

public async initialize(): Promise<void> {
Expand Down
30 changes: 19 additions & 11 deletions src/utils/HashUtils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
export type HashFn = (input: string) => string;

let hash_fn: HashFn = djb2Hash;

export default class HashUtils {
public static hashString(str: string): string {
return this.djb2Hash(str);
return hash_fn(str);
}

private static fasthash(value: string): number {
let hash = 0;
for (let i = 0; i < value.length; i++) {
const character = value.charCodeAt(i);
hash = (hash << 5) - hash + character;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
public static setHashFn(fn: HashFn): void {
hash_fn = fn;
}
}

private static djb2Hash(value: string): string {
return String(this.fasthash(value) >>> 0);
function fasthash(value: string): number {
let hash = 0;
for (let i = 0; i < value.length; i++) {
const character = value.charCodeAt(i);
hash = (hash << 5) - hash + character;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}

function djb2Hash(value: string): string {
return String(fasthash(value) >>> 0);
}
26 changes: 26 additions & 0 deletions tests/HashPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import StatsigStorageExample from "../examples/StatsigStorageExample";
import SDKKeysCache from "../src/SDKKeysCache";
import StatsigOnPrem from "../src/StatsigOnPrem";

describe("Hash plugin", () => {
const storage = new StatsigStorageExample();
const sdkKeysCache = new SDKKeysCache();
const hashFn = (input: string) => `hashed:${input}`;
const statsig = new StatsigOnPrem(storage, { sdkKeysCache, hash: hashFn });

beforeAll(async () => {
await statsig.initialize();
});

afterAll(async () => {
storage.clearAll();
await statsig.clearCache();
});

it("Uses custom hash function", async () => {
await statsig.registerSDKKey("secret-123");
expect(storage.get("statsig:sdkKey:hashed:secret-123")).resolves.toEqual(
"registered"
);
});
});