[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const path = require("path");
|
---|
| 9 |
|
---|
| 10 | const CHAR_HASH = "#".charCodeAt(0);
|
---|
| 11 | const CHAR_SLASH = "/".charCodeAt(0);
|
---|
| 12 | const CHAR_BACKSLASH = "\\".charCodeAt(0);
|
---|
| 13 | const CHAR_A = "A".charCodeAt(0);
|
---|
| 14 | const CHAR_Z = "Z".charCodeAt(0);
|
---|
| 15 | const CHAR_LOWER_A = "a".charCodeAt(0);
|
---|
| 16 | const CHAR_LOWER_Z = "z".charCodeAt(0);
|
---|
| 17 | const CHAR_DOT = ".".charCodeAt(0);
|
---|
| 18 | const CHAR_COLON = ":".charCodeAt(0);
|
---|
| 19 |
|
---|
| 20 | const posixNormalize = path.posix.normalize;
|
---|
| 21 | const winNormalize = path.win32.normalize;
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @enum {number}
|
---|
| 25 | */
|
---|
| 26 | const PathType = Object.freeze({
|
---|
| 27 | Empty: 0,
|
---|
| 28 | Normal: 1,
|
---|
| 29 | Relative: 2,
|
---|
| 30 | AbsoluteWin: 3,
|
---|
| 31 | AbsolutePosix: 4,
|
---|
| 32 | Internal: 5
|
---|
| 33 | });
|
---|
| 34 | exports.PathType = PathType;
|
---|
| 35 |
|
---|
| 36 | const invalidSegmentRegEx =
|
---|
| 37 | /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))?(\\|\/|$)/i;
|
---|
| 38 | exports.invalidSegmentRegEx = invalidSegmentRegEx;
|
---|
| 39 |
|
---|
| 40 | const deprecatedInvalidSegmentRegEx =
|
---|
| 41 | /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))(\\|\/|$)/i;
|
---|
| 42 | exports.deprecatedInvalidSegmentRegEx = deprecatedInvalidSegmentRegEx;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @param {string} p a path
|
---|
| 46 | * @returns {PathType} type of path
|
---|
| 47 | */
|
---|
| 48 | const getType = p => {
|
---|
| 49 | switch (p.length) {
|
---|
| 50 | case 0:
|
---|
| 51 | return PathType.Empty;
|
---|
| 52 | case 1: {
|
---|
| 53 | const c0 = p.charCodeAt(0);
|
---|
| 54 | switch (c0) {
|
---|
| 55 | case CHAR_DOT:
|
---|
| 56 | return PathType.Relative;
|
---|
| 57 | case CHAR_SLASH:
|
---|
| 58 | return PathType.AbsolutePosix;
|
---|
| 59 | case CHAR_HASH:
|
---|
| 60 | return PathType.Internal;
|
---|
| 61 | }
|
---|
| 62 | return PathType.Normal;
|
---|
| 63 | }
|
---|
| 64 | case 2: {
|
---|
| 65 | const c0 = p.charCodeAt(0);
|
---|
| 66 | switch (c0) {
|
---|
| 67 | case CHAR_DOT: {
|
---|
| 68 | const c1 = p.charCodeAt(1);
|
---|
| 69 | switch (c1) {
|
---|
| 70 | case CHAR_DOT:
|
---|
| 71 | case CHAR_SLASH:
|
---|
| 72 | return PathType.Relative;
|
---|
| 73 | }
|
---|
| 74 | return PathType.Normal;
|
---|
| 75 | }
|
---|
| 76 | case CHAR_SLASH:
|
---|
| 77 | return PathType.AbsolutePosix;
|
---|
| 78 | case CHAR_HASH:
|
---|
| 79 | return PathType.Internal;
|
---|
| 80 | }
|
---|
| 81 | const c1 = p.charCodeAt(1);
|
---|
| 82 | if (c1 === CHAR_COLON) {
|
---|
| 83 | if (
|
---|
| 84 | (c0 >= CHAR_A && c0 <= CHAR_Z) ||
|
---|
| 85 | (c0 >= CHAR_LOWER_A && c0 <= CHAR_LOWER_Z)
|
---|
| 86 | ) {
|
---|
| 87 | return PathType.AbsoluteWin;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | return PathType.Normal;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | const c0 = p.charCodeAt(0);
|
---|
| 94 | switch (c0) {
|
---|
| 95 | case CHAR_DOT: {
|
---|
| 96 | const c1 = p.charCodeAt(1);
|
---|
| 97 | switch (c1) {
|
---|
| 98 | case CHAR_SLASH:
|
---|
| 99 | return PathType.Relative;
|
---|
| 100 | case CHAR_DOT: {
|
---|
| 101 | const c2 = p.charCodeAt(2);
|
---|
| 102 | if (c2 === CHAR_SLASH) return PathType.Relative;
|
---|
| 103 | return PathType.Normal;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | return PathType.Normal;
|
---|
| 107 | }
|
---|
| 108 | case CHAR_SLASH:
|
---|
| 109 | return PathType.AbsolutePosix;
|
---|
| 110 | case CHAR_HASH:
|
---|
| 111 | return PathType.Internal;
|
---|
| 112 | }
|
---|
| 113 | const c1 = p.charCodeAt(1);
|
---|
| 114 | if (c1 === CHAR_COLON) {
|
---|
| 115 | const c2 = p.charCodeAt(2);
|
---|
| 116 | if (
|
---|
| 117 | (c2 === CHAR_BACKSLASH || c2 === CHAR_SLASH) &&
|
---|
| 118 | ((c0 >= CHAR_A && c0 <= CHAR_Z) ||
|
---|
| 119 | (c0 >= CHAR_LOWER_A && c0 <= CHAR_LOWER_Z))
|
---|
| 120 | ) {
|
---|
| 121 | return PathType.AbsoluteWin;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | return PathType.Normal;
|
---|
| 125 | };
|
---|
| 126 | exports.getType = getType;
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * @param {string} p a path
|
---|
| 130 | * @returns {string} the normalized path
|
---|
| 131 | */
|
---|
| 132 | const normalize = p => {
|
---|
| 133 | switch (getType(p)) {
|
---|
| 134 | case PathType.Empty:
|
---|
| 135 | return p;
|
---|
| 136 | case PathType.AbsoluteWin:
|
---|
| 137 | return winNormalize(p);
|
---|
| 138 | case PathType.Relative: {
|
---|
| 139 | const r = posixNormalize(p);
|
---|
| 140 | return getType(r) === PathType.Relative ? r : `./${r}`;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | return posixNormalize(p);
|
---|
| 144 | };
|
---|
| 145 | exports.normalize = normalize;
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * @param {string} rootPath the root path
|
---|
| 149 | * @param {string | undefined} request the request path
|
---|
| 150 | * @returns {string} the joined path
|
---|
| 151 | */
|
---|
| 152 | const join = (rootPath, request) => {
|
---|
| 153 | if (!request) return normalize(rootPath);
|
---|
| 154 | const requestType = getType(request);
|
---|
| 155 | switch (requestType) {
|
---|
| 156 | case PathType.AbsolutePosix:
|
---|
| 157 | return posixNormalize(request);
|
---|
| 158 | case PathType.AbsoluteWin:
|
---|
| 159 | return winNormalize(request);
|
---|
| 160 | }
|
---|
| 161 | switch (getType(rootPath)) {
|
---|
| 162 | case PathType.Normal:
|
---|
| 163 | case PathType.Relative:
|
---|
| 164 | case PathType.AbsolutePosix:
|
---|
| 165 | return posixNormalize(`${rootPath}/${request}`);
|
---|
| 166 | case PathType.AbsoluteWin:
|
---|
| 167 | return winNormalize(`${rootPath}\\${request}`);
|
---|
| 168 | }
|
---|
| 169 | switch (requestType) {
|
---|
| 170 | case PathType.Empty:
|
---|
| 171 | return rootPath;
|
---|
| 172 | case PathType.Relative: {
|
---|
| 173 | const r = posixNormalize(rootPath);
|
---|
| 174 | return getType(r) === PathType.Relative ? r : `./${r}`;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | return posixNormalize(rootPath);
|
---|
| 178 | };
|
---|
| 179 | exports.join = join;
|
---|
| 180 |
|
---|
| 181 | /** @type {Map<string, Map<string, string | undefined>>} */
|
---|
| 182 | const joinCache = new Map();
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * @param {string} rootPath the root path
|
---|
| 186 | * @param {string} request the request path
|
---|
| 187 | * @returns {string} the joined path
|
---|
| 188 | */
|
---|
| 189 | const cachedJoin = (rootPath, request) => {
|
---|
| 190 | /** @type {string | undefined} */
|
---|
| 191 | let cacheEntry;
|
---|
| 192 | let cache = joinCache.get(rootPath);
|
---|
| 193 | if (cache === undefined) {
|
---|
| 194 | joinCache.set(rootPath, (cache = new Map()));
|
---|
| 195 | } else {
|
---|
| 196 | cacheEntry = cache.get(request);
|
---|
| 197 | if (cacheEntry !== undefined) return cacheEntry;
|
---|
| 198 | }
|
---|
| 199 | cacheEntry = join(rootPath, request);
|
---|
| 200 | cache.set(request, cacheEntry);
|
---|
| 201 | return cacheEntry;
|
---|
| 202 | };
|
---|
| 203 | exports.cachedJoin = cachedJoin;
|
---|