Skip to content

Commit 2cc95c7

Browse files
authored
feat!: Remove all uses of CJS, add named Flagsmith export (#163)
* Remove all uses of `require` * Use only CJS exports * Add named Flagsmith export * Remove md5, big-integer deps * Remove esModuleInterop * Import test data instead of reading filesystem * Remove unused default export * convert hex to decimal with native bigint * remove uuid dependency * Upgrade GHA workflow dependencies * Version 4.0.0 * Remove redundant Number cast
1 parent 7afadbc commit 2cc95c7

File tree

12 files changed

+600
-3681
lines changed

12 files changed

+600
-3681
lines changed

.github/workflows/pull_request.yaml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,14 @@ jobs:
1414
build-and-test:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v2
18-
- name: Checkout submodules # checkout rest
19-
shell: bash
20-
run: |
21-
# If your submodules are configured to use SSH instead of HTTPS please uncomment the following line
22-
git config --global url."https://github.com/".insteadOf "git@github.com:"
23-
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
24-
git submodule sync --recursive
25-
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
17+
- uses: actions/checkout@v4
18+
with:
19+
submodules: true
2620
- uses: actions/setup-node@v4
2721
with:
2822
node-version: "18.x"
2923
- name: cache node modules
30-
uses: actions/cache@v1
24+
uses: actions/cache@v4
3125
with:
3226
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
3327
key: npm-${{ hashFiles('package-lock.json') }}

flagsmith-engine/features/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { v4 as uuidv4 } from 'uuid';
1+
import { randomUUID as uuidv4 } from "node:crypto";
22
import { getHashedPercentateForObjIds } from '../utils/hashing/index.js';
33

44
export class FeatureModel {

flagsmith-engine/identities/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IdentityFeaturesList } from '../utils/collections.js';
22
import { TraitModel } from './traits/models.js';
33

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

66
export class IdentityModel {
77
identifier: string;

flagsmith-engine/segments/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import semver from 'semver';
1+
import * as semver from 'semver';
22

33
import { FeatureStateModel } from '../features/models.js';
44
import { getCastingFunction as getCastingFunction } from '../utils/index.js';

flagsmith-engine/utils/hashing/index.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
1-
import md5 from 'md5';
2-
import bigInt from 'big-integer';
1+
import {BinaryLike, createHash} from "node:crypto";
2+
3+
const md5 = (data: BinaryLike) => createHash('md5').update(data).digest('hex')
34

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

78
// https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript
8-
function h2d(s: any): string {
9-
function add(x: any, y: any) {
10-
var c = 0,
11-
r = [];
12-
var x = x.split('').map(Number);
13-
var y = y.split('').map(Number);
14-
while (x.length || y.length) {
15-
var s = (x.pop() || 0) + (y.pop() || 0) + c;
16-
r.unshift(s < 10 ? s : s - 10);
17-
c = s < 10 ? 0 : 1;
18-
}
19-
if (c) r.unshift(c);
20-
return r.join('');
21-
}
22-
23-
var dec = '0';
24-
s.split('').forEach(function (chr: any) {
25-
var n = parseInt(chr, 16);
26-
for (var t = 8; t; t >>= 1) {
27-
dec = add(dec, dec);
28-
if (n & t) dec = add(dec, '1');
29-
}
30-
});
31-
return dec;
32-
}
339
/**
3410
* Given a list of object ids, get a floating point number between 0 and 1 based on
3511
* the hash of those ids. This should give the same value every time for any list of ids.
@@ -41,8 +17,8 @@ function h2d(s: any): string {
4117
export function getHashedPercentateForObjIds(objectIds: Array<any>, iterations = 1): number {
4218
let toHash = makeRepeated(objectIds, iterations).join(',');
4319
const hashedValue = md5(toHash);
44-
const hashedInt = bigInt(h2d(hashedValue));
45-
const value = (hashedInt.mod(9999).toJSNumber() / 9998) * 100;
20+
const hashedInt = BigInt('0x' + hashedValue);
21+
const value = (Number((hashedInt % 9999n)) / 9998.0) * 100;
4622

4723
// we ignore this for it's nearly impossible use case to catch
4824
/* istanbul ignore next */

index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Flagsmith from "./sdk/index.js";
2-
31
export {
42
AnalyticsProcessor,
53
FlagsmithAPIError,
@@ -8,7 +6,7 @@ export {
86
FlagsmithCache,
97
DefaultFlag,
108
Flags,
11-
default
9+
Flagsmith,
1210
} from './sdk/index.js';
1311

1412
export {
@@ -23,5 +21,3 @@ export {
2321
SegmentModel,
2422
OrganisationModel
2523
} from './flagsmith-engine/index.js';
26-
27-
module.exports = Flagsmith;

0 commit comments

Comments
 (0)