feat(core): reimplement `readTextFile` for performance (#3631)

This commit is contained in:
Lucas Fernandes Nogueira 2022-03-07 11:34:33 -03:00 committed by GitHub
parent 06ab85b469
commit 834ccc5153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 30 deletions

View File

@ -0,0 +1,6 @@
---
"tauri": patch
"api": patch
---
Reimplement endpoint to read file as string for performance.

File diff suppressed because one or more lines are too long

View File

@ -72,11 +72,16 @@ pub struct FileOperationOptions {
#[derive(Deserialize, CommandModule)]
#[serde(tag = "cmd", rename_all = "camelCase")]
pub enum Cmd {
/// The read text file API.
/// The read binary file API.
ReadFile {
path: SafePathBuf,
options: Option<FileOperationOptions>,
},
/// The read binary file API.
ReadTextFile {
path: SafePathBuf,
options: Option<FileOperationOptions>,
},
/// The write file API.
WriteFile {
path: SafePathBuf,
@ -137,6 +142,24 @@ impl Cmd {
.map_err(Into::into)
}
#[module_command_handler(fs_read_file, "fs > readFile")]
fn read_text_file<R: Runtime>(
context: InvokeContext<R>,
path: SafePathBuf,
options: Option<FileOperationOptions>,
) -> super::Result<String> {
let resolved_path = resolve_path(
&context.config,
&context.package_info,
&context.window,
path,
options.and_then(|o| o.dir),
)?;
file::read_string(&resolved_path)
.with_context(|| format!("path: {}", resolved_path.0.display()))
.map_err(Into::into)
}
#[module_command_handler(fs_write_file, "fs > writeFile")]
fn write_file<R: Runtime>(
context: InvokeContext<R>,

File diff suppressed because one or more lines are too long

View File

@ -355,27 +355,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8"
[[package]]
name = "bzip2"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6afcd980b5f3a45017c57e57a2fcccbb351cc43a356ce117ef760ef8052b89b0"
dependencies = [
"bzip2-sys",
"libc",
]
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "cache-padded"
version = "1.2.0"
@ -4273,11 +4252,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93ab48844d61251bb3835145c521d88aa4031d7139e8485990f60ca911fa0815"
dependencies = [
"byteorder",
"bzip2",
"crc32fast",
"flate2",
"thiserror",
"time",
]
[[package]]

View File

@ -139,14 +139,14 @@ async function readTextFile(
filePath: string,
options: FsOptions = {}
): Promise<string> {
return invokeTauriCommand<number[]>({
return invokeTauriCommand<string>({
__tauriModule: 'Fs',
message: {
cmd: 'readFile',
cmd: 'readTextFile',
path: filePath,
options
}
}).then((data) => new TextDecoder().decode(new Uint8Array(data)))
})
}
/**