[6a3a178] | 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 {import("./fs").InputFileSystem} InputFileSystem */
|
---|
| 9 | /** @typedef {(error: Error|null, result?: Buffer) => void} ErrorFirstCallback */
|
---|
| 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 | * Get scheme if specifier is an absolute URL specifier
|
---|
| 26 | * e.g. Absolute specifiers like 'file:///user/webpack/index.js'
|
---|
| 27 | * https://tools.ietf.org/html/rfc3986#section-3.1
|
---|
| 28 | * @param {string} specifier specifier
|
---|
| 29 | * @returns {string|undefined} scheme if absolute URL specifier provided
|
---|
| 30 | */
|
---|
| 31 | function getScheme(specifier) {
|
---|
| 32 | const start = specifier.charCodeAt(0);
|
---|
| 33 |
|
---|
| 34 | // First char maybe only a letter
|
---|
| 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 | // Scheme must end with colon
|
---|
| 57 | if (ch !== colonCharCode) return undefined;
|
---|
| 58 |
|
---|
| 59 | // Check for Windows absolute path
|
---|
| 60 | // https://url.spec.whatwg.org/#url-miscellaneous
|
---|
| 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 | * @param {string} specifier specifier
|
---|
| 79 | * @returns {string|null} protocol if absolute URL specifier provided
|
---|
| 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;
|
---|