source: trip-planner-front/node_modules/webpack-subresource-integrity/util.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1var crypto = require("crypto");
2var 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
11function addIfNotExist(set, item) {
12 if (set.has(item)) return true;
13 set.add(item);
14 return false;
15}
16
17function 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
35function 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
44function findChunks(chunk) {
45 if (chunk.groupsIterable) {
46 return findChunksWebpack4(chunk);
47 }
48 return findChunksLegacy(chunk);
49}
50
51function 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
63function getTagSrc(tag) {
64 // Get asset path - src from scripts and href from links
65 return tag.attributes.href || tag.attributes.src;
66}
67
68function 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
77function normalizePath(p) {
78 return p
79 .replace(/\?.*$/, "")
80 .split(path.sep)
81 .join("/");
82}
83
84function 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
101function isRuntimeChunk(chunk) {
102 return "hasRuntime" in chunk ? chunk.hasRuntime() : chunk.entry;
103}
104
105function makePlaceholder(hashFuncNames, id) {
106 var placeholder = "*-*-*-CHUNK-SRI-HASH-" + id + "-*-*-*";
107 return computeIntegrity(hashFuncNames, placeholder);
108}
109
110module.exports.computeIntegrity = computeIntegrity;
111module.exports.findChunks = findChunks;
112module.exports.filterTag = filterTag;
113module.exports.getTagSrc = getTagSrc;
114module.exports.normalizePath = normalizePath;
115module.exports.getIntegrityChecksumForAsset = getIntegrityChecksumForAsset;
116module.exports.isRuntimeChunk = isRuntimeChunk;
117module.exports.makePlaceholder = makePlaceholder;
Note: See TracBrowser for help on using the repository browser.