source: frontend/node_modules/webpack/lib/util/URLAbsoluteSpecifier.js

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: 2.5 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Ivan Kopeykin @vankop
4*/
5
6"use strict";
7
8/** @typedef {(error: Error | null, result?: Buffer) => void} ErrorFirstCallback */
9
10const backSlashCharCode = "\\".charCodeAt(0);
11const slashCharCode = "/".charCodeAt(0);
12const aLowerCaseCharCode = "a".charCodeAt(0);
13const zLowerCaseCharCode = "z".charCodeAt(0);
14const aUpperCaseCharCode = "A".charCodeAt(0);
15const zUpperCaseCharCode = "Z".charCodeAt(0);
16const _0CharCode = "0".charCodeAt(0);
17const _9CharCode = "9".charCodeAt(0);
18const plusCharCode = "+".charCodeAt(0);
19const hyphenCharCode = "-".charCodeAt(0);
20const colonCharCode = ":".charCodeAt(0);
21const hashCharCode = "#".charCodeAt(0);
22const queryCharCode = "?".charCodeAt(0);
23/**
24 * Get scheme if specifier is an absolute URL specifier
25 * e.g. Absolute specifiers like 'file:///user/webpack/index.js'
26 * https://tools.ietf.org/html/rfc3986#section-3.1
27 * @param {string} specifier specifier
28 * @returns {string | undefined} scheme if absolute URL specifier provided
29 */
30function getScheme(specifier) {
31 const start = specifier.charCodeAt(0);
32
33 // First char maybe only a letter
34 if (
35 (start < aLowerCaseCharCode || start > zLowerCaseCharCode) &&
36 (start < aUpperCaseCharCode || start > zUpperCaseCharCode)
37 ) {
38 return;
39 }
40
41 let i = 1;
42 let ch = specifier.charCodeAt(i);
43
44 while (
45 (ch >= aLowerCaseCharCode && ch <= zLowerCaseCharCode) ||
46 (ch >= aUpperCaseCharCode && ch <= zUpperCaseCharCode) ||
47 (ch >= _0CharCode && ch <= _9CharCode) ||
48 ch === plusCharCode ||
49 ch === hyphenCharCode
50 ) {
51 if (++i === specifier.length) return;
52 ch = specifier.charCodeAt(i);
53 }
54
55 // Scheme must end with colon
56 if (ch !== colonCharCode) return;
57
58 // Check for Windows absolute path
59 // https://url.spec.whatwg.org/#url-miscellaneous
60 if (i === 1) {
61 const nextChar = i + 1 < specifier.length ? specifier.charCodeAt(i + 1) : 0;
62 if (
63 nextChar === 0 ||
64 nextChar === backSlashCharCode ||
65 nextChar === slashCharCode ||
66 nextChar === hashCharCode ||
67 nextChar === queryCharCode
68 ) {
69 return;
70 }
71 }
72
73 return specifier.slice(0, i).toLowerCase();
74}
75
76/**
77 * Returns protocol if absolute URL specifier provided.
78 * @param {string} specifier specifier
79 * @returns {string | null | undefined} protocol if absolute URL specifier provided
80 */
81function getProtocol(specifier) {
82 const scheme = getScheme(specifier);
83 return scheme === undefined ? undefined : `${scheme}:`;
84}
85
86module.exports.getProtocol = getProtocol;
87module.exports.getScheme = getScheme;
Note: See TracBrowser for help on using the repository browser.