@tauri-apps/plugin-fs
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -13,15 +13,28 @@ /** | ||
* | ||
* The scope configuration is an array of glob patterns describing folder paths that are allowed. | ||
* For instance, this scope configuration only allows accessing files on the | ||
* *databases* folder of the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/#appdatadir | `$APPDATA` directory}: | ||
* The scope configuration is an array of glob patterns describing file/directory paths that are allowed. | ||
* For instance, this scope configuration allows **all** enabled `fs` APIs to (only) access files in the | ||
* *databases* directory of the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/#appdatadir | `$APPDATA` directory}: | ||
* ```json | ||
* { | ||
* "plugins": { | ||
* "fs": { | ||
* "scope": ["$APPDATA/databases/*"] | ||
* "permissions": [ | ||
* { | ||
* "identifier": "fs:scope", | ||
* "allow": [{ "path": "$APPDATA/databases/*" }] | ||
* } | ||
* } | ||
* ] | ||
* } | ||
* ``` | ||
* | ||
* Scopes can also be applied to specific `fs` APIs by using the API's identifier instead of `fs:scope`: | ||
* ```json | ||
* { | ||
* "permissions": [ | ||
* { | ||
* "identifier": "fs:allow-exists", | ||
* "allow": [{ "path": "$APPDATA/databases/*" }] | ||
* } | ||
* ] | ||
* } | ||
* ``` | ||
* | ||
* Notice the use of the `$APPDATA` variable. The value is injected at runtime, resolving to the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/#appdatadir | app data directory}. | ||
@@ -56,4 +69,2 @@ * | ||
* | ||
* Note that this scope applies to **all** APIs on this module. | ||
* | ||
* @module | ||
@@ -309,7 +320,7 @@ */ | ||
/** | ||
* Writes `p.byteLength` bytes from `p` to the underlying data stream. It | ||
* resolves to the number of bytes written from `p` (`0` <= `n` <= | ||
* `p.byteLength`) or reject with the error encountered that caused the | ||
* Writes `data.byteLength` bytes from `data` to the underlying data stream. It | ||
* resolves to the number of bytes written from `data` (`0` <= `n` <= | ||
* `data.byteLength`) or reject with the error encountered that caused the | ||
* write to stop early. `write()` must reject with a non-null error if | ||
* would resolve to `n` < `p.byteLength`. `write()` must not modify the | ||
* would resolve to `n` < `data.byteLength`. `write()` must not modify the | ||
* slice data, even temporarily. | ||
@@ -698,3 +709,3 @@ * | ||
*/ | ||
declare function writeFile(path: string | URL, data: Uint8Array, options?: WriteFileOptions): Promise<void>; | ||
declare function writeFile(path: string | URL, data: Uint8Array | ReadableStream<Uint8Array>, options?: WriteFileOptions): Promise<void>; | ||
/** | ||
@@ -838,3 +849,19 @@ * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting. | ||
declare function watchImmediate(paths: string | string[] | URL | URL[], cb: (event: WatchEvent) => void, options?: WatchOptions): Promise<UnwatchFn>; | ||
/** | ||
* Get the size of a file or directory. For files, the `stat` functions can be used as well. | ||
* | ||
* If `path` is a directory, this function will recursively iterate over every file and every directory inside of `path` and therefore will be very time consuming if used on larger directories. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { size, BaseDirectory } from '@tauri-apps/plugin-fs'; | ||
* // Get the size of the `$APPDATA/tauri` directory. | ||
* const dirSize = await size('tauri', { baseDir: BaseDirectory.AppData }); | ||
* console.log(dirSize); // 1024 | ||
* ``` | ||
* | ||
* @since 2.1.0 | ||
*/ | ||
declare function size(path: string | URL): Promise<number>; | ||
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 }; | ||
export { BaseDirectory, FileHandle, create, open, copyFile, mkdir, readDir, readFile, readTextFile, readTextFileLines, remove, rename, SeekMode, stat, lstat, truncate, writeFile, writeTextFile, exists, watch, watchImmediate }; | ||
export { BaseDirectory, FileHandle, create, open, copyFile, mkdir, readDir, readFile, readTextFile, readTextFileLines, remove, rename, SeekMode, stat, lstat, truncate, writeFile, writeTextFile, exists, watch, watchImmediate, size }; |
@@ -19,15 +19,28 @@ export { BaseDirectory } from '@tauri-apps/api/path'; | ||
* | ||
* The scope configuration is an array of glob patterns describing folder paths that are allowed. | ||
* For instance, this scope configuration only allows accessing files on the | ||
* *databases* folder of the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/#appdatadir | `$APPDATA` directory}: | ||
* The scope configuration is an array of glob patterns describing file/directory paths that are allowed. | ||
* For instance, this scope configuration allows **all** enabled `fs` APIs to (only) access files in the | ||
* *databases* directory of the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/#appdatadir | `$APPDATA` directory}: | ||
* ```json | ||
* { | ||
* "plugins": { | ||
* "fs": { | ||
* "scope": ["$APPDATA/databases/*"] | ||
* "permissions": [ | ||
* { | ||
* "identifier": "fs:scope", | ||
* "allow": [{ "path": "$APPDATA/databases/*" }] | ||
* } | ||
* } | ||
* ] | ||
* } | ||
* ``` | ||
* | ||
* Scopes can also be applied to specific `fs` APIs by using the API's identifier instead of `fs:scope`: | ||
* ```json | ||
* { | ||
* "permissions": [ | ||
* { | ||
* "identifier": "fs:allow-exists", | ||
* "allow": [{ "path": "$APPDATA/databases/*" }] | ||
* } | ||
* ] | ||
* } | ||
* ``` | ||
* | ||
* Notice the use of the `$APPDATA` variable. The value is injected at runtime, resolving to the {@link https://v2.tauri.app/reference/javascript/api/namespacepath/#appdatadir | app data directory}. | ||
@@ -62,4 +75,2 @@ * | ||
* | ||
* Note that this scope applies to **all** APIs on this module. | ||
* | ||
* @module | ||
@@ -102,2 +113,3 @@ */ | ||
for (let i = 0; i < size; i++) { | ||
// eslint-disable-next-line security/detect-object-injection | ||
const byte = bytes[i]; | ||
@@ -254,7 +266,7 @@ x *= 0x100; | ||
/** | ||
* Writes `p.byteLength` bytes from `p` to the underlying data stream. It | ||
* resolves to the number of bytes written from `p` (`0` <= `n` <= | ||
* `p.byteLength`) or reject with the error encountered that caused the | ||
* Writes `data.byteLength` bytes from `data` to the underlying data stream. It | ||
* resolves to the number of bytes written from `data` (`0` <= `n` <= | ||
* `data.byteLength`) or reject with the error encountered that caused the | ||
* write to stop early. `write()` must reject with a non-null error if | ||
* would resolve to `n` < `p.byteLength`. `write()` must not modify the | ||
* would resolve to `n` < `data.byteLength`. `write()` must not modify the | ||
* slice data, even temporarily. | ||
@@ -437,6 +449,8 @@ * | ||
} | ||
return await invoke('plugin:fs|read_text_file', { | ||
const arr = await invoke('plugin:fs|read_text_file', { | ||
path: path instanceof URL ? path.toString() : path, | ||
options | ||
}); | ||
const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr); | ||
return new TextDecoder().decode(bytes); | ||
} | ||
@@ -473,8 +487,18 @@ /** | ||
} | ||
const [line, done] = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid }); | ||
// an iteration is over, reset rid for next iteration | ||
if (done) | ||
const arr = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid }); | ||
const bytes = arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr); | ||
// Rust side will never return an empty array for this command and | ||
// ensure there is at least one elements there. | ||
// | ||
// This is an optimization to include whether we finished iteration or not (1 or 0) | ||
// at the end of returned array to avoid serialization overhead of separate values. | ||
const done = bytes[bytes.byteLength - 1] === 1; | ||
if (done) { | ||
// a full iteration is over, reset rid for next iteration | ||
this.rid = null; | ||
return { value: null, done }; | ||
} | ||
const line = new TextDecoder().decode(bytes.slice(0, bytes.byteLength)); | ||
return { | ||
value: done ? '' : line, | ||
value: line, | ||
done | ||
@@ -623,8 +647,17 @@ }; | ||
} | ||
await invoke('plugin:fs|write_file', data, { | ||
headers: { | ||
path: encodeURIComponent(path instanceof URL ? path.toString() : path), | ||
options: JSON.stringify(options) | ||
if (data instanceof ReadableStream) { | ||
const file = await open(path, options); | ||
for await (const chunk of data) { | ||
await file.write(chunk); | ||
} | ||
}); | ||
await file.close(); | ||
} | ||
else { | ||
await invoke('plugin:fs|write_file', data, { | ||
headers: { | ||
path: encodeURIComponent(path instanceof URL ? path.toString() : path), | ||
options: JSON.stringify(options) | ||
} | ||
}); | ||
} | ||
} | ||
@@ -733,3 +766,26 @@ /** | ||
} | ||
/** | ||
* Get the size of a file or directory. For files, the `stat` functions can be used as well. | ||
* | ||
* If `path` is a directory, this function will recursively iterate over every file and every directory inside of `path` and therefore will be very time consuming if used on larger directories. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { size, BaseDirectory } from '@tauri-apps/plugin-fs'; | ||
* // Get the size of the `$APPDATA/tauri` directory. | ||
* const dirSize = await size('tauri', { baseDir: BaseDirectory.AppData }); | ||
* console.log(dirSize); // 1024 | ||
* ``` | ||
* | ||
* @since 2.1.0 | ||
*/ | ||
async function size(path) { | ||
if (path instanceof URL && path.protocol !== 'file:') { | ||
throw new TypeError('Must be a file URL.'); | ||
} | ||
return await invoke('plugin:fs|size', { | ||
path: path instanceof URL ? path.toString() : path | ||
}); | ||
} | ||
export { FileHandle, SeekMode, copyFile, create, exists, lstat, mkdir, open, readDir, readFile, readTextFile, readTextFileLines, remove, rename, stat, truncate, watch, watchImmediate, writeFile, writeTextFile }; | ||
export { FileHandle, SeekMode, copyFile, create, exists, lstat, mkdir, open, readDir, readFile, readTextFile, readTextFileLines, remove, rename, size, stat, truncate, watch, watchImmediate, writeFile, writeTextFile }; |
{ | ||
"name": "@tauri-apps/plugin-fs", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Access the file system.", | ||
@@ -5,0 +5,0 @@ "license": "MIT OR Apache-2.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92956
2449