Skip to content

Commit

Permalink
Merge pull request #77 from AnnaKudriasheva/add-sri-support
Browse files Browse the repository at this point in the history
Add integrity and crossorigin attributes support
  • Loading branch information
flor-master authored Apr 25, 2024
2 parents aadc3a8 + aa1c4d3 commit adab3a6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ const loadVGSCollect = (config: IConfig = isRequired('config')) => {
vaultId = isRequired('vaultId'),
environment = DEFAULT_CONFIG.environment,
version = DEFAULT_CONFIG.version,
integrity,
crossorigin,
} = config;

if (version === 'canary') {
console.warn(ERROR_MESSAGE.CHANGE_VERSION);
}

registerScriptLoading({ vaultId, environment, version });
registerScriptLoading({
vaultId,
environment,
version,
integrity,
crossorigin,
});

return new Promise((resolve, reject) => {
if (typeof window === 'undefined') {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/IConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export interface IConfig {
vaultId: string;
environment: string;
version: string;
[index: string]: string;
integrity?: string;
crossorigin?: 'use-credentials' | 'anonymous' | '';
}
11 changes: 10 additions & 1 deletion src/utils/loadScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ const scriptExists = () => {
};

const appendScript = (): HTMLScriptElement => {
const { vaultId, environment, version } = getConfig();
const { vaultId, environment, version, integrity, crossorigin } = getConfig();
const script = document.createElement('script');

script.src = `${scriptURL}/vgs-collect/${version}/vgs-collect.js?sessionId=${SESSION_ID}&tenantId=${vaultId}&env=${environment}`;

if (integrity) {
script.integrity = integrity;
}

if (typeof crossorigin === 'string') {
script.crossOrigin = crossorigin;
}

appendElement(script);

return script;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const configSchema: IConfigSchema = {
(typeof value === 'string' &&
/^\d{1,2}\.\d{1,2}(\.\d{1,2})?$/.test(value) &&
!value.startsWith('1.')),
integrity: value => (value ? typeof value === 'string' : true),
crossorigin: value => (value ? typeof value === 'string' : true),
};

const isRequired = (param: string) => {
Expand Down
17 changes: 17 additions & 0 deletions test/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ describe('loadVGSCollect', () => {
);
expect(document.querySelectorAll(SCRIPT_URL).length).toBe(1);
});

test('script wtih integrity and crossorigin attributes', () => {
const { loadVGSCollect } = require('../src/index');
loadVGSCollect({
vaultId: 'tnt12345352',
integrity:
'sha384-STGHtboAf18kBhVVUccsY3AN7RAXJdfyfAEZrhlGkYnKdPxsKIyI8ajYAom0X2zP',
crossorigin: 'anonymous',
});
const scriptElement = document.querySelector(
SCRIPT_URL
) as HTMLScriptElement;
expect(scriptElement.crossOrigin).toBe('anonymous');
expect(scriptElement.integrity).toBe(
'sha384-STGHtboAf18kBhVVUccsY3AN7RAXJdfyfAEZrhlGkYnKdPxsKIyI8ajYAom0X2zP'
);
});
});

describe('check if script exists', () => {
Expand Down

0 comments on commit adab3a6

Please sign in to comment.