1 | ;
|
2 |
|
3 | var _keys = require('babel-runtime/core-js/object/keys');
|
4 |
|
5 | var _keys2 = _interopRequireDefault(_keys);
|
6 |
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
8 |
|
9 | /*
|
10 | Copyright 2017 Google Inc.
|
11 |
|
12 | Licensed under the Apache License, Version 2.0 (the "License");
|
13 | you may not use this file except in compliance with the License.
|
14 | You may obtain a copy of the License at
|
15 |
|
16 | https://www.apache.org/licenses/LICENSE-2.0
|
17 |
|
18 | Unless required by applicable law or agreed to in writing, software
|
19 | distributed under the License is distributed on an "AS IS" BASIS,
|
20 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21 | See the License for the specific language governing permissions and
|
22 | limitations under the License.
|
23 | */
|
24 |
|
25 | var errors = require('./errors');
|
26 |
|
27 | /**
|
28 | * Escaping user input to be treated as a literal string within a regular
|
29 | * expression can be accomplished by simple replacement.
|
30 | *
|
31 | * From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
|
32 | *
|
33 | * @private
|
34 | * @param {string} string The string to be used as part of a regular
|
35 | * expression
|
36 | * @return {string} A string that is safe to use in a regular
|
37 | * expression.
|
38 | *
|
39 | * @private
|
40 | */
|
41 | var escapeRegExp = function escapeRegExp(string) {
|
42 | return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
43 | };
|
44 |
|
45 | module.exports = function (modifyUrlPrefix) {
|
46 | if (!modifyUrlPrefix || typeof modifyUrlPrefix !== 'object' || Array.isArray(modifyUrlPrefix)) {
|
47 | throw new Error(errors['modify-url-prefix-bad-prefixes']);
|
48 | }
|
49 |
|
50 | // If there are no entries in modifyUrlPrefix, just return an identity
|
51 | // function as a shortcut.
|
52 | if ((0, _keys2.default)(modifyUrlPrefix).length === 0) {
|
53 | return function (entry) {
|
54 | return entry;
|
55 | };
|
56 | }
|
57 |
|
58 | (0, _keys2.default)(modifyUrlPrefix).forEach(function (key) {
|
59 | if (typeof modifyUrlPrefix[key] !== 'string') {
|
60 | throw new Error(errors['modify-url-prefix-bad-prefixes']);
|
61 | }
|
62 | });
|
63 |
|
64 | // Escape the user input so it's safe to use in a regex.
|
65 | var safeModifyUrlPrefixes = (0, _keys2.default)(modifyUrlPrefix).map(escapeRegExp);
|
66 | // Join all the `modifyUrlPrefix` keys so a single regex can be used.
|
67 | var prefixMatchesStrings = safeModifyUrlPrefixes.join('|');
|
68 | // Add `^` to the front the prefix matches so it only matches the start of
|
69 | // a string.
|
70 | var modifyRegex = new RegExp(`^(${prefixMatchesStrings})`);
|
71 |
|
72 | return function (originalManifest) {
|
73 | var manifest = originalManifest.map(function (entry) {
|
74 | if (typeof entry.url !== 'string') {
|
75 | throw new Error(errors['manifest-entry-bad-url']);
|
76 | }
|
77 |
|
78 | entry.url = entry.url.replace(modifyRegex, function (match) {
|
79 | return modifyUrlPrefix[match];
|
80 | });
|
81 |
|
82 | return entry;
|
83 | });
|
84 |
|
85 | return { manifest };
|
86 | };
|
87 | }; |
\ | No newline at end of file |