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 } 59 else { 60 return String(request); // may throw TypeError 61 } 62 }); 63 64 return Promise.all( 65 requests.map(function(request) { 66 if (typeof request === 'string') { 67 request = new Request(request); 68 } 69 70 var scheme = new URL(request.url).protocol; 71 72 if (scheme !== 'http:' && scheme !== 'https:') { 73 throw new NetworkError("Invalid scheme"); 74 } 75 76 return fetch(request.clone()); 77 }) 78 ); 79 }).then(function(responses) { 80 // If some of the responses has not OK-eish status, 81 // then whole operation should reject 82 if (responses.some(function(response) { 83 return !response.ok; 84 })) { 85 throw new NetworkError('Incorrect response status'); 86 } 87 88 // TODO: check that requests don't overwrite one another 89 // (don't think this is possible to polyfill due to opaque responses) 90 return Promise.all( 91 responses.map(function(response, i) { 92 return cache.put(requests[i], response); 93 }) 94 ); 95 }).then(function() { 96 return undefined; 97 }); 98 }; 99 100 Cache.prototype.add = function add(request) { 101 return this.addAll([request]); 102 }; 103}());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.