Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,31 @@ export function createStorage<T extends StorageValue>(
};

const getMounts = (base: string, includeParent?: boolean) => {
return context.mountpoints
.filter(
(mountpoint) =>
mountpoint.startsWith(base) ||
(includeParent && base!.startsWith(mountpoint))
)
.map((mountpoint) => ({
relativeBase:
base.length > mountpoint.length
? base!.slice(mountpoint.length)
: undefined,
mountpoint,
driver: context.mounts[mountpoint]!,
}));
const mountpoints = context.mountpoints.filter(
(mountpoint) =>
mountpoint.startsWith(base) ||
(includeParent && base!.startsWith(mountpoint))
);

// Should fallback to the root mount if no mountpoints found
if (mountpoints.length === 0) {
return [
{
relativeBase: undefined,
mountpoint: "",
driver: context.mounts[""]!,
},
];
}

return mountpoints.map((mountpoint) => ({
relativeBase:
base.length > mountpoint.length
? base!.slice(mountpoint.length)
: undefined,
mountpoint,
driver: context.mounts[mountpoint]!,
}));
};

const onChange: WatchCallback = (event, key) => {
Expand Down
9 changes: 9 additions & 0 deletions test/drivers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,13 @@ export function testDriver(opts: TestOptions) {
await ctx.storage.clear();
expect(await ctx.storage.getKeys()).toMatchObject([]);
});

it("clear - clear the root mount when no matched mountpoints", async () => {
const prefix = "s1";
await ctx.storage.setItem(`${prefix}:key`, "value");

expect(await ctx.storage.getKeys(prefix)).toMatchObject(["s1:key"]);
await ctx.storage.clear(prefix);
expect(await ctx.storage.getKeys(prefix)).toMatchObject([]);
});
}
16 changes: 16 additions & 0 deletions test/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ describe("storage", () => {
]
`);

expect(storage.getMounts("/non-exist-mountpoint").map((m) => m.base))
.toMatchInlineSnapshot(`
[
"",
]
`);
expect(
storage
.getMounts("/non-exist-mountpoint", { parents: true })
.map((m) => m.base)
).toMatchInlineSnapshot(`
[
"",
]
`);

expect(storage.getMounts().map((m) => m.base)).toMatchInlineSnapshot(`
[
"cache:sub:",
Expand Down