@tauri-apps/plugin-fs 2.0.0 → 2.0.1
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 +1 -1
- package/dist-js/index.cjs +28 -6
- package/dist-js/index.js +28 -6
- package/package.json +1 -1
package/README.md
CHANGED
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
|
|
149
|
+
const data = await core.invoke('plugin:fs|read', {
|
|
137
150
|
rid: this.rid,
|
|
138
151
|
len: buffer.byteLength
|
|
139
152
|
});
|
|
140
|
-
|
|
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
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
641
|
+
const encoder = new TextEncoder();
|
|
642
|
+
await core.invoke('plugin:fs|write_text_file', encoder.encode(data), {
|
|
643
|
+
headers: {
|
|
644
|
+
path: 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
|
|
147
|
+
const data = await invoke('plugin:fs|read', {
|
|
135
148
|
rid: this.rid,
|
|
136
149
|
len: buffer.byteLength
|
|
137
150
|
});
|
|
138
|
-
|
|
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
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
639
|
+
const encoder = new TextEncoder();
|
|
640
|
+
await invoke('plugin:fs|write_text_file', encoder.encode(data), {
|
|
641
|
+
headers: {
|
|
642
|
+
path: path instanceof URL ? path.toString() : path,
|
|
643
|
+
options: JSON.stringify(options)
|
|
644
|
+
}
|
|
623
645
|
});
|
|
624
646
|
}
|
|
625
647
|
/**
|