Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 17 additions & 17 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
name: Publish NPM Package

on:
push:
tags:
- '*'
push:
tags:
- '*'

jobs:
package:
runs-on: ubuntu-latest
name: Publish NPM Package
package:
runs-on: ubuntu-latest
name: Publish NPM Package

steps:
- name: Cloning repo
uses: actions/checkout@v3
steps:
- name: Cloning repo
uses: actions/checkout@v3

- uses: actions/setup-node@v4
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- uses: actions/setup-node@v4
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'

- run: npm ci
- run: npm run deploy
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- run: npm ci
- run: npm run deploy
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
66 changes: 33 additions & 33 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
name: Unit/Integration Tests

on:
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review
push:
branches:
- main
jobs:
build-and-test:
strategy:
matrix:
node-version: [18.x, 20.x, 22.x]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4
with:
node-version: "${{ matrix.node-version }}"
- name: cache node modules
uses: actions/cache@v4
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: npm-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
restore-keys: |
npm-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
npm-
- run: npm ci
- run: npm test
env:
CI: true
build-and-test:
strategy:
matrix:
node-version: [18.x, 20.x, 22.x]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v4
with:
node-version: '${{ matrix.node-version }}'
- name: cache node modules
uses: actions/cache@v4
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: npm-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
restore-keys: |
npm-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
npm-
- run: npm ci
- run: npm test
env:
CI: true
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.json
*.json
tests/engine/engine-tests/engine-test-data
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ If you encounter a bug or feature request we would like to hear about it. Before

## Testing

To run the local tests you need to run following command beforehand:
To run the local tests you need to run following commands beforehand:

```bash
git submodule add git@github.com:Flagsmith/engine-test-data.git tests/engine/engine-tests/engine-test-data/
git submodule update --init --recursive
```

## Get in touch
Expand Down
4 changes: 2 additions & 2 deletions flagsmith-engine/environments/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function buildEnvironmentModel(environmentJSON: any) {
);
environmentModel.featureStates = featureStates;
if (!!environmentJSON.identity_overrides) {
environmentModel.identityOverrides = environmentJSON.identity_overrides.map((identityData: any) =>
buildIdentityModel(identityData)
environmentModel.identityOverrides = environmentJSON.identity_overrides.map(
(identityData: any) => buildIdentityModel(identityData)
);
}
return environmentModel;
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 { randomUUID as uuidv4 } from "node:crypto";
import { randomUUID as uuidv4 } from 'node:crypto';
import { getHashedPercentateForObjIds } from '../utils/hashing/index.js';

export class FeatureModel {
Expand Down
28 changes: 14 additions & 14 deletions flagsmith-engine/features/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ export function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStat
featuresStateModelJSON.featurestate_uuid
);

featureStateModel.featureSegment = featuresStateModelJSON.feature_segment ?
buildFeatureSegment(featuresStateModelJSON.feature_segment) :
undefined;
featureStateModel.featureSegment = featuresStateModelJSON.feature_segment
? buildFeatureSegment(featuresStateModelJSON.feature_segment)
: undefined;

const multivariateFeatureStateValues = featuresStateModelJSON.multivariate_feature_state_values
? featuresStateModelJSON.multivariate_feature_state_values.map((fsv: any) => {
const featureOption = new MultivariateFeatureOptionModel(
fsv.multivariate_feature_option.value,
fsv.multivariate_feature_option.id
);
return new MultivariateFeatureStateValueModel(
featureOption,
fsv.percentage_allocation,
fsv.id,
fsv.mv_fs_value_uuid
);
})
const featureOption = new MultivariateFeatureOptionModel(
fsv.multivariate_feature_option.value,
fsv.multivariate_feature_option.id
);
return new MultivariateFeatureStateValueModel(
featureOption,
fsv.percentage_allocation,
fsv.id,
fsv.mv_fs_value_uuid
);
})
: [];

featureStateModel.multivariateFeatureStateValues = multivariateFeatureStateValues;
Expand Down
2 changes: 1 addition & 1 deletion flagsmith-engine/identities/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class IdentityModel {
environmentApiKey: string,
identifier: string,
identityUuid?: string,
djangoID?: number,
djangoID?: number
) {
this.identityUuid = identityUuid || uuidv4();
this.createdDate = Date.parse(created_date) || Date.now();
Expand Down
5 changes: 2 additions & 3 deletions flagsmith-engine/segments/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ export function traitsMatchSegmentCondition(
}
const traits = identityTraits.filter(t => t.traitKey === condition.property_);
const trait = traits.length > 0 ? traits[0] : undefined;
if (condition.operator === IS_SET ) {
if (condition.operator === IS_SET) {
return !!trait;
} else if (condition.operator === IS_NOT_SET){
} else if (condition.operator === IS_NOT_SET) {
return trait == undefined;
}
return trait ? condition.matchesTraitValue(trait.traitValue) : false;

}
40 changes: 25 additions & 15 deletions flagsmith-engine/segments/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ export const matchingFunctions = {
thisValue >= otherValue,
[CONDITION_OPERATORS.NOT_EQUAL]: (thisValue: any, otherValue: any) => thisValue != otherValue,
[CONDITION_OPERATORS.CONTAINS]: (thisValue: any, otherValue: any) =>
!!otherValue && otherValue.includes(thisValue),
!!otherValue && otherValue.includes(thisValue)
};

export const semverMatchingFunction = {
...matchingFunctions,
[CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) => semver.eq(thisValue, otherValue),
[CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) => semver.gt(otherValue, thisValue),
[CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) =>
semver.eq(thisValue, otherValue),
[CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) =>
semver.gt(otherValue, thisValue),
[CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
semver.gte(otherValue, thisValue),
[CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) => semver.gt(thisValue, otherValue),
[CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) =>
semver.gt(thisValue, otherValue),
[CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
semver.gte(thisValue, otherValue),
}
semver.gte(thisValue, otherValue)
};

export const getMatchingFunctions = (semver: boolean) => (semver ? semverMatchingFunction : matchingFunctions);
export const getMatchingFunctions = (semver: boolean) =>
semver ? semverMatchingFunction : matchingFunctions;

export class SegmentConditionModel {
EXCEPTION_OPERATOR_METHODS: { [key: string]: string } = {
Expand All @@ -55,7 +59,11 @@ export class SegmentConditionModel {
value: string | null | undefined;
property_: string | null | undefined;

constructor(operator: string, value?: string | null | undefined, property?: string | null | undefined) {
constructor(
operator: string,
value?: string | null | undefined,
property?: string | null | undefined
) {
this.operator = operator;
this.value = value;
this.property_ = property;
Expand All @@ -64,24 +72,26 @@ export class SegmentConditionModel {
matchesTraitValue(traitValue: any) {
const evaluators: { [key: string]: CallableFunction } = {
evaluateNotContains: (traitValue: any) => {
return typeof traitValue == "string" &&
return (
typeof traitValue == 'string' &&
!!this.value &&
!traitValue.includes(this.value?.toString());
!traitValue.includes(this.value?.toString())
);
},
evaluateRegex: (traitValue: any) => {
return !!this.value && !!traitValue?.toString().match(new RegExp(this.value));
},
evaluateModulo: (traitValue: any) => {
if (isNaN(parseFloat(traitValue)) || !this.value) {
return false
return false;
}
const parts = (this.value).split("|");
const parts = this.value.split('|');
const [divisor, reminder] = [parseFloat(parts[0]), parseFloat(parts[1])];
return traitValue % divisor === reminder
return traitValue % divisor === reminder;
},
evaluateIn: (traitValue: any) => {
return this.value?.split(',').includes(traitValue.toString())
},
return this.value?.split(',').includes(traitValue.toString());
}
};

// TODO: move this logic to the evaluator module
Expand Down
6 changes: 3 additions & 3 deletions flagsmith-engine/utils/hashing/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {BinaryLike, createHash} from "node:crypto";
import { BinaryLike, createHash } from 'node:crypto';

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

const makeRepeated = (arr: Array<any>, repeats: number) =>
Array.from({ length: repeats }, () => arr).flat();
Expand All @@ -18,7 +18,7 @@ export function getHashedPercentateForObjIds(objectIds: Array<any>, iterations =
let toHash = makeRepeated(objectIds, iterations).join(',');
const hashedValue = md5(toHash);
const hashedInt = BigInt('0x' + hashedValue);
const value = (Number((hashedInt % 9999n)) / 9998.0) * 100;
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: 4 additions & 2 deletions flagsmith-engine/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { removeSemverSuffix } from "../segments/util.js";
import { removeSemverSuffix } from '../segments/util.js';

export function getCastingFunction(traitType: 'boolean' | 'string' | 'number' | 'semver' | any): CallableFunction {
export function getCastingFunction(
traitType: 'boolean' | 'string' | 'number' | 'semver' | any
): CallableFunction {
switch (traitType) {
case 'boolean':
return (x: any) => !['False', 'false'].includes(x);
Expand Down
41 changes: 18 additions & 23 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
export {
AnalyticsProcessor,
AnalyticsProcessorOptions,
FlagsmithAPIError,
FlagsmithClientError,
EnvironmentDataPollingManager,
FlagsmithCache,
DefaultFlag,
Flags,
Flagsmith,
AnalyticsProcessor,
AnalyticsProcessorOptions,
FlagsmithAPIError,
FlagsmithClientError,
EnvironmentDataPollingManager,
FlagsmithCache,
DefaultFlag,
Flags,
Flagsmith
} from './sdk/index.js';

export {
BaseOfflineHandler,
LocalFileHandler,
} from './sdk/offline_handlers.js';
export { BaseOfflineHandler, LocalFileHandler } from './sdk/offline_handlers.js';

export {
FlagsmithConfig
} from './sdk/types.js'
export { FlagsmithConfig } from './sdk/types.js';

export {
EnvironmentModel,
FeatureModel,
FeatureStateModel,
IdentityModel,
TraitModel,
SegmentModel,
OrganisationModel
EnvironmentModel,
FeatureModel,
FeatureStateModel,
IdentityModel,
TraitModel,
SegmentModel,
OrganisationModel
} from './flagsmith-engine/index.js';
Loading