source: frontend/node_modules/workbox-build/src/lib/modify-url-prefix-transform.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/*
2 Copyright 2018 Google LLC
3
4 Use of this source code is governed by an MIT-style
5 license that can be found in the LICENSE file or at
6 https://opensource.org/licenses/MIT.
7*/
8
9import {errors} from './errors';
10import {escapeRegExp} from './escape-regexp';
11import {ManifestTransform} from '../types';
12
13export function modifyURLPrefixTransform(modifyURLPrefix: {
14 [key: string]: string;
15}): ManifestTransform {
16 if (
17 !modifyURLPrefix ||
18 typeof modifyURLPrefix !== 'object' ||
19 Array.isArray(modifyURLPrefix)
20 ) {
21 throw new Error(errors['modify-url-prefix-bad-prefixes']);
22 }
23
24 // If there are no entries in modifyURLPrefix, just return an identity
25 // function as a shortcut.
26 if (Object.keys(modifyURLPrefix).length === 0) {
27 return (manifest) => {
28 return {manifest};
29 };
30 }
31
32 for (const key of Object.keys(modifyURLPrefix)) {
33 if (typeof modifyURLPrefix[key] !== 'string') {
34 throw new Error(errors['modify-url-prefix-bad-prefixes']);
35 }
36 }
37
38 // Escape the user input so it's safe to use in a regex.
39 const safeModifyURLPrefixes = Object.keys(modifyURLPrefix).map(escapeRegExp);
40 // Join all the `modifyURLPrefix` keys so a single regex can be used.
41 const prefixMatchesStrings = safeModifyURLPrefixes.join('|');
42 // Add `^` to the front the prefix matches so it only matches the start of
43 // a string.
44 const modifyRegex = new RegExp(`^(${prefixMatchesStrings})`);
45
46 return (originalManifest) => {
47 const manifest = originalManifest.map((entry) => {
48 if (typeof entry.url !== 'string') {
49 throw new Error(errors['manifest-entry-bad-url']);
50 }
51
52 entry.url = entry.url.replace(modifyRegex, (match) => {
53 return modifyURLPrefix[match];
54 });
55
56 return entry;
57 });
58
59 return {manifest};
60 };
61}
Note: See TracBrowser for help on using the repository browser.