1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const backSlashCharCode = "\\".charCodeAt(0);
|
12 | const slashCharCode = "/".charCodeAt(0);
|
13 | const aLowerCaseCharCode = "a".charCodeAt(0);
|
14 | const zLowerCaseCharCode = "z".charCodeAt(0);
|
15 | const aUpperCaseCharCode = "A".charCodeAt(0);
|
16 | const zUpperCaseCharCode = "Z".charCodeAt(0);
|
17 | const _0CharCode = "0".charCodeAt(0);
|
18 | const _9CharCode = "9".charCodeAt(0);
|
19 | const plusCharCode = "+".charCodeAt(0);
|
20 | const hyphenCharCode = "-".charCodeAt(0);
|
21 | const colonCharCode = ":".charCodeAt(0);
|
22 | const hashCharCode = "#".charCodeAt(0);
|
23 | const queryCharCode = "?".charCodeAt(0);
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function getScheme(specifier) {
|
32 | const start = specifier.charCodeAt(0);
|
33 |
|
34 |
|
35 | if (
|
36 | (start < aLowerCaseCharCode || start > zLowerCaseCharCode) &&
|
37 | (start < aUpperCaseCharCode || start > zUpperCaseCharCode)
|
38 | ) {
|
39 | return undefined;
|
40 | }
|
41 |
|
42 | let i = 1;
|
43 | let ch = specifier.charCodeAt(i);
|
44 |
|
45 | while (
|
46 | (ch >= aLowerCaseCharCode && ch <= zLowerCaseCharCode) ||
|
47 | (ch >= aUpperCaseCharCode && ch <= zUpperCaseCharCode) ||
|
48 | (ch >= _0CharCode && ch <= _9CharCode) ||
|
49 | ch === plusCharCode ||
|
50 | ch === hyphenCharCode
|
51 | ) {
|
52 | if (++i === specifier.length) return undefined;
|
53 | ch = specifier.charCodeAt(i);
|
54 | }
|
55 |
|
56 |
|
57 | if (ch !== colonCharCode) return undefined;
|
58 |
|
59 |
|
60 |
|
61 | if (i === 1) {
|
62 | const nextChar = i + 1 < specifier.length ? specifier.charCodeAt(i + 1) : 0;
|
63 | if (
|
64 | nextChar === 0 ||
|
65 | nextChar === backSlashCharCode ||
|
66 | nextChar === slashCharCode ||
|
67 | nextChar === hashCharCode ||
|
68 | nextChar === queryCharCode
|
69 | ) {
|
70 | return undefined;
|
71 | }
|
72 | }
|
73 |
|
74 | return specifier.slice(0, i).toLowerCase();
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | function getProtocol(specifier) {
|
82 | const scheme = getScheme(specifier);
|
83 | return scheme === undefined ? undefined : scheme + ":";
|
84 | }
|
85 |
|
86 | exports.getScheme = getScheme;
|
87 | exports.getProtocol = getProtocol;
|