@fedify/cfworkers 2.0.0-dev.237 → 2.0.0-dev.279

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mod.d.ts CHANGED
@@ -3,6 +3,30 @@ import { KvKey, KvStore, KvStoreListEntry, KvStoreSetOptions, MessageQueue, Mess
3
3
 
4
4
  //#region src/mod.d.ts
5
5
 
6
+ /**
7
+ * Result from {@link WorkersMessageQueue.processMessage}.
8
+ * @since 2.0.0
9
+ */
10
+ interface ProcessMessageResult {
11
+ /**
12
+ * Whether the message should be processed. If `false`, the message has not
13
+ * been processed (for example, because an ordering key lock is still held)
14
+ * and the caller is responsible for re-enqueuing or retrying it when
15
+ * appropriate.
16
+ */
17
+ readonly shouldProcess: boolean;
18
+ /**
19
+ * The unwrapped message payload to process.
20
+ * Only present when `shouldProcess` is `true`.
21
+ */
22
+ readonly message?: any;
23
+ /**
24
+ * A cleanup function that must be called after processing the message.
25
+ * This releases the ordering key lock. Only present when `shouldProcess`
26
+ * is `true` and the message had an ordering key.
27
+ */
28
+ readonly release?: () => Promise<void>;
29
+ }
6
30
  /**
7
31
  * Implementation of the {@link KvStore} interface for Cloudflare Workers KV
8
32
  * binding. This class provides a wrapper around Cloudflare's KV namespace to
@@ -26,6 +50,33 @@ declare class WorkersKvStore implements KvStore {
26
50
  */
27
51
  list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
28
52
  }
53
+ /**
54
+ * Options for {@link WorkersMessageQueue}.
55
+ * @since 2.0.0
56
+ */
57
+ interface WorkersMessageQueueOptions {
58
+ /**
59
+ * The KV namespace to use for ordering key locks. If not provided, ordering
60
+ * keys will not be supported.
61
+ *
62
+ * Note: Cloudflare Workers KV has eventual consistency, so ordering key
63
+ * guarantees are best-effort. For strict ordering requirements, consider
64
+ * using Durable Objects.
65
+ */
66
+ readonly orderingKv?: KVNamespace<string>;
67
+ /**
68
+ * The prefix for ordering key lock keys. Defaults to `"__fedify_ordering_"`.
69
+ * @default `"__fedify_ordering_"`
70
+ */
71
+ readonly orderingKeyPrefix?: string;
72
+ /**
73
+ * The TTL (time-to-live) for ordering key locks in seconds.
74
+ * Defaults to 60 seconds. Must be at least 60 seconds due to
75
+ * Cloudflare KV minimum TTL requirement.
76
+ * @default 60
77
+ */
78
+ readonly orderingLockTtl?: number;
79
+ }
29
80
  /**
30
81
  * Implementation of the {@link MessageQueue} interface for Cloudflare
31
82
  * Workers Queues binding. This class provides a wrapper around Cloudflare's
@@ -34,8 +85,8 @@ declare class WorkersKvStore implements KvStore {
34
85
  * Note that this implementation does not support the `listen()` method,
35
86
  * as Cloudflare Workers Queues do not support message consumption in the same
36
87
  * way as other message queue systems. Instead, you should use
37
- * the {@link Federation.processQueuedTask} method to process messages
38
- * passed to the queue.
88
+ * the {@link WorkersMessageQueue.processMessage} method to handle ordering key
89
+ * locks before calling {@link Federation.processQueuedTask}.
39
90
  * @since 1.9.0
40
91
  */
41
92
  declare class WorkersMessageQueue implements MessageQueue {
@@ -46,10 +97,53 @@ declare class WorkersMessageQueue implements MessageQueue {
46
97
  * @since 1.7.0
47
98
  */
48
99
  readonly nativeRetrial = true;
49
- constructor(queue: Queue);
100
+ /**
101
+ * Constructs a new {@link WorkersMessageQueue} with the given queue and
102
+ * optional ordering key configuration.
103
+ * @param queue The Cloudflare Queue binding.
104
+ * @param options Options for ordering key support.
105
+ */
106
+ constructor(queue: Queue, options?: WorkersMessageQueueOptions);
50
107
  enqueue(message: any, options?: MessageQueueEnqueueOptions): Promise<void>;
51
108
  enqueueMany(messages: readonly any[], options?: MessageQueueEnqueueOptions): Promise<void>;
109
+ /**
110
+ * Processes a message from the queue, handling ordering key locks.
111
+ * Call this method before {@link Federation.processQueuedTask} to ensure
112
+ * ordering key semantics are respected.
113
+ *
114
+ * Example usage in a Cloudflare Worker queue handler:
115
+ *
116
+ * ```typescript ignore
117
+ * export default {
118
+ * async queue(batch, env, ctx) {
119
+ * const queue = new WorkersMessageQueue(env.QUEUE, {
120
+ * orderingKv: env.ORDERING_KV,
121
+ * });
122
+ * for (const msg of batch.messages) {
123
+ * const result = await queue.processMessage(msg.body);
124
+ * if (!result.shouldProcess) {
125
+ * msg.retry(); // Re-enqueue to wait for lock
126
+ * continue;
127
+ * }
128
+ * try {
129
+ * await federation.processQueuedTask(ctx, result.message);
130
+ * msg.ack();
131
+ * } catch (e) {
132
+ * msg.retry();
133
+ * } finally {
134
+ * await result.release?.();
135
+ * }
136
+ * }
137
+ * }
138
+ * };
139
+ * ```
140
+ *
141
+ * @param rawMessage The raw message body from the queue.
142
+ * @returns A result object indicating whether to process the message.
143
+ * @since 2.0.0
144
+ */
145
+ processMessage(rawMessage: any): Promise<ProcessMessageResult>;
52
146
  listen(_handler: (message: any) => Promise<void> | void, _options?: MessageQueueListenOptions): Promise<void>;
53
147
  }
54
148
  //#endregion
55
- export { WorkersKvStore, WorkersMessageQueue };
149
+ export { ProcessMessageResult, WorkersKvStore, WorkersMessageQueue, WorkersMessageQueueOptions };
package/dist/mod.js CHANGED
@@ -81,33 +81,112 @@ var WorkersKvStore = class {
81
81
  * Note that this implementation does not support the `listen()` method,
82
82
  * as Cloudflare Workers Queues do not support message consumption in the same
83
83
  * way as other message queue systems. Instead, you should use
84
- * the {@link Federation.processQueuedTask} method to process messages
85
- * passed to the queue.
84
+ * the {@link WorkersMessageQueue.processMessage} method to handle ordering key
85
+ * locks before calling {@link Federation.processQueuedTask}.
86
86
  * @since 1.9.0
87
87
  */
88
88
  var WorkersMessageQueue = class {
89
89
  #queue;
90
+ #orderingKv;
91
+ #orderingKeyPrefix;
92
+ #orderingLockTtl;
90
93
  /**
91
94
  * Cloudflare Queues provide automatic retry with exponential backoff
92
95
  * and Dead Letter Queues.
93
96
  * @since 1.7.0
94
97
  */
95
98
  nativeRetrial = true;
96
- constructor(queue) {
99
+ /**
100
+ * Constructs a new {@link WorkersMessageQueue} with the given queue and
101
+ * optional ordering key configuration.
102
+ * @param queue The Cloudflare Queue binding.
103
+ * @param options Options for ordering key support.
104
+ */
105
+ constructor(queue, options = {}) {
97
106
  this.#queue = queue;
107
+ this.#orderingKv = options.orderingKv;
108
+ this.#orderingKeyPrefix = options.orderingKeyPrefix ?? "__fedify_ordering_";
109
+ this.#orderingLockTtl = Math.max(options.orderingLockTtl ?? 60, 60);
110
+ }
111
+ #getOrderingLockKey(orderingKey) {
112
+ return `${this.#orderingKeyPrefix}${orderingKey}`;
98
113
  }
99
- enqueue(message, options) {
100
- return this.#queue.send(message, {
114
+ async enqueue(message, options) {
115
+ const wrapped = {
116
+ __fedify_ordering_key__: options?.orderingKey,
117
+ __fedify_payload__: message
118
+ };
119
+ await this.#queue.send(wrapped, {
101
120
  contentType: "json",
102
121
  delaySeconds: options?.delay?.total("seconds") ?? 0
103
122
  });
104
123
  }
105
- enqueueMany(messages, options) {
124
+ async enqueueMany(messages, options) {
106
125
  const requests = messages.map((msg) => ({
107
- body: msg,
126
+ body: {
127
+ __fedify_ordering_key__: options?.orderingKey,
128
+ __fedify_payload__: msg
129
+ },
108
130
  contentType: "json"
109
131
  }));
110
- return this.#queue.sendBatch(requests, { delaySeconds: options?.delay?.total("seconds") ?? 0 });
132
+ await this.#queue.sendBatch(requests, { delaySeconds: options?.delay?.total("seconds") ?? 0 });
133
+ }
134
+ /**
135
+ * Processes a message from the queue, handling ordering key locks.
136
+ * Call this method before {@link Federation.processQueuedTask} to ensure
137
+ * ordering key semantics are respected.
138
+ *
139
+ * Example usage in a Cloudflare Worker queue handler:
140
+ *
141
+ * ```typescript ignore
142
+ * export default {
143
+ * async queue(batch, env, ctx) {
144
+ * const queue = new WorkersMessageQueue(env.QUEUE, {
145
+ * orderingKv: env.ORDERING_KV,
146
+ * });
147
+ * for (const msg of batch.messages) {
148
+ * const result = await queue.processMessage(msg.body);
149
+ * if (!result.shouldProcess) {
150
+ * msg.retry(); // Re-enqueue to wait for lock
151
+ * continue;
152
+ * }
153
+ * try {
154
+ * await federation.processQueuedTask(ctx, result.message);
155
+ * msg.ack();
156
+ * } catch (e) {
157
+ * msg.retry();
158
+ * } finally {
159
+ * await result.release?.();
160
+ * }
161
+ * }
162
+ * }
163
+ * };
164
+ * ```
165
+ *
166
+ * @param rawMessage The raw message body from the queue.
167
+ * @returns A result object indicating whether to process the message.
168
+ * @since 2.0.0
169
+ */
170
+ async processMessage(rawMessage) {
171
+ const wrapped = rawMessage;
172
+ const orderingKey = wrapped.__fedify_ordering_key__;
173
+ const message = "__fedify_payload__" in wrapped ? wrapped.__fedify_payload__ : rawMessage;
174
+ if (orderingKey == null || this.#orderingKv == null) return {
175
+ shouldProcess: true,
176
+ message
177
+ };
178
+ const lockKey = this.#getOrderingLockKey(orderingKey);
179
+ const existing = await this.#orderingKv.get(lockKey);
180
+ if (existing != null) return { shouldProcess: false };
181
+ await this.#orderingKv.put(lockKey, Date.now().toString(), { expirationTtl: this.#orderingLockTtl });
182
+ const release = async () => {
183
+ await this.#orderingKv.delete(lockKey);
184
+ };
185
+ return {
186
+ shouldProcess: true,
187
+ message,
188
+ release
189
+ };
111
190
  }
112
191
  listen(_handler, _options) {
113
192
  throw new TypeError("WorkersMessageQueue does not support listen(). Use Federation.processQueuedTask() method instead.");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/cfworkers",
3
- "version": "2.0.0-dev.237+7f2bb1de",
3
+ "version": "2.0.0-dev.279+ce1bdc22",
4
4
  "description": "Adapt Fedify with Cloudflare Workers",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -52,7 +52,7 @@
52
52
  ],
53
53
  "peerDependencies": {
54
54
  "@cloudflare/workers-types": "^4.20250906.0",
55
- "@fedify/fedify": "^2.0.0-dev.237+7f2bb1de"
55
+ "@fedify/fedify": "^2.0.0-dev.279+ce1bdc22"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@cloudflare/vitest-pool-workers": "^0.8.31",
@@ -62,8 +62,9 @@
62
62
  "wrangler": "^4.21.1"
63
63
  },
64
64
  "scripts": {
65
- "build": "tsdown",
66
- "prepublish": "tsdown",
65
+ "build:self": "tsdown",
66
+ "build": "pnpm --filter @fedify/cfworkers... run build:self",
67
+ "prepublish": "pnpm build",
67
68
  "test": "vitest run"
68
69
  }
69
70
  }