nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

FastifyAdapter add middleware got TypeError: Cannot set property hostname of #<IncomingMessage> which has only a getter #11297

Closed qaynam closed 1 year ago

qaynam commented 1 year ago

Is there an existing issue for this?

Current behavior

I get an error when using FastifyAdapter in NestJs with GCP Cloud functions framework.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-8and8c?file=src/main.ts

Steps to reproduce

  1. yarn build
  2. yarn start

Expected behavior

functions like this

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { createServer, IncomingMessage } from 'http';
import { http, HttpFunction } from '@google-cloud/functions-framework';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { FastifyInstance } from 'fastify';
import { bootstrap } from 'src/main';

const instance = new FastifyAdapter({
  ignoreTrailingSlash: true,
  trustProxy: true,
  serverFactory: createServer,
});

let app: NestFastifyApplication;

const handler: HttpFunction = async (req, res) => {
  if (!app) {
    app = await bootstrap(instance);
  }

  const adapter = app.getHttpAdapter();
  const fastifyInstance = adapter.getInstance() as FastifyInstance;

  void fastifyInstance.ready().then((_err) => {
    fastifyInstance.server.emit('request', new IncomingMessage(req.socket), res);
  });
};

export const api = http('api', handler);

Package

Other package

@nestjs/platform-fastify'

NestJS version

9.0.1

Packages versions

{
  "name": "api",
  "version": "1.0.0",
  "private": true,
  "main": "dist/src/functions/api.js",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "dev:watch": "nest build --watch",
    "dev": "PORT=4000 nest build --webpack --webpackPath webpack-hmr.config.js --watch",
    "function": "functions-framework --target=api --port=4000",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"src/**/*.ts\"",
    "test": "NODE_ENV=test jest",
    "compile": "tsc --noEmit",
    "compile:watch": "yarn compile --watch --runInBand",
    "test:watch": "jest --watch --runInBand",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json --runInBand",
    "test:ci": "NODE_ENV=test jest --ci --colors",
    "nest": "nest",
    "migrate": "prisma migrate deploy",
    "g:types": "ts-node ./src/generate-typings.ts",
    "seed": "ts-node -r tsconfig-paths/register ./src/db/seed/seed.ts",
    "local": "nodemon --watch src --exec \"ts-node --swc -r tsconfig-paths/register src/main.ts\""
  },
  "dependencies": {
    "@aws-sdk/client-s3": "^3.245.0",
    "@aws-sdk/s3-request-presigner": "^3.245.0",
    "@nestjs/common": "9.0.1",
    "@nestjs/core": "9.0.1",
    "@nestjs/graphql": "^10.1.5",
    "@nestjs/mercurius": "^10.1.4",
    "@nestjs/platform-fastify": "9.0.1",
    "express": "^4.18.2",
    "fastify": "^4.9.2",
    "firebase-admin": "^11.0.0",
    "firebase-functions": "^4.2.1",
    "graphql": "^16.6.0",
    "jsonwebtoken": "^9.0.0",
    "mercurius": "^11.3.0",
    "nodemailer": "^6.8.0",
    "prisma-client": "^0.0.1",
    "reflect-metadata": "0.1.13",
    "rimraf": "3.0.2",
    "rxjs": "7.5.5",
    "vercel": "^28.17.0",
    "zod": "^3.20.6"
  },
  "devDependencies": {
    "@google-cloud/functions-framework": "^3.1.3",
    "@nestjs/cli": "9.0.0",
    "@nestjs/schematics": "9.0.1",
    "@nestjs/testing": "9.0.1",
    "@swc/core": "^1.3.28",
    "@types/express": "4.17.13",
    "@types/jest": "^28.1.6",
    "@types/jsonwebtoken": "^9.0.0",
    "@types/node": "18.0.3",
    "@types/nodemailer": "^6.4.6",
    "@types/supertest": "2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.48.2",
    "@typescript-eslint/parser": "^5.48.2",
    "copy-webpack-plugin": "^11.0.0",
    "eslint": "8.19.0",
    "eslint-config-prettier": "8.5.0",
    "eslint-import-resolver-typescript": "^3.5.3",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "28.1.2",
    "nodemon": "^2.0.20",
    "prettier": "2.7.1",
    "run-script-webpack-plugin": "^0.1.1",
    "supertest": "6.2.4",
    "swc-loader": "^0.2.3",
    "ts-jest": "28.0.5",
    "ts-loader": "9.3.1",
    "ts-morph": "^16.0.0",
    "ts-node": "10.8.2",
    "tsconfig-paths": "^4.1.2",
    "typescript": "^4.9.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "moduleNameMapper": {
      "^src/(.*)$": "<rootDir>/$1",
      "^@tests/(.*)$": "<rootDir>/../__tests__/$1"
    },
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": [
        "ts-jest",
        {
          "tsconfig": "<rootDir>/../tsconfig.spec.json"
        }
      ]
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

16

In which operating systems have you tested?

Other

No response

qaynam commented 1 year ago

The problem seems to be that if I use FastifyAdapter, I get an error when I use app.use(SomeMiddleware)

micalevisk commented 1 year ago

that happens when you do this .emit:

image

I didn't follow why tho

qaynam commented 1 year ago

that happens when you do this .emit:

image

I didn't follow why tho

But I can't find any other way to pass throw CloudFunction request into nestJS Fastify instance 🤔

kamilmysliwiec commented 1 year ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.