|
| 1 | +// runtime:path — modern, platform-aware path utilities (DECISIONS D26, SPEC §11). |
| 2 | +// |
| 3 | +// Pure computation: it performs no I/O. The host platform and working directory |
| 4 | +// come from runtime:process, so `sep`, `resolve()`, and friends follow the real |
| 5 | +// OS (importing this module therefore needs the `Env` capability, as evaluating |
| 6 | +// runtime:process does). Deliberately free of legacy Node baggage: one |
| 7 | +// platform-correct surface — no `posix`/`win32` dual namespaces, no overloaded |
| 8 | +// signatures — plus first-class file: URL interop (the modern `__dirname`). |
| 9 | + |
| 10 | +import { platform, cwd } from "runtime:process"; |
| 11 | + |
| 12 | +const WINDOWS = platform === "windows"; |
| 13 | +const sep = WINDOWS ? "\\" : "/"; |
| 14 | +const delimiter = WINDOWS ? ";" : ":"; |
| 15 | + |
| 16 | +// On Windows either slash separates; on POSIX only "/". |
| 17 | +const SPLIT = WINDOWS ? /[\\/]+/ : /\/+/; |
| 18 | +const isSepChar = (c) => c === "/" || (WINDOWS && c === "\\"); |
| 19 | +const isDrive = (p) => WINDOWS && p.length >= 2 && p[1] === ":" && /[A-Za-z]/.test(p[0]); |
| 20 | + |
| 21 | +function str(p, name = "path") { |
| 22 | + if (typeof p !== "string") throw new TypeError(`${name} must be a string, got ${typeof p}`); |
| 23 | + return p; |
| 24 | +} |
| 25 | + |
| 26 | +function isAbsolute(p) { |
| 27 | + str(p); |
| 28 | + if (WINDOWS) { |
| 29 | + if (isDrive(p)) return p.length >= 3 && isSepChar(p[2]); // "C:\..." |
| 30 | + return p.length >= 1 && isSepChar(p[0]); // "\..." or "/..." |
| 31 | + } |
| 32 | + return p.length >= 1 && p[0] === "/"; |
| 33 | +} |
| 34 | + |
| 35 | +// Splits a path into its absolute root ("", "/", or "C:\\") and the remainder. |
| 36 | +function split(p) { |
| 37 | + if (WINDOWS) { |
| 38 | + if (isDrive(p)) { |
| 39 | + const abs = p.length >= 3 && isSepChar(p[2]); |
| 40 | + return { root: p.slice(0, 2) + (abs ? sep : ""), rest: p.slice(abs ? 3 : 2), abs }; |
| 41 | + } |
| 42 | + if (p.length >= 1 && isSepChar(p[0])) return { root: sep, rest: p.slice(1), abs: true }; |
| 43 | + return { root: "", rest: p, abs: false }; |
| 44 | + } |
| 45 | + if (p.length >= 1 && p[0] === "/") return { root: "/", rest: p.slice(1), abs: true }; |
| 46 | + return { root: "", rest: p, abs: false }; |
| 47 | +} |
| 48 | + |
| 49 | +// Collapses "." and ".." across a list of segments. Leading ".." are kept only |
| 50 | +// for relative paths (an absolute path cannot climb above its root). |
| 51 | +function collapse(rest, abs) { |
| 52 | + const out = []; |
| 53 | + for (const s of rest.split(SPLIT)) { |
| 54 | + if (s === "" || s === ".") continue; |
| 55 | + if (s === "..") { |
| 56 | + if (out.length && out[out.length - 1] !== "..") out.pop(); |
| 57 | + else if (!abs) out.push(".."); |
| 58 | + } else { |
| 59 | + out.push(s); |
| 60 | + } |
| 61 | + } |
| 62 | + return out; |
| 63 | +} |
| 64 | + |
| 65 | +function normalize(p) { |
| 66 | + str(p); |
| 67 | + if (p.length === 0) return "."; |
| 68 | + const { root, rest, abs } = split(p); |
| 69 | + const body = collapse(rest, abs).join(sep); |
| 70 | + const result = root + body; |
| 71 | + return result.length === 0 ? "." : result; |
| 72 | +} |
| 73 | + |
| 74 | +function join(...segments) { |
| 75 | + const parts = segments.filter((s) => str(s, "segment").length > 0); |
| 76 | + if (parts.length === 0) return "."; |
| 77 | + return normalize(parts.join(sep)); |
| 78 | +} |
| 79 | + |
| 80 | +// Resolves to an absolute path, walking segments right-to-left until one is |
| 81 | +// absolute; anything still relative is anchored at the current directory. |
| 82 | +function resolve(...segments) { |
| 83 | + let resolved = ""; |
| 84 | + let abs = false; |
| 85 | + for (let i = segments.length - 1; i >= 0 && !abs; i--) { |
| 86 | + const s = str(segments[i], "segment"); |
| 87 | + if (s.length === 0) continue; |
| 88 | + resolved = resolved.length ? s + sep + resolved : s; |
| 89 | + abs = isAbsolute(s); |
| 90 | + } |
| 91 | + if (!abs) resolved = resolved.length ? cwd() + sep + resolved : cwd(); |
| 92 | + return normalize(resolved); |
| 93 | +} |
| 94 | + |
| 95 | +function dirname(p) { |
| 96 | + str(p); |
| 97 | + const { root, rest } = split(p); |
| 98 | + const parts = rest.split(SPLIT).filter((s) => s.length > 0); |
| 99 | + if (parts.length <= 1) return root.length ? root.replace(/[\\/]+$/, "") || sep : "."; |
| 100 | + return root + parts.slice(0, -1).join(sep); |
| 101 | +} |
| 102 | + |
| 103 | +function basename(p) { |
| 104 | + str(p); |
| 105 | + const parts = split(p).rest.split(SPLIT).filter((s) => s.length > 0); |
| 106 | + return parts.length ? parts[parts.length - 1] : ""; |
| 107 | +} |
| 108 | + |
| 109 | +function extname(p) { |
| 110 | + const base = basename(p); |
| 111 | + const dot = base.lastIndexOf("."); |
| 112 | + return dot <= 0 ? "" : base.slice(dot); |
| 113 | +} |
| 114 | + |
| 115 | +function parse(p) { |
| 116 | + str(p); |
| 117 | + const { root } = split(p); |
| 118 | + const base = basename(p); |
| 119 | + const ext = extname(p); |
| 120 | + return { |
| 121 | + root, |
| 122 | + dir: dirname(p), |
| 123 | + base, |
| 124 | + name: ext ? base.slice(0, -ext.length) : base, |
| 125 | + ext, |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +function relative(from, to) { |
| 130 | + const a = resolve(str(from, "from")); |
| 131 | + const b = resolve(str(to, "to")); |
| 132 | + if (a === b) return ""; |
| 133 | + const ap = split(a).rest.split(SPLIT).filter(Boolean); |
| 134 | + const bp = split(b).rest.split(SPLIT).filter(Boolean); |
| 135 | + let i = 0; |
| 136 | + while (i < ap.length && i < bp.length && ap[i] === bp[i]) i++; |
| 137 | + const up = new Array(ap.length - i).fill(".."); |
| 138 | + return [...up, ...bp.slice(i)].join(sep) || "."; |
| 139 | +} |
| 140 | + |
| 141 | +// file: URL interop — the modern replacement for __dirname: |
| 142 | +// dirname(fromFileURL(import.meta.url)) |
| 143 | +function fromFileURL(url) { |
| 144 | + const u = url instanceof URL ? url : new URL(str(url, "url")); |
| 145 | + if (u.protocol !== "file:") throw new TypeError(`expected a file: URL, got ${u.protocol}`); |
| 146 | + let p = decodeURIComponent(u.pathname); |
| 147 | + if (WINDOWS) p = p.replace(/^\//, "").replace(/\//g, "\\"); |
| 148 | + return p; |
| 149 | +} |
| 150 | + |
| 151 | +function toFileURL(p) { |
| 152 | + const abs = resolve(str(p)); |
| 153 | + const segs = abs.split(SPLIT); |
| 154 | + // Keep a Windows drive ("C:") verbatim; percent-encode every real segment. |
| 155 | + const encoded = segs |
| 156 | + .map((s, i) => (i === 0 && isDrive(abs) ? s : encodeURIComponent(s))) |
| 157 | + .join("/"); |
| 158 | + return new URL("file://" + (WINDOWS ? "/" + encoded : encoded)); |
| 159 | +} |
| 160 | + |
| 161 | +export { |
| 162 | + sep, |
| 163 | + delimiter, |
| 164 | + isAbsolute, |
| 165 | + normalize, |
| 166 | + join, |
| 167 | + resolve, |
| 168 | + dirname, |
| 169 | + basename, |
| 170 | + extname, |
| 171 | + parse, |
| 172 | + relative, |
| 173 | + fromFileURL, |
| 174 | + toFileURL, |
| 175 | +}; |
| 176 | +export default { |
| 177 | + sep, |
| 178 | + delimiter, |
| 179 | + isAbsolute, |
| 180 | + normalize, |
| 181 | + join, |
| 182 | + resolve, |
| 183 | + dirname, |
| 184 | + basename, |
| 185 | + extname, |
| 186 | + parse, |
| 187 | + relative, |
| 188 | + fromFileURL, |
| 189 | + toFileURL, |
| 190 | +}; |
0 commit comments