transform-manifest.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. /*
  3. Copyright 2018 Google LLC
  4. Use of this source code is governed by an MIT-style
  5. license that can be found in the LICENSE file or at
  6. https://opensource.org/licenses/MIT.
  7. */
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.transformManifest = void 0;
  10. const additional_manifest_entries_transform_1 = require("./additional-manifest-entries-transform");
  11. const errors_1 = require("./errors");
  12. const maximum_size_transform_1 = require("./maximum-size-transform");
  13. const modify_url_prefix_transform_1 = require("./modify-url-prefix-transform");
  14. const no_revision_for_urls_matching_transform_1 = require("./no-revision-for-urls-matching-transform");
  15. async function transformManifest({ additionalManifestEntries, dontCacheBustURLsMatching, fileDetails, manifestTransforms, maximumFileSizeToCacheInBytes, modifyURLPrefix, transformParam, }) {
  16. const allWarnings = [];
  17. // Take the array of fileDetail objects and convert it into an array of
  18. // {url, revision, size} objects, with \ replaced with /.
  19. const normalizedManifest = fileDetails.map((fileDetails) => {
  20. return {
  21. url: fileDetails.file.replace(/\\/g, '/'),
  22. revision: fileDetails.hash,
  23. size: fileDetails.size,
  24. };
  25. });
  26. const transformsToApply = [];
  27. if (maximumFileSizeToCacheInBytes) {
  28. transformsToApply.push((0, maximum_size_transform_1.maximumSizeTransform)(maximumFileSizeToCacheInBytes));
  29. }
  30. if (modifyURLPrefix) {
  31. transformsToApply.push((0, modify_url_prefix_transform_1.modifyURLPrefixTransform)(modifyURLPrefix));
  32. }
  33. if (dontCacheBustURLsMatching) {
  34. transformsToApply.push((0, no_revision_for_urls_matching_transform_1.noRevisionForURLsMatchingTransform)(dontCacheBustURLsMatching));
  35. }
  36. // Run any manifestTransforms functions second-to-last.
  37. if (manifestTransforms) {
  38. transformsToApply.push(...manifestTransforms);
  39. }
  40. // Run additionalManifestEntriesTransform last.
  41. if (additionalManifestEntries) {
  42. transformsToApply.push((0, additional_manifest_entries_transform_1.additionalManifestEntriesTransform)(additionalManifestEntries));
  43. }
  44. let transformedManifest = normalizedManifest;
  45. for (const transform of transformsToApply) {
  46. const result = await transform(transformedManifest, transformParam);
  47. if (!('manifest' in result)) {
  48. throw new Error(errors_1.errors['bad-manifest-transforms-return-value']);
  49. }
  50. transformedManifest = result.manifest;
  51. allWarnings.push(...(result.warnings || []));
  52. }
  53. // Generate some metadata about the manifest before we clear out the size
  54. // properties from each entry.
  55. const count = transformedManifest.length;
  56. let size = 0;
  57. for (const manifestEntry of transformedManifest) {
  58. size += manifestEntry.size || 0;
  59. delete manifestEntry.size;
  60. }
  61. return {
  62. count,
  63. size,
  64. manifestEntries: transformedManifest,
  65. warnings: allWarnings,
  66. };
  67. }
  68. exports.transformManifest = transformManifest;