Skip to content
Closed
14 changes: 4 additions & 10 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Checkout submodules # checkout rest
shell: bash
run: |
# If your submodules are configured to use SSH instead of HTTPS please uncomment the following line
git config --global url."https://github.com/".insteadOf "[email protected]:"
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
git submodule sync --recursive
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4
with:
node-version: "18.x"
- name: cache node modules
uses: actions/cache@v1
uses: actions/cache@v4
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: npm-${{ hashFiles('package-lock.json') }}
Expand Down
2 changes: 1 addition & 1 deletion flagsmith-engine/features/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v4 as uuidv4 } from 'uuid';
import { randomUUID as uuidv4 } from "node:crypto";
import { getHashedPercentateForObjIds } from '../utils/hashing/index.js';

export class FeatureModel {
Expand Down
2 changes: 1 addition & 1 deletion flagsmith-engine/identities/models.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IdentityFeaturesList } from '../utils/collections.js';
import { TraitModel } from './traits/models.js';

const { v4: uuidv4 } = require('uuid');
import { randomUUID as uuidv4 } from 'node:crypto';

export class IdentityModel {
identifier: string;
Expand Down
2 changes: 1 addition & 1 deletion flagsmith-engine/segments/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import semver from 'semver';
import * as semver from 'semver';

import { FeatureStateModel } from '../features/models.js';
import { getCastingFunction as getCastingFunction } from '../utils/index.js';
Expand Down
31 changes: 26 additions & 5 deletions flagsmith-engine/utils/hashing/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import md5 from 'md5';
import bigInt from 'big-integer';
import {BinaryLike, createHash} from "node:crypto";

const md5 = (data: BinaryLike) => createHash('md5').update(data).digest('hex')

const makeRepeated = (arr: Array<any>, repeats: number) =>
Array.from({ length: repeats }, () => arr).flat();

// https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript
function h2d(s: any): string {
function add(x: any, y: any) {
var c = 0,
Expand All @@ -30,6 +30,27 @@ function h2d(s: any): string {
});
return dec;
}


import bigInt from 'big-integer'
export function getHashedPercentateForObjIdsOld(objectIds: Array<any>, iterations = 1): number {
let toHash = makeRepeated(objectIds, iterations).join(',');
const hashedValue = md5(toHash);
const hashedInt = bigInt(h2d(hashedValue));
const value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;

// we ignore this for it's nearly impossible use case to catch
/* istanbul ignore next */
if (value === 100) {
/* istanbul ignore next */
return getHashedPercentateForObjIdsOld(objectIds, iterations + 1);
}

return value;
}


// https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript
/**
* Given a list of object ids, get a floating point number between 0 and 1 based on
* the hash of those ids. This should give the same value every time for any list of ids.
Expand All @@ -41,8 +62,8 @@ function h2d(s: any): string {
export function getHashedPercentateForObjIds(objectIds: Array<any>, iterations = 1): number {
let toHash = makeRepeated(objectIds, iterations).join(',');
const hashedValue = md5(toHash);
const hashedInt = bigInt(h2d(hashedValue));
const value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;
const hashedInt = BigInt('0x' + hashedValue);
const value = (Number((hashedInt % 9999n)) / 9998.0) * 100;

// we ignore this for it's nearly impossible use case to catch
/* istanbul ignore next */
Expand Down
6 changes: 1 addition & 5 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Flagsmith from "./sdk/index.js";

export {
AnalyticsProcessor,
FlagsmithAPIError,
Expand All @@ -8,7 +6,7 @@ export {
FlagsmithCache,
DefaultFlag,
Flags,
default
Flagsmith,
} from './sdk/index.js';

export {
Expand All @@ -23,5 +21,3 @@ export {
SegmentModel,
OrganisationModel
} from './flagsmith-engine/index.js';

module.exports = Flagsmith;
Loading