MCPcopy
hub / github.com/webpack/webpack / getScheme

Function getScheme

lib/util/URLAbsoluteSpecifier.js:30–74  ·  view source on GitHub ↗

* Get scheme if specifier is an absolute URL specifier * e.g. Absolute specifiers like 'file:///user/webpack/index.js' * https://tools.ietf.org/html/rfc3986#section-3.1 * @param {string} specifier specifier * @returns {string | undefined} scheme if absolute URL specifier provided

(specifier)

Source from the content-addressed store, hash-verified

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.

Callers 4

readResourceMethod · 0.85
constructorMethod · 0.85
getProtocolFunction · 0.85

Calls 1

sliceMethod · 0.80

Tested by

no test coverage detected