UNPKG

15.2 kBSource Map (JSON)View Raw
1{"version":3,"names":[],"mappings":"","sources":["packages/workbox-broadcast-cache-update/browser.mjs"],"sourcesContent":["this.workbox = this.workbox || {};\nthis.workbox.broadcastUpdate = (function (exports,WorkboxError_mjs,logger_mjs,assert_mjs) {\n 'use strict';\n\n try {\n self.workbox.v['workbox:broadcast-cache-update:3.6.3'] = 1;\n } catch (e) {} // eslint-disable-line\n\n /*\n Copyright 2016 Google Inc. All Rights Reserved.\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /**\n * Given two `Response's`, compares several header values to see if they are\n * the same or not.\n *\n * @param {Response} firstResponse\n * @param {Response} secondResponse\n * @param {Array<string>} headersToCheck\n * @return {boolean}\n *\n * @memberof workbox.broadcastUpdate\n * @private\n */\n const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => {\n {\n if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {\n throw new WorkboxError_mjs.WorkboxError('invalid-responses-are-same-args');\n }\n }\n\n const atLeastOneHeaderAvailable = headersToCheck.some(header => {\n return firstResponse.headers.has(header) && secondResponse.headers.has(header);\n });\n\n if (!atLeastOneHeaderAvailable) {\n {\n logger_mjs.logger.warn(`Unable to determine where the response has been updated ` + `because none of the headers that would be checked are present.`);\n logger_mjs.logger.debug(`Attempting to compare the following: `, firstResponse, secondResponse, headersToCheck);\n }\n\n // Just return true, indicating the that responses are the same, since we\n // can't determine otherwise.\n return true;\n }\n\n return headersToCheck.every(header => {\n const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header);\n const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header);\n\n return headerStateComparison && headerValueComparison;\n });\n };\n\n /*\n Copyright 2016 Google Inc. All Rights Reserved.\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n var messageTypes = {\n CACHE_UPDATED: 'CACHE_UPDATED'\n };\n\n /*\n Copyright 2016 Google Inc. All Rights Reserved.\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /**\n * You would not normally call this method directly; it's called automatically\n * by an instance of the {@link BroadcastCacheUpdate} class. It's exposed here\n * for the benefit of developers who would rather not use the full\n * `BroadcastCacheUpdate` implementation.\n *\n * Calling this will dispatch a message on the provided\n * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel}\n * to notify interested subscribers about a change to a cached resource.\n *\n * The message that's posted has a formation inspired by the\n * [Flux standard action](https://github.com/acdlite/flux-standard-action#introduction)\n * format like so:\n *\n * ```\n * {\n * type: 'CACHE_UPDATED',\n * meta: 'workbox-broadcast-cache-update',\n * payload: {\n * cacheName: 'the-cache-name',\n * updatedUrl: 'https://example.com/'\n * }\n * }\n * ```\n *\n * (Usage of [Flux](https://facebook.github.io/flux/) itself is not at\n * all required.)\n *\n * @param {BroadcastChannel} channel The `BroadcastChannel` to use.\n * @param {string} cacheName The name of the cache in which the updated\n * `Response` was stored.\n * @param {string} url The URL associated with the updated `Response`.\n * @param {string} source A string identifying this library as the source\n * of the update message.\n *\n * @memberof workbox.broadcastUpdate\n */\n const broadcastUpdate = (channel, cacheName, url, source) => {\n // There are browsers which support service workers but don't support the\n // Broadcast Channel API.\n // See https://github.com/GoogleChrome/workbox/issues/1304\n if (!('BroadcastChannel' in self && channel)) {\n {\n logger_mjs.logger.debug(`${url} was updated, but the Broadcast Channel API is not ` + `available in the current browser.`);\n }\n return;\n }\n\n {\n assert_mjs.assert.isInstance(channel, BroadcastChannel, {\n moduleName: 'workbox-broadcast-cache-update',\n className: '~',\n funcName: 'broadcastUpdate',\n paramName: 'channel'\n });\n assert_mjs.assert.isType(cacheName, 'string', {\n moduleName: 'workbox-broadcast-cache-update',\n className: '~',\n funcName: 'broadcastUpdate',\n paramName: 'cacheName'\n });\n assert_mjs.assert.isType(url, 'string', {\n moduleName: 'workbox-broadcast-cache-update',\n className: '~',\n funcName: 'broadcastUpdate',\n paramName: 'url'\n });\n assert_mjs.assert.isType(source, 'string', {\n moduleName: 'workbox-broadcast-cache-update',\n className: '~',\n funcName: 'broadcastUpdate',\n paramName: 'source'\n });\n }\n\n channel.postMessage({\n type: messageTypes.CACHE_UPDATED,\n meta: source,\n payload: {\n cacheName: cacheName,\n updatedUrl: url\n }\n });\n };\n\n /*\n Copyright 2016 Google Inc. All Rights Reserved.\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /**\n * Uses the [Broadcast Channel API]{@link https://developers.google.com/web/updates/2016/09/broadcastchannel}\n * to notify interested parties when a cached response has been updated.\n *\n * For efficiency's sake, the underlying response bodies are not compared;\n * only specific response headers are checked.\n *\n * @memberof workbox.broadcastUpdate\n */\n class BroadcastCacheUpdate {\n /**\n * Construct a BroadcastCacheUpdate instance with a specific `channelName` to\n * broadcast messages on\n *\n * @param {string} channelName The name that will be used when creating\n * the `BroadcastChannel`.\n * @param {Object} options\n * @param {Array<string>}\n * [options.headersToCheck=['content-length', 'etag', 'last-modified']] A\n * list of headers that will be used to determine whether the responses\n * differ.\n * @param {string} [options.source='workbox-broadcast-cache-update'] An\n * attribution value that indicates where the update originated.\n */\n constructor(channelName, { headersToCheck, source } = {}) {\n {\n if (typeof channelName !== 'string' || channelName.length === 0) {\n throw new WorkboxError_mjs.WorkboxError('channel-name-required');\n }\n }\n\n this._channelName = channelName;\n this._headersToCheck = headersToCheck || ['content-length', 'etag', 'last-modified'];\n this._source = source || 'workbox-broadcast-cache-update';\n\n // TODO assert typeof headersToCheck instanceof Array\n }\n\n /**\n * @return {BroadcastChannel|undefined} The BroadcastChannel instance used for\n * broadcasting updates, or undefined if the browser doesn't support the\n * Broadcast Channel API.\n *\n * @private\n */\n _getChannel() {\n if ('BroadcastChannel' in self && !this._channel) {\n this._channel = new BroadcastChannel(this._channelName);\n }\n return this._channel;\n }\n\n /**\n * Compare two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)\n * and send a message via the\n * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel API}\n * if they differ.\n *\n * Neither of the Responses can be {@link http://stackoverflow.com/questions/39109789|opaque}.\n *\n * @param {Response} firstResponse First responses to compare.\n * @param {Response} secondResponse Second responses to compare.\n * @param {string} url The URL of the updated request.\n * @param {string} cacheName Name of the cache the responses belong to.\n * This is included in the message posted on the broadcast channel.\n */\n notifyIfUpdated(firstResponse, secondResponse, url, cacheName) {\n if (!responsesAreSame(firstResponse, secondResponse, this._headersToCheck)) {\n broadcastUpdate(this._getChannel(), cacheName, url, this._source);\n }\n }\n }\n\n /*\n Copyright 2016 Google Inc. All Rights Reserved.\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /**\n * This plugin will automatically broadcast a message whenever a cached response\n * is updated.\n *\n * @memberof workbox.broadcastUpdate\n */\n class Plugin {\n /**\n * Construct a new instance with a specific `channelName` to\n * broadcast messages on\n *\n * @param {string} channelName The name that will be used when creating\n * the `BroadcastChannel`.\n * @param {Object} options\n * @param {Array<string>}\n * [options.headersToCheck=['content-length', 'etag', 'last-modified']] A\n * list of headers that will be used to determine whether the responses\n * differ.\n * @param {string} [options.source='workbox-broadcast-cache-update'] An\n * attribution value that indicates where the update originated.\n */\n constructor(channelName, options) {\n this._broadcastUpdate = new BroadcastCacheUpdate(channelName, options);\n }\n /**\n * A \"lifecycle\" callback that will be triggered automatically by the\n * `workbox-sw` and `workbox-runtime-caching` handlers when an entry is\n * added to a cache.\n *\n * @private\n * @param {Object} options The input object to this function.\n * @param {string} options.cacheName Name of the cache being updated.\n * @param {Response} [options.oldResponse] The previous cached value, if any.\n * @param {Response} options.newResponse The new value in the cache.\n */\n cacheDidUpdate({ cacheName, oldResponse, newResponse, request }) {\n {\n assert_mjs.assert.isType(cacheName, 'string', {\n moduleName: 'workbox-broadcast-cache-update',\n className: 'Plugin',\n funcName: 'cacheDidUpdate',\n paramName: 'cacheName'\n });\n assert_mjs.assert.isInstance(newResponse, Response, {\n moduleName: 'workbox-broadcast-cache-update',\n className: 'Plugin',\n funcName: 'cacheDidUpdate',\n paramName: 'newResponse'\n });\n assert_mjs.assert.isInstance(request, Request, {\n moduleName: 'workbox-broadcast-cache-update',\n className: 'Plugin',\n funcName: 'cacheDidUpdate',\n paramName: 'request'\n });\n }\n\n if (!oldResponse) {\n // Without a two responses there is nothing to compare.\n return;\n }\n\n this._broadcastUpdate.notifyIfUpdated(oldResponse, newResponse, request.url, cacheName);\n }\n }\n\n /*\n Copyright 2017 Google Inc.\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n /*\n Copyright 2017 Google Inc.\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n */\n\n exports.BroadcastCacheUpdate = BroadcastCacheUpdate;\n exports.Plugin = Plugin;\n exports.broadcastUpdate = broadcastUpdate;\n exports.messageTypes = messageTypes;\n\n return exports;\n\n}({},workbox.core._private,workbox.core._private,workbox.core._private));\n"],"file":"workbox-broadcast-cache-update.dev.js"}
\No newline at end of file