109 lines
3.9 KiB
JavaScript
109 lines
3.9 KiB
JavaScript
/**
|
|
* Copyright 2015 Google Inc. All rights reserved.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/* eslint-env node,worker */
|
|
'use strict';
|
|
|
|
var URL = require('dom-urls');
|
|
|
|
module.exports = {
|
|
stripIgnoredUrlParameters: function(originalUrl,
|
|
ignoreUrlParametersMatching) {
|
|
var url = new URL(originalUrl);
|
|
// Remove the hash; see https://github.com/GoogleChrome/sw-precache/issues/290
|
|
url.hash = '';
|
|
|
|
url.search = url.search.slice(1) // Exclude initial '?'
|
|
.split('&') // Split into an array of 'key=value' strings
|
|
.map(function(kv) {
|
|
return kv.split('='); // Split each 'key=value' string into a [key, value] array
|
|
})
|
|
.filter(function(kv) {
|
|
return ignoreUrlParametersMatching.every(function(ignoredRegex) {
|
|
return !ignoredRegex.test(kv[0]); // Return true iff the key doesn't match any of the regexes.
|
|
});
|
|
})
|
|
.map(function(kv) {
|
|
return kv.join('='); // Join each [key, value] array into a 'key=value' string
|
|
})
|
|
.join('&'); // Join the array of 'key=value' strings into a string with '&' in between each
|
|
|
|
return url.toString();
|
|
},
|
|
|
|
addDirectoryIndex: function(originalUrl, index) {
|
|
var url = new URL(originalUrl);
|
|
if (url.pathname.slice(-1) === '/') {
|
|
url.pathname += index;
|
|
}
|
|
return url.toString();
|
|
},
|
|
|
|
isPathWhitelisted: function(whitelist, absoluteUrlString) {
|
|
// If the whitelist is empty, then consider all URLs to be whitelisted.
|
|
if (whitelist.length === 0) {
|
|
return true;
|
|
}
|
|
|
|
// Otherwise compare each path regex to the path of the URL passed in.
|
|
var path = (new URL(absoluteUrlString)).pathname;
|
|
return whitelist.some(function(whitelistedPathRegex) {
|
|
return path.match(whitelistedPathRegex);
|
|
});
|
|
},
|
|
|
|
createCacheKey: function(originalUrl, paramName, paramValue,
|
|
dontCacheBustUrlsMatching) {
|
|
// Create a new URL object to avoid modifying originalUrl.
|
|
var url = new URL(originalUrl);
|
|
|
|
// If dontCacheBustUrlsMatching is not set, or if we don't have a match,
|
|
// then add in the extra cache-busting URL parameter.
|
|
if (!dontCacheBustUrlsMatching ||
|
|
!(url.pathname.match(dontCacheBustUrlsMatching))) {
|
|
url.search += (url.search ? '&' : '') +
|
|
encodeURIComponent(paramName) + '=' + encodeURIComponent(paramValue);
|
|
}
|
|
|
|
return url.toString();
|
|
},
|
|
|
|
// When passed a redirected response, this will create a new, "clean" response
|
|
// that can be used to respond to a navigation request.
|
|
// See https://bugs.chromium.org/p/chromium/issues/detail?id=669363&desc=2#c1
|
|
cleanResponse: function(originalResponse) {
|
|
// If this is not a redirected response, then we don't have to do anything.
|
|
if (!originalResponse.redirected) {
|
|
return Promise.resolve(originalResponse);
|
|
}
|
|
|
|
// Firefox 50 and below doesn't support the Response.body stream, so we may
|
|
// need to read the entire body to memory as a Blob.
|
|
var bodyPromise = 'body' in originalResponse ?
|
|
Promise.resolve(originalResponse.body) :
|
|
originalResponse.blob();
|
|
|
|
return bodyPromise.then(function(body) {
|
|
// new Response() is happy when passed either a stream or a Blob.
|
|
return new Response(body, {
|
|
headers: originalResponse.headers,
|
|
status: originalResponse.status,
|
|
statusText: originalResponse.statusText
|
|
});
|
|
});
|
|
}
|
|
};
|