UNPKG

browser-extension-url-match

Version:

Browser extension URL pattern matching

51 lines (50 loc) 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHostRegex = getHostRegex; var _fancyRegex = require("fancy-regex"); var _getDummyUrl = require("./getDummyUrl"); function getHostRegex(patternSegments) { const { pattern, scheme, rawHost } = patternSegments; if (!rawHost && scheme !== 'file') { return new TypeError('Host is optional only if the scheme is "file".'); } const isStarHost = rawHost.includes('*'); if (isStarHost) { const segments = rawHost.split('*.'); if (rawHost.length > 1 && (segments.length !== 2 || segments[0] || !segments[1])) { return new TypeError('Host can contain only one wildcard at the start, in the form "*.<host segments>"'); } } const dummyUrl = (0, _getDummyUrl.getDummyUrl)(patternSegments, { subdomain: '' }); if (!dummyUrl) { return new TypeError(`Pattern "${pattern}" cannot be used to construct a valid URL.`); } const dummyHost = dummyUrl.host; if (/:\d+$/.test(dummyHost)) { return new TypeError(`Host "${rawHost}" cannot include a port number. All ports are matched by default.`); } if (/[^.a-z0-9\-]/.test(dummyHost)) { return new TypeError(`Host "${rawHost}" contains invalid characters.`); } const host = isStarHost ? '*.' + dummyHost : dummyHost; if (rawHost === '*') { return /.+/; } else if (host.startsWith('*.')) { return (0, _fancyRegex.regex)()` ^ (?:[^.]+\.)* # any number of dot-terminated segments ${host.slice(2)} # rest after leading *. $ `; } else { return (0, _fancyRegex.regex)()`^${host}$`; } }