| 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 |
|
|---|
| 10 | const backSlashCharCode = "\\".charCodeAt(0);
|
|---|
| 11 | const slashCharCode = "/".charCodeAt(0);
|
|---|
| 12 | const aLowerCaseCharCode = "a".charCodeAt(0);
|
|---|
| 13 | const zLowerCaseCharCode = "z".charCodeAt(0);
|
|---|
| 14 | const aUpperCaseCharCode = "A".charCodeAt(0);
|
|---|
| 15 | const zUpperCaseCharCode = "Z".charCodeAt(0);
|
|---|
| 16 | const _0CharCode = "0".charCodeAt(0);
|
|---|
| 17 | const _9CharCode = "9".charCodeAt(0);
|
|---|
| 18 | const plusCharCode = "+".charCodeAt(0);
|
|---|
| 19 | const hyphenCharCode = "-".charCodeAt(0);
|
|---|
| 20 | const colonCharCode = ":".charCodeAt(0);
|
|---|
| 21 | const hashCharCode = "#".charCodeAt(0);
|
|---|
| 22 | const 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 | */
|
|---|
| 30 | function 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 | */
|
|---|
| 81 | function getProtocol(specifier) {
|
|---|
| 82 | const scheme = getScheme(specifier);
|
|---|
| 83 | return scheme === undefined ? undefined : `${scheme}:`;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | module.exports.getProtocol = getProtocol;
|
|---|
| 87 | module.exports.getScheme = getScheme;
|
|---|