@tauri-apps/plugin-fs 2.2.0 → 2.3.0

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
@@ -68,9 +68,9 @@ fn main() {
68
68
  Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
69
69
 
70
70
  ```javascript
71
- import { metadata } from '@tauri-apps/plugin-fs'
71
+ import { stat } from '@tauri-apps/plugin-fs'
72
72
 
73
- await metadata('/path/to/file')
73
+ await stat('/path/to/file')
74
74
  ```
75
75
 
76
76
  ## Contributing
package/dist-js/index.cjs CHANGED
@@ -14,7 +14,7 @@ var core = require('@tauri-apps/api/core');
14
14
  * This module prevents path traversal, not allowing parent directory accessors to be used
15
15
  * (i.e. "/usr/path/to/../file" or "../path/to/file" paths are not allowed).
16
16
  * Paths accessed with this API must be either relative to one of the {@link BaseDirectory | base directories}
17
- * or created with the {@link https://v2.tauri.app/reference/javascript/api/namespacepath | path API}.
17
+ * or created with the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/ | path API}.
18
18
  *
19
19
  * The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.
20
20
  *
@@ -349,8 +349,8 @@ async function open(path, options) {
349
349
  * @since 2.0.0
350
350
  */
351
351
  async function copyFile(fromPath, toPath, options) {
352
- if ((fromPath instanceof URL && fromPath.protocol !== 'file:') ||
353
- (toPath instanceof URL && toPath.protocol !== 'file:')) {
352
+ if ((fromPath instanceof URL && fromPath.protocol !== 'file:')
353
+ || (toPath instanceof URL && toPath.protocol !== 'file:')) {
354
354
  throw new TypeError('Must be a file URL.');
355
355
  }
356
356
  await core.invoke('plugin:fs|copy_file', {
@@ -542,8 +542,8 @@ async function remove(path, options) {
542
542
  * @since 2.0.0
543
543
  */
544
544
  async function rename(oldPath, newPath, options) {
545
- if ((oldPath instanceof URL && oldPath.protocol !== 'file:') ||
546
- (newPath instanceof URL && newPath.protocol !== 'file:')) {
545
+ if ((oldPath instanceof URL && oldPath.protocol !== 'file:')
546
+ || (newPath instanceof URL && newPath.protocol !== 'file:')) {
547
547
  throw new TypeError('Must be a file URL.');
548
548
  }
549
549
  await core.invoke('plugin:fs|rename', {
@@ -642,10 +642,19 @@ async function writeFile(path, data, options) {
642
642
  }
643
643
  if (data instanceof ReadableStream) {
644
644
  const file = await open(path, options);
645
- for await (const chunk of data) {
646
- await file.write(chunk);
645
+ const reader = data.getReader();
646
+ try {
647
+ while (true) {
648
+ const { done, value } = await reader.read();
649
+ if (done)
650
+ break;
651
+ await file.write(value);
652
+ }
653
+ }
654
+ finally {
655
+ reader.releaseLock();
656
+ await file.close();
647
657
  }
648
- await file.close();
649
658
  }
650
659
  else {
651
660
  await core.invoke('plugin:fs|write_file', data, {
@@ -699,20 +708,9 @@ async function exists(path, options) {
699
708
  options
700
709
  });
701
710
  }
702
- async function unwatch(rid) {
703
- await core.invoke('plugin:fs|unwatch', { rid });
711
+ class Watcher extends core.Resource {
704
712
  }
705
- /**
706
- * Watch changes (after a delay) on files or directories.
707
- *
708
- * @since 2.0.0
709
- */
710
- async function watch(paths, cb, options) {
711
- const opts = {
712
- recursive: false,
713
- delayMs: 2000,
714
- ...options
715
- };
713
+ async function watchInternal(paths, cb, options) {
716
714
  const watchPaths = Array.isArray(paths) ? paths : [paths];
717
715
  for (const path of watchPaths) {
718
716
  if (path instanceof URL && path.protocol !== 'file:') {
@@ -723,40 +721,37 @@ async function watch(paths, cb, options) {
723
721
  onEvent.onmessage = cb;
724
722
  const rid = await core.invoke('plugin:fs|watch', {
725
723
  paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
726
- options: opts,
724
+ options,
727
725
  onEvent
728
726
  });
727
+ const watcher = new Watcher(rid);
729
728
  return () => {
730
- void unwatch(rid);
729
+ void watcher.close();
731
730
  };
732
731
  }
732
+ // TODO: Return `Watcher` instead in v3
733
+ /**
734
+ * Watch changes (after a delay) on files or directories.
735
+ *
736
+ * @since 2.0.0
737
+ */
738
+ async function watch(paths, cb, options) {
739
+ return await watchInternal(paths, cb, {
740
+ delayMs: 2000,
741
+ ...options
742
+ });
743
+ }
744
+ // TODO: Return `Watcher` instead in v3
733
745
  /**
734
746
  * Watch changes on files or directories.
735
747
  *
736
748
  * @since 2.0.0
737
749
  */
738
750
  async function watchImmediate(paths, cb, options) {
739
- const opts = {
740
- recursive: false,
751
+ return await watchInternal(paths, cb, {
741
752
  ...options,
742
- delayMs: null
743
- };
744
- const watchPaths = Array.isArray(paths) ? paths : [paths];
745
- for (const path of watchPaths) {
746
- if (path instanceof URL && path.protocol !== 'file:') {
747
- throw new TypeError('Must be a file URL.');
748
- }
749
- }
750
- const onEvent = new core.Channel();
751
- onEvent.onmessage = cb;
752
- const rid = await core.invoke('plugin:fs|watch', {
753
- paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
754
- options: opts,
755
- onEvent
753
+ delayMs: undefined
756
754
  });
757
- return () => {
758
- void unwatch(rid);
759
- };
760
755
  }
761
756
  /**
762
757
  * Get the size of a file or directory. For files, the `stat` functions can be used as well.
@@ -6,7 +6,7 @@
6
6
  * This module prevents path traversal, not allowing parent directory accessors to be used
7
7
  * (i.e. "/usr/path/to/../file" or "../path/to/file" paths are not allowed).
8
8
  * Paths accessed with this API must be either relative to one of the {@link BaseDirectory | base directories}
9
- * or created with the {@link https://v2.tauri.app/reference/javascript/api/namespacepath | path API}.
9
+ * or created with the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/ | path API}.
10
10
  *
11
11
  * The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.
12
12
  *
package/dist-js/index.js CHANGED
@@ -12,7 +12,7 @@ import { Resource, invoke, Channel } from '@tauri-apps/api/core';
12
12
  * This module prevents path traversal, not allowing parent directory accessors to be used
13
13
  * (i.e. "/usr/path/to/../file" or "../path/to/file" paths are not allowed).
14
14
  * Paths accessed with this API must be either relative to one of the {@link BaseDirectory | base directories}
15
- * or created with the {@link https://v2.tauri.app/reference/javascript/api/namespacepath | path API}.
15
+ * or created with the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/ | path API}.
16
16
  *
17
17
  * The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.
18
18
  *
@@ -347,8 +347,8 @@ async function open(path, options) {
347
347
  * @since 2.0.0
348
348
  */
349
349
  async function copyFile(fromPath, toPath, options) {
350
- if ((fromPath instanceof URL && fromPath.protocol !== 'file:') ||
351
- (toPath instanceof URL && toPath.protocol !== 'file:')) {
350
+ if ((fromPath instanceof URL && fromPath.protocol !== 'file:')
351
+ || (toPath instanceof URL && toPath.protocol !== 'file:')) {
352
352
  throw new TypeError('Must be a file URL.');
353
353
  }
354
354
  await invoke('plugin:fs|copy_file', {
@@ -540,8 +540,8 @@ async function remove(path, options) {
540
540
  * @since 2.0.0
541
541
  */
542
542
  async function rename(oldPath, newPath, options) {
543
- if ((oldPath instanceof URL && oldPath.protocol !== 'file:') ||
544
- (newPath instanceof URL && newPath.protocol !== 'file:')) {
543
+ if ((oldPath instanceof URL && oldPath.protocol !== 'file:')
544
+ || (newPath instanceof URL && newPath.protocol !== 'file:')) {
545
545
  throw new TypeError('Must be a file URL.');
546
546
  }
547
547
  await invoke('plugin:fs|rename', {
@@ -640,10 +640,19 @@ async function writeFile(path, data, options) {
640
640
  }
641
641
  if (data instanceof ReadableStream) {
642
642
  const file = await open(path, options);
643
- for await (const chunk of data) {
644
- await file.write(chunk);
643
+ const reader = data.getReader();
644
+ try {
645
+ while (true) {
646
+ const { done, value } = await reader.read();
647
+ if (done)
648
+ break;
649
+ await file.write(value);
650
+ }
651
+ }
652
+ finally {
653
+ reader.releaseLock();
654
+ await file.close();
645
655
  }
646
- await file.close();
647
656
  }
648
657
  else {
649
658
  await invoke('plugin:fs|write_file', data, {
@@ -697,20 +706,9 @@ async function exists(path, options) {
697
706
  options
698
707
  });
699
708
  }
700
- async function unwatch(rid) {
701
- await invoke('plugin:fs|unwatch', { rid });
709
+ class Watcher extends Resource {
702
710
  }
703
- /**
704
- * Watch changes (after a delay) on files or directories.
705
- *
706
- * @since 2.0.0
707
- */
708
- async function watch(paths, cb, options) {
709
- const opts = {
710
- recursive: false,
711
- delayMs: 2000,
712
- ...options
713
- };
711
+ async function watchInternal(paths, cb, options) {
714
712
  const watchPaths = Array.isArray(paths) ? paths : [paths];
715
713
  for (const path of watchPaths) {
716
714
  if (path instanceof URL && path.protocol !== 'file:') {
@@ -721,40 +719,37 @@ async function watch(paths, cb, options) {
721
719
  onEvent.onmessage = cb;
722
720
  const rid = await invoke('plugin:fs|watch', {
723
721
  paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
724
- options: opts,
722
+ options,
725
723
  onEvent
726
724
  });
725
+ const watcher = new Watcher(rid);
727
726
  return () => {
728
- void unwatch(rid);
727
+ void watcher.close();
729
728
  };
730
729
  }
730
+ // TODO: Return `Watcher` instead in v3
731
+ /**
732
+ * Watch changes (after a delay) on files or directories.
733
+ *
734
+ * @since 2.0.0
735
+ */
736
+ async function watch(paths, cb, options) {
737
+ return await watchInternal(paths, cb, {
738
+ delayMs: 2000,
739
+ ...options
740
+ });
741
+ }
742
+ // TODO: Return `Watcher` instead in v3
731
743
  /**
732
744
  * Watch changes on files or directories.
733
745
  *
734
746
  * @since 2.0.0
735
747
  */
736
748
  async function watchImmediate(paths, cb, options) {
737
- const opts = {
738
- recursive: false,
749
+ return await watchInternal(paths, cb, {
739
750
  ...options,
740
- delayMs: null
741
- };
742
- const watchPaths = Array.isArray(paths) ? paths : [paths];
743
- for (const path of watchPaths) {
744
- if (path instanceof URL && path.protocol !== 'file:') {
745
- throw new TypeError('Must be a file URL.');
746
- }
747
- }
748
- const onEvent = new Channel();
749
- onEvent.onmessage = cb;
750
- const rid = await invoke('plugin:fs|watch', {
751
- paths: watchPaths.map((p) => (p instanceof URL ? p.toString() : p)),
752
- options: opts,
753
- onEvent
751
+ delayMs: undefined
754
752
  });
755
- return () => {
756
- void unwatch(rid);
757
- };
758
753
  }
759
754
  /**
760
755
  * Get the size of a file or directory. For files, the `stat` functions can be used as well.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-fs",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Access the file system.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "authors": [