-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (34 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var createHash = require('crypto').createHash,
minimatch = require('minimatch');
function createSubresourceIntegrity({
pattern = '*.{css,js}',
algorithm = 'sha512',
minimatchOptions = {matchBase: true}
} = {}) {
switch (algorithm) {
case 'sha512':
case 'sha384':
case 'sha256':
break;
default:
throw new Error('algorithm: ' + algorithm +
' is invalid. Should be sha256, sha384 or sha512');
}
if (!pattern) {
throw new Error('pattern is empty');
}
if (minimatchOptions !== Object(minimatchOptions)) {
throw new Error('minimatchOptions should be an Object');
}
return function subresourceIntegrity(files, ms, done) {
ms._metadata.subresource = Object.keys(files)
.filter(minimatch.filter(pattern, minimatchOptions))
.reduce((subresource, name) => {
subresource[name] = algorithm + '-' +
createHash(algorithm).update(files[name].contents).digest('base64');
return subresource;
}, {});
done();
};
};
module.exports = createSubresourceIntegrity;