aboutsummaryrefslogtreecommitdiff
path: root/assets/cache-polyfill.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/cache-polyfill.js')
-rw-r--r--assets/cache-polyfill.js102
1 files changed, 0 insertions, 102 deletions
diff --git a/assets/cache-polyfill.js b/assets/cache-polyfill.js
deleted file mode 100644
index 1449734..0000000
--- a/assets/cache-polyfill.js
+++ /dev/null
@@ -1,102 +0,0 @@
1/**
2 * Copyright 2015 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18(function() {
19 var nativeAddAll = Cache.prototype.addAll;
20 var userAgent = navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/);
21
22 // Has nice behavior of `var` which everyone hates
23 if (userAgent) {
24 var agent = userAgent[1];
25 var version = parseInt(userAgent[2]);
26 }
27
28 if (nativeAddAll && (!userAgent || (agent === 'Firefox' && version >= 46) || (agent === 'Chrome' && version >= 50))) {
29 return;
30 }
31
32 Cache.prototype.addAll = function addAll(requests) {
33 var cache = this;
34
35 // Since DOMExceptions are not constructable:
36 function NetworkError(message) {
37 this.name = 'NetworkError';
38 this.code = 19;
39 this.message = message;
40 }
41
42 NetworkError.prototype = Object.create(Error.prototype);
43
44 return Promise.resolve()
45 .then(function() {
46 if (arguments.length < 1) throw new TypeError();
47
48 // Simulate sequence<(Request or USVString)> binding:
49 var sequence = [];
50
51 requests = requests.map(function(request) {
52 if (request instanceof Request) {
53 return request;
54 } else {
55 return String(request); // may throw TypeError
56 }
57 });
58
59 return Promise.all(
60 requests.map(function(request) {
61 if (typeof request === 'string') {
62 request = new Request(request);
63 }
64
65 var scheme = new URL(request.url).protocol;
66
67 if (scheme !== 'http:' && scheme !== 'https:') {
68 throw new NetworkError('Invalid scheme');
69 }
70
71 return fetch(request.clone());
72 })
73 );
74 })
75 .then(function(responses) {
76 // If some of the responses has not OK-eish status,
77 // then whole operation should reject
78 if (
79 responses.some(function(response) {
80 return !response.ok;
81 })
82 ) {
83 throw new NetworkError('Incorrect response status');
84 }
85
86 // TODO: check that requests don't overwrite one another
87 // (don't think this is possible to polyfill due to opaque responses)
88 return Promise.all(
89 responses.map(function(response, i) {
90 return cache.put(requests[i], response);
91 })
92 );
93 })
94 .then(function() {
95 return undefined;
96 });
97 };
98
99 Cache.prototype.add = function add(request) {
100 return this.addAll([request]);
101 };
102})();