diff --git a/cli/tests/unit_node/testdata/child_process_stdio.js b/cli/tests/unit_node/testdata/child_process_stdio.js index 399b890ed1..b13b095623 100644 --- a/cli/tests/unit_node/testdata/child_process_stdio.js +++ b/cli/tests/unit_node/testdata/child_process_stdio.js @@ -1,9 +1,10 @@ import childProcess from "node:child_process"; import process from "node:process"; import * as path from "node:path"; +import { fileURLToPath } from "node:url"; const script = path.join( - path.dirname(path.fromFileUrl(import.meta.url)), + path.dirname(fileURLToPath(import.meta.url)), "node_modules", "foo", "index.js", diff --git a/cli/tests/unit_node/testdata/child_process_stdio_012.js b/cli/tests/unit_node/testdata/child_process_stdio_012.js index 682d8a084a..e3717f9852 100644 --- a/cli/tests/unit_node/testdata/child_process_stdio_012.js +++ b/cli/tests/unit_node/testdata/child_process_stdio_012.js @@ -1,9 +1,10 @@ import childProcess from "node:child_process"; import process from "node:process"; import * as path from "node:path"; +import { fileURLToPath } from "node:url"; const script = path.join( - path.dirname(path.fromFileUrl(import.meta.url)), + path.dirname(fileURLToPath(import.meta.url)), "node_modules", "foo", "index.js", diff --git a/cli/tests/unit_node/testdata/child_process_unref.js b/cli/tests/unit_node/testdata/child_process_unref.js index cc7815d97c..8201ac44d1 100644 --- a/cli/tests/unit_node/testdata/child_process_unref.js +++ b/cli/tests/unit_node/testdata/child_process_unref.js @@ -1,8 +1,9 @@ import cp from "node:child_process"; import * as path from "node:path"; +import { fileURLToPath } from "node:url"; const script = path.join( - path.dirname(path.fromFileUrl(import.meta.url)), + path.dirname(fileURLToPath(import.meta.url)), "infinite_loop.js", ); const childProcess = cp.spawn(Deno.execPath(), ["run", script]); diff --git a/ext/node/polyfills/_fs/_fs_exists.ts b/ext/node/polyfills/_fs/_fs_exists.ts index 37ce599e96..32eb7948a3 100644 --- a/ext/node/polyfills/_fs/_fs_exists.ts +++ b/ext/node/polyfills/_fs/_fs_exists.ts @@ -1,5 +1,5 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; type ExistsCallback = (exists: boolean) => void; @@ -9,7 +9,7 @@ type ExistsCallback = (exists: boolean) => void; * Deprecated in node api */ export function exists(path: string | URL, callback: ExistsCallback) { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; Deno.lstat(path).then(() => callback(true), () => callback(false)); } @@ -30,7 +30,7 @@ Object.defineProperty(exists, kCustomPromisifiedSymbol, { * are implemented. See https://github.com/denoland/deno/issues/3403 */ export function existsSync(path: string | URL): boolean { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; try { Deno.lstatSync(path); return true; diff --git a/ext/node/polyfills/_fs/_fs_link.ts b/ext/node/polyfills/_fs/_fs_link.ts index 3039ade099..affa428523 100644 --- a/ext/node/polyfills/_fs/_fs_link.ts +++ b/ext/node/polyfills/_fs/_fs_link.ts @@ -1,6 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { promisify } from "ext:deno_node/internal/util.mjs"; /** @@ -13,9 +13,9 @@ export function link( callback: CallbackWithError, ) { existingPath = existingPath instanceof URL - ? fromFileUrl(existingPath) + ? pathFromURL(existingPath) : existingPath; - newPath = newPath instanceof URL ? fromFileUrl(newPath) : newPath; + newPath = newPath instanceof URL ? pathFromURL(newPath) : newPath; Deno.link(existingPath, newPath).then(() => callback(null), callback); } @@ -38,9 +38,9 @@ export function linkSync( newPath: string | URL, ) { existingPath = existingPath instanceof URL - ? fromFileUrl(existingPath) + ? pathFromURL(existingPath) : existingPath; - newPath = newPath instanceof URL ? fromFileUrl(newPath) : newPath; + newPath = newPath instanceof URL ? pathFromURL(newPath) : newPath; Deno.linkSync(existingPath, newPath); } diff --git a/ext/node/polyfills/_fs/_fs_readFile.ts b/ext/node/polyfills/_fs/_fs_readFile.ts index b3bd5b94c7..04e42e391d 100644 --- a/ext/node/polyfills/_fs/_fs_readFile.ts +++ b/ext/node/polyfills/_fs/_fs_readFile.ts @@ -8,7 +8,7 @@ import { import { Buffer } from "ext:deno_node/buffer.ts"; import { readAll } from "ext:deno_io/12_io.js"; import { FileHandle } from "ext:deno_node/internal/fs/handle.ts"; -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { BinaryEncodings, Encodings, @@ -57,7 +57,7 @@ export function readFile( optOrCallback?: FileOptionsArgument | Callback | null | undefined, callback?: Callback, ) { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; let cb: Callback | undefined; if (typeof optOrCallback === "function") { cb = optOrCallback; @@ -105,7 +105,7 @@ export function readFileSync( path: string | URL, opt?: FileOptionsArgument, ): string | Buffer { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; const data = Deno.readFileSync(path); const encoding = getEncoding(opt); if (encoding && encoding !== "binary") { diff --git a/ext/node/polyfills/_fs/_fs_readlink.ts b/ext/node/polyfills/_fs/_fs_readlink.ts index 9823761529..c5e344c4bc 100644 --- a/ext/node/polyfills/_fs/_fs_readlink.ts +++ b/ext/node/polyfills/_fs/_fs_readlink.ts @@ -6,7 +6,7 @@ import { MaybeEmpty, notImplemented, } from "ext:deno_node/_utils.ts"; -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { promisify } from "ext:deno_node/internal/util.mjs"; type ReadlinkCallback = ( @@ -55,7 +55,7 @@ export function readlink( optOrCallback: ReadlinkCallback | ReadlinkOptions, callback?: ReadlinkCallback, ) { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; let cb: ReadlinkCallback | undefined; if (typeof optOrCallback === "function") { @@ -83,7 +83,7 @@ export function readlinkSync( path: string | URL, opt?: ReadlinkOptions, ): string | Uint8Array { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; return maybeEncode(Deno.readLinkSync(path), getEncoding(opt)); } diff --git a/ext/node/polyfills/_fs/_fs_rename.ts b/ext/node/polyfills/_fs/_fs_rename.ts index 5d87619c75..ff0a7246b8 100644 --- a/ext/node/polyfills/_fs/_fs_rename.ts +++ b/ext/node/polyfills/_fs/_fs_rename.ts @@ -1,5 +1,5 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { promisify } from "ext:deno_node/internal/util.mjs"; export function rename( @@ -7,8 +7,8 @@ export function rename( newPath: string | URL, callback: (err?: Error) => void, ) { - oldPath = oldPath instanceof URL ? fromFileUrl(oldPath) : oldPath; - newPath = newPath instanceof URL ? fromFileUrl(newPath) : newPath; + oldPath = oldPath instanceof URL ? pathFromURL(oldPath) : oldPath; + newPath = newPath instanceof URL ? pathFromURL(newPath) : newPath; if (!callback) throw new Error("No callback function supplied"); @@ -21,8 +21,8 @@ export const renamePromise = promisify(rename) as ( ) => Promise; export function renameSync(oldPath: string | URL, newPath: string | URL) { - oldPath = oldPath instanceof URL ? fromFileUrl(oldPath) : oldPath; - newPath = newPath instanceof URL ? fromFileUrl(newPath) : newPath; + oldPath = oldPath instanceof URL ? pathFromURL(oldPath) : oldPath; + newPath = newPath instanceof URL ? pathFromURL(newPath) : newPath; Deno.renameSync(oldPath, newPath); } diff --git a/ext/node/polyfills/_fs/_fs_symlink.ts b/ext/node/polyfills/_fs/_fs_symlink.ts index 23e4deaa5a..48da49dd11 100644 --- a/ext/node/polyfills/_fs/_fs_symlink.ts +++ b/ext/node/polyfills/_fs/_fs_symlink.ts @@ -1,6 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { promisify } from "ext:deno_node/internal/util.mjs"; type SymlinkType = "file" | "dir"; @@ -11,8 +11,8 @@ export function symlink( typeOrCallback: SymlinkType | CallbackWithError, maybeCallback?: CallbackWithError, ) { - target = target instanceof URL ? fromFileUrl(target) : target; - path = path instanceof URL ? fromFileUrl(path) : path; + target = target instanceof URL ? pathFromURL(target) : target; + path = path instanceof URL ? pathFromURL(path) : path; const type: SymlinkType = typeof typeOrCallback === "string" ? typeOrCallback @@ -38,8 +38,8 @@ export function symlinkSync( path: string | URL, type?: SymlinkType, ) { - target = target instanceof URL ? fromFileUrl(target) : target; - path = path instanceof URL ? fromFileUrl(path) : path; + target = target instanceof URL ? pathFromURL(target) : target; + path = path instanceof URL ? pathFromURL(path) : path; type = type || "file"; Deno.symlinkSync(target, path, { type }); diff --git a/ext/node/polyfills/_fs/_fs_truncate.ts b/ext/node/polyfills/_fs/_fs_truncate.ts index b6d39352b5..596c112111 100644 --- a/ext/node/polyfills/_fs/_fs_truncate.ts +++ b/ext/node/polyfills/_fs/_fs_truncate.ts @@ -1,6 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { promisify } from "ext:deno_node/internal/util.mjs"; export function truncate( @@ -8,7 +8,7 @@ export function truncate( lenOrCallback: number | CallbackWithError, maybeCallback?: CallbackWithError, ) { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; const len: number | undefined = typeof lenOrCallback === "number" ? lenOrCallback : undefined; @@ -27,7 +27,7 @@ export const truncatePromise = promisify(truncate) as ( ) => Promise; export function truncateSync(path: string | URL, len?: number) { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; Deno.truncateSync(path, len); } diff --git a/ext/node/polyfills/_fs/_fs_utimes.ts b/ext/node/polyfills/_fs/_fs_utimes.ts index 93b108f008..0fef8fd05f 100644 --- a/ext/node/polyfills/_fs/_fs_utimes.ts +++ b/ext/node/polyfills/_fs/_fs_utimes.ts @@ -1,7 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import type { CallbackWithError } from "ext:deno_node/_fs/_fs_common.ts"; -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { promisify } from "ext:deno_node/internal/util.mjs"; function getValidTime( @@ -30,7 +30,7 @@ export function utimes( mtime: number | string | Date, callback: CallbackWithError, ) { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; if (!callback) { throw new Deno.errors.InvalidData("No callback function supplied"); @@ -53,7 +53,7 @@ export function utimesSync( atime: number | string | Date, mtime: number | string | Date, ) { - path = path instanceof URL ? fromFileUrl(path) : path; + path = path instanceof URL ? pathFromURL(path) : path; atime = getValidTime(atime, "atime"); mtime = getValidTime(mtime, "mtime"); diff --git a/ext/node/polyfills/_fs/_fs_writeFile.ts b/ext/node/polyfills/_fs/_fs_writeFile.ts index a72d491471..fd573147b4 100644 --- a/ext/node/polyfills/_fs/_fs_writeFile.ts +++ b/ext/node/polyfills/_fs/_fs_writeFile.ts @@ -1,6 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { Encodings } from "ext:deno_node/_utils.ts"; -import { fromFileUrl } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { Buffer } from "ext:deno_node/buffer.ts"; import { CallbackWithError, @@ -41,7 +41,7 @@ export function writeFile( throw new TypeError("Callback must be a function."); } - pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid; + pathOrRid = pathOrRid instanceof URL ? pathFromURL(pathOrRid) : pathOrRid; const flag: string | undefined = isFileOptions(options) ? options.flag @@ -107,7 +107,7 @@ export function writeFileSync( data: string | Uint8Array | Object, options?: Encodings | WriteFileOptions, ) { - pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid; + pathOrRid = pathOrRid instanceof URL ? pathFromURL(pathOrRid) : pathOrRid; const flag: string | undefined = isFileOptions(options) ? options.flag diff --git a/ext/node/polyfills/path/_posix.ts b/ext/node/polyfills/path/_posix.ts index 3f42bbe866..7190795a38 100644 --- a/ext/node/polyfills/path/_posix.ts +++ b/ext/node/polyfills/path/_posix.ts @@ -477,24 +477,6 @@ export function parse(path: string): ParsedPath { return ret; } -/** - * Converts a file URL to a path string. - * - * ```ts - * fromFileUrl("file:///home/foo"); // "/home/foo" - * ``` - * @param url of a file URL - */ -export function fromFileUrl(url: string | URL): string { - url = url instanceof URL ? url : new URL(url); - if (url.protocol != "file:") { - throw new TypeError("Must be a file URL."); - } - return decodeURIComponent( - url.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"), - ); -} - /** * Converts a path string to a file URL. * @@ -513,14 +495,12 @@ export function toFileUrl(path: string): URL { ); return url; } - export default { basename, delimiter, dirname, extname, format, - fromFileUrl, isAbsolute, join, normalize, diff --git a/ext/node/polyfills/path/_win32.ts b/ext/node/polyfills/path/_win32.ts index 73321e7090..856ffdff5d 100644 --- a/ext/node/polyfills/path/_win32.ts +++ b/ext/node/polyfills/path/_win32.ts @@ -951,33 +951,6 @@ export function parse(path: string): ParsedPath { return ret; } -/** - * Converts a file URL to a path string. - * - * ```ts - * fromFileUrl("file:///home/foo"); // "\\home\\foo" - * fromFileUrl("file:///C:/Users/foo"); // "C:\\Users\\foo" - * fromFileUrl("file://localhost/home/foo"); // "\\\\localhost\\home\\foo" - * ``` - * @param url of a file URL - */ -export function fromFileUrl(url: string | URL): string { - url = url instanceof URL ? url : new URL(url); - if (url.protocol != "file:") { - throw new TypeError("Must be a file URL."); - } - let path = decodeURIComponent( - url.pathname.replace(/\//g, "\\").replace(/%(?![0-9A-Fa-f]{2})/g, "%25"), - ).replace(/^\\*([A-Za-z]:)(\\|$)/, "$1\\"); - if (url.hostname != "") { - // Note: The `URL` implementation guarantees that the drive letter and - // hostname are mutually exclusive. Otherwise it would not have been valid - // to append the hostname and path like this. - path = `\\\\${url.hostname}${path}`; - } - return path; -} - /** * Converts a path string to a file URL. * @@ -1012,7 +985,6 @@ export default { dirname, extname, format, - fromFileUrl, isAbsolute, join, normalize, diff --git a/ext/node/polyfills/path/mod.ts b/ext/node/polyfills/path/mod.ts index ee231e17d7..92611d5023 100644 --- a/ext/node/polyfills/path/mod.ts +++ b/ext/node/polyfills/path/mod.ts @@ -28,7 +28,6 @@ export const { dirname, extname, format, - fromFileUrl, isAbsolute, join, normalize, diff --git a/ext/node/polyfills/path/posix.ts b/ext/node/polyfills/path/posix.ts index 40e291f270..17e8900857 100644 --- a/ext/node/polyfills/path/posix.ts +++ b/ext/node/polyfills/path/posix.ts @@ -10,7 +10,6 @@ export const { dirname, extname, format, - fromFileUrl, isAbsolute, join, normalize, diff --git a/ext/node/polyfills/path/win32.ts b/ext/node/polyfills/path/win32.ts index e847b0a3bb..b015c0eba1 100644 --- a/ext/node/polyfills/path/win32.ts +++ b/ext/node/polyfills/path/win32.ts @@ -10,7 +10,6 @@ export const { dirname, extname, format, - fromFileUrl, isAbsolute, join, normalize, diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts index b676e87d75..444dc12e4b 100644 --- a/ext/node/polyfills/process.ts +++ b/ext/node/polyfills/process.ts @@ -13,7 +13,8 @@ import { } from "ext:deno_node/internal/errors.ts"; import { getOptionValue } from "ext:deno_node/internal/options.ts"; import { assert } from "ext:deno_node/_util/asserts.ts"; -import { fromFileUrl, join } from "ext:deno_node/path.ts"; +import { join } from "ext:deno_node/path.ts"; +import { pathFromURL } from "ext:deno_web/00_infra.js"; import { arch as arch_, chdir, @@ -705,7 +706,7 @@ internals.__bootstrapNodeProcess = function ( Object.defineProperty(argv, "1", { get: () => { if (Deno.mainModule.startsWith("file:")) { - return fromFileUrl(Deno.mainModule); + return pathFromURL(new URL(Deno.mainModule)); } else { return join(Deno.cwd(), "$deno$node.js"); }