Skip to content

Commit

Permalink
feat(fs): support readOnly and noClear options
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 6, 2023
1 parent 1f32b30 commit f2dddbd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ By default, everything is stored in memory. We can mount additional storage spac

When operating with a `key` that starts with mountpoint, instead of default storage, mounted driver will be called.

In addition to `base`, you can set `readOnly` and `noClear` to disable write and clear operations.

```js
import { createStorage } from "unstorage";
import fsDriver from "unstorage/drivers/fs";
Expand Down
14 changes: 14 additions & 0 deletions src/drivers/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import anymatch from "anymatch";
export interface FSStorageOptions {
base?: string;
ignore?: string[];
readOnly?: boolean;
noClear?: boolean;
watchOptions?: WatchOptions;
}

Expand Down Expand Up @@ -59,18 +61,30 @@ export default defineDriver((opts: FSStorageOptions = {}) => {
return { atime, mtime, size };
},
setItem(key, value) {
if (opts.readOnly) {
return;
}
return writeFile(r(key), value, "utf8");
},
setItemRaw(key, value) {
if (opts.readOnly) {
return;
}
return writeFile(r(key), value);
},
removeItem(key) {
if (opts.readOnly) {
return;
}
return unlink(r(key));
},
getKeys() {
return readdirRecursive(r("."), anymatch(opts.ignore || []));
},
async clear() {
if (opts.readOnly || opts.noClear) {
return;
}
await rmRecursive(r("."));
},
async dispose() {
Expand Down

0 comments on commit f2dddbd

Please sign in to comment.