|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | (function() { |
19 | | - var nativeAddAll = Cache.prototype.addAll; |
20 | | - var userAgent = navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/); |
| 19 | + var nativeAddAll = Cache.prototype.addAll; |
| 20 | + var userAgent = navigator.userAgent.match(/(Firefox|Chrome)\/(\d+\.)/); |
21 | 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 | | - } |
| 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 | 27 |
|
28 | | - if (nativeAddAll && (!userAgent || (agent === "Firefox" && version >= 46) || (agent === "Chrome" && version >= 50))) { |
29 | | - return; |
30 | | - } |
| 28 | + if (nativeAddAll && (!userAgent || (agent === "Firefox" && version >= 46) || (agent === "Chrome" && version >= 50))) { |
| 29 | + return; |
| 30 | + } |
31 | 31 |
|
32 | | - Cache.prototype.addAll = function addAll(requests) { |
33 | | - var cache = this; |
| 32 | + Cache.prototype.addAll = function addAll(requests) { |
| 33 | + var cache = this; |
34 | 34 |
|
35 | | - // Since DOMExceptions are not constructable: |
36 | | - function NetworkError(message) { |
37 | | - this.name = "NetworkError"; |
38 | | - this.code = 19; |
39 | | - this.message = message; |
40 | | - } |
| 35 | + // Since DOMExceptions are not constructable: |
| 36 | + function NetworkError(message) { |
| 37 | + this.name = "NetworkError"; |
| 38 | + this.code = 19; |
| 39 | + this.message = message; |
| 40 | + } |
41 | 41 |
|
42 | | - NetworkError.prototype = Object.create(Error.prototype); |
| 42 | + NetworkError.prototype = Object.create(Error.prototype); |
43 | 43 |
|
44 | | - return Promise.resolve() |
45 | | - .then(function() { |
46 | | - if (arguments.length < 1) throw new TypeError(); |
| 44 | + return Promise.resolve() |
| 45 | + .then(function() { |
| 46 | + if (arguments.length < 1) throw new TypeError(); |
47 | 47 |
|
48 | | - // Simulate sequence<(Request or USVString)> binding: |
49 | | - var sequence = []; |
| 48 | + // Simulate sequence<(Request or USVString)> binding: |
| 49 | + var sequence = []; |
50 | 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 | | - }); |
| 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 | 58 |
|
59 | | - return Promise.all( |
60 | | - requests.map(function(request) { |
61 | | - if (typeof request === "string") { |
62 | | - request = new Request(request); |
63 | | - } |
| 59 | + return Promise.all( |
| 60 | + requests.map(function(request) { |
| 61 | + if (typeof request === "string") { |
| 62 | + request = new Request(request); |
| 63 | + } |
64 | 64 |
|
65 | | - var scheme = new URL(request.url).protocol; |
| 65 | + var scheme = new URL(request.url).protocol; |
66 | 66 |
|
67 | | - if (scheme !== "http:" && scheme !== "https:") { |
68 | | - throw new NetworkError("Invalid scheme"); |
69 | | - } |
| 67 | + if (scheme !== "http:" && scheme !== "https:") { |
| 68 | + throw new NetworkError("Invalid scheme"); |
| 69 | + } |
70 | 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 | | - } |
| 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 | 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 | | - }; |
| 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 | 98 |
|
99 | | - Cache.prototype.add = function add(request) { |
100 | | - return this.addAll([request]); |
101 | | - }; |
| 99 | + Cache.prototype.add = function add(request) { |
| 100 | + return this.addAll([request]); |
| 101 | + }; |
102 | 102 | })(); |
0 commit comments