diff --git a/src/providers/remoteControl/__tests__/inline.spec.ts b/src/providers/remoteControl/__tests__/inline.spec.ts index d5ef26d..06483e1 100644 --- a/src/providers/remoteControl/__tests__/inline.spec.ts +++ b/src/providers/remoteControl/__tests__/inline.spec.ts @@ -38,6 +38,10 @@ describe('remoteControl / inline', () => { const windowStub = { JSON, + /* eslint-disable no-restricted-globals */ + isFinite, + isNaN, + /* eslint-enable no-restricted-globals */ } as unknown as Window; beforeEach(() => { diff --git a/src/providers/remoteControl/__tests__/remoteControl.spec.ts b/src/providers/remoteControl/__tests__/remoteControl.spec.ts index 5a35e10..c54cd44 100644 --- a/src/providers/remoteControl/__tests__/remoteControl.spec.ts +++ b/src/providers/remoteControl/__tests__/remoteControl.spec.ts @@ -102,10 +102,12 @@ describe('isAllowedOrigin', () => { }); describe('getResourceUrl', () => { + // eslint-disable-next-line no-restricted-globals + const ctx = { isFinite, isNaN } as Window; it('Only allowed langs', () => { ['ru', 'en', 'tr'].forEach((lang) => { chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang, appVersion: '1.2.3', fileId: 'button', @@ -115,7 +117,7 @@ describe('getResourceUrl', () => { ); }); chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'de', appVersion: '1.2.3', fileId: 'button', @@ -126,7 +128,7 @@ describe('getResourceUrl', () => { it('Only allowed ids', () => { ['button', 'form'].forEach((fileId) => { chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: '1.2.3', fileId, @@ -136,7 +138,7 @@ describe('getResourceUrl', () => { ); }); chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: '1.2.3', fileId: '', @@ -146,7 +148,7 @@ describe('getResourceUrl', () => { it('Validate version', () => { chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: '11.22.33', fileId: 'button', @@ -155,7 +157,7 @@ describe('getResourceUrl', () => { 'https://yastatic.net/s3/metrika/11.22.33/form-selector/button_ru.js', ); chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: '1684933', fileId: 'button', @@ -164,31 +166,40 @@ describe('getResourceUrl', () => { 'https://yastatic.net/s3/metrika/1684933/form-selector/button_ru.js', ); chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: 'invalidVer', fileId: 'button', }), ).to.eq('https://yastatic.net/s3/metrika/form-selector/button_ru.js'); chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: '1.a', fileId: 'button', }), ).to.eq('https://yastatic.net/s3/metrika/1/form-selector/button_ru.js'); chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: '/.//.', fileId: 'button', }), ).to.eq('https://yastatic.net/s3/metrika/form-selector/button_ru.js'); + chai.expect( + remoteControl.getResourceUrl(ctx, { + lang: 'ru', + appVersion: '0.0.0', + fileId: 'button', + }), + ).to.eq( + 'https://yastatic.net/s3/metrika/0.0.0/form-selector/button_ru.js', + ); }); it('Beta url', () => { chai.expect( - remoteControl.getResourceUrl({ + remoteControl.getResourceUrl(ctx, { lang: 'ru', appVersion: '1.2.3', fileId: 'button', diff --git a/src/providers/remoteControl/remoteControl.ts b/src/providers/remoteControl/remoteControl.ts index 0c73aad..28ba27d 100644 --- a/src/providers/remoteControl/remoteControl.ts +++ b/src/providers/remoteControl/remoteControl.ts @@ -23,21 +23,16 @@ import { firstArg, ctxBindArgs, call, + curry2, } from 'src/utils/function'; import { getPath } from 'src/utils/object'; import { cEvent } from 'src/utils/events'; import { flags } from '@inject'; -import { - arrayJoin, - cForEach, - cMap, - filterFalsy, - includes, -} from 'src/utils/array'; +import { arrayJoin, cFilter, cForEach, cMap, includes } from 'src/utils/array'; import { closestButton, selectButtons } from 'src/utils/dom/button'; import { closestForm, getFormData, selectForms } from 'src/utils/dom/form'; import { checkStatusFn } from 'src/providers/statusCheck/statusCheckFn'; -import { parseDecimalInt } from 'src/utils/number'; +import { isNumber, parseDecimalInt } from 'src/utils/number'; import { AnyFunc } from 'src/utils/function/types'; /* eslint-disable camelcase */ @@ -164,7 +159,10 @@ const AVAILABLE_FILES = ['form', 'button', 'status']; const BETA_URL = 'https://s3.mds.yandex.net/internal-metrika-betas'; const URL = 'https://yastatic.net/s3/metrika'; -export const getResourceUrl = (message: InlineMessageProps): string => { +export const getResourceUrl = ( + ctx: Window, + message: InlineMessageProps, +): string => { const { ['lang']: lang = '', ['appVersion']: appVersion = '', @@ -173,7 +171,8 @@ export const getResourceUrl = (message: InlineMessageProps): string => { } = message; const validVersion = arrayJoin( SPLITTER, - filterFalsy( + cFilter( + curry2(isNumber)(ctx), cMap(pipe(firstArg, parseDecimalInt), appVersion.split(SPLITTER)), ), ); @@ -255,7 +254,7 @@ export const handleMessage = memo( const args = [ctx, event, message]; cForEach(pipe(ctxBindArgs(args), call), REMOTE_CONTROL_LISTENERS); if (message['inline']) { - const src = getResourceUrl(message); + const src = getResourceUrl(ctx, message); const { id = '' } = message; setupUtilsAndLoadScript(ctx, src, id); } else if ( diff --git a/src/providers/statusCheck/__tests__/statusCheck.spec.ts b/src/providers/statusCheck/__tests__/statusCheck.spec.ts index 55e9428..78ba7c3 100644 --- a/src/providers/statusCheck/__tests__/statusCheck.spec.ts +++ b/src/providers/statusCheck/__tests__/statusCheck.spec.ts @@ -106,7 +106,7 @@ describe('CHECK_STATUS_FEATURE', () => { const [, callback, time] = setDefer.getCall(0).args; chai.expect(time).to.equal(0); callback(); - sinon.assert.calledOnceWithExactly(getResourceUrlStub, { + sinon.assert.calledOnceWithExactly(getResourceUrlStub, ctx, { ['lang']: 'ru', ['fileId']: 'status', }); diff --git a/src/providers/statusCheck/statusCheck.ts b/src/providers/statusCheck/statusCheck.ts index 7df8e25..c43de07 100644 --- a/src/providers/statusCheck/statusCheck.ts +++ b/src/providers/statusCheck/statusCheck.ts @@ -23,7 +23,7 @@ export const checkStatusRaw = (ctx: Window, counterOptions: CounterOptions) => { counterOptions.id === id && counterOptions.counterType === DEFAULT_COUNTER_TYPE ) { - const src = getResourceUrl({ + const src = getResourceUrl(ctx, { ['lang']: langForCheck(ctx), ['fileId']: 'status', });