Skip to content

Commit 834ccc5

Browse files
authored
feat(core): reimplement readTextFile for performance (#3631)
1 parent 06ab85b commit 834ccc5

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

.changes/read-fs-read-file.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
"api": patch
4+
---
5+
6+
Reimplement endpoint to read file as string for performance.

core/tauri/scripts/bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/endpoints/file_system.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ pub struct FileOperationOptions {
7272
#[derive(Deserialize, CommandModule)]
7373
#[serde(tag = "cmd", rename_all = "camelCase")]
7474
pub enum Cmd {
75-
/// The read text file API.
75+
/// The read binary file API.
7676
ReadFile {
7777
path: SafePathBuf,
7878
options: Option<FileOperationOptions>,
7979
},
80+
/// The read binary file API.
81+
ReadTextFile {
82+
path: SafePathBuf,
83+
options: Option<FileOperationOptions>,
84+
},
8085
/// The write file API.
8186
WriteFile {
8287
path: SafePathBuf,
@@ -137,6 +142,24 @@ impl Cmd {
137142
.map_err(Into::into)
138143
}
139144

145+
#[module_command_handler(fs_read_file, "fs > readFile")]
146+
fn read_text_file<R: Runtime>(
147+
context: InvokeContext<R>,
148+
path: SafePathBuf,
149+
options: Option<FileOperationOptions>,
150+
) -> super::Result<String> {
151+
let resolved_path = resolve_path(
152+
&context.config,
153+
&context.package_info,
154+
&context.window,
155+
path,
156+
options.and_then(|o| o.dir),
157+
)?;
158+
file::read_string(&resolved_path)
159+
.with_context(|| format!("path: {}", resolved_path.0.display()))
160+
.map_err(Into::into)
161+
}
162+
140163
#[module_command_handler(fs_write_file, "fs > writeFile")]
141164
fn write_file<R: Runtime>(
142165
context: InvokeContext<R>,

examples/api/dist/assets/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src-tauri/Cargo.lock

-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/api/src/fs.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ async function readTextFile(
139139
filePath: string,
140140
options: FsOptions = {}
141141
): Promise<string> {
142-
return invokeTauriCommand<number[]>({
142+
return invokeTauriCommand<string>({
143143
__tauriModule: 'Fs',
144144
message: {
145-
cmd: 'readFile',
145+
cmd: 'readTextFile',
146146
path: filePath,
147147
options
148148
}
149-
}).then((data) => new TextDecoder().decode(new Uint8Array(data)))
149+
})
150150
}
151151

152152
/**

0 commit comments

Comments
 (0)