1 | var crypto = require("crypto");
|
---|
2 | var path = require("path");
|
---|
3 | /**
|
---|
4 | * Copyright (c) 2015-present, Waysact Pty Ltd
|
---|
5 | *
|
---|
6 | * This source code is licensed under the MIT license found in the
|
---|
7 | * LICENSE file in the root directory of this source tree.
|
---|
8 | */
|
---|
9 |
|
---|
10 |
|
---|
11 | function addIfNotExist(set, item) {
|
---|
12 | if (set.has(item)) return true;
|
---|
13 | set.add(item);
|
---|
14 | return false;
|
---|
15 | }
|
---|
16 |
|
---|
17 | function findChunksWebpack4(chunk) {
|
---|
18 | var allChunks = new Set();
|
---|
19 | var groupsVisited = new Set();
|
---|
20 |
|
---|
21 | (function recurseChunk(childChunk) {
|
---|
22 | function recurseGroup(group) {
|
---|
23 | if (addIfNotExist(groupsVisited, group.id)) return;
|
---|
24 | group.chunks.forEach(recurseChunk);
|
---|
25 | group.childrenIterable.forEach(recurseGroup);
|
---|
26 | }
|
---|
27 |
|
---|
28 | if (addIfNotExist(allChunks, childChunk)) return;
|
---|
29 | childChunk.groupsIterable.forEach(recurseGroup);
|
---|
30 | })(chunk);
|
---|
31 |
|
---|
32 | return allChunks;
|
---|
33 | }
|
---|
34 |
|
---|
35 | function findChunksLegacy(chunk) {
|
---|
36 | var allChunks = new Set();
|
---|
37 | (function recurseChunk(childChunk) {
|
---|
38 | if (addIfNotExist(allChunks, childChunk)) return;
|
---|
39 | childChunk.chunks.forEach(recurseChunk);
|
---|
40 | })(chunk);
|
---|
41 | return allChunks;
|
---|
42 | }
|
---|
43 |
|
---|
44 | function findChunks(chunk) {
|
---|
45 | if (chunk.groupsIterable) {
|
---|
46 | return findChunksWebpack4(chunk);
|
---|
47 | }
|
---|
48 | return findChunksLegacy(chunk);
|
---|
49 | }
|
---|
50 |
|
---|
51 | function computeIntegrity(hashFuncNames, source) {
|
---|
52 | return hashFuncNames
|
---|
53 | .map(function mapHashFuncName(hashFuncName) {
|
---|
54 | var hash = crypto
|
---|
55 | .createHash(hashFuncName)
|
---|
56 | .update(source, "utf8")
|
---|
57 | .digest("base64");
|
---|
58 | return hashFuncName + "-" + hash;
|
---|
59 | })
|
---|
60 | .join(" ");
|
---|
61 | }
|
---|
62 |
|
---|
63 | function getTagSrc(tag) {
|
---|
64 | // Get asset path - src from scripts and href from links
|
---|
65 | return tag.attributes.href || tag.attributes.src;
|
---|
66 | }
|
---|
67 |
|
---|
68 | function filterTag(tag) {
|
---|
69 | // Process only script and link tags with a url
|
---|
70 | return (
|
---|
71 | (tag.tagName === "script" || tag.tagName === "link") &&
|
---|
72 | tag.attributes &&
|
---|
73 | getTagSrc(tag)
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | function normalizePath(p) {
|
---|
78 | return p
|
---|
79 | .replace(/\?.*$/, "")
|
---|
80 | .split(path.sep)
|
---|
81 | .join("/");
|
---|
82 | }
|
---|
83 |
|
---|
84 | function getIntegrityChecksumForAsset(assets, src) {
|
---|
85 | var normalizedSrc;
|
---|
86 | var normalizedKey;
|
---|
87 | var asset = assets[src];
|
---|
88 | if (asset) {
|
---|
89 | return asset.integrity;
|
---|
90 | }
|
---|
91 | normalizedSrc = normalizePath(src);
|
---|
92 | normalizedKey = Object.keys(assets).find(function test(assetKey) {
|
---|
93 | return normalizePath(assetKey) === normalizedSrc;
|
---|
94 | });
|
---|
95 | if (normalizedKey) {
|
---|
96 | return assets[normalizedKey].integrity;
|
---|
97 | }
|
---|
98 | return null;
|
---|
99 | }
|
---|
100 |
|
---|
101 | function isRuntimeChunk(chunk) {
|
---|
102 | return "hasRuntime" in chunk ? chunk.hasRuntime() : chunk.entry;
|
---|
103 | }
|
---|
104 |
|
---|
105 | function makePlaceholder(hashFuncNames, id) {
|
---|
106 | var placeholder = "*-*-*-CHUNK-SRI-HASH-" + id + "-*-*-*";
|
---|
107 | return computeIntegrity(hashFuncNames, placeholder);
|
---|
108 | }
|
---|
109 |
|
---|
110 | module.exports.computeIntegrity = computeIntegrity;
|
---|
111 | module.exports.findChunks = findChunks;
|
---|
112 | module.exports.filterTag = filterTag;
|
---|
113 | module.exports.getTagSrc = getTagSrc;
|
---|
114 | module.exports.normalizePath = normalizePath;
|
---|
115 | module.exports.getIntegrityChecksumForAsset = getIntegrityChecksumForAsset;
|
---|
116 | module.exports.isRuntimeChunk = isRuntimeChunk;
|
---|
117 | module.exports.makePlaceholder = makePlaceholder;
|
---|