| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 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 | import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
|
|---|
| 9 | import { logger } from 'workbox-core/_private/logger.js';
|
|---|
| 10 | import './_version.js';
|
|---|
| 11 | /**
|
|---|
| 12 | * Given two `Response's`, compares several header values to see if they are
|
|---|
| 13 | * the same or not.
|
|---|
| 14 | *
|
|---|
| 15 | * @param {Response} firstResponse
|
|---|
| 16 | * @param {Response} secondResponse
|
|---|
| 17 | * @param {Array<string>} headersToCheck
|
|---|
| 18 | * @return {boolean}
|
|---|
| 19 | *
|
|---|
| 20 | * @memberof workbox-broadcast-update
|
|---|
| 21 | */
|
|---|
| 22 | const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => {
|
|---|
| 23 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 24 | if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
|
|---|
| 25 | throw new WorkboxError('invalid-responses-are-same-args');
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 | const atLeastOneHeaderAvailable = headersToCheck.some((header) => {
|
|---|
| 29 | return (firstResponse.headers.has(header) && secondResponse.headers.has(header));
|
|---|
| 30 | });
|
|---|
| 31 | if (!atLeastOneHeaderAvailable) {
|
|---|
| 32 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 33 | logger.warn(`Unable to determine where the response has been updated ` +
|
|---|
| 34 | `because none of the headers that would be checked are present.`);
|
|---|
| 35 | logger.debug(`Attempting to compare the following: `, firstResponse, secondResponse, headersToCheck);
|
|---|
| 36 | }
|
|---|
| 37 | // Just return true, indicating the that responses are the same, since we
|
|---|
| 38 | // can't determine otherwise.
|
|---|
| 39 | return true;
|
|---|
| 40 | }
|
|---|
| 41 | return headersToCheck.every((header) => {
|
|---|
| 42 | const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header);
|
|---|
| 43 | const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header);
|
|---|
| 44 | return headerStateComparison && headerValueComparison;
|
|---|
| 45 | });
|
|---|
| 46 | };
|
|---|
| 47 | export { responsesAreSame };
|
|---|