mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 10:59:13 +00:00
refactor: move "pathFromURL" to deno_web extension (#18037)
This API is required by several extensions like "ext/node", "ext/ffi" and also FS APIs that we want to move to a separate crate. Because of that "pathFromURL" API was moved to "deno_web" extension so other extension crates can rely on it.
This commit is contained in:
parent
1ab16e2426
commit
76b173b60c
8 changed files with 89 additions and 75 deletions
|
@ -1,18 +1,10 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
const core = globalThis.Deno.core;
|
||||
const internals = globalThis.__bootstrap.internals;
|
||||
const primordials = globalThis.__bootstrap.primordials;
|
||||
const {
|
||||
decodeURIComponent,
|
||||
ObjectPrototypeIsPrototypeOf,
|
||||
Promise,
|
||||
SafeArrayIterator,
|
||||
SafeRegExp,
|
||||
StringPrototypeReplace,
|
||||
TypeError,
|
||||
} = primordials;
|
||||
import { URLPrototype } from "internal:deno_url/00_url.js";
|
||||
let logDebug = false;
|
||||
let logSource = "JS";
|
||||
|
||||
|
@ -46,64 +38,6 @@ function createResolvable() {
|
|||
return promise;
|
||||
}
|
||||
|
||||
// Keep in sync with `fromFileUrl()` in `std/path/win32.ts`.
|
||||
function pathFromURLWin32(url) {
|
||||
let p = StringPrototypeReplace(
|
||||
url.pathname,
|
||||
new SafeRegExp(/^\/*([A-Za-z]:)(\/|$)/),
|
||||
"$1/",
|
||||
);
|
||||
p = StringPrototypeReplace(
|
||||
p,
|
||||
/\//g,
|
||||
"\\",
|
||||
);
|
||||
p = StringPrototypeReplace(
|
||||
p,
|
||||
new SafeRegExp(/%(?![0-9A-Fa-f]{2})/g),
|
||||
"%25",
|
||||
);
|
||||
let path = decodeURIComponent(p);
|
||||
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;
|
||||
}
|
||||
|
||||
// Keep in sync with `fromFileUrl()` in `std/path/posix.ts`.
|
||||
function pathFromURLPosix(url) {
|
||||
if (url.hostname !== "") {
|
||||
throw new TypeError(`Host must be empty.`);
|
||||
}
|
||||
|
||||
return decodeURIComponent(
|
||||
StringPrototypeReplace(
|
||||
url.pathname,
|
||||
new SafeRegExp(/%(?![0-9A-Fa-f]{2})/g),
|
||||
"%25",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function pathFromURL(pathOrUrl) {
|
||||
if (ObjectPrototypeIsPrototypeOf(URLPrototype, pathOrUrl)) {
|
||||
if (pathOrUrl.protocol != "file:") {
|
||||
throw new TypeError("Must be a file URL.");
|
||||
}
|
||||
|
||||
return core.build.os == "windows"
|
||||
? pathFromURLWin32(pathOrUrl)
|
||||
: pathFromURLPosix(pathOrUrl);
|
||||
}
|
||||
return pathOrUrl;
|
||||
}
|
||||
|
||||
// TODO(bartlomieju): remove
|
||||
internals.pathFromURL = pathFromURL;
|
||||
|
||||
function writable(value) {
|
||||
return {
|
||||
value,
|
||||
|
@ -145,7 +79,6 @@ export {
|
|||
getterOnly,
|
||||
log,
|
||||
nonEnumerable,
|
||||
pathFromURL,
|
||||
readOnly,
|
||||
setLogDebug,
|
||||
writable,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue