// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { assertArg } from "../_common/from_file_url.ts";

/**
 * Converts a file URL to a path string.
 *
 * ```ts
 * import { fromFileUrl } from "@std/path/posix/from-file-url";
 *
 * fromFileUrl("file:///home/foo"); // "/home/foo"
 * ```
 * @param url of a file URL
 */
export function fromFileUrl(url: URL | string): string {
  url = assertArg(url);
  return decodeURIComponent(
    url.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
  );
}

// denoCacheMetadata={"headers":{"cache-control":"public, max-age=31536000, immutable","content-length":"560","x-jsr-backend":"modules","etag":"\"53317dade365c9e6b14149db7538f876\"","accept-ranges":"bytes","cf-cache-status":"HIT","cf-ray":"a223e6585c077db6-LAX","access-control-allow-origin":"*","server":"cloudflare","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","cross-origin-resource-policy":"cross-origin","set-cookie":"__cf_bm=vX8E9F6nb8NHNTBVVfD6.R_pBnw4KeRiSyf63ou6XRs-1785241875.2556124-1.0.1.1-5iLiq3xCNv909EDDQJRnOw78ekNMaBpZZ_mmyEWE1UDTZF7gI8wnGxqlVr.eFlfXYhjzmuTPGrtOcObsG3Cm3AevL_f.phgP59_d_V8yVXnCY9HI_9Lx5ltCvynp218f; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","age":"3518757","content-type":"text/typescript","x-content-type-options":"nosniff","x-robots-tag":"noindex","date":"Tue, 28 Jul 2026 12:31:15 GMT","alt-svc":"h3=\":443\"; ma=86400","access-control-expose-headers":"*","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=VbD6c9ePWOBRVQjo1tDEWsaeRe2geV0EDmmN8qFOPspAZpmQ2aafAL0SY0s%2BTSCWlevIvaltDLUgigluDREf%2FLUm3BxVwHrP1Lp4c1Q9M%2BTK0L5mFPI3SnQ%3D\"}]}","vary":"Origin"},"url":"https://jsr.io/@std/path/0.224.0/posix/from_file_url.ts","time":1785241875}