Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 76bc1a6

Browse files
CraigglesOmrbbot
andauthored
adjust for alarm storage (#298)
* adjust for alarm storage * test case more clear * better comments * adjust options not overwritten * Update plugin.spec.ts Co-authored-by: MrBBot <[email protected]>
1 parent 5c5b0ad commit 76bc1a6

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

packages/durable-objects/src/storage.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,31 @@ async function list<Value = unknown>(
183183
"list() cannot be called with both start and startAfter values."
184184
);
185185
}
186+
options = { ...options };
186187
const originalLimit = options.limit;
188+
// Since alarms now exist in storage, add 1 to the limit to account for
189+
// the alarm key.
190+
if (options.limit !== undefined) options.limit++;
187191
if (options.startAfter !== undefined) {
188192
// If *exclusive* `startAfter` is set, set it as the *inclusive* `start`.
189193
// Then if `startAfter` does exist as a key, we can remove it later.
190194
// To ensure we still return `limit` keys in this case, add 1 to the limit
191195
// if one is set.
192-
options = { ...options, start: options.startAfter };
196+
options.start = options.startAfter;
193197
if (options.limit !== undefined) options.limit++;
194198
}
195199

196200
const { keys } = await storage.list(options);
197-
let keyNames = keys.map(({ name }) => name);
201+
let keyNames = keys
202+
.map(({ name }) => name)
203+
.filter((name) => name !== ALARM_KEY);
198204

199-
if (options.startAfter !== undefined) {
200-
if (keyNames[0] === options.startAfter) {
201-
// If the first key matched `startAfter`, remove it as this is exclusive.
202-
keyNames.splice(0, 1);
203-
} else if (originalLimit !== undefined) {
204-
// Otherwise, make sure the original `limit` still holds.
205-
keyNames = keyNames.slice(0, originalLimit);
206-
}
205+
if (options.startAfter !== undefined && keyNames[0] === options.startAfter) {
206+
// If the first key matched `startAfter`, remove it as this is exclusive.
207+
keyNames.splice(0, 1);
207208
}
209+
// Make sure the original `limit` still holds.
210+
if (originalLimit !== undefined) keyNames = keyNames.slice(0, originalLimit);
208211

209212
return get(
210213
storage,

packages/durable-objects/test/plugin.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,29 @@ test("DurableObjectsPlugin: setup alarms and dispose alarms", async (t) => {
337337
plugin.dispose();
338338
t.false(plugin.durableObjectsAlarms);
339339
});
340+
341+
test("DurableObjectsPlugin: set alarm and run list filters out alarm", async (t) => {
342+
class Object1 implements DurableObject {
343+
constructor(private readonly state: DurableObjectState) {}
344+
345+
fetch = async () => {
346+
await this.state.storage.setAlarm(Date.now() + 60 * 1000);
347+
const list = await this.state.storage.list();
348+
return new Response(JSON.stringify(list));
349+
};
350+
alarm = () => {};
351+
}
352+
353+
const factory = new MemoryStorageFactory();
354+
const plugin = new DurableObjectsPlugin(ctx, {
355+
durableObjects: { OBJECT1: "Object1" },
356+
});
357+
358+
const result = await plugin.setup(factory);
359+
plugin.beforeReload();
360+
plugin.reload({}, { Object1 }, new Map());
361+
362+
const ns1: DurableObjectNamespace = result.bindings?.OBJECT1;
363+
const res1 = await ns1.get(ns1.newUniqueId()).fetch("/");
364+
t.is(await res1.text(), "{}");
365+
});

0 commit comments

Comments
 (0)