summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/node_compat/test/parallel/test-path.js19
-rw-r--r--ext/node/polyfills/path/_posix.ts21
-rw-r--r--ext/node/polyfills/path/_util.ts15
-rw-r--r--ext/node/polyfills/path/_win32.ts30
-rw-r--r--ext/node/polyfills/path/mod.ts1
-rw-r--r--ext/node/polyfills/path/posix.ts1
-rw-r--r--ext/node/polyfills/path/win32.ts1
-rw-r--r--ext/node/polyfills/worker_threads.ts65
8 files changed, 77 insertions, 76 deletions
diff --git a/cli/tests/node_compat/test/parallel/test-path.js b/cli/tests/node_compat/test/parallel/test-path.js
index 03bbfebb2..657fdc951 100644
--- a/cli/tests/node_compat/test/parallel/test-path.js
+++ b/cli/tests/node_compat/test/parallel/test-path.js
@@ -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);
diff --git a/ext/node/polyfills/path/_posix.ts b/ext/node/polyfills/path/_posix.ts
index 7190795a3..29fecf131 100644
--- a/ext/node/polyfills/path/_posix.ts
+++ b/ext/node/polyfills/path/_posix.ts
@@ -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,
};
diff --git a/ext/node/polyfills/path/_util.ts b/ext/node/polyfills/path/_util.ts
index ea2afd873..fcf32d55c 100644
--- a/ext/node/polyfills/path/_util.ts
+++ b/ext/node/polyfills/path/_util.ts
@@ -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;
- });
-}
diff --git a/ext/node/polyfills/path/_win32.ts b/ext/node/polyfills/path/_win32.ts
index 856ffdff5..dccd00d3c 100644
--- a/ext/node/polyfills/path/_win32.ts
+++ b/ext/node/polyfills/path/_win32.ts
@@ -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,
};
diff --git a/ext/node/polyfills/path/mod.ts b/ext/node/polyfills/path/mod.ts
index 92611d502..f0f431d2b 100644
--- a/ext/node/polyfills/path/mod.ts
+++ b/ext/node/polyfills/path/mod.ts
@@ -35,7 +35,6 @@ export const {
relative,
resolve,
sep,
- toFileUrl,
toNamespacedPath,
} = path;
export default path;
diff --git a/ext/node/polyfills/path/posix.ts b/ext/node/polyfills/path/posix.ts
index 17e890085..12c4946b1 100644
--- a/ext/node/polyfills/path/posix.ts
+++ b/ext/node/polyfills/path/posix.ts
@@ -17,7 +17,6 @@ export const {
relative,
resolve,
sep,
- toFileUrl,
toNamespacedPath,
} = path.posix;
diff --git a/ext/node/polyfills/path/win32.ts b/ext/node/polyfills/path/win32.ts
index b015c0eba..b595c11eb 100644
--- a/ext/node/polyfills/path/win32.ts
+++ b/ext/node/polyfills/path/win32.ts
@@ -17,7 +17,6 @@ export const {
relative,
resolve,
sep,
- toFileUrl,
toNamespacedPath,
} = path.win32;
diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts
index 32f9885af..0529383cc 100644
--- a/ext/node/polyfills/worker_threads.ts
+++ b/ext/node/polyfills/worker_threads.ts
@@ -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(