Skip to content

Commit

Permalink
TW-1364 Use 'semver' module for versions comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Apr 11, 2024
1 parent 2c96fa0 commit 16c25e3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 51 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"pino-pretty": "^4.7.1",
"qs": "^6.10.3",
"semaphore": "^1.1.0",
"semver": "^7.6.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"yup": "^1.3.2"
Expand Down
35 changes: 5 additions & 30 deletions src/advertising/external-ads.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { satisfies as versionSatisfiesRange } from 'semver';

import { objectStorageMethodsFactory, redisClient } from '../redis';
import { isDefined } from '../utils/helpers';

Expand Down Expand Up @@ -84,8 +86,7 @@ interface AdStylesOverrides {
}

interface ExtVersionConstraints {
firstExtVersion?: string;
lastExtVersion?: string;
extVersion?: string;
}

export interface AdPlacesRule extends ExtVersionConstraints {
Expand Down Expand Up @@ -176,31 +177,5 @@ export const addAdProvidersForAllSites = async (providers: string[]) =>
export const removeAdProvidersForAllSites = async (providers: string[]) =>
redisClient.srem(AD_PROVIDERS_ALL_SITES_KEY, ...providers);

const compareVersions = (a: string, b: string) => {
const [aMajor = 0, aMinor = 0, aPatch = 0] = a.split('.').map(Number);
const [bMajor = 0, bMinor = 0, bPatch = 0] = b.split('.').map(Number);

if (aMajor !== bMajor) {
return aMajor - bMajor;
}

if (aMinor !== bMinor) {
return aMinor - bMinor;
}

return aPatch - bPatch;
};

export const filterByVersion = <T extends ExtVersionConstraints>(rules: T[], version = '1.20.1') => {
return rules.filter(({ firstExtVersion, lastExtVersion }) => {
if (isDefined(firstExtVersion) && compareVersions(firstExtVersion, version) > 0) {
return false;
}

if (isDefined(lastExtVersion) && compareVersions(lastExtVersion, version) < 0) {
return false;
}

return true;
});
};
export const filterByVersion = <T extends ExtVersionConstraints>(rules: T[], version = '1.20.1') =>
rules.filter(({ extVersion }) => !isDefined(extVersion) || versionSatisfiesRange(version, extVersion));
14 changes: 3 additions & 11 deletions src/routers/slise-ad-rules/ad-places.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,11 @@ import {
* ExtVersionConstraints:
* type: object
* properties:
* firstExtVersion:
* extVersion:
* type: string
* format: version
* description: >
* The first extension version where the rule is applicable. If not specified, the rule is applicable
* for all extension versions not greater than the `lastExtVersion` value. If neither of them is specified,
* the rule is applicable for all extension versions.
* lastExtVersion:
* type: string
* format: version
* description: >
* The last extension version where the rule is applicable. If not specified, the rule is applicable
* for all extension versions not less than the `firstExtVersion` value.
* A range of versions where the rule is applicable. If not specified, the rule is applicable
* for all versions. See the [ranges format](https://www.npmjs.com/package/semver#ranges)
* AdPlacesRule:
* allOf:
* - $ref: '#/components/schemas/ExtVersionConstraints'
Expand Down
17 changes: 7 additions & 10 deletions src/utils/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { validRange as getValidatedRange } from 'semver';
import {
array as arraySchema,
ArraySchema as IArraySchema,
Expand Down Expand Up @@ -60,8 +61,8 @@ const regexStringSchema = stringSchema().test('is-regex', function (value: strin

export const regexStringListSchema = arraySchema().of(regexStringSchema.clone().required());

const versionSchema = stringSchema().test('is-version', function (value: string | undefined) {
return !isDefined(value) || /^\d+\.\d+\.\d+$/.test(value);
const versionRangeSchema = stringSchema().test('is-version-range', function (value: string | undefined) {
return !isDefined(value) || isDefined(getValidatedRange(value));
});

const cssSelectorSchema = stringSchema().test('is-css-selector', function (value: string | undefined) {
Expand Down Expand Up @@ -122,8 +123,7 @@ const adPlacesRulesSchema = arraySchema()
.required(),
stylesOverrides: arraySchema().of(adStylesOverridesSchema.clone().required()),
shouldHideOriginal: booleanSchema().default(false),
firstExtVersion: versionSchema,
lastExtVersion: versionSchema
extVersion: versionRangeSchema
})
.required()
)
Expand Down Expand Up @@ -163,8 +163,7 @@ const permanentAdPlacesRulesSchema = arraySchema()
elementToMeasureSelector: cssSelectorSchema,
stylesOverrides: arraySchema().of(adStylesOverridesSchema.clone().required()),
shouldHideOriginal: booleanSchema().default(false),
firstExtVersion: versionSchema,
lastExtVersion: versionSchema
extVersion: versionRangeSchema
})
.test('insertion-place-specified', (value: PermanentAdPlacesRule | undefined) => {
if (!value) {
Expand Down Expand Up @@ -195,8 +194,7 @@ const adProvidersByDomainRulesSchema = arraySchema()
.shape({
urlRegexes: arraySchema().of(regexStringSchema.clone().required()).required(),
providers: arraySchema().of(stringSchema().required()).required(),
firstExtVersion: versionSchema,
lastExtVersion: versionSchema
extVersion: versionRangeSchema
})
.required()
)
Expand All @@ -207,8 +205,7 @@ export const adProvidersByDomainsRulesDictionarySchema: IObjectSchema<Record<str

const adProvidersSelectorsRuleSchema = objectSchema().shape({
selectors: cssSelectorsListSchema.clone().required(),
firstExtVersion: versionSchema,
lastExtVersion: versionSchema
extVersion: versionRangeSchema
});

export const adProvidersDictionarySchema: IObjectSchema<Record<string, AdProviderSelectorsRule[]>> =
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4014,6 +4014,13 @@ semver@^7.2.1, semver@^7.3.7, semver@^7.3.8:
dependencies:
lru-cache "^6.0.0"

semver@^7.6.0:
version "7.6.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.0.tgz#1a46a4db4bffcccd97b743b5005c8325f23d4e2d"
integrity sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==
dependencies:
lru-cache "^6.0.0"

[email protected]:
version "0.18.0"
resolved "https://registry.yarnpkg.com/send/-/send-0.18.0.tgz#670167cc654b05f5aa4a767f9113bb371bc706be"
Expand Down

0 comments on commit 16c25e3

Please sign in to comment.