Unverified Commit 5972481d authored by An Nyeong's avatar An Nyeong Committed by Hong Minhee
Browse files

Create fastify package

@fedify/fastify package provides a simple integration of fedify and fastify

- implement integration using fp, fastify-plugin system
- simple tests for integration
- simple example (tested on Node.js and Bun)

problems and todos

- tests for bun has been failed with timeout error
parent f28b6855
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
    "./packages/cfworkers",
    "./packages/denokv",
    "./packages/express",
    "./packages/fastify",
    "./packages/h3",
    "./packages/hono",
    "./packages/koa",
@@ -15,6 +16,8 @@
    "./packages/sveltekit",
    "./packages/testing",
    "./examples/blog",
    "./examples/cloudflare-workers",
    "./examples/fastify",
    "./examples/hono-sample"
  ],
  "imports": {
+1 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ added in the future.[^1]
  -  [Hollo: a federated single-user microblogging
     software](https://github.com/fedify-dev/hollo)
  -  [Hono integration sample](./hono-sample/)
  -  [Fastify integration example](./fastify/)
  -  [Fedify–Express integration example](./express/)
  -  [Fedify–Next.js integration example using `@fedify/next`](./next-integration/)
  -  [Fedify–Next.js 14 integration example](./next14-app-router/)
+4 −0
Original line number Diff line number Diff line
Fastify integration example
===========================

This is a simple example application that demonstrates of integration Fedify with Fastify.
+47 −0
Original line number Diff line number Diff line
import { createFederation, MemoryKvStore, Person } from "@fedify/fedify";
import Fastify from "fastify";
import fedifyPlugin from "../../packages/fastify/src/index.ts";

const fastify = Fastify({ logger: true });

const federation = createFederation<void>({
  kv: new MemoryKvStore(),
});

// Add a simple actor
federation.setActorDispatcher(
  "/users/{identifier}",
  async (ctx, identifier) => {
    return new Person({
      id: ctx.getActorUri(identifier),
      name: identifier,
      summary: "This is a summary of the user",
      preferredUsername: identifier,
      url: new URL("/", ctx.url),
    });
  },
);

// Regular application routes
fastify.get("/", async () => {
  return {
    message: "Hello World! This is a Fastify server with Fedify integration.",
  };
});

// Start the server
const start = async () => {
  // Register the Fedify plugin
  await fastify.register(fedifyPlugin, {
    federation,
    contextDataFactory: () => undefined,
  });

  await fastify.listen({ port: 3000, host: "0.0.0.0" });
  console.log("Server listening on http://localhost:3000");
  console.log("Try visiting:");
  console.log("- http://localhost:3000/ (regular route)");
  console.log("- http://localhost:3000/users/alice (federation route)");
};

start();
+19 −0
Original line number Diff line number Diff line
{
  "name": "@fedify/fastify-example",
  "version": "0.1.0",
  "private": true,
  "type": "module",
  "scripts": {
    "start": "tsx index.ts",
    "dev": "tsx watch index.ts"
  },
  "dependencies": {
    "@fedify/fastify": "workspace:",
    "@fedify/fedify": "workspace:",
    "fastify": "catalog:"
  },
  "devDependencies": {
    "@types/node": "catalog:",
    "tsx": "^4.19.0"
  }
}
Loading