-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize.ts
More file actions
297 lines (283 loc) · 9.61 KB
/
normalize.ts
File metadata and controls
297 lines (283 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* @fileoverview Path normalization — the core `normalizePath` and its
* MSYS drive-letter helper. The rest of the path module's surface
* (predicates, conversion, resolution) lives in sibling leaves and is
* re-exported here so existing `paths/normalize` importers keep working.
*
* - `normalizePath` — backslash → forward-slash, segment collapse, UNC + namespace preservation
* - `msysDriveToNative` — `/c/path` → `C:/path` on Windows
*/
import { WIN32 } from '../constants/platform'
import { search } from '../strings/search'
import {
StringPrototypeCharCodeAt,
StringPrototypeSlice,
} from '../primordials/string'
import { msysDriveRegExp, pathLikeToString, slashRegExp } from './_internal'
// On Windows, convert MSYS drive notation to native: /c/path → C:/path
export function msysDriveToNative(normalized: string): string {
/* c8 ignore start - Windows-only branch. */
if (WIN32) {
return normalized.replace(
msysDriveRegExp,
(_, letter, sep) => `${letter.toUpperCase()}:${sep || '/'}`,
)
}
/* c8 ignore stop */
return normalized
}
/**
* Normalize a path by converting backslashes to forward slashes and collapsing segments.
*
* - Converts all backslashes (`\`) to forward slashes (`/`)
* - Collapses repeated slashes
* - Resolves `.` and `..` segments
* - Preserves UNC path prefixes (`//server/share`)
* - Preserves Windows namespace prefixes (`//./`, `//?/`)
* - Returns `.` for empty or collapsed paths
* - On Windows: MSYS drive letters `/c/path` become `C:/path`
*
* @param {string | Buffer | URL} pathLike - The path to normalize
* @returns {string} The normalized path
*
* @security
* **WARNING**: This function resolves `..` patterns as part of normalization, which means
* paths like `/../etc/passwd` become `/etc/passwd`. When processing untrusted user input
* (HTTP requests, file uploads, URL parameters), you MUST validate for path traversal
* attacks BEFORE calling this function.
*
* @example
* ```typescript
* normalizePath('foo/bar//baz') // 'foo/bar/baz'
* normalizePath('foo/./bar') // 'foo/bar'
* normalizePath('foo/bar/../baz') // 'foo/baz'
* normalizePath('C:\\Users\\u\\file.txt') // 'C:/Users/u/file.txt'
* normalizePath('\\\\server\\share\\file') // '//server/share/file'
* normalizePath('') // '.'
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function normalizePath(pathLike: string | Buffer | URL): string {
const filepath = pathLikeToString(pathLike)
const { length } = filepath
if (length === 0) {
return '.'
}
if (length < 2) {
return length === 1 &&
StringPrototypeCharCodeAt(filepath, 0) === 92 /*'\\'*/
? '/'
: filepath
}
let code = 0
let start = 0
// Ensure win32 namespaces have two leading slashes so they are handled
// properly by path.win32.parse() after being normalized.
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#namespaces
let prefix = ''
if (length > 4 && StringPrototypeCharCodeAt(filepath, 3) === 92 /*'\\'*/) {
const code2 = StringPrototypeCharCodeAt(filepath, 2)
// Look for \\?\ or \\.\
if (
(code2 === 63 /*'?'*/ || code2 === 46) /*'.'*/ &&
StringPrototypeCharCodeAt(filepath, 0) === 92 /*'\\'*/ &&
StringPrototypeCharCodeAt(filepath, 1) === 92 /*'\\'*/
) {
start = 2
prefix = '//'
}
}
if (start === 0) {
/* c8 ignore start - UNC path detection (\\server\share). Rare
input; not exercised by typical test fixtures. */
// UNC paths must start with exactly two slashes, not more.
if (
length > 2 &&
((StringPrototypeCharCodeAt(filepath, 0) === 92 /*'\\'*/ &&
StringPrototypeCharCodeAt(filepath, 1) === 92 /*'\\'*/ &&
StringPrototypeCharCodeAt(filepath, 2) !== 92) /*'\\'*/ ||
(StringPrototypeCharCodeAt(filepath, 0) === 47 /*'/'*/ &&
StringPrototypeCharCodeAt(filepath, 1) === 47 /*'/'*/ &&
StringPrototypeCharCodeAt(filepath, 2) !== 47)) /*'/'*/
) {
// Valid UNC requires server/share.
let firstSegmentEnd = -1
let hasSecondSegment = false
// Skip leading slashes after the initial double slash.
let i = 2
while (
i < length &&
(StringPrototypeCharCodeAt(filepath, i) === 47 /*'/'*/ ||
StringPrototypeCharCodeAt(filepath, i) === 92) /*'\\'*/
) {
i++
}
// Find the end of first segment (server name).
while (i < length) {
const char = StringPrototypeCharCodeAt(filepath, i)
if (char === 47 /*'/'*/ || char === 92 /*'\\'*/) {
firstSegmentEnd = i
break
}
i++
}
if (firstSegmentEnd > 2) {
i = firstSegmentEnd
while (
i < length &&
(StringPrototypeCharCodeAt(filepath, i) === 47 /*'/'*/ ||
StringPrototypeCharCodeAt(filepath, i) === 92) /*'\\'*/
) {
i++
}
if (i < length) {
hasSecondSegment = true
}
}
if (firstSegmentEnd > 2 && hasSecondSegment) {
// Valid UNC — preserve double leading slashes.
start = 2
prefix = '//'
} else {
// Repeated slashes, treat as regular path.
code = StringPrototypeCharCodeAt(filepath, start)
while (code === 47 /*'/'*/ || code === 92 /*'\\'*/) {
start += 1
code = StringPrototypeCharCodeAt(filepath, start)
}
if (start) {
prefix = '/'
}
}
/* c8 ignore stop */
} else {
// Trim leading slashes for regular paths.
code = StringPrototypeCharCodeAt(filepath, start)
while (code === 47 /*'/'*/ || code === 92 /*'\\'*/) {
start += 1
code = StringPrototypeCharCodeAt(filepath, start)
}
if (start) {
prefix = '/'
}
}
}
let nextIndex = search(filepath, slashRegExp, { fromIndex: start })
// Single-segment-no-separator early-return path; sub-arms each fire on
// specific inputs.
/* c8 ignore start */
if (nextIndex === -1) {
const segment = filepath.slice(start)
if (segment === '.' || segment.length === 0) {
return prefix || '.'
}
if (segment === '..') {
return prefix ? StringPrototypeSlice(prefix, 0, -1) || '/' : '..'
}
return msysDriveToNative(prefix + segment)
}
/* c8 ignore stop */
// Process segments and handle '.', '..', and empty segments.
/* c8 ignore start */
let collapsed = ''
let segmentCount = 0
let leadingDotDots = 0
while (nextIndex !== -1) {
const segment = filepath.slice(start, nextIndex)
if (segment.length > 0 && segment !== '.') {
if (segment === '..') {
if (segmentCount > 0) {
const lastSeparatorIndex = collapsed.lastIndexOf('/')
if (lastSeparatorIndex === -1) {
collapsed = ''
segmentCount = 0
if (leadingDotDots > 0 && !prefix) {
collapsed = '..'
leadingDotDots = 1
}
} else {
const lastSegmentStart = lastSeparatorIndex + 1
const lastSegmentValue = collapsed.slice(lastSegmentStart)
if (lastSegmentValue === '..') {
collapsed = `${collapsed}/${segment}`
leadingDotDots += 1
} else {
collapsed = collapsed.slice(0, lastSeparatorIndex)
segmentCount -= 1
}
}
} else if (!prefix) {
collapsed = collapsed + (collapsed.length === 0 ? '' : '/') + segment
leadingDotDots += 1
}
} else {
collapsed = collapsed + (collapsed.length === 0 ? '' : '/') + segment
segmentCount += 1
}
}
start = nextIndex + 1
code = StringPrototypeCharCodeAt(filepath, start)
while (code === 47 /*'/'*/ || code === 92 /*'\\'*/) {
start += 1
code = StringPrototypeCharCodeAt(filepath, start)
}
nextIndex = search(filepath, slashRegExp, { fromIndex: start })
}
const lastSegment = filepath.slice(start)
if (lastSegment.length > 0 && lastSegment !== '.') {
if (lastSegment === '..') {
if (segmentCount > 0) {
const lastSeparatorIndex = collapsed.lastIndexOf('/')
if (lastSeparatorIndex === -1) {
collapsed = ''
segmentCount = 0
if (leadingDotDots > 0 && !prefix) {
collapsed = '..'
leadingDotDots = 1
}
} else {
const lastSegmentStart = lastSeparatorIndex + 1
const lastSegmentValue = collapsed.slice(lastSegmentStart)
if (lastSegmentValue === '..') {
collapsed = `${collapsed}/${lastSegment}`
leadingDotDots += 1
} else {
collapsed = collapsed.slice(0, lastSeparatorIndex)
segmentCount -= 1
}
}
} else if (!prefix) {
collapsed =
collapsed + (collapsed.length === 0 ? '' : '/') + lastSegment
leadingDotDots += 1
}
} else {
collapsed = collapsed + (collapsed.length === 0 ? '' : '/') + lastSegment
segmentCount += 1
}
}
/* c8 ignore stop */
if (collapsed.length === 0) {
return prefix || '.'
}
return msysDriveToNative(prefix + collapsed)
}
// Re-exports — preserve the historical `paths/normalize` surface so
// downstream importers don't have to chase the split.
export { getUrl, pathLikeToString } from './_internal'
export {
fromUnixPath,
splitPath,
toUnixPath,
trimLeadingDotSlash,
} from './conversion'
export {
isAbsolute,
isNodeModules,
isPath,
isPathSeparator,
isRelative,
isUnixPath,
isWindowsDeviceRoot,
} from './predicates'
export { relative, relativeResolve, resolve } from './resolve'