@fedify/cfworkers 2.0.0-pr.490.2 → 2.0.0-pr.559.4

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright 2024–2025 Hong Minhee
3
+ Copyright 2024–2026 Hong Minhee
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <!-- deno-fmt-ignore-file -->
2
2
 
3
3
  @fedify/cfworkers: Adapt Fedify with Cloudflare Workers
4
- ======================================================
4
+ =======================================================
5
5
 
6
6
  [![JSR][JSR badge]][JSR]
7
7
  [![npm][npm badge]][npm]
@@ -47,6 +47,20 @@ export default {
47
47
  }>;
48
48
  ~~~~
49
49
 
50
+ [JSR badge]: https://jsr.io/badges/@fedify/cfworkers
51
+ [JSR]: https://jsr.io/@fedify/cfworkers
52
+ [npm badge]: https://img.shields.io/npm/v/@fedify/cfworkers?logo=npm
53
+ [npm]: https://www.npmjs.com/package/@fedify/cfworkers
54
+ [@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
55
+ [@fedify@hollo.social]: https://hollo.social/@fedify
56
+ [Fedify]: https://fedify.dev/
57
+ [`KvStore`]: https://jsr.io/@fedify/fedify/doc/federation/~/KvStore
58
+ [`MessageQueue`]: https://jsr.io/@fedify/fedify/doc/federation/~/MessageQueue
59
+ [Cloudflare Workers]: https://workers.cloudflare.com/
60
+ [`WorkersKvStore`]: https://jsr.io/@fedify/cfworkers/doc/~/WorkersKvStore
61
+ [`WorkersMessageQueue`]: https://jsr.io/@fedify/cfworkers/doc/~/WorkersMessageQueue
62
+
63
+
50
64
  `WorkersKvStore`
51
65
  ----------------
52
66
 
@@ -55,6 +69,9 @@ that uses Cloudflare's built-in [Cloudflare Workers KV] API. It provides
55
69
  persistent storage and good performance for Cloudflare Workers environments.
56
70
  It's suitable for production use in Cloudflare Workers applications.
57
71
 
72
+ [Cloudflare Workers KV]: https://developers.cloudflare.com/kv/
73
+
74
+
58
75
  `WorkersMessageQueue`
59
76
  ---------------------
60
77
 
@@ -65,9 +82,10 @@ Cloudflare Workers environments. It requires a Cloudflare Queues setup and
65
82
  management.
66
83
 
67
84
  > [!NOTE]
68
- > Since your `KVNamespace` and `Queue` are not bound to global variables, but rather
69
- > passed as arguments to the `fetch()` and `queue()` methods, you need to instantiate
70
- > your `Federation` object inside these methods, rather than at the top level.
85
+ > Since your `KVNamespace` and `Queue` are not bound to global variables, but
86
+ > rather passed as arguments to the `fetch()` and `queue()` methods, you need
87
+ > to instantiate your `Federation` object inside these methods, rather than at
88
+ > the top level.
71
89
  >
72
90
  > For better organization, you probably want to use a builder pattern to
73
91
  > register your dispatchers and listeners before instantiating the `Federation`
@@ -83,6 +101,9 @@ management.
83
101
  > process the messages. The `queue()` method is the only way to consume
84
102
  > messages from the queue in Cloudflare Workers.
85
103
 
104
+ [Cloudflare Queues]: https://developers.cloudflare.com/queues/
105
+
106
+
86
107
  Installation
87
108
  ------------
88
109
 
@@ -93,18 +114,3 @@ pnpm add @fedify/cfworkers # pnpm
93
114
  yarn add @fedify/cfworkers # Yarn
94
115
  bun add @fedify/cfworkers # Bun
95
116
  ~~~~
96
-
97
- [JSR]: https://jsr.io/@fedify/cfworkers
98
- [JSR badge]: https://jsr.io/badges/@fedify/cfworkers
99
- [npm]: https://www.npmjs.com/package/@fedify/cfworkers
100
- [npm badge]: https://img.shields.io/npm/v/@fedify/cfworkers?logo=npm
101
- [@fedify@hollo.social badge]: https://fedi-badge.deno.dev/@fedify@hollo.social/followers.svg
102
- [@fedify@hollo.social]: https://hollo.social/@fedify
103
- [Fedify]: https://fedify.dev/
104
- [`KvStore`]: https://jsr.io/@fedify/fedify/doc/federation/~/KvStore
105
- [`MessageQueue`]: https://jsr.io/@fedify/fedify/doc/federation/~/MessageQueue
106
- [`WorkersKvStore`]: https://jsr.io/@fedify/cfworkers/doc/~/WorkersKvStore
107
- [`WorkersMessageQueue`]: https://jsr.io/@fedify/cfworkers/doc/~/WorkersMessageQueue
108
- [Cloudflare Workers]: https://workers.cloudflare.com/
109
- [Cloudflare Workers KV]: https://developers.cloudflare.com/kv/
110
- [Cloudflare Queues]: https://developers.cloudflare.com/queues/
package/dist/mod.d.ts CHANGED
@@ -1,8 +1,32 @@
1
1
  import { KVNamespace, Queue } from "@cloudflare/workers-types/experimental";
2
- import { KvKey, KvStore, KvStoreSetOptions, MessageQueue, MessageQueueEnqueueOptions, MessageQueueListenOptions } from "@fedify/fedify/federation";
2
+ import { KvKey, KvStore, KvStoreListEntry, KvStoreSetOptions, MessageQueue, MessageQueueEnqueueOptions, MessageQueueListenOptions } from "@fedify/fedify/federation";
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
@@ -20,6 +44,38 @@ declare class WorkersKvStore implements KvStore {
20
44
  get<T = unknown>(key: KvKey): Promise<T | undefined>;
21
45
  set(key: KvKey, value: unknown, options?: KvStoreSetOptions): Promise<void>;
22
46
  delete(key: KvKey): Promise<void>;
47
+ /**
48
+ * {@inheritDoc KvStore.list}
49
+ * @since 1.10.0
50
+ */
51
+ list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
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;
23
79
  }
24
80
  /**
25
81
  * Implementation of the {@link MessageQueue} interface for Cloudflare
@@ -29,8 +85,8 @@ declare class WorkersKvStore implements KvStore {
29
85
  * Note that this implementation does not support the `listen()` method,
30
86
  * as Cloudflare Workers Queues do not support message consumption in the same
31
87
  * way as other message queue systems. Instead, you should use
32
- * the {@link Federation.processQueuedTask} method to process messages
33
- * passed to the queue.
88
+ * the {@link WorkersMessageQueue.processMessage} method to handle ordering key
89
+ * locks before calling {@link Federation.processQueuedTask}.
34
90
  * @since 1.9.0
35
91
  */
36
92
  declare class WorkersMessageQueue implements MessageQueue {
@@ -41,10 +97,53 @@ declare class WorkersMessageQueue implements MessageQueue {
41
97
  * @since 1.7.0
42
98
  */
43
99
  readonly nativeRetrial = true;
44
- 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);
45
107
  enqueue(message: any, options?: MessageQueueEnqueueOptions): Promise<void>;
46
- enqueueMany(messages: any[], options?: MessageQueueEnqueueOptions): Promise<void>;
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>;
47
146
  listen(_handler: (message: any) => Promise<void> | void, _options?: MessageQueueListenOptions): Promise<void>;
48
147
  }
49
148
  //#endregion
50
- export { WorkersKvStore, WorkersMessageQueue };
149
+ export { ProcessMessageResult, WorkersKvStore, WorkersMessageQueue, WorkersMessageQueueOptions };
package/dist/mod.js CHANGED
@@ -34,6 +34,44 @@ var WorkersKvStore = class {
34
34
  delete(key) {
35
35
  return this.#namespace.delete(this.#encodeKey(key));
36
36
  }
37
+ /**
38
+ * {@inheritDoc KvStore.list}
39
+ * @since 1.10.0
40
+ */
41
+ async *list(prefix) {
42
+ let pattern;
43
+ let exactKey = null;
44
+ if (prefix == null || prefix.length === 0) pattern = "[";
45
+ else {
46
+ exactKey = this.#encodeKey(prefix);
47
+ pattern = JSON.stringify(prefix).slice(0, -1) + ",";
48
+ }
49
+ if (exactKey != null) {
50
+ const { value, metadata } = await this.#namespace.getWithMetadata(exactKey, "json");
51
+ if (value != null && (metadata == null || metadata.expires == null || metadata.expires >= Date.now())) yield {
52
+ key: prefix,
53
+ value
54
+ };
55
+ }
56
+ let cursor;
57
+ do {
58
+ const result = await this.#namespace.list({
59
+ prefix: pattern,
60
+ cursor
61
+ });
62
+ cursor = result.list_complete ? void 0 : result.cursor;
63
+ for (const keyInfo of result.keys) {
64
+ const metadata = keyInfo.metadata;
65
+ if (metadata?.expires != null && metadata.expires < Date.now()) continue;
66
+ const value = await this.#namespace.get(keyInfo.name, "json");
67
+ if (value == null) continue;
68
+ yield {
69
+ key: JSON.parse(keyInfo.name),
70
+ value
71
+ };
72
+ }
73
+ } while (cursor != null);
74
+ }
37
75
  };
38
76
  /**
39
77
  * Implementation of the {@link MessageQueue} interface for Cloudflare
@@ -43,33 +81,112 @@ var WorkersKvStore = class {
43
81
  * Note that this implementation does not support the `listen()` method,
44
82
  * as Cloudflare Workers Queues do not support message consumption in the same
45
83
  * way as other message queue systems. Instead, you should use
46
- * the {@link Federation.processQueuedTask} method to process messages
47
- * passed to the queue.
84
+ * the {@link WorkersMessageQueue.processMessage} method to handle ordering key
85
+ * locks before calling {@link Federation.processQueuedTask}.
48
86
  * @since 1.9.0
49
87
  */
50
88
  var WorkersMessageQueue = class {
51
89
  #queue;
90
+ #orderingKv;
91
+ #orderingKeyPrefix;
92
+ #orderingLockTtl;
52
93
  /**
53
94
  * Cloudflare Queues provide automatic retry with exponential backoff
54
95
  * and Dead Letter Queues.
55
96
  * @since 1.7.0
56
97
  */
57
98
  nativeRetrial = true;
58
- 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 = {}) {
59
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);
60
110
  }
61
- enqueue(message, options) {
62
- return this.#queue.send(message, {
111
+ #getOrderingLockKey(orderingKey) {
112
+ return `${this.#orderingKeyPrefix}${orderingKey}`;
113
+ }
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, {
63
120
  contentType: "json",
64
121
  delaySeconds: options?.delay?.total("seconds") ?? 0
65
122
  });
66
123
  }
67
- enqueueMany(messages, options) {
124
+ async enqueueMany(messages, options) {
68
125
  const requests = messages.map((msg) => ({
69
- body: msg,
126
+ body: {
127
+ __fedify_ordering_key__: options?.orderingKey,
128
+ __fedify_payload__: msg
129
+ },
70
130
  contentType: "json"
71
131
  }));
72
- 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
+ };
73
190
  }
74
191
  listen(_handler, _options) {
75
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-pr.490.2+99a396d5",
3
+ "version": "2.0.0-pr.559.4+6357309b",
4
4
  "description": "Adapt Fedify with Cloudflare Workers",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -51,16 +51,20 @@
51
51
  "package.json"
52
52
  ],
53
53
  "peerDependencies": {
54
- "@cloudflare/workers-types": "^4.20250529.0",
55
- "@fedify/fedify": "^2.0.0-pr.490.2+99a396d5"
54
+ "@cloudflare/workers-types": "^4.20250906.0",
55
+ "@fedify/fedify": "^2.0.0-pr.559.4+6357309b"
56
56
  },
57
57
  "devDependencies": {
58
+ "@cloudflare/vitest-pool-workers": "^0.8.31",
58
59
  "tsdown": "^0.12.9",
59
- "typescript": "^5.9.3"
60
+ "typescript": "^5.9.3",
61
+ "vitest": "~3.2.0",
62
+ "wrangler": "^4.21.1"
60
63
  },
61
64
  "scripts": {
62
- "build": "tsdown",
63
- "prepublish": "tsdown",
64
- "test": "deno task codegen && tsdown && cd dist/ && node --test"
65
+ "build:self": "tsdown",
66
+ "build": "pnpm --filter @fedify/cfworkers... run build:self",
67
+ "prepublish": "pnpm build",
68
+ "test": "vitest run"
65
69
  }
66
70
  }