1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | exports.SRI_HASH_ALGORITHMS = ['sha256', 'sha384', 'sha512'];
|
8 |
|
9 | function calculateBase64IntegrityFromFilename(path, hashFunction, hashDigest) {
|
10 | if (!isHashAlgorithmSupportedByBrowsers(hashFunction)) {
|
11 | return false;
|
12 | }
|
13 |
|
14 | if (hashDigest && hashDigest !== 'hex') {
|
15 | return false;
|
16 | }
|
17 |
|
18 | const chunkInfo =
|
19 | path.match(/.+?-(?:([a-f0-9]+))?\.[^.]+$/) ||
|
20 | path.match(/^([a-f0-9]+)\.[^.]+$/);
|
21 |
|
22 | if (!chunkInfo || !chunkInfo[1]) {
|
23 | return false;
|
24 | }
|
25 |
|
26 | const hexHash = chunkInfo[1];
|
27 | const base64Hash = Buffer.from(hexHash, 'hex').toString('base64');
|
28 | return `${hashFunction}-${base64Hash}`;
|
29 | }
|
30 |
|
31 | exports.calculateBase64IntegrityFromFilename = calculateBase64IntegrityFromFilename;
|
32 |
|
33 | function isHashAlgorithmSupportedByBrowsers(hashFunction) {
|
34 | return exports.SRI_HASH_ALGORITHMS.includes(hashFunction);
|
35 | } |
\ | No newline at end of file |