Skip to content

Commit 85bdbb7

Browse files
fix(setItems): call driver native setItems only to avoid duplicate write (#392)
1 parent d8316dc commit 85bdbb7

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export function createStorage<T extends StorageValue>(
231231
async setItems(items, commonOptions) {
232232
await runBatch(items, commonOptions, async (batch) => {
233233
if (batch.driver.setItems) {
234-
await asyncCall(
234+
return asyncCall(
235235
batch.driver.setItems,
236236
batch.items.map((item) => ({
237237
key: item.relativeKey,

test/storage.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,33 @@ describe("utils", () => {
160160
expect(async () => await storage.setItem("foo", [])).not.toThrow();
161161
});
162162
});
163+
164+
describe("Regression", () => {
165+
it("setItems doeesn't upload twice", async () => {
166+
/**
167+
* https://github.com/unjs/unstorage/pull/392
168+
*/
169+
170+
const setItem = vi.fn();
171+
const setItems = vi.fn();
172+
173+
const driver = memory();
174+
const storage = createStorage({
175+
driver: {
176+
...driver,
177+
setItem: (...args) => {
178+
setItem(...args);
179+
return driver.setItem?.(...args);
180+
},
181+
setItems: (...args) => {
182+
setItems(...args);
183+
return driver.setItems?.(...args);
184+
},
185+
},
186+
});
187+
188+
await storage.setItems([{ key: "foo.txt", value: "bar" }]);
189+
expect(setItem).toHaveBeenCalledTimes(0);
190+
expect(setItems).toHaveBeenCalledTimes(1);
191+
});
192+
});

0 commit comments

Comments
 (0)