|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | -// The `node:url` compatibility shim: the two path/URL converters |
4 | | -// (docs/ns-builtin-modules.md). Parsing goes through the URL intrinsic rather |
5 | | -// than a hand-rolled scan, so authority normalization (`file://localhost/x` |
6 | | -// has no host, per the URL spec), percent-decoding and path canonicalization |
7 | | -// all follow the spec instead of an approximation. |
| 3 | +// The `node:url` compatibility shim over `ns:url` (docs/ns-builtin-modules.md). |
| 4 | +// The two surfaces coincide today, so this only re-exports; any adaptation to |
| 5 | +// Node's API belongs here, never in the standard module. |
8 | 6 |
|
9 | | -const { |
10 | | - decodeURIComponent, |
11 | | - ObjectFreeze, |
12 | | - StringPrototypeCharCodeAt, |
13 | | - StringPrototypeStartsWith, |
14 | | - TypeError, |
15 | | - URL, |
16 | | -} = primordials; |
17 | | - |
18 | | -const INVALID_ARG = |
19 | | - 'The "path" argument must be of type string or an instance of URL.'; |
20 | | - |
21 | | -function toUrl(input) { |
22 | | - let href; |
23 | | - if (typeof input === "string") { |
24 | | - href = input; |
25 | | - } else if (input !== null && typeof input === "object" && |
26 | | - typeof input.href === "string") { |
27 | | - // Duck-typed so a URL from another realm still works. |
28 | | - href = input.href; |
29 | | - } else { |
30 | | - throw new TypeError(INVALID_ARG); |
31 | | - } |
32 | | - |
33 | | - try { |
34 | | - return new URL(href); |
35 | | - } catch { |
36 | | - throw new TypeError(INVALID_ARG); |
37 | | - } |
38 | | -} |
39 | | - |
40 | | -function fileURLToPath(input) { |
41 | | - const url = toUrl(input); |
42 | | - |
43 | | - if (url.protocol !== "file:") { |
44 | | - throw new TypeError("The URL must be of scheme file"); |
45 | | - } |
46 | | - // The URL parser already folded a "localhost" authority to the empty host, |
47 | | - // so anything left here is a real remote host and names no local file. |
48 | | - if (url.hostname !== "") { |
49 | | - throw new TypeError('File URL host must be "localhost" or empty'); |
50 | | - } |
51 | | - |
52 | | - // `pathname` carries neither the query nor the fragment. |
53 | | - const pathname = url.pathname; |
54 | | - for (let i = 0; i < pathname.length; i++) { |
55 | | - if (pathname[i] !== "%") { |
56 | | - continue; |
57 | | - } |
58 | | - // %2F would decode to a separator and silently change the path's shape. |
59 | | - const third = StringPrototypeCharCodeAt(pathname, i + 2) | 0x20; |
60 | | - if (pathname[i + 1] === "2" && third === 102 /* 'f' */) { |
61 | | - throw new TypeError("File URL path must not include encoded / characters"); |
62 | | - } |
63 | | - } |
| 7 | +const { ObjectFreeze } = primordials; |
| 8 | +const { URL, URLSearchParams, fileURLToPath, pathToFileURL } = |
| 9 | + require("ns:url"); |
64 | 10 |
|
65 | | - return decodeURIComponent(pathname); |
66 | | -} |
67 | | - |
68 | | -const kHexDigits = "0123456789ABCDEF"; |
69 | | - |
70 | | -// Percent-encode everything the URL parser would otherwise read as syntax (or |
71 | | -// reject), leaving `/` as the separator it is. Non-ASCII is left alone: the |
72 | | -// parser UTF-8 encodes it correctly on its own. |
73 | | -function encodePathChars(filepath) { |
74 | | - let encoded = ""; |
75 | | - for (let i = 0; i < filepath.length; i++) { |
76 | | - const char = filepath[i]; |
77 | | - const code = StringPrototypeCharCodeAt(filepath, i); |
78 | | - const mustEncode = |
79 | | - code < 0x21 || code === 0x7f || char === "%" || char === "?" || |
80 | | - char === "#" || char === "\\" || char === '"' || char === "<" || |
81 | | - char === ">" || char === "`" || char === "{" || char === "}"; |
82 | | - if (mustEncode) { |
83 | | - encoded += "%" + kHexDigits[(code >> 4) & 0xf] + kHexDigits[code & 0xf]; |
84 | | - } else { |
85 | | - encoded += char; |
86 | | - } |
87 | | - } |
88 | | - return encoded; |
89 | | -} |
90 | | - |
91 | | -function pathToFileURL(filepath) { |
92 | | - if (typeof filepath !== "string") { |
93 | | - throw new TypeError('The "path" argument must be of type string.'); |
94 | | - } |
95 | | - // Node resolves a relative path against the process working directory; there |
96 | | - // is no such thing here, so a relative path has no single correct answer. |
97 | | - if (!StringPrototypeStartsWith(filepath, "/")) { |
98 | | - throw new TypeError('The "path" argument must be an absolute path.'); |
99 | | - } |
100 | | - |
101 | | - return new URL("file://" + encodePathChars(filepath)); |
102 | | -} |
103 | | - |
104 | | -module.exports = ObjectFreeze({ fileURLToPath, pathToFileURL }); |
| 11 | +module.exports = ObjectFreeze({ |
| 12 | + URL, |
| 13 | + URLSearchParams, |
| 14 | + fileURLToPath, |
| 15 | + pathToFileURL, |
| 16 | +}); |
0 commit comments