-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(fs): support any UTF-8 path in writeFile #1640
Conversation
…rt `path` to URL to avoid errors caused by Chinese characters.
Package Changes Through 841b5b1There are 1 changes which include fs with prerelease Planned Package VersionsThe following package releases are the planned based on the context of changes in this pull request.
Add another change file through the GitHub UI by following this link. Read about change files or the docs at github.com/jbolda/covector |
is this a problem only for writeFile? i expected this to happen for any FS API call.. |
Only the |
plugins/fs/guest-js/index.ts
Outdated
@@ -1009,6 +1009,10 @@ async function writeFile( | |||
throw new TypeError("Must be a file URL."); | |||
} | |||
|
|||
if (!options || !options.baseDir) { | |||
path = path instanceof URL ? path : new URL(encodeURI(path)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will throw if provided path is something like this ./file
or just file
, because new URL()
expects a valid URL e.g. <scheme>://<domain>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm aware of this issue and appreciate you pointing it out. To resolve this, I decided to abandon using new URL()
and instead used TextEncoder
, adding decoding logic on the Rust side.
如果是mac的话,path没有file://协议开头,new URL会出错的,还要把path加上file |
* In the `writeFile` function, when `options.baseDir` is not set, convert `path` to URL to avoid errors caused by Chinese characters. * fmt * use TextEncoder * use percent encoding * add change file * fmt --------- Co-authored-by: Lucas Nogueira <[email protected]>
improper input parsing
In the
writeFile
function, when path is a string that contains Chinese characters or spaces, andoptions.baseDir
is not provided, it can lead to errors. This PR introduces a fix: when path is a string and contains Chinese characters or spaces, it converts such paths to URL format, thus preventing these errors. As a result, paths obtained from save dialogs can be directly used in thewriteFile
function without requiring preliminary conversion to URL format.在writeFile函数中,当path为字符串且包含中文字符或空格时,如果没有提供options.baseDir,会导致错误。此PR引入了一个修复方案:当path为字符串并且包含中文字符或空格时,将其转换为URL格式,以防止这些错误的发生。这样一来,通过保存对话框获取的路径可以被直接用于writeFile函数,而不需要预先转换为URL格式。