@netlify/edge-bundler 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/deno/lib/common.ts +3 -6
  2. package/deno/vendor/deno.land/std@0.127.0/_util/assert.ts +15 -0
  3. package/deno/vendor/deno.land/std@0.127.0/_util/os.ts +22 -0
  4. package/deno/vendor/deno.land/std@0.127.0/path/_constants.ts +49 -0
  5. package/deno/vendor/deno.land/std@0.127.0/path/_interface.ts +30 -0
  6. package/deno/vendor/deno.land/std@0.127.0/path/_util.ts +133 -0
  7. package/deno/vendor/deno.land/std@0.127.0/path/common.ts +40 -0
  8. package/deno/vendor/deno.land/std@0.127.0/path/glob.ts +413 -0
  9. package/deno/vendor/deno.land/std@0.127.0/path/mod.ts +35 -0
  10. package/deno/vendor/deno.land/std@0.127.0/path/posix.ts +516 -0
  11. package/deno/vendor/deno.land/std@0.127.0/path/separator.ts +7 -0
  12. package/deno/vendor/deno.land/std@0.127.0/path/win32.ts +1008 -0
  13. package/deno/vendor/deno.land/std@0.98.0/async/deferred.ts +26 -0
  14. package/deno/vendor/deno.land/std@0.98.0/async/delay.ts +9 -0
  15. package/deno/vendor/deno.land/std@0.98.0/async/mod.ts +6 -0
  16. package/deno/vendor/deno.land/std@0.98.0/async/mux_async_iterator.ts +69 -0
  17. package/deno/vendor/deno.land/std@0.98.0/async/pool.ts +68 -0
  18. package/deno/vendor/deno.land/std@0.98.0/async/tee.ts +102 -0
  19. package/deno/vendor/deno.land/x/eszip@v0.28.0/eszip_wasm.generated.js +573 -0
  20. package/deno/vendor/deno.land/x/eszip@v0.28.0/eszip_wasm_bg.wasm +0 -0
  21. package/deno/vendor/deno.land/x/eszip@v0.28.0/loader.ts +67 -0
  22. package/deno/vendor/deno.land/x/eszip@v0.28.0/mod.ts +33 -0
  23. package/deno/vendor/deno.land/x/retry@v2.0.0/deps.ts +5 -0
  24. package/deno/vendor/deno.land/x/retry@v2.0.0/misc.ts +25 -0
  25. package/deno/vendor/deno.land/x/retry@v2.0.0/mod.ts +50 -0
  26. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/decorator.ts +31 -0
  27. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/options.ts +36 -0
  28. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/retry.ts +63 -0
  29. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/tooManyTries.ts +11 -0
  30. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/options.ts +5 -0
  31. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/tools.ts +15 -0
  32. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/untilDefined/decorators.ts +35 -0
  33. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/untilDefined/retry.ts +23 -0
  34. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/untilResponse/decorators.ts +18 -0
  35. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/untilResponse/retry.ts +8 -0
  36. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/untilTruthy/decorators.ts +31 -0
  37. package/deno/vendor/deno.land/x/retry@v2.0.0/retry/utils/untilTruthy/retry.ts +29 -0
  38. package/deno/vendor/deno.land/x/retry@v2.0.0/wait/decorators.ts +36 -0
  39. package/deno/vendor/deno.land/x/retry@v2.0.0/wait/options.ts +10 -0
  40. package/deno/vendor/deno.land/x/retry@v2.0.0/wait/timeoutError.ts +9 -0
  41. package/deno/vendor/deno.land/x/retry@v2.0.0/wait/wait.ts +46 -0
  42. package/deno/vendor/import_map.json +5 -0
  43. package/dist/node/bundler.js +1 -0
  44. package/dist/node/bundler.test.js +41 -6
  45. package/dist/node/feature_flags.d.ts +1 -1
  46. package/dist/node/feature_flags.js +1 -0
  47. package/dist/node/formats/eszip.d.ts +3 -1
  48. package/dist/node/formats/eszip.js +13 -6
  49. package/package.json +1 -1
@@ -0,0 +1,1008 @@
1
+ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
2
+ // Copyright the Browserify authors. MIT License.
3
+ // Ported from https://github.com/browserify/path-browserify/
4
+ // This module is browser compatible.
5
+
6
+ import type { FormatInputPathObject, ParsedPath } from "./_interface.ts";
7
+ import {
8
+ CHAR_BACKWARD_SLASH,
9
+ CHAR_COLON,
10
+ CHAR_DOT,
11
+ CHAR_QUESTION_MARK,
12
+ } from "./_constants.ts";
13
+
14
+ import {
15
+ _format,
16
+ assertPath,
17
+ encodeWhitespace,
18
+ isPathSeparator,
19
+ isWindowsDeviceRoot,
20
+ normalizeString,
21
+ } from "./_util.ts";
22
+ import { assert } from "../_util/assert.ts";
23
+
24
+ export const sep = "\\";
25
+ export const delimiter = ";";
26
+
27
+ /**
28
+ * Resolves path segments into a `path`
29
+ * @param pathSegments to process to path
30
+ */
31
+ export function resolve(...pathSegments: string[]): string {
32
+ let resolvedDevice = "";
33
+ let resolvedTail = "";
34
+ let resolvedAbsolute = false;
35
+
36
+ for (let i = pathSegments.length - 1; i >= -1; i--) {
37
+ let path: string;
38
+ // deno-lint-ignore no-explicit-any
39
+ const { Deno } = globalThis as any;
40
+ if (i >= 0) {
41
+ path = pathSegments[i];
42
+ } else if (!resolvedDevice) {
43
+ if (typeof Deno?.cwd !== "function") {
44
+ throw new TypeError("Resolved a drive-letter-less path without a CWD.");
45
+ }
46
+ path = Deno.cwd();
47
+ } else {
48
+ if (
49
+ typeof Deno?.env?.get !== "function" || typeof Deno?.cwd !== "function"
50
+ ) {
51
+ throw new TypeError("Resolved a relative path without a CWD.");
52
+ }
53
+ path = Deno.cwd();
54
+
55
+ // Verify that a cwd was found and that it actually points
56
+ // to our drive. If not, default to the drive's root.
57
+ if (
58
+ path === undefined ||
59
+ path.slice(0, 3).toLowerCase() !== `${resolvedDevice.toLowerCase()}\\`
60
+ ) {
61
+ path = `${resolvedDevice}\\`;
62
+ }
63
+ }
64
+
65
+ assertPath(path);
66
+
67
+ const len = path.length;
68
+
69
+ // Skip empty entries
70
+ if (len === 0) continue;
71
+
72
+ let rootEnd = 0;
73
+ let device = "";
74
+ let isAbsolute = false;
75
+ const code = path.charCodeAt(0);
76
+
77
+ // Try to match a root
78
+ if (len > 1) {
79
+ if (isPathSeparator(code)) {
80
+ // Possible UNC root
81
+
82
+ // If we started with a separator, we know we at least have an
83
+ // absolute path of some kind (UNC or otherwise)
84
+ isAbsolute = true;
85
+
86
+ if (isPathSeparator(path.charCodeAt(1))) {
87
+ // Matched double path separator at beginning
88
+ let j = 2;
89
+ let last = j;
90
+ // Match 1 or more non-path separators
91
+ for (; j < len; ++j) {
92
+ if (isPathSeparator(path.charCodeAt(j))) break;
93
+ }
94
+ if (j < len && j !== last) {
95
+ const firstPart = path.slice(last, j);
96
+ // Matched!
97
+ last = j;
98
+ // Match 1 or more path separators
99
+ for (; j < len; ++j) {
100
+ if (!isPathSeparator(path.charCodeAt(j))) break;
101
+ }
102
+ if (j < len && j !== last) {
103
+ // Matched!
104
+ last = j;
105
+ // Match 1 or more non-path separators
106
+ for (; j < len; ++j) {
107
+ if (isPathSeparator(path.charCodeAt(j))) break;
108
+ }
109
+ if (j === len) {
110
+ // We matched a UNC root only
111
+ device = `\\\\${firstPart}\\${path.slice(last)}`;
112
+ rootEnd = j;
113
+ } else if (j !== last) {
114
+ // We matched a UNC root with leftovers
115
+
116
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
117
+ rootEnd = j;
118
+ }
119
+ }
120
+ }
121
+ } else {
122
+ rootEnd = 1;
123
+ }
124
+ } else if (isWindowsDeviceRoot(code)) {
125
+ // Possible device root
126
+
127
+ if (path.charCodeAt(1) === CHAR_COLON) {
128
+ device = path.slice(0, 2);
129
+ rootEnd = 2;
130
+ if (len > 2) {
131
+ if (isPathSeparator(path.charCodeAt(2))) {
132
+ // Treat separator following drive name as an absolute path
133
+ // indicator
134
+ isAbsolute = true;
135
+ rootEnd = 3;
136
+ }
137
+ }
138
+ }
139
+ }
140
+ } else if (isPathSeparator(code)) {
141
+ // `path` contains just a path separator
142
+ rootEnd = 1;
143
+ isAbsolute = true;
144
+ }
145
+
146
+ if (
147
+ device.length > 0 &&
148
+ resolvedDevice.length > 0 &&
149
+ device.toLowerCase() !== resolvedDevice.toLowerCase()
150
+ ) {
151
+ // This path points to another device so it is not applicable
152
+ continue;
153
+ }
154
+
155
+ if (resolvedDevice.length === 0 && device.length > 0) {
156
+ resolvedDevice = device;
157
+ }
158
+ if (!resolvedAbsolute) {
159
+ resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`;
160
+ resolvedAbsolute = isAbsolute;
161
+ }
162
+
163
+ if (resolvedAbsolute && resolvedDevice.length > 0) break;
164
+ }
165
+
166
+ // At this point the path should be resolved to a full absolute path,
167
+ // but handle relative paths to be safe (might happen when process.cwd()
168
+ // fails)
169
+
170
+ // Normalize the tail path
171
+ resolvedTail = normalizeString(
172
+ resolvedTail,
173
+ !resolvedAbsolute,
174
+ "\\",
175
+ isPathSeparator,
176
+ );
177
+
178
+ return resolvedDevice + (resolvedAbsolute ? "\\" : "") + resolvedTail || ".";
179
+ }
180
+
181
+ /**
182
+ * Normalizes a `path`
183
+ * @param path to normalize
184
+ */
185
+ export function normalize(path: string): string {
186
+ assertPath(path);
187
+ const len = path.length;
188
+ if (len === 0) return ".";
189
+ let rootEnd = 0;
190
+ let device: string | undefined;
191
+ let isAbsolute = false;
192
+ const code = path.charCodeAt(0);
193
+
194
+ // Try to match a root
195
+ if (len > 1) {
196
+ if (isPathSeparator(code)) {
197
+ // Possible UNC root
198
+
199
+ // If we started with a separator, we know we at least have an absolute
200
+ // path of some kind (UNC or otherwise)
201
+ isAbsolute = true;
202
+
203
+ if (isPathSeparator(path.charCodeAt(1))) {
204
+ // Matched double path separator at beginning
205
+ let j = 2;
206
+ let last = j;
207
+ // Match 1 or more non-path separators
208
+ for (; j < len; ++j) {
209
+ if (isPathSeparator(path.charCodeAt(j))) break;
210
+ }
211
+ if (j < len && j !== last) {
212
+ const firstPart = path.slice(last, j);
213
+ // Matched!
214
+ last = j;
215
+ // Match 1 or more path separators
216
+ for (; j < len; ++j) {
217
+ if (!isPathSeparator(path.charCodeAt(j))) break;
218
+ }
219
+ if (j < len && j !== last) {
220
+ // Matched!
221
+ last = j;
222
+ // Match 1 or more non-path separators
223
+ for (; j < len; ++j) {
224
+ if (isPathSeparator(path.charCodeAt(j))) break;
225
+ }
226
+ if (j === len) {
227
+ // We matched a UNC root only
228
+ // Return the normalized version of the UNC root since there
229
+ // is nothing left to process
230
+
231
+ return `\\\\${firstPart}\\${path.slice(last)}\\`;
232
+ } else if (j !== last) {
233
+ // We matched a UNC root with leftovers
234
+
235
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
236
+ rootEnd = j;
237
+ }
238
+ }
239
+ }
240
+ } else {
241
+ rootEnd = 1;
242
+ }
243
+ } else if (isWindowsDeviceRoot(code)) {
244
+ // Possible device root
245
+
246
+ if (path.charCodeAt(1) === CHAR_COLON) {
247
+ device = path.slice(0, 2);
248
+ rootEnd = 2;
249
+ if (len > 2) {
250
+ if (isPathSeparator(path.charCodeAt(2))) {
251
+ // Treat separator following drive name as an absolute path
252
+ // indicator
253
+ isAbsolute = true;
254
+ rootEnd = 3;
255
+ }
256
+ }
257
+ }
258
+ }
259
+ } else if (isPathSeparator(code)) {
260
+ // `path` contains just a path separator, exit early to avoid unnecessary
261
+ // work
262
+ return "\\";
263
+ }
264
+
265
+ let tail: string;
266
+ if (rootEnd < len) {
267
+ tail = normalizeString(
268
+ path.slice(rootEnd),
269
+ !isAbsolute,
270
+ "\\",
271
+ isPathSeparator,
272
+ );
273
+ } else {
274
+ tail = "";
275
+ }
276
+ if (tail.length === 0 && !isAbsolute) tail = ".";
277
+ if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) {
278
+ tail += "\\";
279
+ }
280
+ if (device === undefined) {
281
+ if (isAbsolute) {
282
+ if (tail.length > 0) return `\\${tail}`;
283
+ else return "\\";
284
+ } else if (tail.length > 0) {
285
+ return tail;
286
+ } else {
287
+ return "";
288
+ }
289
+ } else if (isAbsolute) {
290
+ if (tail.length > 0) return `${device}\\${tail}`;
291
+ else return `${device}\\`;
292
+ } else if (tail.length > 0) {
293
+ return device + tail;
294
+ } else {
295
+ return device;
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Verifies whether path is absolute
301
+ * @param path to verify
302
+ */
303
+ export function isAbsolute(path: string): boolean {
304
+ assertPath(path);
305
+ const len = path.length;
306
+ if (len === 0) return false;
307
+
308
+ const code = path.charCodeAt(0);
309
+ if (isPathSeparator(code)) {
310
+ return true;
311
+ } else if (isWindowsDeviceRoot(code)) {
312
+ // Possible device root
313
+
314
+ if (len > 2 && path.charCodeAt(1) === CHAR_COLON) {
315
+ if (isPathSeparator(path.charCodeAt(2))) return true;
316
+ }
317
+ }
318
+ return false;
319
+ }
320
+
321
+ /**
322
+ * Join all given a sequence of `paths`,then normalizes the resulting path.
323
+ * @param paths to be joined and normalized
324
+ */
325
+ export function join(...paths: string[]): string {
326
+ const pathsCount = paths.length;
327
+ if (pathsCount === 0) return ".";
328
+
329
+ let joined: string | undefined;
330
+ let firstPart: string | null = null;
331
+ for (let i = 0; i < pathsCount; ++i) {
332
+ const path = paths[i];
333
+ assertPath(path);
334
+ if (path.length > 0) {
335
+ if (joined === undefined) joined = firstPart = path;
336
+ else joined += `\\${path}`;
337
+ }
338
+ }
339
+
340
+ if (joined === undefined) return ".";
341
+
342
+ // Make sure that the joined path doesn't start with two slashes, because
343
+ // normalize() will mistake it for an UNC path then.
344
+ //
345
+ // This step is skipped when it is very clear that the user actually
346
+ // intended to point at an UNC path. This is assumed when the first
347
+ // non-empty string arguments starts with exactly two slashes followed by
348
+ // at least one more non-slash character.
349
+ //
350
+ // Note that for normalize() to treat a path as an UNC path it needs to
351
+ // have at least 2 components, so we don't filter for that here.
352
+ // This means that the user can use join to construct UNC paths from
353
+ // a server name and a share name; for example:
354
+ // path.join('//server', 'share') -> '\\\\server\\share\\')
355
+ let needsReplace = true;
356
+ let slashCount = 0;
357
+ assert(firstPart != null);
358
+ if (isPathSeparator(firstPart.charCodeAt(0))) {
359
+ ++slashCount;
360
+ const firstLen = firstPart.length;
361
+ if (firstLen > 1) {
362
+ if (isPathSeparator(firstPart.charCodeAt(1))) {
363
+ ++slashCount;
364
+ if (firstLen > 2) {
365
+ if (isPathSeparator(firstPart.charCodeAt(2))) ++slashCount;
366
+ else {
367
+ // We matched a UNC path in the first part
368
+ needsReplace = false;
369
+ }
370
+ }
371
+ }
372
+ }
373
+ }
374
+ if (needsReplace) {
375
+ // Find any more consecutive slashes we need to replace
376
+ for (; slashCount < joined.length; ++slashCount) {
377
+ if (!isPathSeparator(joined.charCodeAt(slashCount))) break;
378
+ }
379
+
380
+ // Replace the slashes if needed
381
+ if (slashCount >= 2) joined = `\\${joined.slice(slashCount)}`;
382
+ }
383
+
384
+ return normalize(joined);
385
+ }
386
+
387
+ /**
388
+ * It will solve the relative path from `from` to `to`, for instance:
389
+ * from = 'C:\\orandea\\test\\aaa'
390
+ * to = 'C:\\orandea\\impl\\bbb'
391
+ * The output of the function should be: '..\\..\\impl\\bbb'
392
+ * @param from relative path
393
+ * @param to relative path
394
+ */
395
+ export function relative(from: string, to: string): string {
396
+ assertPath(from);
397
+ assertPath(to);
398
+
399
+ if (from === to) return "";
400
+
401
+ const fromOrig = resolve(from);
402
+ const toOrig = resolve(to);
403
+
404
+ if (fromOrig === toOrig) return "";
405
+
406
+ from = fromOrig.toLowerCase();
407
+ to = toOrig.toLowerCase();
408
+
409
+ if (from === to) return "";
410
+
411
+ // Trim any leading backslashes
412
+ let fromStart = 0;
413
+ let fromEnd = from.length;
414
+ for (; fromStart < fromEnd; ++fromStart) {
415
+ if (from.charCodeAt(fromStart) !== CHAR_BACKWARD_SLASH) break;
416
+ }
417
+ // Trim trailing backslashes (applicable to UNC paths only)
418
+ for (; fromEnd - 1 > fromStart; --fromEnd) {
419
+ if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH) break;
420
+ }
421
+ const fromLen = fromEnd - fromStart;
422
+
423
+ // Trim any leading backslashes
424
+ let toStart = 0;
425
+ let toEnd = to.length;
426
+ for (; toStart < toEnd; ++toStart) {
427
+ if (to.charCodeAt(toStart) !== CHAR_BACKWARD_SLASH) break;
428
+ }
429
+ // Trim trailing backslashes (applicable to UNC paths only)
430
+ for (; toEnd - 1 > toStart; --toEnd) {
431
+ if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH) break;
432
+ }
433
+ const toLen = toEnd - toStart;
434
+
435
+ // Compare paths to find the longest common path from root
436
+ const length = fromLen < toLen ? fromLen : toLen;
437
+ let lastCommonSep = -1;
438
+ let i = 0;
439
+ for (; i <= length; ++i) {
440
+ if (i === length) {
441
+ if (toLen > length) {
442
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
443
+ // We get here if `from` is the exact base path for `to`.
444
+ // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
445
+ return toOrig.slice(toStart + i + 1);
446
+ } else if (i === 2) {
447
+ // We get here if `from` is the device root.
448
+ // For example: from='C:\\'; to='C:\\foo'
449
+ return toOrig.slice(toStart + i);
450
+ }
451
+ }
452
+ if (fromLen > length) {
453
+ if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
454
+ // We get here if `to` is the exact base path for `from`.
455
+ // For example: from='C:\\foo\\bar'; to='C:\\foo'
456
+ lastCommonSep = i;
457
+ } else if (i === 2) {
458
+ // We get here if `to` is the device root.
459
+ // For example: from='C:\\foo\\bar'; to='C:\\'
460
+ lastCommonSep = 3;
461
+ }
462
+ }
463
+ break;
464
+ }
465
+ const fromCode = from.charCodeAt(fromStart + i);
466
+ const toCode = to.charCodeAt(toStart + i);
467
+ if (fromCode !== toCode) break;
468
+ else if (fromCode === CHAR_BACKWARD_SLASH) lastCommonSep = i;
469
+ }
470
+
471
+ // We found a mismatch before the first common path separator was seen, so
472
+ // return the original `to`.
473
+ if (i !== length && lastCommonSep === -1) {
474
+ return toOrig;
475
+ }
476
+
477
+ let out = "";
478
+ if (lastCommonSep === -1) lastCommonSep = 0;
479
+ // Generate the relative path based on the path difference between `to` and
480
+ // `from`
481
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
482
+ if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
483
+ if (out.length === 0) out += "..";
484
+ else out += "\\..";
485
+ }
486
+ }
487
+
488
+ // Lastly, append the rest of the destination (`to`) path that comes after
489
+ // the common path parts
490
+ if (out.length > 0) {
491
+ return out + toOrig.slice(toStart + lastCommonSep, toEnd);
492
+ } else {
493
+ toStart += lastCommonSep;
494
+ if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) ++toStart;
495
+ return toOrig.slice(toStart, toEnd);
496
+ }
497
+ }
498
+
499
+ /**
500
+ * Resolves path to a namespace path
501
+ * @param path to resolve to namespace
502
+ */
503
+ export function toNamespacedPath(path: string): string {
504
+ // Note: this will *probably* throw somewhere.
505
+ if (typeof path !== "string") return path;
506
+ if (path.length === 0) return "";
507
+
508
+ const resolvedPath = resolve(path);
509
+
510
+ if (resolvedPath.length >= 3) {
511
+ if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
512
+ // Possible UNC root
513
+
514
+ if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
515
+ const code = resolvedPath.charCodeAt(2);
516
+ if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
517
+ // Matched non-long UNC root, convert the path to a long UNC path
518
+ return `\\\\?\\UNC\\${resolvedPath.slice(2)}`;
519
+ }
520
+ }
521
+ } else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0))) {
522
+ // Possible device root
523
+
524
+ if (
525
+ resolvedPath.charCodeAt(1) === CHAR_COLON &&
526
+ resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH
527
+ ) {
528
+ // Matched device root, convert the path to a long UNC path
529
+ return `\\\\?\\${resolvedPath}`;
530
+ }
531
+ }
532
+ }
533
+
534
+ return path;
535
+ }
536
+
537
+ /**
538
+ * Return the directory name of a `path`.
539
+ * @param path to determine name for
540
+ */
541
+ export function dirname(path: string): string {
542
+ assertPath(path);
543
+ const len = path.length;
544
+ if (len === 0) return ".";
545
+ let rootEnd = -1;
546
+ let end = -1;
547
+ let matchedSlash = true;
548
+ let offset = 0;
549
+ const code = path.charCodeAt(0);
550
+
551
+ // Try to match a root
552
+ if (len > 1) {
553
+ if (isPathSeparator(code)) {
554
+ // Possible UNC root
555
+
556
+ rootEnd = offset = 1;
557
+
558
+ if (isPathSeparator(path.charCodeAt(1))) {
559
+ // Matched double path separator at beginning
560
+ let j = 2;
561
+ let last = j;
562
+ // Match 1 or more non-path separators
563
+ for (; j < len; ++j) {
564
+ if (isPathSeparator(path.charCodeAt(j))) break;
565
+ }
566
+ if (j < len && j !== last) {
567
+ // Matched!
568
+ last = j;
569
+ // Match 1 or more path separators
570
+ for (; j < len; ++j) {
571
+ if (!isPathSeparator(path.charCodeAt(j))) break;
572
+ }
573
+ if (j < len && j !== last) {
574
+ // Matched!
575
+ last = j;
576
+ // Match 1 or more non-path separators
577
+ for (; j < len; ++j) {
578
+ if (isPathSeparator(path.charCodeAt(j))) break;
579
+ }
580
+ if (j === len) {
581
+ // We matched a UNC root only
582
+ return path;
583
+ }
584
+ if (j !== last) {
585
+ // We matched a UNC root with leftovers
586
+
587
+ // Offset by 1 to include the separator after the UNC root to
588
+ // treat it as a "normal root" on top of a (UNC) root
589
+ rootEnd = offset = j + 1;
590
+ }
591
+ }
592
+ }
593
+ }
594
+ } else if (isWindowsDeviceRoot(code)) {
595
+ // Possible device root
596
+
597
+ if (path.charCodeAt(1) === CHAR_COLON) {
598
+ rootEnd = offset = 2;
599
+ if (len > 2) {
600
+ if (isPathSeparator(path.charCodeAt(2))) rootEnd = offset = 3;
601
+ }
602
+ }
603
+ }
604
+ } else if (isPathSeparator(code)) {
605
+ // `path` contains just a path separator, exit early to avoid
606
+ // unnecessary work
607
+ return path;
608
+ }
609
+
610
+ for (let i = len - 1; i >= offset; --i) {
611
+ if (isPathSeparator(path.charCodeAt(i))) {
612
+ if (!matchedSlash) {
613
+ end = i;
614
+ break;
615
+ }
616
+ } else {
617
+ // We saw the first non-path separator
618
+ matchedSlash = false;
619
+ }
620
+ }
621
+
622
+ if (end === -1) {
623
+ if (rootEnd === -1) return ".";
624
+ else end = rootEnd;
625
+ }
626
+ return path.slice(0, end);
627
+ }
628
+
629
+ /**
630
+ * Return the last portion of a `path`. Trailing directory separators are ignored.
631
+ * @param path to process
632
+ * @param ext of path directory
633
+ */
634
+ export function basename(path: string, ext = ""): string {
635
+ if (ext !== undefined && typeof ext !== "string") {
636
+ throw new TypeError('"ext" argument must be a string');
637
+ }
638
+
639
+ assertPath(path);
640
+
641
+ let start = 0;
642
+ let end = -1;
643
+ let matchedSlash = true;
644
+ let i: number;
645
+
646
+ // Check for a drive letter prefix so as not to mistake the following
647
+ // path separator as an extra separator at the end of the path that can be
648
+ // disregarded
649
+ if (path.length >= 2) {
650
+ const drive = path.charCodeAt(0);
651
+ if (isWindowsDeviceRoot(drive)) {
652
+ if (path.charCodeAt(1) === CHAR_COLON) start = 2;
653
+ }
654
+ }
655
+
656
+ if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
657
+ if (ext.length === path.length && ext === path) return "";
658
+ let extIdx = ext.length - 1;
659
+ let firstNonSlashEnd = -1;
660
+ for (i = path.length - 1; i >= start; --i) {
661
+ const code = path.charCodeAt(i);
662
+ if (isPathSeparator(code)) {
663
+ // If we reached a path separator that was not part of a set of path
664
+ // separators at the end of the string, stop now
665
+ if (!matchedSlash) {
666
+ start = i + 1;
667
+ break;
668
+ }
669
+ } else {
670
+ if (firstNonSlashEnd === -1) {
671
+ // We saw the first non-path separator, remember this index in case
672
+ // we need it if the extension ends up not matching
673
+ matchedSlash = false;
674
+ firstNonSlashEnd = i + 1;
675
+ }
676
+ if (extIdx >= 0) {
677
+ // Try to match the explicit extension
678
+ if (code === ext.charCodeAt(extIdx)) {
679
+ if (--extIdx === -1) {
680
+ // We matched the extension, so mark this as the end of our path
681
+ // component
682
+ end = i;
683
+ }
684
+ } else {
685
+ // Extension does not match, so our result is the entire path
686
+ // component
687
+ extIdx = -1;
688
+ end = firstNonSlashEnd;
689
+ }
690
+ }
691
+ }
692
+ }
693
+
694
+ if (start === end) end = firstNonSlashEnd;
695
+ else if (end === -1) end = path.length;
696
+ return path.slice(start, end);
697
+ } else {
698
+ for (i = path.length - 1; i >= start; --i) {
699
+ if (isPathSeparator(path.charCodeAt(i))) {
700
+ // If we reached a path separator that was not part of a set of path
701
+ // separators at the end of the string, stop now
702
+ if (!matchedSlash) {
703
+ start = i + 1;
704
+ break;
705
+ }
706
+ } else if (end === -1) {
707
+ // We saw the first non-path separator, mark this as the end of our
708
+ // path component
709
+ matchedSlash = false;
710
+ end = i + 1;
711
+ }
712
+ }
713
+
714
+ if (end === -1) return "";
715
+ return path.slice(start, end);
716
+ }
717
+ }
718
+
719
+ /**
720
+ * Return the extension of the `path`.
721
+ * @param path with extension
722
+ */
723
+ export function extname(path: string): string {
724
+ assertPath(path);
725
+ let start = 0;
726
+ let startDot = -1;
727
+ let startPart = 0;
728
+ let end = -1;
729
+ let matchedSlash = true;
730
+ // Track the state of characters (if any) we see before our first dot and
731
+ // after any path separator we find
732
+ let preDotState = 0;
733
+
734
+ // Check for a drive letter prefix so as not to mistake the following
735
+ // path separator as an extra separator at the end of the path that can be
736
+ // disregarded
737
+
738
+ if (
739
+ path.length >= 2 &&
740
+ path.charCodeAt(1) === CHAR_COLON &&
741
+ isWindowsDeviceRoot(path.charCodeAt(0))
742
+ ) {
743
+ start = startPart = 2;
744
+ }
745
+
746
+ for (let i = path.length - 1; i >= start; --i) {
747
+ const code = path.charCodeAt(i);
748
+ if (isPathSeparator(code)) {
749
+ // If we reached a path separator that was not part of a set of path
750
+ // separators at the end of the string, stop now
751
+ if (!matchedSlash) {
752
+ startPart = i + 1;
753
+ break;
754
+ }
755
+ continue;
756
+ }
757
+ if (end === -1) {
758
+ // We saw the first non-path separator, mark this as the end of our
759
+ // extension
760
+ matchedSlash = false;
761
+ end = i + 1;
762
+ }
763
+ if (code === CHAR_DOT) {
764
+ // If this is our first dot, mark it as the start of our extension
765
+ if (startDot === -1) startDot = i;
766
+ else if (preDotState !== 1) preDotState = 1;
767
+ } else if (startDot !== -1) {
768
+ // We saw a non-dot and non-path separator before our dot, so we should
769
+ // have a good chance at having a non-empty extension
770
+ preDotState = -1;
771
+ }
772
+ }
773
+
774
+ if (
775
+ startDot === -1 ||
776
+ end === -1 ||
777
+ // We saw a non-dot character immediately before the dot
778
+ preDotState === 0 ||
779
+ // The (right-most) trimmed path component is exactly '..'
780
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
781
+ ) {
782
+ return "";
783
+ }
784
+ return path.slice(startDot, end);
785
+ }
786
+
787
+ /**
788
+ * Generate a path from `FormatInputPathObject` object.
789
+ * @param pathObject with path
790
+ */
791
+ export function format(pathObject: FormatInputPathObject): string {
792
+ if (pathObject === null || typeof pathObject !== "object") {
793
+ throw new TypeError(
794
+ `The "pathObject" argument must be of type Object. Received type ${typeof pathObject}`,
795
+ );
796
+ }
797
+ return _format("\\", pathObject);
798
+ }
799
+
800
+ /**
801
+ * Return a `ParsedPath` object of the `path`.
802
+ * @param path to process
803
+ */
804
+ export function parse(path: string): ParsedPath {
805
+ assertPath(path);
806
+
807
+ const ret: ParsedPath = { root: "", dir: "", base: "", ext: "", name: "" };
808
+
809
+ const len = path.length;
810
+ if (len === 0) return ret;
811
+
812
+ let rootEnd = 0;
813
+ let code = path.charCodeAt(0);
814
+
815
+ // Try to match a root
816
+ if (len > 1) {
817
+ if (isPathSeparator(code)) {
818
+ // Possible UNC root
819
+
820
+ rootEnd = 1;
821
+ if (isPathSeparator(path.charCodeAt(1))) {
822
+ // Matched double path separator at beginning
823
+ let j = 2;
824
+ let last = j;
825
+ // Match 1 or more non-path separators
826
+ for (; j < len; ++j) {
827
+ if (isPathSeparator(path.charCodeAt(j))) break;
828
+ }
829
+ if (j < len && j !== last) {
830
+ // Matched!
831
+ last = j;
832
+ // Match 1 or more path separators
833
+ for (; j < len; ++j) {
834
+ if (!isPathSeparator(path.charCodeAt(j))) break;
835
+ }
836
+ if (j < len && j !== last) {
837
+ // Matched!
838
+ last = j;
839
+ // Match 1 or more non-path separators
840
+ for (; j < len; ++j) {
841
+ if (isPathSeparator(path.charCodeAt(j))) break;
842
+ }
843
+ if (j === len) {
844
+ // We matched a UNC root only
845
+
846
+ rootEnd = j;
847
+ } else if (j !== last) {
848
+ // We matched a UNC root with leftovers
849
+
850
+ rootEnd = j + 1;
851
+ }
852
+ }
853
+ }
854
+ }
855
+ } else if (isWindowsDeviceRoot(code)) {
856
+ // Possible device root
857
+
858
+ if (path.charCodeAt(1) === CHAR_COLON) {
859
+ rootEnd = 2;
860
+ if (len > 2) {
861
+ if (isPathSeparator(path.charCodeAt(2))) {
862
+ if (len === 3) {
863
+ // `path` contains just a drive root, exit early to avoid
864
+ // unnecessary work
865
+ ret.root = ret.dir = path;
866
+ return ret;
867
+ }
868
+ rootEnd = 3;
869
+ }
870
+ } else {
871
+ // `path` contains just a drive root, exit early to avoid
872
+ // unnecessary work
873
+ ret.root = ret.dir = path;
874
+ return ret;
875
+ }
876
+ }
877
+ }
878
+ } else if (isPathSeparator(code)) {
879
+ // `path` contains just a path separator, exit early to avoid
880
+ // unnecessary work
881
+ ret.root = ret.dir = path;
882
+ return ret;
883
+ }
884
+
885
+ if (rootEnd > 0) ret.root = path.slice(0, rootEnd);
886
+
887
+ let startDot = -1;
888
+ let startPart = rootEnd;
889
+ let end = -1;
890
+ let matchedSlash = true;
891
+ let i = path.length - 1;
892
+
893
+ // Track the state of characters (if any) we see before our first dot and
894
+ // after any path separator we find
895
+ let preDotState = 0;
896
+
897
+ // Get non-dir info
898
+ for (; i >= rootEnd; --i) {
899
+ code = path.charCodeAt(i);
900
+ if (isPathSeparator(code)) {
901
+ // If we reached a path separator that was not part of a set of path
902
+ // separators at the end of the string, stop now
903
+ if (!matchedSlash) {
904
+ startPart = i + 1;
905
+ break;
906
+ }
907
+ continue;
908
+ }
909
+ if (end === -1) {
910
+ // We saw the first non-path separator, mark this as the end of our
911
+ // extension
912
+ matchedSlash = false;
913
+ end = i + 1;
914
+ }
915
+ if (code === CHAR_DOT) {
916
+ // If this is our first dot, mark it as the start of our extension
917
+ if (startDot === -1) startDot = i;
918
+ else if (preDotState !== 1) preDotState = 1;
919
+ } else if (startDot !== -1) {
920
+ // We saw a non-dot and non-path separator before our dot, so we should
921
+ // have a good chance at having a non-empty extension
922
+ preDotState = -1;
923
+ }
924
+ }
925
+
926
+ if (
927
+ startDot === -1 ||
928
+ end === -1 ||
929
+ // We saw a non-dot character immediately before the dot
930
+ preDotState === 0 ||
931
+ // The (right-most) trimmed path component is exactly '..'
932
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
933
+ ) {
934
+ if (end !== -1) {
935
+ ret.base = ret.name = path.slice(startPart, end);
936
+ }
937
+ } else {
938
+ ret.name = path.slice(startPart, startDot);
939
+ ret.base = path.slice(startPart, end);
940
+ ret.ext = path.slice(startDot, end);
941
+ }
942
+
943
+ // If the directory is the root, use the entire root as the `dir` including
944
+ // the trailing slash if any (`C:\abc` -> `C:\`). Otherwise, strip out the
945
+ // trailing slash (`C:\abc\def` -> `C:\abc`).
946
+ if (startPart > 0 && startPart !== rootEnd) {
947
+ ret.dir = path.slice(0, startPart - 1);
948
+ } else ret.dir = ret.root;
949
+
950
+ return ret;
951
+ }
952
+
953
+ /**
954
+ * Converts a file URL to a path string.
955
+ *
956
+ * ```ts
957
+ * import { fromFileUrl } from "./win32.ts";
958
+ * fromFileUrl("file:///home/foo"); // "\\home\\foo"
959
+ * fromFileUrl("file:///C:/Users/foo"); // "C:\\Users\\foo"
960
+ * fromFileUrl("file://localhost/home/foo"); // "\\\\localhost\\home\\foo"
961
+ * ```
962
+ * @param url of a file URL
963
+ */
964
+ export function fromFileUrl(url: string | URL): string {
965
+ url = url instanceof URL ? url : new URL(url);
966
+ if (url.protocol != "file:") {
967
+ throw new TypeError("Must be a file URL.");
968
+ }
969
+ let path = decodeURIComponent(
970
+ url.pathname.replace(/\//g, "\\").replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
971
+ ).replace(/^\\*([A-Za-z]:)(\\|$)/, "$1\\");
972
+ if (url.hostname != "") {
973
+ // Note: The `URL` implementation guarantees that the drive letter and
974
+ // hostname are mutually exclusive. Otherwise it would not have been valid
975
+ // to append the hostname and path like this.
976
+ path = `\\\\${url.hostname}${path}`;
977
+ }
978
+ return path;
979
+ }
980
+
981
+ /**
982
+ * Converts a path string to a file URL.
983
+ *
984
+ * ```ts
985
+ * import { toFileUrl } from "./win32.ts";
986
+ * toFileUrl("\\home\\foo"); // new URL("file:///home/foo")
987
+ * toFileUrl("C:\\Users\\foo"); // new URL("file:///C:/Users/foo")
988
+ * toFileUrl("\\\\127.0.0.1\\home\\foo"); // new URL("file://127.0.0.1/home/foo")
989
+ * ```
990
+ * @param path to convert to file URL
991
+ */
992
+ export function toFileUrl(path: string): URL {
993
+ if (!isAbsolute(path)) {
994
+ throw new TypeError("Must be an absolute path.");
995
+ }
996
+ const [, hostname, pathname] = path.match(
997
+ /^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/,
998
+ )!;
999
+ const url = new URL("file:///");
1000
+ url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25"));
1001
+ if (hostname != null && hostname != "localhost") {
1002
+ url.hostname = hostname;
1003
+ if (!url.hostname) {
1004
+ throw new TypeError("Invalid hostname.");
1005
+ }
1006
+ }
1007
+ return url;
1008
+ }