@tauri-apps/plugin-fs 2.0.0-rc.2 → 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 CHANGED
@@ -2,9 +2,17 @@
2
2
 
3
3
  Access the file system.
4
4
 
5
+ | Platform | Supported |
6
+ | -------- | --------- |
7
+ | Linux | ✓ |
8
+ | Windows | ✓ |
9
+ | macOS | ✓ |
10
+ | Android | ✓ |
11
+ | iOS | ✓ |
12
+
5
13
  ## Install
6
14
 
7
- _This plugin requires a Rust version of at least **1.75**_
15
+ _This plugin requires a Rust version of at least **1.77.2**_
8
16
 
9
17
  There are three general methods of installation that we can recommend.
10
18
 
@@ -18,7 +26,7 @@ Install the Core plugin by adding the following to your `Cargo.toml` file:
18
26
 
19
27
  ```toml
20
28
  [dependencies]
21
- tauri-plugin-fs = "2.0.0-rc"
29
+ tauri-plugin-fs = "2.0.0"
22
30
  # alternatively with Git:
23
31
  tauri-plugin-fs = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
24
32
  ```
@@ -60,9 +68,9 @@ fn main() {
60
68
  Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
61
69
 
62
70
  ```javascript
63
- import { metadata } from "@tauri-apps/plugin-fs";
71
+ import { metadata } from '@tauri-apps/plugin-fs'
64
72
 
65
- await metadata("/path/to/file");
73
+ await metadata('/path/to/file')
66
74
  ```
67
75
 
68
76
  ## Contributing
package/dist-js/index.cjs CHANGED
@@ -89,9 +89,22 @@ function parseFileInfo(r) {
89
89
  gid: r.gid,
90
90
  rdev: r.rdev,
91
91
  blksize: r.blksize,
92
- blocks: r.blocks,
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
  *
@@ -119,7 +132,7 @@ class FileHandle extends core.Resource {
119
132
  * @example
120
133
  * ```typescript
121
134
  * import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
122
- * // if "$APP/foo/bar.txt" contains the text "hello world":
135
+ * // if "$APPCONFIG/foo/bar.txt" contains the text "hello world":
123
136
  * const file = await open("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
124
137
  * const buf = new Uint8Array(100);
125
138
  * const numberOfBytesRead = await file.read(buf); // 11 bytes
@@ -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
- len: buffer.byteLength,
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
  /**
@@ -173,10 +193,10 @@ class FileHandle extends core.Resource {
173
193
  * @since 2.0.0
174
194
  */
175
195
  async seek(offset, whence) {
176
- return await core.invoke("plugin:fs|seek", {
196
+ return await core.invoke('plugin:fs|seek', {
177
197
  rid: this.rid,
178
198
  offset,
179
- whence,
199
+ whence
180
200
  });
181
201
  }
182
202
  /**
@@ -194,8 +214,8 @@ class FileHandle extends core.Resource {
194
214
  * @since 2.0.0
195
215
  */
196
216
  async stat() {
197
- const res = await core.invoke("plugin:fs|fstat", {
198
- rid: this.rid,
217
+ const res = await core.invoke('plugin:fs|fstat', {
218
+ rid: this.rid
199
219
  });
200
220
  return parseFileInfo(res);
201
221
  }
@@ -224,9 +244,9 @@ class FileHandle extends core.Resource {
224
244
  * @since 2.0.0
225
245
  */
226
246
  async truncate(len) {
227
- await core.invoke("plugin:fs|ftruncate", {
247
+ await core.invoke('plugin:fs|ftruncate', {
228
248
  rid: this.rid,
229
- len,
249
+ len
230
250
  });
231
251
  }
232
252
  /**
@@ -250,9 +270,9 @@ class FileHandle extends core.Resource {
250
270
  * @since 2.0.0
251
271
  */
252
272
  async write(data) {
253
- return await core.invoke("plugin:fs|write", {
273
+ return await core.invoke('plugin:fs|write', {
254
274
  rid: this.rid,
255
- data,
275
+ data
256
276
  });
257
277
  }
258
278
  }
@@ -271,12 +291,12 @@ class FileHandle extends core.Resource {
271
291
  * @since 2.0.0
272
292
  */
273
293
  async function create(path, options) {
274
- if (path instanceof URL && path.protocol !== "file:") {
275
- throw new TypeError("Must be a file URL.");
294
+ if (path instanceof URL && path.protocol !== 'file:') {
295
+ throw new TypeError('Must be a file URL.');
276
296
  }
277
- const rid = await core.invoke("plugin:fs|create", {
297
+ const rid = await core.invoke('plugin:fs|create', {
278
298
  path: path instanceof URL ? path.toString() : path,
279
- options,
299
+ options
280
300
  });
281
301
  return new FileHandle(rid);
282
302
  }
@@ -297,12 +317,12 @@ async function create(path, options) {
297
317
  * @since 2.0.0
298
318
  */
299
319
  async function open(path, options) {
300
- if (path instanceof URL && path.protocol !== "file:") {
301
- throw new TypeError("Must be a file URL.");
320
+ if (path instanceof URL && path.protocol !== 'file:') {
321
+ throw new TypeError('Must be a file URL.');
302
322
  }
303
- const rid = await core.invoke("plugin:fs|open", {
323
+ const rid = await core.invoke('plugin:fs|open', {
304
324
  path: path instanceof URL ? path.toString() : path,
305
- options,
325
+ options
306
326
  });
307
327
  return new FileHandle(rid);
308
328
  }
@@ -317,14 +337,14 @@ async function open(path, options) {
317
337
  * @since 2.0.0
318
338
  */
319
339
  async function copyFile(fromPath, toPath, options) {
320
- if ((fromPath instanceof URL && fromPath.protocol !== "file:") ||
321
- (toPath instanceof URL && toPath.protocol !== "file:")) {
322
- throw new TypeError("Must be a file URL.");
340
+ if ((fromPath instanceof URL && fromPath.protocol !== 'file:') ||
341
+ (toPath instanceof URL && toPath.protocol !== 'file:')) {
342
+ throw new TypeError('Must be a file URL.');
323
343
  }
324
- await core.invoke("plugin:fs|copy_file", {
344
+ await core.invoke('plugin:fs|copy_file', {
325
345
  fromPath: fromPath instanceof URL ? fromPath.toString() : fromPath,
326
346
  toPath: toPath instanceof URL ? toPath.toString() : toPath,
327
- options,
347
+ options
328
348
  });
329
349
  }
330
350
  /**
@@ -338,12 +358,12 @@ async function copyFile(fromPath, toPath, options) {
338
358
  * @since 2.0.0
339
359
  */
340
360
  async function mkdir(path, options) {
341
- if (path instanceof URL && path.protocol !== "file:") {
342
- throw new TypeError("Must be a file URL.");
361
+ if (path instanceof URL && path.protocol !== 'file:') {
362
+ throw new TypeError('Must be a file URL.');
343
363
  }
344
- await core.invoke("plugin:fs|mkdir", {
364
+ await core.invoke('plugin:fs|mkdir', {
345
365
  path: path instanceof URL ? path.toString() : path,
346
- options,
366
+ options
347
367
  });
348
368
  }
349
369
  /**
@@ -369,12 +389,12 @@ async function mkdir(path, options) {
369
389
  * @since 2.0.0
370
390
  */
371
391
  async function readDir(path, options) {
372
- if (path instanceof URL && path.protocol !== "file:") {
373
- throw new TypeError("Must be a file URL.");
392
+ if (path instanceof URL && path.protocol !== 'file:') {
393
+ throw new TypeError('Must be a file URL.');
374
394
  }
375
- return await core.invoke("plugin:fs|read_dir", {
395
+ return await core.invoke('plugin:fs|read_dir', {
376
396
  path: path instanceof URL ? path.toString() : path,
377
- options,
397
+ options
378
398
  });
379
399
  }
380
400
  /**
@@ -389,16 +409,14 @@ async function readDir(path, options) {
389
409
  * @since 2.0.0
390
410
  */
391
411
  async function readFile(path, options) {
392
- if (path instanceof URL && path.protocol !== "file:") {
393
- throw new TypeError("Must be a file URL.");
412
+ if (path instanceof URL && path.protocol !== 'file:') {
413
+ throw new TypeError('Must be a file URL.');
394
414
  }
395
- const arr = await core.invoke("plugin:fs|read_file", {
415
+ const arr = await core.invoke('plugin:fs|read_file', {
396
416
  path: path instanceof URL ? path.toString() : path,
397
- options,
417
+ options
398
418
  });
399
- return arr instanceof ArrayBuffer
400
- ? new Uint8Array(arr)
401
- : Uint8Array.from(arr);
419
+ return arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr);
402
420
  }
403
421
  /**
404
422
  * Reads and returns the entire contents of a file as UTF-8 string.
@@ -411,12 +429,12 @@ async function readFile(path, options) {
411
429
  * @since 2.0.0
412
430
  */
413
431
  async function readTextFile(path, options) {
414
- if (path instanceof URL && path.protocol !== "file:") {
415
- throw new TypeError("Must be a file URL.");
432
+ if (path instanceof URL && path.protocol !== 'file:') {
433
+ throw new TypeError('Must be a file URL.');
416
434
  }
417
- return await core.invoke("plugin:fs|read_text_file", {
435
+ return await core.invoke('plugin:fs|read_text_file', {
418
436
  path: path instanceof URL ? path.toString() : path,
419
- options,
437
+ options
420
438
  });
421
439
  }
422
440
  /**
@@ -435,8 +453,8 @@ async function readTextFile(path, options) {
435
453
  * @since 2.0.0
436
454
  */
437
455
  async function readTextFileLines(path, options) {
438
- if (path instanceof URL && path.protocol !== "file:") {
439
- throw new TypeError("Must be a file URL.");
456
+ if (path instanceof URL && path.protocol !== 'file:') {
457
+ throw new TypeError('Must be a file URL.');
440
458
  }
441
459
  const pathStr = path instanceof URL ? path.toString() : path;
442
460
  return await Promise.resolve({
@@ -444,23 +462,23 @@ async function readTextFileLines(path, options) {
444
462
  rid: null,
445
463
  async next() {
446
464
  if (this.rid === null) {
447
- this.rid = await core.invoke("plugin:fs|read_text_file_lines", {
465
+ this.rid = await core.invoke('plugin:fs|read_text_file_lines', {
448
466
  path: pathStr,
449
- options,
467
+ options
450
468
  });
451
469
  }
452
- const [line, done] = await core.invoke("plugin:fs|read_text_file_lines_next", { rid: this.rid });
470
+ const [line, done] = await core.invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
453
471
  // an iteration is over, reset rid for next iteration
454
472
  if (done)
455
473
  this.rid = null;
456
474
  return {
457
- value: done ? "" : line,
458
- done,
475
+ value: done ? '' : line,
476
+ done
459
477
  };
460
478
  },
461
479
  [Symbol.asyncIterator]() {
462
480
  return this;
463
- },
481
+ }
464
482
  });
465
483
  }
466
484
  /**
@@ -476,12 +494,12 @@ async function readTextFileLines(path, options) {
476
494
  * @since 2.0.0
477
495
  */
478
496
  async function remove(path, options) {
479
- if (path instanceof URL && path.protocol !== "file:") {
480
- throw new TypeError("Must be a file URL.");
497
+ if (path instanceof URL && path.protocol !== 'file:') {
498
+ throw new TypeError('Must be a file URL.');
481
499
  }
482
- await core.invoke("plugin:fs|remove", {
500
+ await core.invoke('plugin:fs|remove', {
483
501
  path: path instanceof URL ? path.toString() : path,
484
- options,
502
+ options
485
503
  });
486
504
  }
487
505
  /**
@@ -500,14 +518,14 @@ async function remove(path, options) {
500
518
  * @since 2.0.0
501
519
  */
502
520
  async function rename(oldPath, newPath, options) {
503
- if ((oldPath instanceof URL && oldPath.protocol !== "file:") ||
504
- (newPath instanceof URL && newPath.protocol !== "file:")) {
505
- throw new TypeError("Must be a file URL.");
521
+ if ((oldPath instanceof URL && oldPath.protocol !== 'file:') ||
522
+ (newPath instanceof URL && newPath.protocol !== 'file:')) {
523
+ throw new TypeError('Must be a file URL.');
506
524
  }
507
- await core.invoke("plugin:fs|rename", {
525
+ await core.invoke('plugin:fs|rename', {
508
526
  oldPath: oldPath instanceof URL ? oldPath.toString() : oldPath,
509
527
  newPath: newPath instanceof URL ? newPath.toString() : newPath,
510
- options,
528
+ options
511
529
  });
512
530
  }
513
531
  /**
@@ -524,9 +542,9 @@ async function rename(oldPath, newPath, options) {
524
542
  * @since 2.0.0
525
543
  */
526
544
  async function stat(path, options) {
527
- const res = await core.invoke("plugin:fs|stat", {
545
+ const res = await core.invoke('plugin:fs|stat', {
528
546
  path: path instanceof URL ? path.toString() : path,
529
- options,
547
+ options
530
548
  });
531
549
  return parseFileInfo(res);
532
550
  }
@@ -545,9 +563,9 @@ async function stat(path, options) {
545
563
  * @since 2.0.0
546
564
  */
547
565
  async function lstat(path, options) {
548
- const res = await core.invoke("plugin:fs|lstat", {
566
+ const res = await core.invoke('plugin:fs|lstat', {
549
567
  path: path instanceof URL ? path.toString() : path,
550
- options,
568
+ options
551
569
  });
552
570
  return parseFileInfo(res);
553
571
  }
@@ -572,13 +590,13 @@ async function lstat(path, options) {
572
590
  * @since 2.0.0
573
591
  */
574
592
  async function truncate(path, len, options) {
575
- if (path instanceof URL && path.protocol !== "file:") {
576
- throw new TypeError("Must be a file URL.");
593
+ if (path instanceof URL && path.protocol !== 'file:') {
594
+ throw new TypeError('Must be a file URL.');
577
595
  }
578
- await core.invoke("plugin:fs|truncate", {
596
+ await core.invoke('plugin:fs|truncate', {
579
597
  path: path instanceof URL ? path.toString() : path,
580
598
  len,
581
- options,
599
+ options
582
600
  });
583
601
  }
584
602
  /**
@@ -595,14 +613,14 @@ async function truncate(path, len, options) {
595
613
  * @since 2.0.0
596
614
  */
597
615
  async function writeFile(path, data, options) {
598
- if (path instanceof URL && path.protocol !== "file:") {
599
- throw new TypeError("Must be a file URL.");
616
+ if (path instanceof URL && path.protocol !== 'file:') {
617
+ throw new TypeError('Must be a file URL.');
600
618
  }
601
- await core.invoke("plugin:fs|write_file", data, {
619
+ await core.invoke('plugin:fs|write_file', data, {
602
620
  headers: {
603
- path: path instanceof URL ? path.toString() : path,
604
- options: JSON.stringify(options),
605
- },
621
+ path: encodeURIComponent(path instanceof URL ? path.toString() : path),
622
+ options: JSON.stringify(options)
623
+ }
606
624
  });
607
625
  }
608
626
  /**
@@ -617,13 +635,15 @@ async function writeFile(path, data, options) {
617
635
  * @since 2.0.0
618
636
  */
619
637
  async function writeTextFile(path, data, options) {
620
- if (path instanceof URL && path.protocol !== "file:") {
621
- throw new TypeError("Must be a file URL.");
638
+ if (path instanceof URL && path.protocol !== 'file:') {
639
+ throw new TypeError('Must be a file URL.');
622
640
  }
623
- await core.invoke("plugin:fs|write_text_file", {
624
- path: path instanceof URL ? path.toString() : path,
625
- data,
626
- options,
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
+ }
627
647
  });
628
648
  }
629
649
  /**
@@ -638,16 +658,16 @@ async function writeTextFile(path, data, options) {
638
658
  * @since 2.0.0
639
659
  */
640
660
  async function exists(path, options) {
641
- if (path instanceof URL && path.protocol !== "file:") {
642
- throw new TypeError("Must be a file URL.");
661
+ if (path instanceof URL && path.protocol !== 'file:') {
662
+ throw new TypeError('Must be a file URL.');
643
663
  }
644
- return await core.invoke("plugin:fs|exists", {
664
+ return await core.invoke('plugin:fs|exists', {
645
665
  path: path instanceof URL ? path.toString() : path,
646
- options,
666
+ options
647
667
  });
648
668
  }
649
669
  async function unwatch(rid) {
650
- await core.invoke("plugin:fs|unwatch", { rid });
670
+ await core.invoke('plugin:fs|unwatch', { rid });
651
671
  }
652
672
  /**
653
673
  * Watch changes (after a delay) on files or directories.
@@ -658,20 +678,20 @@ async function watch(paths, cb, options) {
658
678
  const opts = {
659
679
  recursive: false,
660
680
  delayMs: 2000,
661
- ...options,
681
+ ...options
662
682
  };
663
683
  const watchPaths = Array.isArray(paths) ? paths : [paths];
664
684
  for (const path of watchPaths) {
665
- if (path instanceof URL && path.protocol !== "file:") {
666
- throw new TypeError("Must be a file URL.");
685
+ if (path instanceof URL && path.protocol !== 'file:') {
686
+ throw new TypeError('Must be a file URL.');
667
687
  }
668
688
  }
669
689
  const onEvent = new core.Channel();
670
690
  onEvent.onmessage = cb;
671
- const rid = await core.invoke("plugin:fs|watch", {
691
+ const rid = await core.invoke('plugin:fs|watch', {
672
692
  paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
673
693
  options: opts,
674
- onEvent,
694
+ onEvent
675
695
  });
676
696
  return () => {
677
697
  void unwatch(rid);
@@ -686,20 +706,20 @@ async function watchImmediate(paths, cb, options) {
686
706
  const opts = {
687
707
  recursive: false,
688
708
  ...options,
689
- delayMs: null,
709
+ delayMs: null
690
710
  };
691
711
  const watchPaths = Array.isArray(paths) ? paths : [paths];
692
712
  for (const path of watchPaths) {
693
- if (path instanceof URL && path.protocol !== "file:") {
694
- throw new TypeError("Must be a file URL.");
713
+ if (path instanceof URL && path.protocol !== 'file:') {
714
+ throw new TypeError('Must be a file URL.');
695
715
  }
696
716
  }
697
717
  const onEvent = new core.Channel();
698
718
  onEvent.onmessage = cb;
699
- const rid = await core.invoke("plugin:fs|watch", {
719
+ const rid = await core.invoke('plugin:fs|watch', {
700
720
  paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
701
721
  options: opts,
702
- onEvent,
722
+ onEvent
703
723
  });
704
724
  return () => {
705
725
  void unwatch(rid);
@@ -56,8 +56,8 @@
56
56
  *
57
57
  * @module
58
58
  */
59
- import { BaseDirectory } from "@tauri-apps/api/path";
60
- import { Resource } from "@tauri-apps/api/core";
59
+ import { BaseDirectory } from '@tauri-apps/api/path';
60
+ import { Resource } from '@tauri-apps/api/core';
61
61
  declare enum SeekMode {
62
62
  Start = 0,
63
63
  Current = 1,
@@ -219,7 +219,7 @@ declare class FileHandle extends Resource {
219
219
  * @example
220
220
  * ```typescript
221
221
  * import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
222
- * // if "$APP/foo/bar.txt" contains the text "hello world":
222
+ * // if "$APPCONFIG/foo/bar.txt" contains the text "hello world":
223
223
  * const file = await open("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
224
224
  * const buf = new Uint8Array(100);
225
225
  * const numberOfBytesRead = await file.read(buf); // 11 bytes
@@ -751,7 +751,7 @@ interface WatchEvent {
751
751
  /**
752
752
  * @since 2.0.0
753
753
  */
754
- type WatchEventKind = "any" | {
754
+ type WatchEventKind = 'any' | {
755
755
  access: WatchEventKindAccess;
756
756
  } | {
757
757
  create: WatchEventKindCreate;
@@ -759,61 +759,61 @@ type WatchEventKind = "any" | {
759
759
  modify: WatchEventKindModify;
760
760
  } | {
761
761
  remove: WatchEventKindRemove;
762
- } | "other";
762
+ } | 'other';
763
763
  /**
764
764
  * @since 2.0.0
765
765
  */
766
766
  type WatchEventKindAccess = {
767
- kind: "any";
767
+ kind: 'any';
768
768
  } | {
769
- kind: "close";
770
- mode: "any" | "execute" | "read" | "write" | "other";
769
+ kind: 'close';
770
+ mode: 'any' | 'execute' | 'read' | 'write' | 'other';
771
771
  } | {
772
- kind: "open";
773
- mode: "any" | "execute" | "read" | "write" | "other";
772
+ kind: 'open';
773
+ mode: 'any' | 'execute' | 'read' | 'write' | 'other';
774
774
  } | {
775
- kind: "other";
775
+ kind: 'other';
776
776
  };
777
777
  /**
778
778
  * @since 2.0.0
779
779
  */
780
780
  type WatchEventKindCreate = {
781
- kind: "any";
781
+ kind: 'any';
782
782
  } | {
783
- kind: "file";
783
+ kind: 'file';
784
784
  } | {
785
- kind: "folder";
785
+ kind: 'folder';
786
786
  } | {
787
- kind: "other";
787
+ kind: 'other';
788
788
  };
789
789
  /**
790
790
  * @since 2.0.0
791
791
  */
792
792
  type WatchEventKindModify = {
793
- kind: "any";
793
+ kind: 'any';
794
794
  } | {
795
- kind: "data";
796
- mode: "any" | "size" | "content" | "other";
795
+ kind: 'data';
796
+ mode: 'any' | 'size' | 'content' | 'other';
797
797
  } | {
798
- kind: "metadata";
799
- mode: "any" | "access-time" | "write-time" | "permissions" | "ownership" | "extended" | "other";
798
+ kind: 'metadata';
799
+ mode: 'any' | 'access-time' | 'write-time' | 'permissions' | 'ownership' | 'extended' | 'other';
800
800
  } | {
801
- kind: "rename";
802
- mode: "any" | "to" | "from" | "both" | "other";
801
+ kind: 'rename';
802
+ mode: 'any' | 'to' | 'from' | 'both' | 'other';
803
803
  } | {
804
- kind: "other";
804
+ kind: 'other';
805
805
  };
806
806
  /**
807
807
  * @since 2.0.0
808
808
  */
809
809
  type WatchEventKindRemove = {
810
- kind: "any";
810
+ kind: 'any';
811
811
  } | {
812
- kind: "file";
812
+ kind: 'file';
813
813
  } | {
814
- kind: "folder";
814
+ kind: 'folder';
815
815
  } | {
816
- kind: "other";
816
+ kind: 'other';
817
817
  };
818
818
  /**
819
819
  * @since 2.0.0
@@ -831,5 +831,5 @@ declare function watch(paths: string | string[] | URL | URL[], cb: (event: Watch
831
831
  * @since 2.0.0
832
832
  */
833
833
  declare function watchImmediate(paths: string | string[] | URL | URL[], cb: (event: WatchEvent) => void, options?: WatchOptions): Promise<UnwatchFn>;
834
- export type { CreateOptions, OpenOptions, CopyFileOptions, MkdirOptions, DirEntry, ReadDirOptions, ReadFileOptions, RemoveOptions, RenameOptions, StatOptions, TruncateOptions, WriteFileOptions, ExistsOptions, FileInfo, WatchOptions, DebouncedWatchOptions, WatchEvent, WatchEventKind, WatchEventKindAccess, WatchEventKindCreate, WatchEventKindModify, WatchEventKindRemove, UnwatchFn, };
835
- export { BaseDirectory, FileHandle, create, open, copyFile, mkdir, readDir, readFile, readTextFile, readTextFileLines, remove, rename, SeekMode, stat, lstat, truncate, writeFile, writeTextFile, exists, watch, watchImmediate, };
834
+ export type { CreateOptions, OpenOptions, CopyFileOptions, MkdirOptions, DirEntry, ReadDirOptions, ReadFileOptions, RemoveOptions, RenameOptions, StatOptions, TruncateOptions, WriteFileOptions, ExistsOptions, FileInfo, WatchOptions, DebouncedWatchOptions, WatchEvent, WatchEventKind, WatchEventKindAccess, WatchEventKindCreate, WatchEventKindModify, WatchEventKindRemove, UnwatchFn };
835
+ export { BaseDirectory, FileHandle, create, open, copyFile, mkdir, readDir, readFile, readTextFile, readTextFileLines, remove, rename, SeekMode, stat, lstat, truncate, writeFile, writeTextFile, exists, watch, watchImmediate };
package/dist-js/index.js CHANGED
@@ -87,9 +87,22 @@ function parseFileInfo(r) {
87
87
  gid: r.gid,
88
88
  rdev: r.rdev,
89
89
  blksize: r.blksize,
90
- blocks: r.blocks,
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
  *
@@ -117,7 +130,7 @@ class FileHandle extends Resource {
117
130
  * @example
118
131
  * ```typescript
119
132
  * import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
120
- * // if "$APP/foo/bar.txt" contains the text "hello world":
133
+ * // if "$APPCONFIG/foo/bar.txt" contains the text "hello world":
121
134
  * const file = await open("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
122
135
  * const buf = new Uint8Array(100);
123
136
  * const numberOfBytesRead = await file.read(buf); // 11 bytes
@@ -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
- len: buffer.byteLength,
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
  /**
@@ -171,10 +191,10 @@ class FileHandle extends Resource {
171
191
  * @since 2.0.0
172
192
  */
173
193
  async seek(offset, whence) {
174
- return await invoke("plugin:fs|seek", {
194
+ return await invoke('plugin:fs|seek', {
175
195
  rid: this.rid,
176
196
  offset,
177
- whence,
197
+ whence
178
198
  });
179
199
  }
180
200
  /**
@@ -192,8 +212,8 @@ class FileHandle extends Resource {
192
212
  * @since 2.0.0
193
213
  */
194
214
  async stat() {
195
- const res = await invoke("plugin:fs|fstat", {
196
- rid: this.rid,
215
+ const res = await invoke('plugin:fs|fstat', {
216
+ rid: this.rid
197
217
  });
198
218
  return parseFileInfo(res);
199
219
  }
@@ -222,9 +242,9 @@ class FileHandle extends Resource {
222
242
  * @since 2.0.0
223
243
  */
224
244
  async truncate(len) {
225
- await invoke("plugin:fs|ftruncate", {
245
+ await invoke('plugin:fs|ftruncate', {
226
246
  rid: this.rid,
227
- len,
247
+ len
228
248
  });
229
249
  }
230
250
  /**
@@ -248,9 +268,9 @@ class FileHandle extends Resource {
248
268
  * @since 2.0.0
249
269
  */
250
270
  async write(data) {
251
- return await invoke("plugin:fs|write", {
271
+ return await invoke('plugin:fs|write', {
252
272
  rid: this.rid,
253
- data,
273
+ data
254
274
  });
255
275
  }
256
276
  }
@@ -269,12 +289,12 @@ class FileHandle extends Resource {
269
289
  * @since 2.0.0
270
290
  */
271
291
  async function create(path, options) {
272
- if (path instanceof URL && path.protocol !== "file:") {
273
- throw new TypeError("Must be a file URL.");
292
+ if (path instanceof URL && path.protocol !== 'file:') {
293
+ throw new TypeError('Must be a file URL.');
274
294
  }
275
- const rid = await invoke("plugin:fs|create", {
295
+ const rid = await invoke('plugin:fs|create', {
276
296
  path: path instanceof URL ? path.toString() : path,
277
- options,
297
+ options
278
298
  });
279
299
  return new FileHandle(rid);
280
300
  }
@@ -295,12 +315,12 @@ async function create(path, options) {
295
315
  * @since 2.0.0
296
316
  */
297
317
  async function open(path, options) {
298
- if (path instanceof URL && path.protocol !== "file:") {
299
- throw new TypeError("Must be a file URL.");
318
+ if (path instanceof URL && path.protocol !== 'file:') {
319
+ throw new TypeError('Must be a file URL.');
300
320
  }
301
- const rid = await invoke("plugin:fs|open", {
321
+ const rid = await invoke('plugin:fs|open', {
302
322
  path: path instanceof URL ? path.toString() : path,
303
- options,
323
+ options
304
324
  });
305
325
  return new FileHandle(rid);
306
326
  }
@@ -315,14 +335,14 @@ async function open(path, options) {
315
335
  * @since 2.0.0
316
336
  */
317
337
  async function copyFile(fromPath, toPath, options) {
318
- if ((fromPath instanceof URL && fromPath.protocol !== "file:") ||
319
- (toPath instanceof URL && toPath.protocol !== "file:")) {
320
- throw new TypeError("Must be a file URL.");
338
+ if ((fromPath instanceof URL && fromPath.protocol !== 'file:') ||
339
+ (toPath instanceof URL && toPath.protocol !== 'file:')) {
340
+ throw new TypeError('Must be a file URL.');
321
341
  }
322
- await invoke("plugin:fs|copy_file", {
342
+ await invoke('plugin:fs|copy_file', {
323
343
  fromPath: fromPath instanceof URL ? fromPath.toString() : fromPath,
324
344
  toPath: toPath instanceof URL ? toPath.toString() : toPath,
325
- options,
345
+ options
326
346
  });
327
347
  }
328
348
  /**
@@ -336,12 +356,12 @@ async function copyFile(fromPath, toPath, options) {
336
356
  * @since 2.0.0
337
357
  */
338
358
  async function mkdir(path, options) {
339
- if (path instanceof URL && path.protocol !== "file:") {
340
- throw new TypeError("Must be a file URL.");
359
+ if (path instanceof URL && path.protocol !== 'file:') {
360
+ throw new TypeError('Must be a file URL.');
341
361
  }
342
- await invoke("plugin:fs|mkdir", {
362
+ await invoke('plugin:fs|mkdir', {
343
363
  path: path instanceof URL ? path.toString() : path,
344
- options,
364
+ options
345
365
  });
346
366
  }
347
367
  /**
@@ -367,12 +387,12 @@ async function mkdir(path, options) {
367
387
  * @since 2.0.0
368
388
  */
369
389
  async function readDir(path, options) {
370
- if (path instanceof URL && path.protocol !== "file:") {
371
- throw new TypeError("Must be a file URL.");
390
+ if (path instanceof URL && path.protocol !== 'file:') {
391
+ throw new TypeError('Must be a file URL.');
372
392
  }
373
- return await invoke("plugin:fs|read_dir", {
393
+ return await invoke('plugin:fs|read_dir', {
374
394
  path: path instanceof URL ? path.toString() : path,
375
- options,
395
+ options
376
396
  });
377
397
  }
378
398
  /**
@@ -387,16 +407,14 @@ async function readDir(path, options) {
387
407
  * @since 2.0.0
388
408
  */
389
409
  async function readFile(path, options) {
390
- if (path instanceof URL && path.protocol !== "file:") {
391
- throw new TypeError("Must be a file URL.");
410
+ if (path instanceof URL && path.protocol !== 'file:') {
411
+ throw new TypeError('Must be a file URL.');
392
412
  }
393
- const arr = await invoke("plugin:fs|read_file", {
413
+ const arr = await invoke('plugin:fs|read_file', {
394
414
  path: path instanceof URL ? path.toString() : path,
395
- options,
415
+ options
396
416
  });
397
- return arr instanceof ArrayBuffer
398
- ? new Uint8Array(arr)
399
- : Uint8Array.from(arr);
417
+ return arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr);
400
418
  }
401
419
  /**
402
420
  * Reads and returns the entire contents of a file as UTF-8 string.
@@ -409,12 +427,12 @@ async function readFile(path, options) {
409
427
  * @since 2.0.0
410
428
  */
411
429
  async function readTextFile(path, options) {
412
- if (path instanceof URL && path.protocol !== "file:") {
413
- throw new TypeError("Must be a file URL.");
430
+ if (path instanceof URL && path.protocol !== 'file:') {
431
+ throw new TypeError('Must be a file URL.');
414
432
  }
415
- return await invoke("plugin:fs|read_text_file", {
433
+ return await invoke('plugin:fs|read_text_file', {
416
434
  path: path instanceof URL ? path.toString() : path,
417
- options,
435
+ options
418
436
  });
419
437
  }
420
438
  /**
@@ -433,8 +451,8 @@ async function readTextFile(path, options) {
433
451
  * @since 2.0.0
434
452
  */
435
453
  async function readTextFileLines(path, options) {
436
- if (path instanceof URL && path.protocol !== "file:") {
437
- throw new TypeError("Must be a file URL.");
454
+ if (path instanceof URL && path.protocol !== 'file:') {
455
+ throw new TypeError('Must be a file URL.');
438
456
  }
439
457
  const pathStr = path instanceof URL ? path.toString() : path;
440
458
  return await Promise.resolve({
@@ -442,23 +460,23 @@ async function readTextFileLines(path, options) {
442
460
  rid: null,
443
461
  async next() {
444
462
  if (this.rid === null) {
445
- this.rid = await invoke("plugin:fs|read_text_file_lines", {
463
+ this.rid = await invoke('plugin:fs|read_text_file_lines', {
446
464
  path: pathStr,
447
- options,
465
+ options
448
466
  });
449
467
  }
450
- const [line, done] = await invoke("plugin:fs|read_text_file_lines_next", { rid: this.rid });
468
+ const [line, done] = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
451
469
  // an iteration is over, reset rid for next iteration
452
470
  if (done)
453
471
  this.rid = null;
454
472
  return {
455
- value: done ? "" : line,
456
- done,
473
+ value: done ? '' : line,
474
+ done
457
475
  };
458
476
  },
459
477
  [Symbol.asyncIterator]() {
460
478
  return this;
461
- },
479
+ }
462
480
  });
463
481
  }
464
482
  /**
@@ -474,12 +492,12 @@ async function readTextFileLines(path, options) {
474
492
  * @since 2.0.0
475
493
  */
476
494
  async function remove(path, options) {
477
- if (path instanceof URL && path.protocol !== "file:") {
478
- throw new TypeError("Must be a file URL.");
495
+ if (path instanceof URL && path.protocol !== 'file:') {
496
+ throw new TypeError('Must be a file URL.');
479
497
  }
480
- await invoke("plugin:fs|remove", {
498
+ await invoke('plugin:fs|remove', {
481
499
  path: path instanceof URL ? path.toString() : path,
482
- options,
500
+ options
483
501
  });
484
502
  }
485
503
  /**
@@ -498,14 +516,14 @@ async function remove(path, options) {
498
516
  * @since 2.0.0
499
517
  */
500
518
  async function rename(oldPath, newPath, options) {
501
- if ((oldPath instanceof URL && oldPath.protocol !== "file:") ||
502
- (newPath instanceof URL && newPath.protocol !== "file:")) {
503
- throw new TypeError("Must be a file URL.");
519
+ if ((oldPath instanceof URL && oldPath.protocol !== 'file:') ||
520
+ (newPath instanceof URL && newPath.protocol !== 'file:')) {
521
+ throw new TypeError('Must be a file URL.');
504
522
  }
505
- await invoke("plugin:fs|rename", {
523
+ await invoke('plugin:fs|rename', {
506
524
  oldPath: oldPath instanceof URL ? oldPath.toString() : oldPath,
507
525
  newPath: newPath instanceof URL ? newPath.toString() : newPath,
508
- options,
526
+ options
509
527
  });
510
528
  }
511
529
  /**
@@ -522,9 +540,9 @@ async function rename(oldPath, newPath, options) {
522
540
  * @since 2.0.0
523
541
  */
524
542
  async function stat(path, options) {
525
- const res = await invoke("plugin:fs|stat", {
543
+ const res = await invoke('plugin:fs|stat', {
526
544
  path: path instanceof URL ? path.toString() : path,
527
- options,
545
+ options
528
546
  });
529
547
  return parseFileInfo(res);
530
548
  }
@@ -543,9 +561,9 @@ async function stat(path, options) {
543
561
  * @since 2.0.0
544
562
  */
545
563
  async function lstat(path, options) {
546
- const res = await invoke("plugin:fs|lstat", {
564
+ const res = await invoke('plugin:fs|lstat', {
547
565
  path: path instanceof URL ? path.toString() : path,
548
- options,
566
+ options
549
567
  });
550
568
  return parseFileInfo(res);
551
569
  }
@@ -570,13 +588,13 @@ async function lstat(path, options) {
570
588
  * @since 2.0.0
571
589
  */
572
590
  async function truncate(path, len, options) {
573
- if (path instanceof URL && path.protocol !== "file:") {
574
- throw new TypeError("Must be a file URL.");
591
+ if (path instanceof URL && path.protocol !== 'file:') {
592
+ throw new TypeError('Must be a file URL.');
575
593
  }
576
- await invoke("plugin:fs|truncate", {
594
+ await invoke('plugin:fs|truncate', {
577
595
  path: path instanceof URL ? path.toString() : path,
578
596
  len,
579
- options,
597
+ options
580
598
  });
581
599
  }
582
600
  /**
@@ -593,14 +611,14 @@ async function truncate(path, len, options) {
593
611
  * @since 2.0.0
594
612
  */
595
613
  async function writeFile(path, data, options) {
596
- if (path instanceof URL && path.protocol !== "file:") {
597
- throw new TypeError("Must be a file URL.");
614
+ if (path instanceof URL && path.protocol !== 'file:') {
615
+ throw new TypeError('Must be a file URL.');
598
616
  }
599
- await invoke("plugin:fs|write_file", data, {
617
+ await invoke('plugin:fs|write_file', data, {
600
618
  headers: {
601
- path: path instanceof URL ? path.toString() : path,
602
- options: JSON.stringify(options),
603
- },
619
+ path: encodeURIComponent(path instanceof URL ? path.toString() : path),
620
+ options: JSON.stringify(options)
621
+ }
604
622
  });
605
623
  }
606
624
  /**
@@ -615,13 +633,15 @@ async function writeFile(path, data, options) {
615
633
  * @since 2.0.0
616
634
  */
617
635
  async function writeTextFile(path, data, options) {
618
- if (path instanceof URL && path.protocol !== "file:") {
619
- throw new TypeError("Must be a file URL.");
636
+ if (path instanceof URL && path.protocol !== 'file:') {
637
+ throw new TypeError('Must be a file URL.');
620
638
  }
621
- await invoke("plugin:fs|write_text_file", {
622
- path: path instanceof URL ? path.toString() : path,
623
- data,
624
- options,
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
+ }
625
645
  });
626
646
  }
627
647
  /**
@@ -636,16 +656,16 @@ async function writeTextFile(path, data, options) {
636
656
  * @since 2.0.0
637
657
  */
638
658
  async function exists(path, options) {
639
- if (path instanceof URL && path.protocol !== "file:") {
640
- throw new TypeError("Must be a file URL.");
659
+ if (path instanceof URL && path.protocol !== 'file:') {
660
+ throw new TypeError('Must be a file URL.');
641
661
  }
642
- return await invoke("plugin:fs|exists", {
662
+ return await invoke('plugin:fs|exists', {
643
663
  path: path instanceof URL ? path.toString() : path,
644
- options,
664
+ options
645
665
  });
646
666
  }
647
667
  async function unwatch(rid) {
648
- await invoke("plugin:fs|unwatch", { rid });
668
+ await invoke('plugin:fs|unwatch', { rid });
649
669
  }
650
670
  /**
651
671
  * Watch changes (after a delay) on files or directories.
@@ -656,20 +676,20 @@ async function watch(paths, cb, options) {
656
676
  const opts = {
657
677
  recursive: false,
658
678
  delayMs: 2000,
659
- ...options,
679
+ ...options
660
680
  };
661
681
  const watchPaths = Array.isArray(paths) ? paths : [paths];
662
682
  for (const path of watchPaths) {
663
- if (path instanceof URL && path.protocol !== "file:") {
664
- throw new TypeError("Must be a file URL.");
683
+ if (path instanceof URL && path.protocol !== 'file:') {
684
+ throw new TypeError('Must be a file URL.');
665
685
  }
666
686
  }
667
687
  const onEvent = new Channel();
668
688
  onEvent.onmessage = cb;
669
- const rid = await invoke("plugin:fs|watch", {
689
+ const rid = await invoke('plugin:fs|watch', {
670
690
  paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
671
691
  options: opts,
672
- onEvent,
692
+ onEvent
673
693
  });
674
694
  return () => {
675
695
  void unwatch(rid);
@@ -684,20 +704,20 @@ async function watchImmediate(paths, cb, options) {
684
704
  const opts = {
685
705
  recursive: false,
686
706
  ...options,
687
- delayMs: null,
707
+ delayMs: null
688
708
  };
689
709
  const watchPaths = Array.isArray(paths) ? paths : [paths];
690
710
  for (const path of watchPaths) {
691
- if (path instanceof URL && path.protocol !== "file:") {
692
- throw new TypeError("Must be a file URL.");
711
+ if (path instanceof URL && path.protocol !== 'file:') {
712
+ throw new TypeError('Must be a file URL.');
693
713
  }
694
714
  }
695
715
  const onEvent = new Channel();
696
716
  onEvent.onmessage = cb;
697
- const rid = await invoke("plugin:fs|watch", {
717
+ const rid = await invoke('plugin:fs|watch', {
698
718
  paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
699
719
  options: opts,
700
- onEvent,
720
+ onEvent
701
721
  });
702
722
  return () => {
703
723
  void unwatch(rid);
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-fs",
3
- "version": "2.0.0-rc.2",
3
+ "version": "2.0.1",
4
4
  "description": "Access the file system.",
5
- "license": "MIT or APACHE-2.0",
5
+ "license": "MIT OR Apache-2.0",
6
6
  "authors": [
7
7
  "Tauri Programme within The Commons Conservancy"
8
8
  ],
@@ -25,6 +25,6 @@
25
25
  "LICENSE"
26
26
  ],
27
27
  "dependencies": {
28
- "@tauri-apps/api": "^2.0.0-rc.4"
28
+ "@tauri-apps/api": "^2.0.0"
29
29
  }
30
30
  }