0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(ext/node): remove path.toFileUrl (#19536)

This commit is contained in:
Ryan Clements 2023-06-26 00:08:17 -04:00 committed by GitHub
parent d8293cd8bc
commit b37b286f7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 77 additions and 76 deletions

View file

@ -30,6 +30,8 @@
const common = require('../common');
const assert = require('assert');
const path = require('path');
const posix = require('path/posix');
const win32 = require('path/win32');
// Test thrown TypeErrors
const typeErrorTests = [true, false, 7, null, {}, undefined, [], NaN];
@ -74,8 +76,15 @@ assert.strictEqual(path.win32.delimiter, ';');
// posix
assert.strictEqual(path.posix.delimiter, ':');
// TODO(wafuwafu13): Enable this
// if (common.isWindows)
// assert.strictEqual(path, path.win32);
// else
// assert.strictEqual(path, path.posix);
if (common.isWindows)
assert.strictEqual(path, path.win32);
else
assert.strictEqual(path, path.posix);
// referential invariants
assert.strictEqual(path.posix, posix);
assert.strictEqual(path.win32, win32);
assert.strictEqual(path.posix, path.posix.posix);
assert.strictEqual(path.win32, path.posix.win32);
assert.strictEqual(path.posix, path.win32.posix);
assert.strictEqual(path.win32, path.win32.win32);

View file

@ -12,7 +12,6 @@ import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
import {
_format,
assertPath,
encodeWhitespace,
isPosixPathSeparator,
normalizeString,
} from "ext:deno_node/path/_util.ts";
@ -476,25 +475,6 @@ export function parse(path: string): ParsedPath {
return ret;
}
/**
* Converts a path string to a file URL.
*
* ```ts
* toFileUrl("/home/foo"); // new URL("file:///home/foo")
* ```
* @param path to convert to file URL
*/
export function toFileUrl(path: string): URL {
if (!isAbsolute(path)) {
throw new TypeError("Must be an absolute path.");
}
const url = new URL("file:///");
url.pathname = encodeWhitespace(
path.replace(/%/g, "%25").replace(/\\/g, "%5C"),
);
return url;
}
export default {
basename,
delimiter,
@ -508,6 +488,5 @@ export default {
relative,
resolve,
sep,
toFileUrl,
toNamespacedPath,
};

View file

@ -114,18 +114,3 @@ export function _format(
if (dir === pathObject.root) return dir + base;
return dir + sep + base;
}
const WHITESPACE_ENCODINGS: Record<string, string> = {
"\u0009": "%09",
"\u000A": "%0A",
"\u000B": "%0B",
"\u000C": "%0C",
"\u000D": "%0D",
"\u0020": "%20",
};
export function encodeWhitespace(string: string): string {
return string.replaceAll(/[\s]/g, (c) => {
return WHITESPACE_ENCODINGS[c] ?? c;
});
}

View file

@ -17,7 +17,6 @@ import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
import {
_format,
assertPath,
encodeWhitespace,
isPathSeparator,
isWindowsDeviceRoot,
normalizeString,
@ -951,34 +950,6 @@ export function parse(path: string): ParsedPath {
return ret;
}
/**
* Converts a path string to a file URL.
*
* ```ts
* toFileUrl("\\home\\foo"); // new URL("file:///home/foo")
* toFileUrl("C:\\Users\\foo"); // new URL("file:///C:/Users/foo")
* toFileUrl("\\\\127.0.0.1\\home\\foo"); // new URL("file://127.0.0.1/home/foo")
* ```
* @param path to convert to file URL
*/
export function toFileUrl(path: string): URL {
if (!isAbsolute(path)) {
throw new TypeError("Must be an absolute path.");
}
const [, hostname, pathname] = path.match(
/^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/,
)!;
const url = new URL("file:///");
url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25"));
if (hostname != null && hostname != "localhost") {
url.hostname = hostname;
if (!url.hostname) {
throw new TypeError("Invalid hostname.");
}
}
return url;
}
export default {
basename,
delimiter,
@ -992,6 +963,5 @@ export default {
relative,
resolve,
sep,
toFileUrl,
toNamespacedPath,
};

View file

@ -35,7 +35,6 @@ export const {
relative,
resolve,
sep,
toFileUrl,
toNamespacedPath,
} = path;
export default path;

View file

@ -17,7 +17,6 @@ export const {
relative,
resolve,
sep,
toFileUrl,
toNamespacedPath,
} = path.posix;

View file

@ -17,7 +17,6 @@ export const {
relative,
resolve,
sep,
toFileUrl,
toNamespacedPath,
} = path.win32;

View file

@ -1,7 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
import { resolve, toFileUrl } from "ext:deno_node/path.ts";
import { isAbsolute, resolve } from "ext:deno_node/path.ts";
import { notImplemented } from "ext:deno_node/_utils.ts";
import { EventEmitter, once } from "ext:deno_node/events.ts";
import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js";
@ -32,6 +32,67 @@ export interface WorkerOptions {
workerData?: unknown;
}
const WHITESPACE_ENCODINGS: Record<string, string> = {
"\u0009": "%09",
"\u000A": "%0A",
"\u000B": "%0B",
"\u000C": "%0C",
"\u000D": "%0D",
"\u0020": "%20",
};
function encodeWhitespace(string: string): string {
return string.replaceAll(/[\s]/g, (c) => {
return WHITESPACE_ENCODINGS[c] ?? c;
});
}
function toFileUrlPosix(path: string): URL {
if (!isAbsolute(path)) {
throw new TypeError("Must be an absolute path.");
}
const url = new URL("file:///");
url.pathname = encodeWhitespace(
path.replace(/%/g, "%25").replace(/\\/g, "%5C"),
);
return url;
}
function toFileUrlWin32(path: string): URL {
if (!isAbsolute(path)) {
throw new TypeError("Must be an absolute path.");
}
const [, hostname, pathname] = path.match(
/^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/,
)!;
const url = new URL("file:///");
url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25"));
if (hostname != null && hostname != "localhost") {
url.hostname = hostname;
if (!url.hostname) {
throw new TypeError("Invalid hostname.");
}
}
return url;
}
/**
* Converts a path string to a file URL.
*
* ```ts
* toFileUrl("/home/foo"); // new URL("file:///home/foo")
* toFileUrl("\\home\\foo"); // new URL("file:///home/foo")
* toFileUrl("C:\\Users\\foo"); // new URL("file:///C:/Users/foo")
* toFileUrl("\\\\127.0.0.1\\home\\foo"); // new URL("file://127.0.0.1/home/foo")
* ```
* @param path to convert to file URL
*/
function toFileUrl(path: string): URL {
return core.build.os == "windows"
? toFileUrlWin32(path)
: toFileUrlPosix(path);
}
const kHandle = Symbol("kHandle");
const PRIVATE_WORKER_THREAD_NAME = "$DENO_STD_NODE_WORKER_THREAD";
class _Worker extends EventEmitter {
@ -68,7 +129,7 @@ class _Worker extends EventEmitter {
specifier =
`data:text/javascript,(async function() {const { createRequire } = await import("node:module");const require = createRequire("${cwdFileUrl}");require("${specifier}");})();`;
} else {
specifier = toFileUrl(specifier);
specifier = toFileUrl(specifier as string);
}
}
const handle = this[kHandle] = new Worker(