Skip to content

Commit 912f017

Browse files
authored
fix(flag-evaluation): use js-sha256 which can fall back to pure js (#390)
This solution should fall back to a pure JS based solution is "crypto" isn't available and doesn't require us to change the public interface of the package from sync to async.
1 parent 9fa29a6 commit 912f017

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

packages/flag-evaluation/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bucketco/flag-evaluation",
3-
"version": "0.2.0",
3+
"version": "0.1.1",
44
"license": "MIT",
55
"repository": {
66
"type": "git",
@@ -33,5 +33,8 @@
3333
"ts-node": "^10.9.2",
3434
"typescript": "^5.7.3",
3535
"vitest": "^2.0.5"
36+
},
37+
"dependencies": {
38+
"js-sha256": "0.11.0"
3639
}
3740
}

packages/flag-evaluation/src/index.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
try {
2-
// crypto not available on globalThis in Node.js v18
3-
// eslint-disable-next-line @typescript-eslint/no-require-imports
4-
globalThis.crypto ??= require("node:crypto").webcrypto;
5-
} catch {
6-
// ignore
7-
}
1+
import { sha256 } from "js-sha256";
82

93
/**
104
* Represents a filter class with a specific type property.
@@ -271,13 +265,12 @@ export async function hashInt(hashInput: string): Promise<number> {
271265
// 3. multiply by 100000 to get a number between 0 and 100000 and compare it to the threshold
272266
//
273267
// we only need 20 bits to get to 100000 because 2^20 is 1048576
274-
const msgUint8 = new TextEncoder().encode(hashInput);
275-
276-
// Hash the message
277-
const hashBuffer = await crypto.subtle.digest("SHA-256", msgUint8);
268+
const value =
269+
new DataView(sha256.create().update(hashInput).arrayBuffer()).getUint32(
270+
0,
271+
true,
272+
) & 0xfffff;
278273

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

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ __metadata:
560560
"@bucketco/tsconfig": "workspace:^"
561561
"@types/node": "npm:^22.12.0"
562562
eslint: "npm:^9.21.0"
563+
js-sha256: "npm:0.11.0"
563564
prettier: "npm:^3.5.2"
564565
ts-node: "npm:^10.9.2"
565566
typescript: "npm:^5.7.3"
@@ -11901,6 +11902,13 @@ __metadata:
1190111902
languageName: node
1190211903
linkType: hard
1190311904

11905+
"js-sha256@npm:0.11.0":
11906+
version: 0.11.0
11907+
resolution: "js-sha256@npm:0.11.0"
11908+
checksum: 10c0/90980fe01ca01fbd166751fb16c4caa09c1ab997e8bf77c0764cc05c772c6044946f4c1b3bad266ce78357280d2131d3dc0cf2dd7646e78272996bd4d590aa4f
11909+
languageName: node
11910+
linkType: hard
11911+
1190411912
"js-tokens@npm:^3.0.0 || ^4.0.0, js-tokens@npm:^4.0.0":
1190511913
version: 4.0.0
1190611914
resolution: "js-tokens@npm:4.0.0"

0 commit comments

Comments
 (0)