diff options
Diffstat (limited to 'assets/cache-polyfill.js')
| -rw-r--r-- | assets/cache-polyfill.js | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/assets/cache-polyfill.js b/assets/cache-polyfill.js index 7db3ec0..1449734 100644 --- a/assets/cache-polyfill.js +++ b/assets/cache-polyfill.js | |||
| @@ -15,88 +15,88 @@ | |||
| 15 | * | 15 | * |
| 16 | */ | 16 | */ |
| 17 | 17 | ||
| 18 | (function () { | 18 | (function() { |
| 19 | var nativeAddAll = Cache.prototype.addAll; | 19 | var nativeAddAll = Cache.prototype.addAll; |
| 20 | var userAgent = navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/); | 20 | var userAgent = navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/); |
| 21 | 21 | ||
| 22 | // Has nice behavior of `var` which everyone hates | 22 | // Has nice behavior of `var` which everyone hates |
| 23 | if (userAgent) { | 23 | if (userAgent) { |
| 24 | var agent = userAgent[1]; | 24 | var agent = userAgent[1]; |
| 25 | var version = parseInt(userAgent[2]); | 25 | var version = parseInt(userAgent[2]); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | if ( | 28 | if (nativeAddAll && (!userAgent || (agent === 'Firefox' && version >= 46) || (agent === 'Chrome' && version >= 50))) { |
| 29 | nativeAddAll && (!userAgent || | 29 | return; |
| 30 | (agent === 'Firefox' && version >= 46) || | 30 | } |
| 31 | (agent === 'Chrome' && version >= 50) | ||
| 32 | ) | ||
| 33 | ) { | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | 31 | ||
| 37 | Cache.prototype.addAll = function addAll(requests) { | 32 | Cache.prototype.addAll = function addAll(requests) { |
| 38 | var cache = this; | 33 | var cache = this; |
| 39 | 34 | ||
| 40 | // Since DOMExceptions are not constructable: | 35 | // Since DOMExceptions are not constructable: |
| 41 | function NetworkError(message) { | 36 | function NetworkError(message) { |
| 42 | this.name = 'NetworkError'; | 37 | this.name = 'NetworkError'; |
| 43 | this.code = 19; | 38 | this.code = 19; |
| 44 | this.message = message; | 39 | this.message = message; |
| 45 | } | 40 | } |
| 46 | 41 | ||
| 47 | NetworkError.prototype = Object.create(Error.prototype); | 42 | NetworkError.prototype = Object.create(Error.prototype); |
| 48 | 43 | ||
| 49 | return Promise.resolve().then(function () { | 44 | return Promise.resolve() |
| 50 | if (arguments.length < 1) throw new TypeError(); | 45 | .then(function() { |
| 46 | if (arguments.length < 1) throw new TypeError(); | ||
| 51 | 47 | ||
| 52 | // Simulate sequence<(Request or USVString)> binding: | 48 | // Simulate sequence<(Request or USVString)> binding: |
| 53 | var sequence = []; | 49 | var sequence = []; |
| 54 | 50 | ||
| 55 | requests = requests.map(function (request) { | 51 | requests = requests.map(function(request) { |
| 56 | if (request instanceof Request) { | 52 | if (request instanceof Request) { |
| 57 | return request; | 53 | return request; |
| 58 | } else { | 54 | } else { |
| 59 | return String(request); // may throw TypeError | 55 | return String(request); // may throw TypeError |
| 60 | } | 56 | } |
| 61 | }); | 57 | }); |
| 62 | 58 | ||
| 63 | return Promise.all( | 59 | return Promise.all( |
| 64 | requests.map(function (request) { | 60 | requests.map(function(request) { |
| 65 | if (typeof request === 'string') { | 61 | if (typeof request === 'string') { |
| 66 | request = new Request(request); | 62 | request = new Request(request); |
| 67 | } | 63 | } |
| 68 | 64 | ||
| 69 | var scheme = new URL(request.url).protocol; | 65 | var scheme = new URL(request.url).protocol; |
| 70 | 66 | ||
| 71 | if (scheme !== 'http:' && scheme !== 'https:') { | 67 | if (scheme !== 'http:' && scheme !== 'https:') { |
| 72 | throw new NetworkError("Invalid scheme"); | 68 | throw new NetworkError('Invalid scheme'); |
| 73 | } | 69 | } |
| 74 | 70 | ||
| 75 | return fetch(request.clone()); | 71 | return fetch(request.clone()); |
| 76 | }) | 72 | }) |
| 77 | ); | 73 | ); |
| 78 | }).then(function (responses) { | 74 | }) |
| 79 | // If some of the responses has not OK-eish status, | 75 | .then(function(responses) { |
| 80 | // then whole operation should reject | 76 | // If some of the responses has not OK-eish status, |
| 81 | if (responses.some(function (response) { | 77 | // then whole operation should reject |
| 82 | return !response.ok; | 78 | if ( |
| 83 | })) { | 79 | responses.some(function(response) { |
| 84 | throw new NetworkError('Incorrect response status'); | 80 | return !response.ok; |
| 85 | } | 81 | }) |
| 82 | ) { | ||
| 83 | throw new NetworkError('Incorrect response status'); | ||
| 84 | } | ||
| 86 | 85 | ||
| 87 | // TODO: check that requests don't overwrite one another | 86 | // TODO: check that requests don't overwrite one another |
| 88 | // (don't think this is possible to polyfill due to opaque responses) | 87 | // (don't think this is possible to polyfill due to opaque responses) |
| 89 | return Promise.all( | 88 | return Promise.all( |
| 90 | responses.map(function (response, i) { | 89 | responses.map(function(response, i) { |
| 91 | return cache.put(requests[i], response); | 90 | return cache.put(requests[i], response); |
| 92 | }) | 91 | }) |
| 93 | ); | 92 | ); |
| 94 | }).then(function () { | 93 | }) |
| 95 | return undefined; | 94 | .then(function() { |
| 96 | }); | 95 | return undefined; |
| 97 | }; | 96 | }); |
| 97 | }; | ||
| 98 | 98 | ||
| 99 | Cache.prototype.add = function add(request) { | 99 | Cache.prototype.add = function add(request) { |
| 100 | return this.addAll([request]); | 100 | return this.addAll([request]); |
| 101 | }; | 101 | }; |
| 102 | }()); | 102 | })(); |
