mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
fix(ext/node): remove path.toFileUrl (#19536)
This commit is contained in:
parent
d8293cd8bc
commit
b37b286f7f
8 changed files with 77 additions and 76 deletions
|
@ -30,6 +30,8 @@
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const posix = require('path/posix');
|
||||||
|
const win32 = require('path/win32');
|
||||||
|
|
||||||
// Test thrown TypeErrors
|
// Test thrown TypeErrors
|
||||||
const typeErrorTests = [true, false, 7, null, {}, undefined, [], NaN];
|
const typeErrorTests = [true, false, 7, null, {}, undefined, [], NaN];
|
||||||
|
@ -74,8 +76,15 @@ assert.strictEqual(path.win32.delimiter, ';');
|
||||||
// posix
|
// posix
|
||||||
assert.strictEqual(path.posix.delimiter, ':');
|
assert.strictEqual(path.posix.delimiter, ':');
|
||||||
|
|
||||||
// TODO(wafuwafu13): Enable this
|
if (common.isWindows)
|
||||||
// if (common.isWindows)
|
assert.strictEqual(path, path.win32);
|
||||||
// assert.strictEqual(path, path.win32);
|
else
|
||||||
// else
|
assert.strictEqual(path, path.posix);
|
||||||
// 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);
|
||||||
|
|
|
@ -12,7 +12,6 @@ import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
|
||||||
import {
|
import {
|
||||||
_format,
|
_format,
|
||||||
assertPath,
|
assertPath,
|
||||||
encodeWhitespace,
|
|
||||||
isPosixPathSeparator,
|
isPosixPathSeparator,
|
||||||
normalizeString,
|
normalizeString,
|
||||||
} from "ext:deno_node/path/_util.ts";
|
} from "ext:deno_node/path/_util.ts";
|
||||||
|
@ -476,25 +475,6 @@ export function parse(path: string): ParsedPath {
|
||||||
|
|
||||||
return ret;
|
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 {
|
export default {
|
||||||
basename,
|
basename,
|
||||||
delimiter,
|
delimiter,
|
||||||
|
@ -508,6 +488,5 @@ export default {
|
||||||
relative,
|
relative,
|
||||||
resolve,
|
resolve,
|
||||||
sep,
|
sep,
|
||||||
toFileUrl,
|
|
||||||
toNamespacedPath,
|
toNamespacedPath,
|
||||||
};
|
};
|
||||||
|
|
|
@ -114,18 +114,3 @@ export function _format(
|
||||||
if (dir === pathObject.root) return dir + base;
|
if (dir === pathObject.root) return dir + base;
|
||||||
return dir + sep + 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;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
|
@ -17,7 +17,6 @@ import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
|
||||||
import {
|
import {
|
||||||
_format,
|
_format,
|
||||||
assertPath,
|
assertPath,
|
||||||
encodeWhitespace,
|
|
||||||
isPathSeparator,
|
isPathSeparator,
|
||||||
isWindowsDeviceRoot,
|
isWindowsDeviceRoot,
|
||||||
normalizeString,
|
normalizeString,
|
||||||
|
@ -951,34 +950,6 @@ export function parse(path: string): ParsedPath {
|
||||||
return ret;
|
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 {
|
export default {
|
||||||
basename,
|
basename,
|
||||||
delimiter,
|
delimiter,
|
||||||
|
@ -992,6 +963,5 @@ export default {
|
||||||
relative,
|
relative,
|
||||||
resolve,
|
resolve,
|
||||||
sep,
|
sep,
|
||||||
toFileUrl,
|
|
||||||
toNamespacedPath,
|
toNamespacedPath,
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,7 +35,6 @@ export const {
|
||||||
relative,
|
relative,
|
||||||
resolve,
|
resolve,
|
||||||
sep,
|
sep,
|
||||||
toFileUrl,
|
|
||||||
toNamespacedPath,
|
toNamespacedPath,
|
||||||
} = path;
|
} = path;
|
||||||
export default path;
|
export default path;
|
||||||
|
|
|
@ -17,7 +17,6 @@ export const {
|
||||||
relative,
|
relative,
|
||||||
resolve,
|
resolve,
|
||||||
sep,
|
sep,
|
||||||
toFileUrl,
|
|
||||||
toNamespacedPath,
|
toNamespacedPath,
|
||||||
} = path.posix;
|
} = path.posix;
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@ export const {
|
||||||
relative,
|
relative,
|
||||||
resolve,
|
resolve,
|
||||||
sep,
|
sep,
|
||||||
toFileUrl,
|
|
||||||
toNamespacedPath,
|
toNamespacedPath,
|
||||||
} = path.win32;
|
} = path.win32;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||||
// Copyright Joyent and Node contributors. 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 { notImplemented } from "ext:deno_node/_utils.ts";
|
||||||
import { EventEmitter, once } from "ext:deno_node/events.ts";
|
import { EventEmitter, once } from "ext:deno_node/events.ts";
|
||||||
import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js";
|
import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js";
|
||||||
|
@ -32,6 +32,67 @@ export interface WorkerOptions {
|
||||||
workerData?: unknown;
|
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 kHandle = Symbol("kHandle");
|
||||||
const PRIVATE_WORKER_THREAD_NAME = "$DENO_STD_NODE_WORKER_THREAD";
|
const PRIVATE_WORKER_THREAD_NAME = "$DENO_STD_NODE_WORKER_THREAD";
|
||||||
class _Worker extends EventEmitter {
|
class _Worker extends EventEmitter {
|
||||||
|
@ -68,7 +129,7 @@ class _Worker extends EventEmitter {
|
||||||
specifier =
|
specifier =
|
||||||
`data:text/javascript,(async function() {const { createRequire } = await import("node:module");const require = createRequire("${cwdFileUrl}");require("${specifier}");})();`;
|
`data:text/javascript,(async function() {const { createRequire } = await import("node:module");const require = createRequire("${cwdFileUrl}");require("${specifier}");})();`;
|
||||||
} else {
|
} else {
|
||||||
specifier = toFileUrl(specifier);
|
specifier = toFileUrl(specifier as string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const handle = this[kHandle] = new Worker(
|
const handle = this[kHandle] = new Worker(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue