@netlify/plugin-nextjs 4.40.0 → 4.40.2

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