source: trip-planner-front/node_modules/enhanced-resolve/lib/util/path.js@ bdd6491

Last change on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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