温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/url/fileURLToPath
点击访问原文链接

Node url.fileURLToPath function | API Reference | Bun

Node url.fileURLToPath function | API Reference | BunBuildDocsReferenceGuidesBlogDiscord/node:url/fileURLToPathFfileURLToPath

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:url/fileURLToPathFfileURLToPath

function

url.fileURLToPathfunction fileURLToPath(url: string | URL,options?: FileUrlToPathOptions): string;

This function ensures the correct decodings of percent-encoded characters as well as ensuring a cross-platform valid absolute path string.

import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);

new URL('file:http:///C:/path/').pathname; // Incorrect: /C:/path/
fileURLToPath('file:http:///C:/path/'); // Correct: C:\path\ (Windows)

new URL('file:http://nas/foo.txt').pathname; // Incorrect: /foo.txt
fileURLToPath('file:http://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)

new URL('file:http:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
fileURLToPath('file:http:///你好.txt'); // Correct: /你好.txt (POSIX)

new URL('file:http:///hello world').pathname; // Incorrect: /hello%20world
fileURLToPath('file:http:///hello world'); // Correct: /hello world (POSIX)

Security Considerations:

This function decodes percent-encoded characters, including encoded dot-segments (%2e as . and %2e%2e as ..), and then normalizes the resulting path. This means that encoded directory traversal sequences (such as %2e%2e) are decoded and processed as actual path traversal, even though encoded slashes (%2F, %5C) are correctly rejected.

Applications must not rely on fileURLToPath() alone to prevent directory traversal attacks. Always perform explicit path validation and security checks on the returned path value to ensure it remains within expected boundaries before using it for file system operations.

@param url

The file URL string or URL object to convert to a path.

@returns

The fully-resolved platform-specific Node.js file path.

Referenced typesinterface URLhash: stringhost: stringhostname: stringhref: stringreadonly origin: stringpassword: stringpathname: stringport: stringprotocol: stringsearch: stringreadonly searchParams: URLSearchParamsusername: stringtoJSON(): string;interface FileUrlToPathOptionswindows?: boolean

true if the path should be return as a windows filepath, false for posix, and undefined for the system default.

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

Node url.fileURLToPath function | API Reference | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for function node:url.fileURLToPath | Bun