@tauri-apps/plugin-fs 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,7 +12,7 @@ Access the file system.
12
12
 
13
13
  ## Install
14
14
 
15
- _This plugin requires a Rust version of at least **1.78**_
15
+ _This plugin requires a Rust version of at least **1.77.2**_
16
16
 
17
17
  There are three general methods of installation that we can recommend.
18
18
 
package/dist-js/index.cjs CHANGED
@@ -92,6 +92,19 @@ function parseFileInfo(r) {
92
92
  blocks: r.blocks
93
93
  };
94
94
  }
95
+ // https://gist.github.com/zapthedingbat/38ebfbedd98396624e5b5f2ff462611d
96
+ /** Converts a big-endian eight byte array to number */
97
+ function fromBytes(buffer) {
98
+ const bytes = new Uint8ClampedArray(buffer);
99
+ const size = bytes.byteLength;
100
+ let x = 0;
101
+ for (let i = 0; i < size; i++) {
102
+ const byte = bytes[i];
103
+ x *= 0x100;
104
+ x += byte;
105
+ }
106
+ return x;
107
+ }
95
108
  /**
96
109
  * The Tauri abstraction for reading and writing files.
97
110
  *
@@ -133,11 +146,18 @@ class FileHandle extends core.Resource {
133
146
  if (buffer.byteLength === 0) {
134
147
  return 0;
135
148
  }
136
- const [data, nread] = await core.invoke('plugin:fs|read', {
149
+ const data = await core.invoke('plugin:fs|read', {
137
150
  rid: this.rid,
138
151
  len: buffer.byteLength
139
152
  });
140
- buffer.set(data);
153
+ // Rust side will never return an empty array for this command and
154
+ // ensure there is at least 8 elements there.
155
+ //
156
+ // This is an optimization to include the number of read bytes (as bigendian bytes)
157
+ // at the end of returned array to avoid serialization overhead of separate values.
158
+ const nread = fromBytes(data.slice(-8));
159
+ const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
160
+ buffer.set(bytes.slice(0, bytes.length - 8));
141
161
  return nread === 0 ? null : nread;
142
162
  }
143
163
  /**
@@ -618,10 +638,12 @@ async function writeTextFile(path, data, options) {
618
638
  if (path instanceof URL && path.protocol !== 'file:') {
619
639
  throw new TypeError('Must be a file URL.');
620
640
  }
621
- await core.invoke('plugin:fs|write_text_file', {
622
- path: path instanceof URL ? path.toString() : path,
623
- data,
624
- options
641
+ const encoder = new TextEncoder();
642
+ await core.invoke('plugin:fs|write_text_file', encoder.encode(data), {
643
+ headers: {
644
+ path: encodeURIComponent(path instanceof URL ? path.toString() : path),
645
+ options: JSON.stringify(options)
646
+ }
625
647
  });
626
648
  }
627
649
  /**
package/dist-js/index.js CHANGED
@@ -90,6 +90,19 @@ function parseFileInfo(r) {
90
90
  blocks: r.blocks
91
91
  };
92
92
  }
93
+ // https://gist.github.com/zapthedingbat/38ebfbedd98396624e5b5f2ff462611d
94
+ /** Converts a big-endian eight byte array to number */
95
+ function fromBytes(buffer) {
96
+ const bytes = new Uint8ClampedArray(buffer);
97
+ const size = bytes.byteLength;
98
+ let x = 0;
99
+ for (let i = 0; i < size; i++) {
100
+ const byte = bytes[i];
101
+ x *= 0x100;
102
+ x += byte;
103
+ }
104
+ return x;
105
+ }
93
106
  /**
94
107
  * The Tauri abstraction for reading and writing files.
95
108
  *
@@ -131,11 +144,18 @@ class FileHandle extends Resource {
131
144
  if (buffer.byteLength === 0) {
132
145
  return 0;
133
146
  }
134
- const [data, nread] = await invoke('plugin:fs|read', {
147
+ const data = await invoke('plugin:fs|read', {
135
148
  rid: this.rid,
136
149
  len: buffer.byteLength
137
150
  });
138
- buffer.set(data);
151
+ // Rust side will never return an empty array for this command and
152
+ // ensure there is at least 8 elements there.
153
+ //
154
+ // This is an optimization to include the number of read bytes (as bigendian bytes)
155
+ // at the end of returned array to avoid serialization overhead of separate values.
156
+ const nread = fromBytes(data.slice(-8));
157
+ const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
158
+ buffer.set(bytes.slice(0, bytes.length - 8));
139
159
  return nread === 0 ? null : nread;
140
160
  }
141
161
  /**
@@ -616,10 +636,12 @@ async function writeTextFile(path, data, options) {
616
636
  if (path instanceof URL && path.protocol !== 'file:') {
617
637
  throw new TypeError('Must be a file URL.');
618
638
  }
619
- await invoke('plugin:fs|write_text_file', {
620
- path: path instanceof URL ? path.toString() : path,
621
- data,
622
- options
639
+ const encoder = new TextEncoder();
640
+ await invoke('plugin:fs|write_text_file', encoder.encode(data), {
641
+ headers: {
642
+ path: encodeURIComponent(path instanceof URL ? path.toString() : path),
643
+ options: JSON.stringify(options)
644
+ }
623
645
  });
624
646
  }
625
647
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-fs",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Access the file system.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "authors": [