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

feat!: Remove all uses of CJS, add named Flagsmith export #163

Merged
merged 12 commits into from
Nov 7, 2024
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';
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved

import { FeatureStateModel } from '../features/models.js';
import { getCastingFunction as getCastingFunction } from '../utils/index.js';
Expand Down
34 changes: 5 additions & 29 deletions flagsmith-engine/utils/hashing/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
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,
r = [];
var x = x.split('').map(Number);
var y = y.split('').map(Number);
while (x.length || y.length) {
var s = (x.pop() || 0) + (y.pop() || 0) + c;
r.unshift(s < 10 ? s : s - 10);
c = s < 10 ? 0 : 1;
}
if (c) r.unshift(c);
return r.join('');
}

var dec = '0';
s.split('').forEach(function (chr: any) {
var n = parseInt(chr, 16);
for (var t = 8; t; t >>= 1) {
dec = add(dec, dec);
if (n & t) dec = add(dec, '1');
}
});
return dec;
}
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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 +17,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;
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved

// 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