Unverified Commit 065a0d45 authored by Hong Minhee's avatar Hong Minhee
Browse files

Docs for `WorkersKvStore`

parent 6aab3eca
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import deflist from "markdown-it-deflist";
import footnote from "markdown-it-footnote";
import { jsrRef } from "markdown-it-jsr-ref";
import process from "node:process";
import { ModuleKind, ModuleResolutionKind, ScriptTarget } from "typescript";
import { defineConfig } from "vitepress";
import {
  groupIconMdPlugin,
@@ -210,12 +211,16 @@ export default withMermaid(defineConfig({
      transformerTwoslash({
        twoslashOptions: {
          compilerOptions: {
            moduleResolution: ModuleResolutionKind.Bundler,
            module: ModuleKind.ESNext,
            target: ScriptTarget.ESNext,
            lib: ["dom", "dom.iterable", "esnext"],
            types: [
              "dom",
              "dom.iterable",
              "esnext",
              "@teidesu/deno-types/full",
              "@cloudflare/workers-types/experimental",
            ],
            // @ts-ignore: Although it's typed as string, it's actually an array
            jsx: ["react-jsx"],
+49 −0
Original line number Diff line number Diff line
@@ -142,6 +142,55 @@ const federation = createFederation<void>({
[`PostgresKvStore`]: https://jsr.io/@fedify/postgres/doc/kv/~/PostgresKvStore
[@fedify/postgres]: https://github.com/fedify-dev/postgres

### `WorkersKvStore` (Cloudflare Workers only)

*This API is available since Fedify 1.6.0.*

`WorkersKvStore` is a key–value store implementation for [Cloudflare Workers]
that uses Cloudflare's built-in [Cloudflare Workers KV] API.  It provides
persistent storage and good performance for Cloudflare Workers environments.
It's suitable for production use in Cloudflare Workers applications.

Best for
:   Production use in Cloudflare Workers environments.

Pros
:   Persistent storage, good performance, easy to set up.

Cons
:   Only available in Cloudflare Workers runtime.

~~~~ typescript twoslash
// @noErrors: 2345
import type { FederationBuilder } from "@fedify/fedify";
const builder = undefined as unknown as FederationBuilder<void>;
// ---cut-before---
import type { Federation } from "@fedify/fedify";
import { WorkersKvStore } from "@fedify/fedify/x/cfworkers";

export default {
  async fetch(request, env, ctx) {
    const federation: Federation<void> = await builder.build({
      kv: new WorkersKvStore(env.KV_BINDING),
    });
    return await federation.fetch(request, { contextData: undefined });
  }
} satisfies ExportedHandler<{ KV_BINDING: KVNamespace<string> }>;
~~~~

> [!NOTE]
> Since your `KVNamespace` is not bound to a global variable, but rather
> passed as an argument to the `fetch()` method, you need to instantiate
> your `Federation` object inside the `fetch()` method.
>
> For better organization, you probably want to use a builder pattern to
> register your dispatchers and listeners before instantiating the `Federation`
> object.  See the [*Builder pattern for structuring*
> section](./federation.md#builder-pattern-for-structuring) for details.

[Cloudflare Workers]: https://workers.cloudflare.com/
[Cloudflare Workers KV]: https://developers.cloudflare.com/kv/


Implementing a custom `KvStore`
-------------------------------
+3 −1
Original line number Diff line number Diff line
{
  "devDependencies": {
    "@braintree/sanitize-url": "^7.1.1",
    "@cloudflare/workers-types": "4.20250529.0",
    "@deno/kv": "^0.8.4",
    "@fedify/amqp": "^0.2.0",
    "@fedify/fedify": "^1.6.1-dev.828",
    "@fedify/fedify": "1.6.1-pr.242.861",
    "@fedify/postgres": "^0.3.0",
    "@fedify/redis": "^0.4.0",
    "@hono/node-server": "^1.13.7",
@@ -30,6 +31,7 @@
    "mermaid": "^11.4.1",
    "postgres": "^3.4.5",
    "stringify-entities": "^4.0.4",
    "typescript": "^5.8.3",
    "vitepress": "^1.5.0",
    "vitepress-plugin-group-icons": "^1.3.5",
    "vitepress-plugin-llms": "^1.1.0",
+16 −5
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@ importers:
      '@braintree/sanitize-url':
        specifier: ^7.1.1
        version: 7.1.1
      '@cloudflare/workers-types':
        specifier: 4.20250529.0
        version: 4.20250529.0
      '@deno/kv':
        specifier: ^0.8.4
        version: 0.8.4
@@ -18,8 +21,8 @@ importers:
        specifier: ^0.2.0
        version: 0.2.0(web-streams-polyfill@3.3.3)
      '@fedify/fedify':
        specifier: ^1.6.1-dev.828
        version: 1.6.1-dev.828(web-streams-polyfill@3.3.3)
        specifier: 1.6.1-pr.242.861
        version: 1.6.1-pr.242.861(web-streams-polyfill@3.3.3)
      '@fedify/postgres':
        specifier: ^0.3.0
        version: 0.3.0(web-streams-polyfill@3.3.3)
@@ -98,6 +101,9 @@ importers:
      stringify-entities:
        specifier: ^4.0.4
        version: 4.0.4
      typescript:
        specifier: ^5.8.3
        version: 5.8.3
      vitepress:
        specifier: ^1.5.0
        version: 1.6.3(@algolia/client-search@5.25.0)(@types/node@22.15.21)(postcss@8.5.3)(search-insights@2.17.3)(typescript@5.8.3)
@@ -235,6 +241,9 @@ packages:
  '@chevrotain/utils@11.0.3':
    resolution: {integrity: sha512-YslZMgtJUyuMbZ+aKvfF3x1f5liK4mWNxghFRv7jqRR9C3R3fAOGTTKvxXDa2Y1s9zSbcpuO0cAxDYsc9SrXoQ==}

  '@cloudflare/workers-types@4.20250529.0':
    resolution: {integrity: sha512-l6tVFpI6MUChMD0wK+Jhikb+aCbrmIR58CVpV/BhRT4THjl+nFhTT5N5ZqX42FDXdE3hCPLjueBMpPRhPUOB2A==}

  '@deno/kv-darwin-arm64@0.8.4':
    resolution: {integrity: sha512-j86nnE1QdLw20OrUs/6Iw6ZYzC8pmfU1+K4hNSVHO9K0bfy3VBd4JSHkHLmYCiHDkgIm+wTxct33thl6HxXz0Q==}
    engines: {node: '>= 18'}
@@ -451,8 +460,8 @@ packages:
    resolution: {integrity: sha512-s4ev+HMu6TNH1/RMNvIGvyn17D2dahq8Fpn/aDuBBngi0rHuOodZsoOnQMvEl9MoU0isv9GhPq5BP8oPYSmGaw==}
    engines: {bun: '>=1.1.0', deno: '>=2.0.0', node: '>=20.0.0'}

  '@fedify/fedify@1.6.1-dev.828':
    resolution: {integrity: sha512-2yDwPBDOSfPvOusHb6f7fzF9M/o/tsOLeQtu7IclXJPFAzCkVa5FKd8w696+5X1j21f91LgvEwZ/fQvPAp06Vg==}
  '@fedify/fedify@1.6.1-pr.242.861':
    resolution: {integrity: sha512-EiBxK8oJsiqrvnQzzqZj4HaPEpIa09PuGQT6v8aC9ZlOoqwzsNId4d6R7FnyfzLUNtPCKHmOaCCWVh6xSqfWvA==}
    engines: {bun: '>=1.1.0', deno: '>=2.0.0', node: '>=22.0.0'}

  '@fedify/postgres@0.3.0':
@@ -2816,6 +2825,8 @@ snapshots:

  '@chevrotain/utils@11.0.3': {}

  '@cloudflare/workers-types@4.20250529.0': {}

  '@deno/kv-darwin-arm64@0.8.4':
    optional: true

@@ -2998,7 +3009,7 @@ snapshots:
    transitivePeerDependencies:
      - web-streams-polyfill

  '@fedify/fedify@1.6.1-dev.828(web-streams-polyfill@3.3.3)':
  '@fedify/fedify@1.6.1-pr.242.861(web-streams-polyfill@3.3.3)':
    dependencies:
      '@cfworker/json-schema': 4.1.1
      '@es-toolkit/es-toolkit': es-toolkit@1.38.0