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

chore(rc): Remove Remote Config dependency on Long JS #2614

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
142 changes: 65 additions & 77 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@
"farmhash-modern": "^1.1.0",
"jsonwebtoken": "^9.0.0",
"jwks-rsa": "^3.1.0",
"long": "^5.2.3",
"node-forge": "^1.3.1",
"uuid": "^10.0.0"
},
Expand Down
37 changes: 22 additions & 15 deletions src/remote-config/condition-evaluator-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
PercentConditionOperator
} from './remote-config-api';
import * as farmhash from 'farmhash-modern';
import long = require('long');

/**
* Encapsulates condition evaluation logic to simplify organization and
Expand Down Expand Up @@ -147,26 +146,18 @@ export class ConditionEvaluator {
const seedPrefix = seed && seed.length > 0 ? `${seed}.` : '';
const stringToHash = `${seedPrefix}${context.randomizationId}`;

const hash64 = ConditionEvaluator.hashSeededRandomizationId(stringToHash)

// Using a 64-bit long for consistency with the Remote Config fetch endpoint.
let hash64 = long.fromString(farmhash.fingerprint64(stringToHash).toString());
const instanceMicroPercentile = hash64 % BigInt(100 * 1_000_000);

// Negate the hash if its value is less than 0. We handle this manually because the
// Long library doesn't provided an absolute value method.
if (hash64.lt(0)) {
hash64 = hash64.negate();
}

const instanceMicroPercentile = hash64.mod(100 * 1_000_000);

switch (percentOperator) {
case PercentConditionOperator.LESS_OR_EQUAL:
return instanceMicroPercentile.lte(normalizedMicroPercent);
return instanceMicroPercentile <= normalizedMicroPercent;
case PercentConditionOperator.GREATER_THAN:
return instanceMicroPercentile.gt(normalizedMicroPercent);
return instanceMicroPercentile > normalizedMicroPercent;
case PercentConditionOperator.BETWEEN:
return instanceMicroPercentile.gt(normalizedMicroPercentLowerBound)
&& instanceMicroPercentile.lte(normalizedMicroPercentUpperBound);
return instanceMicroPercentile > normalizedMicroPercentLowerBound
&& instanceMicroPercentile <= normalizedMicroPercentUpperBound;
case PercentConditionOperator.UNKNOWN:
default:
break;
Expand All @@ -175,4 +166,20 @@ export class ConditionEvaluator {
// TODO: add logging once we have a wrapped logger.
return false;
}

// Visible for testing
static hashSeededRandomizationId(seededRandomizationId: string): bigint {
// For consistency with the Remote Config fetch endpoint's percent condition behavior
// we use Farmhash's fingerprint64 algorithm and interpret the resulting unsigned value
// as a signed value.
let hash64 = BigInt.asIntN(64, farmhash.fingerprint64(seededRandomizationId));

// Manually negate the hash if its value is less than 0, since Math.abs doesn't
// support BigInt.
if (hash64 < 0) {
hash64 = -hash64;
}

return hash64;
}
}
Loading
Loading