Skip to content

Commit adab3a6

Browse files
authored
Merge pull request #77 from AnnaKudriasheva/add-sri-support
Add integrity and crossorigin attributes support
2 parents aadc3a8 + aa1c4d3 commit adab3a6

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ const loadVGSCollect = (config: IConfig = isRequired('config')) => {
2424
vaultId = isRequired('vaultId'),
2525
environment = DEFAULT_CONFIG.environment,
2626
version = DEFAULT_CONFIG.version,
27+
integrity,
28+
crossorigin,
2729
} = config;
2830

2931
if (version === 'canary') {
3032
console.warn(ERROR_MESSAGE.CHANGE_VERSION);
3133
}
3234

33-
registerScriptLoading({ vaultId, environment, version });
35+
registerScriptLoading({
36+
vaultId,
37+
environment,
38+
version,
39+
integrity,
40+
crossorigin,
41+
});
3442

3543
return new Promise((resolve, reject) => {
3644
if (typeof window === 'undefined') {

src/utils/IConfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export interface IConfig {
22
vaultId: string;
33
environment: string;
44
version: string;
5-
[index: string]: string;
5+
integrity?: string;
6+
crossorigin?: 'use-credentials' | 'anonymous' | '';
67
}

src/utils/loadScript.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ const scriptExists = () => {
2424
};
2525

2626
const appendScript = (): HTMLScriptElement => {
27-
const { vaultId, environment, version } = getConfig();
27+
const { vaultId, environment, version, integrity, crossorigin } = getConfig();
2828
const script = document.createElement('script');
2929

3030
script.src = `${scriptURL}/vgs-collect/${version}/vgs-collect.js?sessionId=${SESSION_ID}&tenantId=${vaultId}&env=${environment}`;
31+
32+
if (integrity) {
33+
script.integrity = integrity;
34+
}
35+
36+
if (typeof crossorigin === 'string') {
37+
script.crossOrigin = crossorigin;
38+
}
39+
3140
appendElement(script);
3241

3342
return script;

src/utils/validation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const configSchema: IConfigSchema = {
1111
(typeof value === 'string' &&
1212
/^\d{1,2}\.\d{1,2}(\.\d{1,2})?$/.test(value) &&
1313
!value.startsWith('1.')),
14+
integrity: value => (value ? typeof value === 'string' : true),
15+
crossorigin: value => (value ? typeof value === 'string' : true),
1416
};
1517

1618
const isRequired = (param: string) => {

test/load.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ describe('loadVGSCollect', () => {
3939
);
4040
expect(document.querySelectorAll(SCRIPT_URL).length).toBe(1);
4141
});
42+
43+
test('script wtih integrity and crossorigin attributes', () => {
44+
const { loadVGSCollect } = require('../src/index');
45+
loadVGSCollect({
46+
vaultId: 'tnt12345352',
47+
integrity:
48+
'sha384-STGHtboAf18kBhVVUccsY3AN7RAXJdfyfAEZrhlGkYnKdPxsKIyI8ajYAom0X2zP',
49+
crossorigin: 'anonymous',
50+
});
51+
const scriptElement = document.querySelector(
52+
SCRIPT_URL
53+
) as HTMLScriptElement;
54+
expect(scriptElement.crossOrigin).toBe('anonymous');
55+
expect(scriptElement.integrity).toBe(
56+
'sha384-STGHtboAf18kBhVVUccsY3AN7RAXJdfyfAEZrhlGkYnKdPxsKIyI8ajYAom0X2zP'
57+
);
58+
});
4259
});
4360

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

0 commit comments

Comments
 (0)