1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(ext/node): remove fromFileUrl from "node:path" (#19504)

This commit is contained in:
Ryan Clements 2023-06-16 06:43:59 -04:00 committed by GitHub
parent 239dc5e681
commit d32287d211
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 42 additions and 89 deletions

View file

@ -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",

View file

@ -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",

View file

@ -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]);

View file

@ -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;

View file

@ -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);
}

View file

@ -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") {

View file

@ -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));
}

View file

@ -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<void>;
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);
}

View file

@ -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 });

View file

@ -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<void>;
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);
}

View file

@ -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");

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -28,7 +28,6 @@ export const {
dirname,
extname,
format,
fromFileUrl,
isAbsolute,
join,
normalize,

View file

@ -10,7 +10,6 @@ export const {
dirname,
extname,
format,
fromFileUrl,
isAbsolute,
join,
normalize,

View file

@ -10,7 +10,6 @@ export const {
dirname,
extname,
format,
fromFileUrl,
isAbsolute,
join,
normalize,

View file

@ -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");
}