diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2018-08-08 14:08:39 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2018-08-08 14:08:39 +0200 |
| commit | e146e2536b31765313c7ac8f17b7596df54ee952 (patch) | |
| tree | cf95bedc43f55de397a45a2059df9d41781f365a /assets/cache-polyfill.js | |
| parent | 07523ee5c6a5747869db57387ee63286cecd8c20 (diff) | |
| download | mitjafelicijan.com-e146e2536b31765313c7ac8f17b7596df54ee952.tar.gz | |
content update
Diffstat (limited to 'assets/cache-polyfill.js')
| -rw-r--r-- | assets/cache-polyfill.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/assets/cache-polyfill.js b/assets/cache-polyfill.js new file mode 100644 index 0000000..7db3ec0 --- /dev/null +++ b/assets/cache-polyfill.js | |||
| @@ -0,0 +1,102 @@ | |||
| 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 ( | ||
| 29 | nativeAddAll && (!userAgent || | ||
| 30 | (agent === 'Firefox' && version >= 46) || | ||
| 31 | (agent === 'Chrome' && version >= 50) | ||
| 32 | ) | ||
| 33 | ) { | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 37 | Cache.prototype.addAll = function addAll(requests) { | ||
| 38 | var cache = this; | ||
| 39 | |||
| 40 | // Since DOMExceptions are not constructable: | ||
| 41 | function NetworkError(message) { | ||
| 42 | this.name = 'NetworkError'; | ||
| 43 | this.code = 19; | ||
| 44 | this.message = message; | ||
| 45 | } | ||
| 46 | |||
| 47 | NetworkError.prototype = Object.create(Error.prototype); | ||
| 48 | |||
| 49 | return Promise.resolve().then(function () { | ||
| 50 | if (arguments.length < 1) throw new TypeError(); | ||
| 51 | |||
| 52 | // Simulate sequence<(Request or USVString)> binding: | ||
| 53 | var sequence = []; | ||
| 54 | |||
| 55 | requests = requests.map(function (request) { | ||
| 56 | if (request instanceof Request) { | ||
| 57 | return request; | ||
| 58 | } else { | ||
| 59 | return String(request); // may throw TypeError | ||
| 60 | } | ||
| 61 | }); | ||
| 62 | |||
| 63 | return Promise.all( | ||
| 64 | requests.map(function (request) { | ||
| 65 | if (typeof request === 'string') { | ||
| 66 | request = new Request(request); | ||
| 67 | } | ||
| 68 | |||
| 69 | var scheme = new URL(request.url).protocol; | ||
| 70 | |||
| 71 | if (scheme !== 'http:' && scheme !== 'https:') { | ||
| 72 | throw new NetworkError("Invalid scheme"); | ||
| 73 | } | ||
| 74 | |||
| 75 | return fetch(request.clone()); | ||
| 76 | }) | ||
| 77 | ); | ||
| 78 | }).then(function (responses) { | ||
| 79 | // If some of the responses has not OK-eish status, | ||
| 80 | // then whole operation should reject | ||
| 81 | if (responses.some(function (response) { | ||
| 82 | return !response.ok; | ||
| 83 | })) { | ||
| 84 | throw new NetworkError('Incorrect response status'); | ||
| 85 | } | ||
| 86 | |||
| 87 | // TODO: check that requests don't overwrite one another | ||
| 88 | // (don't think this is possible to polyfill due to opaque responses) | ||
| 89 | return Promise.all( | ||
| 90 | responses.map(function (response, i) { | ||
| 91 | return cache.put(requests[i], response); | ||
| 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 | }()); | ||
