UNPKG

lighthouse

Version:

Automated auditing, performance metrics, and best practices for the web.

191 lines (189 loc) 6.51 kB
// @ts-nocheck // generated by yarn build-cdt-lib /* eslint-disable */ "use strict"; /* * Copyright (C) 2012 Google Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following disclaimer * in the documentation and/or other materials provided with the * distribution. * * Neither the name of Google Inc. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ParsedURL = void 0; exports.normalizePath = normalizePath; exports.schemeIs = schemeIs; ; /** * http://tools.ietf.org/html/rfc3986#section-5.2.4 */ function normalizePath(path) { if (path.indexOf('..') === -1 && path.indexOf('.') === -1) { return path; } // Remove leading slash (will be added back below) so we // can handle all (including empty) segments consistently. const segments = (path[0] === '/' ? path.substring(1) : path).split('/'); const normalizedSegments = []; for (const segment of segments) { if (segment === '.') { continue; } else if (segment === '..') { normalizedSegments.pop(); } else { normalizedSegments.push(segment); } } let normalizedPath = normalizedSegments.join('/'); if (path[0] === '/' && normalizedPath) { normalizedPath = '/' + normalizedPath; } if (normalizedPath[normalizedPath.length - 1] !== '/' && ((path[path.length - 1] === '/') || (segments[segments.length - 1] === '.') || (segments[segments.length - 1] === '..'))) { normalizedPath = normalizedPath + '/'; } return normalizedPath; } function schemeIs(url, scheme) { try { return (new URL(url)).protocol === scheme; } catch { return false; } } class ParsedURL { isValid; url; scheme; user; host; port; path; queryParams; fragment; folderPathComponents; lastPathComponent; blobInnerScheme; #displayNameInternal; #dataURLDisplayNameInternal; constructor(url) { this.isValid = false; this.url = url; this.scheme = ''; this.user = ''; this.host = ''; this.port = ''; this.path = ''; this.queryParams = ''; this.fragment = ''; this.folderPathComponents = ''; this.lastPathComponent = ''; const isBlobUrl = this.url.startsWith('blob:'); const urlToMatch = isBlobUrl ? url.substring(5) : url; const match = urlToMatch.match(ParsedURL.urlRegex()); if (match) { this.isValid = true; if (isBlobUrl) { this.blobInnerScheme = match[2].toLowerCase(); this.scheme = 'blob'; } else { this.scheme = match[2].toLowerCase(); } this.user = match[3] ?? ''; this.host = match[4] ?? ''; this.port = match[5] ?? ''; this.path = match[6] ?? '/'; this.queryParams = match[7] ?? ''; this.fragment = match[8] ?? ''; } else { if (this.url.startsWith('data:')) { this.scheme = 'data'; return; } if (this.url.startsWith('blob:')) { this.scheme = 'blob'; return; } if (this.url === 'about:blank') { this.scheme = 'about'; return; } this.path = this.url; } const lastSlashExceptTrailingIndex = this.path.lastIndexOf('/', this.path.length - 2); if (lastSlashExceptTrailingIndex !== -1) { this.lastPathComponent = this.path.substring(lastSlashExceptTrailingIndex + 1); } else { this.lastPathComponent = this.path; } const lastSlashIndex = this.path.lastIndexOf('/'); if (lastSlashIndex !== -1) { this.folderPathComponents = this.path.substring(0, lastSlashIndex); } } static concatenate(devToolsPath, ...appendage) { return devToolsPath.concat(...appendage); } static beginsWithWindowsDriveLetter(url) { return /^[A-Za-z]:/.test(url); } static beginsWithScheme(url) { return /^[A-Za-z][A-Za-z0-9+.-]*:/.test(url); } static isRelativeURL(url) { return !this.beginsWithScheme(url) || this.beginsWithWindowsDriveLetter(url); } get displayName() { if (this.#displayNameInternal) { return this.#displayNameInternal; } if (this.isDataURL()) { return this.dataURLDisplayName(); } if (this.isBlobURL()) { return this.url; } if (this.isAboutBlank()) { return this.url; } this.#displayNameInternal = this.lastPathComponent; if (!this.#displayNameInternal) { this.#displayNameInternal = (this.host || '') + '/'; } if (this.#displayNameInternal === '/') { this.#displayNameInternal = this.url; } return this.#displayNameInternal; } static urlRegexInstance = null; } exports.ParsedURL = ParsedURL;