diff --git a/packages/flag-evaluation/package.json b/packages/flag-evaluation/package.json index 1ab54c98..8df1442b 100644 --- a/packages/flag-evaluation/package.json +++ b/packages/flag-evaluation/package.json @@ -1,6 +1,6 @@ { "name": "@bucketco/flag-evaluation", - "version": "0.2.0", + "version": "0.1.1", "license": "MIT", "repository": { "type": "git", @@ -33,5 +33,8 @@ "ts-node": "^10.9.2", "typescript": "^5.7.3", "vitest": "^2.0.5" + }, + "dependencies": { + "js-sha256": "0.11.0" } } diff --git a/packages/flag-evaluation/src/index.ts b/packages/flag-evaluation/src/index.ts index 85e32aa0..d3bbd5ff 100644 --- a/packages/flag-evaluation/src/index.ts +++ b/packages/flag-evaluation/src/index.ts @@ -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. @@ -271,13 +265,12 @@ export async function hashInt(hashInput: string): Promise { // 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); } diff --git a/yarn.lock b/yarn.lock index 3101669f..3fca25b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"