@fedify/cfworkers 1.9.0-pr.388.1454 → 1.9.0-pr.388.1457
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/README.md +110 -0
- package/dist/mod.d.ts +2 -2
- package/dist/mod.js +2 -2
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
<!-- deno-fmt-ignore-file -->
|
|
2
|
+
|
|
3
|
+
@fedify/cfworkers: Adapt Fedify with Cloudflare Workers
|
|
4
|
+
======================================================
|
|
5
|
+
|
|
6
|
+
[![JSR][JSR badge]][JSR]
|
|
7
|
+
[![npm][npm badge]][npm]
|
|
8
|
+
[![Follow @fedify@hollo.social][@fedify@hollo.social badge]][@fedify@hollo.social]
|
|
9
|
+
|
|
10
|
+
*This package is available since Fedify 1.9.0.*
|
|
11
|
+
|
|
12
|
+
This package provides [Fedify]'s [`KvStore`] and [`MessageQueue`]
|
|
13
|
+
implementations for [Cloudflare Workers]:
|
|
14
|
+
|
|
15
|
+
- [`WorkersKvStore`]
|
|
16
|
+
- [`WorkersMessageQueue`]
|
|
17
|
+
|
|
18
|
+
~~~~ typescript
|
|
19
|
+
import type { Federation } from "@fedify/fedify";
|
|
20
|
+
import { WorkersKvStore, WorkersMessageQueue } from "@fedify/cfworkers";
|
|
21
|
+
|
|
22
|
+
export default {
|
|
23
|
+
async fetch(request, env, ctx) {
|
|
24
|
+
const federation = createFederation({
|
|
25
|
+
kv: new WorkersKvStore(env.KV_BINDING),
|
|
26
|
+
queue: new WorkersMessageQueue(env.QUEUE_BINDING),
|
|
27
|
+
// ... other options
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return federation.handle(request, { contextData: env });
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
async queue(batch, env, ctx) {
|
|
34
|
+
const federation = createFederation({
|
|
35
|
+
kv: new WorkersKvStore(env.KV_BINDING),
|
|
36
|
+
queue: new WorkersMessageQueue(env.QUEUE_BINDING),
|
|
37
|
+
// ... other options
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
for (const message of batch.messages) {
|
|
41
|
+
await federation.processQueuedTask(message.body);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
} satisfies ExportedHandler<{
|
|
45
|
+
KV_BINDING: KVNamespace<string>;
|
|
46
|
+
QUEUE_BINDING: Queue;
|
|
47
|
+
}>;
|
|
48
|
+
~~~~
|
|
49
|
+
|
|
50
|
+
`WorkersKvStore`
|
|
51
|
+
----------------
|
|
52
|
+
|
|
53
|
+
`WorkersKvStore` is a key–value store implementation for [Cloudflare Workers]
|
|
54
|
+
that uses Cloudflare's built-in [Cloudflare Workers KV] API. It provides
|
|
55
|
+
persistent storage and good performance for Cloudflare Workers environments.
|
|
56
|
+
It's suitable for production use in Cloudflare Workers applications.
|
|
57
|
+
|
|
58
|
+
`WorkersMessageQueue`
|
|
59
|
+
---------------------
|
|
60
|
+
|
|
61
|
+
`WorkersMessageQueue` is a message queue implementation for [Cloudflare Workers]
|
|
62
|
+
that uses Cloudflare's built-in [Cloudflare Queues] API. It provides
|
|
63
|
+
scalability and high performance, making it suitable for production use in
|
|
64
|
+
Cloudflare Workers environments. It requires a Cloudflare Queues setup and
|
|
65
|
+
management.
|
|
66
|
+
|
|
67
|
+
> [!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.
|
|
71
|
+
>
|
|
72
|
+
> For better organization, you probably want to use a builder pattern to
|
|
73
|
+
> register your dispatchers and listeners before instantiating the `Federation`
|
|
74
|
+
> object.
|
|
75
|
+
|
|
76
|
+
> [!NOTE]
|
|
77
|
+
> The [Cloudflare Queues] API does not provide a way to poll messages from
|
|
78
|
+
> the queue, so `WorkersMessageQueue.listen()` method always throws
|
|
79
|
+
> a `TypeError` when invoked. Instead, you should define a `queue()` method
|
|
80
|
+
> in your Cloudflare worker, which will be called by the Cloudflare Queues
|
|
81
|
+
> API when new messages are available in the queue. Inside the `queue()`
|
|
82
|
+
> method, you need to call `Federation.processQueuedTask()` method to manually
|
|
83
|
+
> process the messages. The `queue()` method is the only way to consume
|
|
84
|
+
> messages from the queue in Cloudflare Workers.
|
|
85
|
+
|
|
86
|
+
Installation
|
|
87
|
+
------------
|
|
88
|
+
|
|
89
|
+
~~~~ sh
|
|
90
|
+
deno add jsr:@fedify/cfworkers # Deno
|
|
91
|
+
npm add @fedify/cfworkers # npm
|
|
92
|
+
pnpm add @fedify/cfworkers # pnpm
|
|
93
|
+
yarn add @fedify/cfworkers # Yarn
|
|
94
|
+
bun add @fedify/cfworkers # Bun
|
|
95
|
+
~~~~
|
|
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
|
@@ -12,7 +12,7 @@ import { KvKey, KvStore, KvStoreSetOptions, MessageQueue, MessageQueueEnqueueOpt
|
|
|
12
12
|
* operation, as Cloudflare Workers KV does not support atomic compare-and-swap
|
|
13
13
|
* operations. If you need this functionality, consider using a different
|
|
14
14
|
* key–value store that supports atomic operations.
|
|
15
|
-
* @since 1.
|
|
15
|
+
* @since 1.9.0
|
|
16
16
|
*/
|
|
17
17
|
declare class WorkersKvStore implements KvStore {
|
|
18
18
|
#private;
|
|
@@ -31,7 +31,7 @@ declare class WorkersKvStore implements KvStore {
|
|
|
31
31
|
* way as other message queue systems. Instead, you should use
|
|
32
32
|
* the {@link Federation.processQueuedTask} method to process messages
|
|
33
33
|
* passed to the queue.
|
|
34
|
-
* @since 1.
|
|
34
|
+
* @since 1.9.0
|
|
35
35
|
*/
|
|
36
36
|
declare class WorkersMessageQueue implements MessageQueue {
|
|
37
37
|
#private;
|
package/dist/mod.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* operation, as Cloudflare Workers KV does not support atomic compare-and-swap
|
|
9
9
|
* operations. If you need this functionality, consider using a different
|
|
10
10
|
* key–value store that supports atomic operations.
|
|
11
|
-
* @since 1.
|
|
11
|
+
* @since 1.9.0
|
|
12
12
|
*/
|
|
13
13
|
var WorkersKvStore = class {
|
|
14
14
|
#namespace;
|
|
@@ -45,7 +45,7 @@ var WorkersKvStore = class {
|
|
|
45
45
|
* way as other message queue systems. Instead, you should use
|
|
46
46
|
* the {@link Federation.processQueuedTask} method to process messages
|
|
47
47
|
* passed to the queue.
|
|
48
|
-
* @since 1.
|
|
48
|
+
* @since 1.9.0
|
|
49
49
|
*/
|
|
50
50
|
var WorkersMessageQueue = class {
|
|
51
51
|
#queue;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/cfworkers",
|
|
3
|
-
"version": "1.9.0-pr.388.
|
|
3
|
+
"version": "1.9.0-pr.388.1457+92d31874",
|
|
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.20250529.0",
|
|
55
|
-
"@fedify/fedify": "1.9.0-pr.388.
|
|
55
|
+
"@fedify/fedify": "1.9.0-pr.388.1457+92d31874"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"tsdown": "^0.12.9",
|