Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

69 wiersze
2.3 KiB

  1. /*
  2. Copyright 2016 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
  14. import {logger} from 'workbox-core/_private/logger.mjs';
  15. import '../_version.mjs';
  16. /**
  17. * Given two `Response's`, compares several header values to see if they are
  18. * the same or not.
  19. *
  20. * @param {Response} firstResponse
  21. * @param {Response} secondResponse
  22. * @param {Array<string>} headersToCheck
  23. * @return {boolean}
  24. *
  25. * @memberof workbox.broadcastUpdate
  26. * @private
  27. */
  28. const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => {
  29. if (process.env.NODE_ENV !== 'production') {
  30. if (!(firstResponse instanceof Response &&
  31. secondResponse instanceof Response)) {
  32. throw new WorkboxError('invalid-responses-are-same-args');
  33. }
  34. }
  35. const atLeastOneHeaderAvailable = headersToCheck.some((header) => {
  36. return firstResponse.headers.has(header) &&
  37. secondResponse.headers.has(header);
  38. });
  39. if (!atLeastOneHeaderAvailable) {
  40. if (process.env.NODE_ENV !== 'production') {
  41. logger.warn(`Unable to determine where the response has been updated ` +
  42. `because none of the headers that would be checked are present.`);
  43. logger.debug(`Attempting to compare the following: `,
  44. firstResponse, secondResponse, headersToCheck);
  45. }
  46. // Just return true, indicating the that responses are the same, since we
  47. // can't determine otherwise.
  48. return true;
  49. }
  50. return headersToCheck.every((header) => {
  51. const headerStateComparison = firstResponse.headers.has(header) ===
  52. secondResponse.headers.has(header);
  53. const headerValueComparison = firstResponse.headers.get(header) ===
  54. secondResponse.headers.get(header);
  55. return headerStateComparison && headerValueComparison;
  56. });
  57. };
  58. export {responsesAreSame};