summaryrefslogtreecommitdiff
path: root/dist/workbox-v3.2.0/workbox-broadcast-cache-update.dev.js
blob: ae1fec1871af0d41b8b719ba59b43c6e2404aab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
this.workbox = this.workbox || {};
this.workbox.broadcastUpdate = (function (exports,WorkboxError_mjs,logger_mjs,assert_mjs) {
'use strict';

try {
  self.workbox.v['workbox:broadcast-cache-update:3.2.0'] = 1;
} catch (e) {} // eslint-disable-line

/*
 Copyright 2016 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/**
 * Given two `Response's`, compares several header values to see if they are
 * the same or not.
 *
 * @param {Response} firstResponse
 * @param {Response} secondResponse
 * @param {Array<string>} headersToCheck
 * @return {boolean}
 *
 * @memberof workbox.broadcastUpdate
 * @private
 */
const responsesAreSame = (firstResponse, secondResponse, headersToCheck) => {
  {
    if (!(firstResponse instanceof Response && secondResponse instanceof Response)) {
      throw new WorkboxError_mjs.WorkboxError('invalid-responses-are-same-args');
    }
  }

  const atLeastOneHeaderAvailable = headersToCheck.some(header => {
    return firstResponse.headers.has(header) && secondResponse.headers.has(header);
  });

  if (!atLeastOneHeaderAvailable) {
    {
      logger_mjs.logger.warn(`Unable to determine where the response has been updated ` + `because none of the headers that would be checked are present.`);
      logger_mjs.logger.debug(`Attempting to compare the following: `, firstResponse, secondResponse, headersToCheck);
    }

    // Just return true, indicating the that responses are the same, since we
    // can't determine otherwise.
    return true;
  }

  return headersToCheck.every(header => {
    const headerStateComparison = firstResponse.headers.has(header) === secondResponse.headers.has(header);
    const headerValueComparison = firstResponse.headers.get(header) === secondResponse.headers.get(header);

    return headerStateComparison && headerValueComparison;
  });
};

/*
 Copyright 2016 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

var messageTypes = {
  CACHE_UPDATED: 'CACHE_UPDATED'
};

/*
 Copyright 2016 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/**
 * You would not normally call this method directly; it's called automatically
 * by an instance of the {@link BroadcastCacheUpdate} class. It's exposed here
 * for the benefit of developers who would rather not use the full
 * `BroadcastCacheUpdate` implementation.
 *
 * Calling this will dispatch a message on the provided
 * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel}
 * to notify interested subscribers about a change to a cached resource.
 *
 * The message that's posted has a formation inspired by the
 * [Flux standard action](https://github.com/acdlite/flux-standard-action#introduction)
 * format like so:
 *
 * ```
 * {
 *   type: 'CACHE_UPDATED',
 *   meta: 'workbox-broadcast-cache-update',
 *   payload: {
 *     cacheName: 'the-cache-name',
 *     updatedUrl: 'https://example.com/'
 *   }
 * }
 * ```
 *
 * (Usage of [Flux](https://facebook.github.io/flux/) itself is not at
 * all required.)
 *
 * @param {BroadcastChannel} channel The `BroadcastChannel` to use.
 * @param {string} cacheName The name of the cache in which the updated
 *        `Response` was stored.
 * @param {string} url The URL associated with the updated `Response`.
 * @param {string} source A string identifying this library as the source
 *        of the update message.
 *
 * @memberof workbox.broadcastUpdate
 */
const broadcastUpdate = (channel, cacheName, url, source) => {
  // There are browsers which support service workers but don't support the
  // Broadcast Channel API.
  // See https://github.com/GoogleChrome/workbox/issues/1304
  if (!('BroadcastChannel' in self && channel)) {
    {
      logger_mjs.logger.debug(`${url} was updated, but the Broadcast Channel API is not ` + `available in the current browser.`);
    }
    return;
  }

  {
    assert_mjs.assert.isInstance(channel, BroadcastChannel, {
      moduleName: 'workbox-broadcast-cache-update',
      className: '~',
      funcName: 'broadcastUpdate',
      paramName: 'channel'
    });
    assert_mjs.assert.isType(cacheName, 'string', {
      moduleName: 'workbox-broadcast-cache-update',
      className: '~',
      funcName: 'broadcastUpdate',
      paramName: 'cacheName'
    });
    assert_mjs.assert.isType(url, 'string', {
      moduleName: 'workbox-broadcast-cache-update',
      className: '~',
      funcName: 'broadcastUpdate',
      paramName: 'url'
    });
    assert_mjs.assert.isType(source, 'string', {
      moduleName: 'workbox-broadcast-cache-update',
      className: '~',
      funcName: 'broadcastUpdate',
      paramName: 'source'
    });
  }

  channel.postMessage({
    type: messageTypes.CACHE_UPDATED,
    meta: source,
    payload: {
      cacheName: cacheName,
      updatedUrl: url
    }
  });
};

/*
 Copyright 2016 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/**
 * Uses the [Broadcast Channel API]{@link https://developers.google.com/web/updates/2016/09/broadcastchannel}
 * to notify interested parties when a cached response has been updated.
 *
 * For efficiency's sake, the underlying response bodies are not compared;
 * only specific response headers are checked.
 *
 * @memberof workbox.broadcastUpdate
 */
class BroadcastCacheUpdate {
  /**
   * Construct a BroadcastCacheUpdate instance with a specific `channelName` to
   * broadcast messages on
   *
   * @param {string} channelName The name that will be used when creating
   * the `BroadcastChannel`.
   * @param {Object} options
   * @param {Array<string>}
   * [options.headersToCheck=['content-length', 'etag', 'last-modified']] A
   * list of headers that will be used to determine whether the responses
   * differ.
   * @param {string} [options.source='workbox-broadcast-cache-update'] An
   * attribution value that indicates where the update originated.
   */
  constructor(channelName, { headersToCheck, source } = {}) {
    {
      if (typeof channelName !== 'string' || channelName.length === 0) {
        throw new WorkboxError_mjs.WorkboxError('channel-name-required');
      }
    }

    this._channelName = channelName;
    this._headersToCheck = headersToCheck || ['content-length', 'etag', 'last-modified'];
    this._source = source || 'workbox-broadcast-cache-update';

    // TODO assert typeof headersToCheck instanceof Array
  }

  /**
   * @return {BroadcastChannel|undefined} The BroadcastChannel instance used for
   * broadcasting updates, or undefined if the browser doesn't support the
   * Broadcast Channel API.
   *
   * @private
   */
  _getChannel() {
    if ('BroadcastChannel' in self && !this._channel) {
      this._channel = new BroadcastChannel(this._channelName);
    }
    return this._channel;
  }

  /**
   * Compare two [Responses](https://developer.mozilla.org/en-US/docs/Web/API/Response)
   * and send a message via the
   * {@link https://developers.google.com/web/updates/2016/09/broadcastchannel|Broadcast Channel API}
   * if they differ.
   *
   * Neither of the Responses can be {@link http://stackoverflow.com/questions/39109789|opaque}.
   *
   * @param {Response} firstResponse First responses to compare.
   * @param {Response} secondResponse Second responses to compare.
   * @param {string} url The URL of the updated request.
   * @param {string} cacheName Name of the cache the responses belong to.
   * This is included in the message posted on the broadcast channel.
   */
  notifyIfUpdated(firstResponse, secondResponse, url, cacheName) {
    if (!responsesAreSame(firstResponse, secondResponse, this._headersToCheck)) {
      broadcastUpdate(this._getChannel(), cacheName, url, this._source);
    }
  }
}

/*
 Copyright 2016 Google Inc. All Rights Reserved.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

/**
 * This plugin will automatically broadcast a message whenever a cached response
 * is updated.
 *
 * @memberof workbox.broadcastUpdate
 */
class Plugin {
  /**
  * Construct a new instance with a specific `channelName` to
  * broadcast messages on
  *
  * @param {string} channelName The name that will be used when creating
  * the `BroadcastChannel`.
  * @param {Object} options
  * @param {Array<string>}
  * [options.headersToCheck=['content-length', 'etag', 'last-modified']] A
  * list of headers that will be used to determine whether the responses
  * differ.
  * @param {string} [options.source='workbox-broadcast-cache-update'] An
  * attribution value that indicates where the update originated.
  */
  constructor(channelName, options) {
    this._broadcastUpdate = new BroadcastCacheUpdate(channelName, options);
  }
  /**
   * A "lifecycle" callback that will be triggered automatically by the
   * `workbox-sw` and `workbox-runtime-caching` handlers when an entry is
   * added to a cache.
   *
   * @private
   * @param {Object} input The input object to this function.
   * @param {string} input.cacheName Name of the cache the responses belong to.
   * @param {Response} [input.oldResponse] The previous cached value, if any.
   * @param {Response} input.newResponse The new value in the cache.
   */
  cacheDidUpdate({ cacheName, oldResponse, newResponse, request }) {
    {
      assert_mjs.assert.isType(cacheName, 'string', {
        moduleName: 'workbox-broadcast-cache-update',
        className: 'Plugin',
        funcName: 'cacheDidUpdate',
        paramName: 'cacheName'
      });
      assert_mjs.assert.isInstance(newResponse, Response, {
        moduleName: 'workbox-broadcast-cache-update',
        className: 'Plugin',
        funcName: 'cacheDidUpdate',
        paramName: 'newResponse'
      });
      assert_mjs.assert.isInstance(request, Request, {
        moduleName: 'workbox-broadcast-cache-update',
        className: 'Plugin',
        funcName: 'cacheDidUpdate',
        paramName: 'request'
      });
    }

    if (!oldResponse) {
      // Without a two responses there is nothing to compare.
      return;
    }

    this._broadcastUpdate.notifyIfUpdated(oldResponse, newResponse, request.url, cacheName);
  }
}

/*
  Copyright 2017 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

/*
  Copyright 2017 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

exports.BroadcastCacheUpdate = BroadcastCacheUpdate;
exports.Plugin = Plugin;
exports.broadcastUpdate = broadcastUpdate;
exports.messageTypes = messageTypes;

return exports;

}({},workbox.core._private,workbox.core._private,workbox.core._private));

//# sourceMappingURL=workbox-broadcast-cache-update.dev.js.map