Skip to content

[WIP] Support Nestjs Integration #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft

[WIP] Support Nestjs Integration #309

wants to merge 11 commits into from

Conversation

malkoG
Copy link

@malkoG malkoG commented Jul 17, 2025

Summary

closes: #269
Support NestJS integration

Benefits

NestJS Developers who want to develop fediverse app, They can easily build ActivityPub app thanks to Fedify

Checklist

  • Did you add a changelog entry to the CHANGES.md?
  • Did you write some relevant docs about this change (if it's a new feature)?
  • Did you write a regression test to reproduce the bug (if it's a bug fix)?
  • Did you write some tests for this change (if it's a new feature)?
  • Did you run deno task test-all on your machine?

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @malkoG, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request initiates the development of a dedicated integration layer for the Fedify library with the NestJS framework. It establishes a new package containing a NestJS module for managing the Fedify Federation instance and a middleware to bridge the request/response handling between NestJS's Express-based environment and Fedify's Web API expectations. This work-in-progress aims to provide a robust and idiomatic way to use Fedify within NestJS applications.

Highlights

  • New NestJS Integration Package: This pull request introduces a brand new @fedify/nestjs package, designed to seamlessly integrate the Fedify library into NestJS applications. This marks a significant expansion of Fedify's compatibility with popular web frameworks.
  • Core NestJS Module (FedifyModule): A new FedifyModule has been added, providing a forRoot static method. This module allows NestJS applications to easily configure and provide a Federation instance, managing its lifecycle and dependencies within the NestJS ecosystem.
  • Request/Response Adaptation Middleware: A key component is the integrateFederation middleware. This middleware handles the crucial task of converting standard Express Request and Response objects (used by NestJS) into Web API Request and Response objects, which are required by Fedify's federation.fetch method. It also maps Fedify's response back to Express.
  • Package Structure and Configuration: The PR includes all necessary files for the new package, such as package.json, deno.json, and tsdown.config.ts, setting up its build process, dependencies (including @nestjs/common and express), and export points.
  • Placeholder README Discrepancy: The nestjs/README.md file appears to be a placeholder copied directly from the @fedify/express package. This README will need to be updated to accurately reflect the integration steps and usage for NestJS applications.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added component/integration Web framework integration status/in progress In progress type/feature Requests for new features labels Jul 17, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new package for NestJS integration with Fedify. The review focuses on improving the robustness of the middleware by streaming request and response bodies, and ensuring documentation and configuration are accurate.

Comment on lines 36 to 39
body: ['GET', 'HEAD'].includes(req.method) ? undefined : JSON.stringify(req.body),
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The request body is being handled by serializing the request body (JSON.stringify(req.body)), which is inefficient and may not work for non-JSON payloads. Stream the raw request body using Readable.toWeb(req) from node:stream.

        const webRequest = new Request(url.toString(), {
          method: req.method,
          headers,
          // @ts-ignore: duplex is not supported in Deno, but it is in Node.js
          duplex: "half",
          body: ['GET', 'HEAD'].includes(req.method)
            ? undefined
            : Readable.toWeb(req),
        });

Comment on lines 48 to 51
const body = await response.text();
res.send(body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The response is buffered using await response.text(), which can lead to high memory usage for large responses. Stream the response body directly to the client by iterating over response.body's ReadableStream and writing each chunk to the Express response.

          res.status(response.status);
          if (response.body) {
            for await (const chunk of response.body) {
              res.write(chunk);
            }
          }
          res.end();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

nestjs/deno.json Outdated
},
"imports": {
"express": "npm:express@^4.0.0",
"nestjs": "npm:@nestjs/core@^11.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Inconsistent indentation detected. This line uses a tab character for indentation, while the rest of the file uses spaces. Use spaces for indentation to maintain consistency.

    "nestjs": "npm:@nestjs/core@^11.0.1"

Copy link
Contributor

github-actions bot commented Jul 17, 2025

The docs for this pull request have been published:

https://6115a1a0.fedify.pages.dev

@malkoG
Copy link
Author

malkoG commented Jul 17, 2025

I am facing this error. Investigating...

fedify-backend   | 2 export * from "./fedify.module.ts";
fedify-backend   |                 ~~~~~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | ../../node_modules/@fedify/nestjs/index.ts:3:15 - error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
fedify-backend   | 
fedify-backend   | 3 export * from "./fedify.constants.ts";
fedify-backend   |                 ~~~~~~~~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | ../../node_modules/@fedify/nestjs/index.ts:6:15 - error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
fedify-backend   | 
fedify-backend   | 6 export * from "./fedify.middleware.ts";
fedify-backend   |                 ~~~~~~~~~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/app.module.ts:22:66 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 22 import { InProcessMessageQueue, MemoryKvStore, Federation } from '@fedify/fedify';
fedify-backend   |                                                                     ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/libs/fedify-nestjs/fedify.middleware.ts:4:28 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 4 import { Federation } from '@fedify/fedify';
fedify-backend   |                              ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/libs/fedify-nestjs/fedify.module.ts:2:88 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 2 import { createFederation, MemoryKvStore, KvStore, MessageQueue, DocumentLoader } from '@fedify/fedify';
fedify-backend   |                                                                                          ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/modules/federation/federation.service.ts:9:28 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 9 import { Federation } from '@fedify/fedify';
fedify-backend   |                              ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/modules/federation/handlers/activity.handler.ts:5:102 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 5 import { Follow as FedifyFollow, Undo, Create, Like, Update, Delete, Announce, RequestContext } from '@fedify/fedify';
fedify-backend   |                                                                                                        ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/modules/federation/handlers/actor.handler.ts:6:97 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 6 import { Application, Federation, Image, importPkcs1, importSpki, Person, RequestContext } from '@fedify/fedify';
fedify-backend   |                                                                                                   ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/modules/federation/handlers/nodeinfo.handler.ts:6:57 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 6 import { Federation, parseSemVer, RequestContext } from '@fedify/fedify';
fedify-backend   |                                                           ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/modules/federation/handlers/webfinger.handler.ts:6:44 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 6 import { Federation, RequestContext } from '@fedify/fedify';
fedify-backend   |                                              ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/modules/federation/services/activity-delivery.service.ts:6:103 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 6 import { Create, Delete, Federation, PUBLIC_COLLECTION, RequestContext, Update, Note as APNote } from '@fedify/fedify';
fedify-backend   |                                                                                                         ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | src/modules/federation/services/context.service.ts:2:28 - error TS2307: Cannot find module '@fedify/fedify' or its corresponding type declarations.
fedify-backend   | 
fedify-backend   | 2 import { Federation } from '@fedify/fedify';
fedify-backend   |                              ~~~~~~~~~~~~~~~~
fedify-backend   | 
fedify-backend   | [5:31:20 PM] Found 17 errors. Watching for file changes.
fedify-backend   | 
w Enable Watch

Copy link
Member

@dahlia dahlia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have some tests as well?

Comment on lines 48 to 51
const body = await response.text();
res.send(body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@malkoG
Copy link
Author

malkoG commented Jul 18, 2025

Wait. I am facing this error. I will resolve it.

fedify-backend   | (node:39) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenate
d.
fedify-backend   | (Use `node --trace-deprecation ...` to show where the warning was created)
fedify-backend   | node:internal/modules/cjs/loader:657
fedify-backend   |       throw e;
fedify-backend   |       ^
fedify-backend   | 
fedify-backend   | Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /app/node_modules/@fedify/nestjs/package.json
fedify-backend   |     at exportsNotFound (node:internal/modules/esm/resolve:313:10)
fedify-backend   |     at packageExportsResolve (node:internal/modules/esm/resolve:603:13)
fedify-backend   |     at resolveExports (node:internal/modules/cjs/loader:650:36)
fedify-backend   |     at Module._findPath (node:internal/modules/cjs/loader:717:31)
fedify-backend   |     at Module._resolveFilename (node:internal/modules/cjs/loader:1355:27)
fedify-backend   |     at defaultResolveImpl (node:internal/modules/cjs/loader:1025:19)
fedify-backend   |     at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1030:22)
fedify-backend   |     at Module._load (node:internal/modules/cjs/loader:1179:37)
fedify-backend   |     at TracingChannel.traceSync (node:diagnostics_channel:322:14)
fedify-backend   |     at wrapModuleLoad (node:internal/modules/cjs/loader:235:24) {
fedify-backend   |   code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
fedify-backend   | }

Copy link
Contributor

The latest push to this pull request has been published to JSR and npm as a pre-release:

Package Version JSR npm
@fedify/fedify 1.8.0-pr.309.1063+be0bfe1f JSR npm
@fedify/cli 1.8.0-pr.309.1063+be0bfe1f JSR
@fedify/amqp 1.8.0-pr.309.1063+be0bfe1f JSR npm
@fedify/express 1.8.0-pr.309.1063+be0bfe1f JSR npm
@fedify/h3 1.8.0-pr.309.1063+be0bfe1f JSR npm
@fedify/postgres 1.8.0-pr.309.1063+be0bfe1f JSR npm
@fedify/redis 1.8.0-pr.309.1063+be0bfe1f JSR npm

@malkoG malkoG deployed to github-pages July 18, 2025 03:55 — with GitHub Actions Active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/integration Web framework integration status/in progress In progress type/feature Requests for new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NestJS integration
2 participants