Skip to content
Merged
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
5 changes: 4 additions & 1 deletion packages/flag-evaluation/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bucketco/flag-evaluation",
"version": "0.2.0",
"version": "0.1.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,5 +33,8 @@
"ts-node": "^10.9.2",
"typescript": "^5.7.3",
"vitest": "^2.0.5"
},
"dependencies": {
"js-sha256": "0.11.0"
}
}
19 changes: 6 additions & 13 deletions packages/flag-evaluation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
try {
// crypto not available on globalThis in Node.js v18
// eslint-disable-next-line @typescript-eslint/no-require-imports
globalThis.crypto ??= require("node:crypto").webcrypto;
} catch {
// ignore
}
import { sha256 } from "js-sha256";

/**
* Represents a filter class with a specific type property.
Expand Down Expand Up @@ -271,13 +265,12 @@ export async function hashInt(hashInput: string): Promise<number> {
// 3. multiply by 100000 to get a number between 0 and 100000 and compare it to the threshold
//
// we only need 20 bits to get to 100000 because 2^20 is 1048576
const msgUint8 = new TextEncoder().encode(hashInput);

// Hash the message
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8);
const value =
new DataView(sha256.create().update(hashInput).arrayBuffer()).getUint32(
0,
true,
) & 0xfffff;

const view = new DataView(hashBuffer);
const value = view.getUint32(0, true) & 0xfffff;
return Math.floor((value / 0xfffff) * 100000);
}

Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ __metadata:
"@bucketco/tsconfig": "workspace:^"
"@types/node": "npm:^22.12.0"
eslint: "npm:^9.21.0"
js-sha256: "npm:0.11.0"
prettier: "npm:^3.5.2"
ts-node: "npm:^10.9.2"
typescript: "npm:^5.7.3"
Expand Down Expand Up @@ -11901,6 +11902,13 @@ __metadata:
languageName: node
linkType: hard

"js-sha256@npm:0.11.0":
version: 0.11.0
resolution: "js-sha256@npm:0.11.0"
checksum: 10c0/90980fe01ca01fbd166751fb16c4caa09c1ab997e8bf77c0764cc05c772c6044946f4c1b3bad266ce78357280d2131d3dc0cf2dd7646e78272996bd4d590aa4f
languageName: node
linkType: hard

"js-tokens@npm:^3.0.0 || ^4.0.0, js-tokens@npm:^4.0.0":
version: 4.0.0
resolution: "js-tokens@npm:4.0.0"
Expand Down