util.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. function addIfNotExist(set, item) {
  10. if (set.has(item)) return true;
  11. set.add(item);
  12. return false;
  13. }
  14. function findChunksWebpack4(chunk) {
  15. var allChunks = new Set();
  16. var groupsVisited = new Set();
  17. (function recurseChunk(childChunk) {
  18. function recurseGroup(group) {
  19. if (addIfNotExist(groupsVisited, group.id)) return;
  20. group.chunks.forEach(recurseChunk);
  21. group.childrenIterable.forEach(recurseGroup);
  22. }
  23. if (addIfNotExist(allChunks, childChunk)) return;
  24. childChunk.groupsIterable.forEach(recurseGroup);
  25. })(chunk);
  26. return allChunks;
  27. }
  28. function findChunksLegacy(chunk) {
  29. var allChunks = new Set();
  30. (function recurseChunk(childChunk) {
  31. if (addIfNotExist(allChunks, childChunk)) return;
  32. childChunk.chunks.forEach(recurseChunk);
  33. })(chunk);
  34. return allChunks;
  35. }
  36. function findChunks(chunk) {
  37. if (chunk.groupsIterable) {
  38. return findChunksWebpack4(chunk);
  39. }
  40. return findChunksLegacy(chunk);
  41. }
  42. function computeIntegrity(hashFuncNames, source) {
  43. return hashFuncNames
  44. .map(function mapHashFuncName(hashFuncName) {
  45. var hash = crypto
  46. .createHash(hashFuncName)
  47. .update(source, "utf8")
  48. .digest("base64");
  49. return hashFuncName + "-" + hash;
  50. })
  51. .join(" ");
  52. }
  53. function getTagSrc(tag) {
  54. // Get asset path - src from scripts and href from links
  55. return tag.attributes.href || tag.attributes.src;
  56. }
  57. function filterTag(tag) {
  58. // Process only script and link tags with a url
  59. return (tag.tagName === "script" || tag.tagName === "link") && getTagSrc(tag);
  60. }
  61. function normalizePath(p) {
  62. return p
  63. .replace(/\?.*$/, "")
  64. .split(path.sep)
  65. .join("/");
  66. }
  67. function getIntegrityChecksumForAsset(assets, src) {
  68. var normalizedSrc;
  69. var normalizedKey;
  70. var asset = assets[src];
  71. if (asset) {
  72. return asset.integrity;
  73. }
  74. normalizedSrc = normalizePath(src);
  75. normalizedKey = Object.keys(assets).find(function test(assetKey) {
  76. return normalizePath(assetKey) === normalizedSrc;
  77. });
  78. if (normalizedKey) {
  79. return assets[normalizedKey].integrity;
  80. }
  81. return null;
  82. }
  83. function isRuntimeChunk(chunk) {
  84. return "hasRuntime" in chunk ? chunk.hasRuntime() : chunk.entry;
  85. }
  86. function makePlaceholder(id) {
  87. return "*-*-*-CHUNK-SRI-HASH-" + id + "-*-*-*";
  88. }
  89. module.exports.computeIntegrity = computeIntegrity;
  90. module.exports.findChunks = findChunks;
  91. module.exports.filterTag = filterTag;
  92. module.exports.getTagSrc = getTagSrc;
  93. module.exports.normalizePath = normalizePath;
  94. module.exports.getIntegrityChecksumForAsset = getIntegrityChecksumForAsset;
  95. module.exports.isRuntimeChunk = isRuntimeChunk;
  96. module.exports.makePlaceholder = makePlaceholder;