Skip to content
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

How to make friends with fastify and express like middleware? #958

Open
MrWaip opened this issue Nov 6, 2023 · 6 comments
Open

How to make friends with fastify and express like middleware? #958

MrWaip opened this issue Nov 6, 2023 · 6 comments

Comments

@MrWaip
Copy link

MrWaip commented Nov 6, 2023

You have already researched for similar issues?

Yes, I do.

What are you trying to achieve, or the steps to reproduce?

Hi. I really want to make friends with "fastify", "vite dev server" and "svelte/kit".

The problem is that the plugin "svelte/kit" for "vite" is a middleware "express like".

At first I tried using "fastify/middie". It worked. But since the "svelte/kit middleware" directly interacts with the response, the fastifay hooks stop working. This is expected, as it is written in the documentation.

But I am not satisfied with this behavior.

For this reason, I had the idea to do this:

import fp from "fastify-plugin";
import { PassThrough } from "stream";

class Tunnel extends PassThrough {
  /** @param {import('fastify').FastifyReply} rep  */
  constructor(rep) {
    super();

    this.rep = rep;
  }

  get statusCode() {
    return this.rep.statusCode;
  }

  set statusCode(code) {
    this.rep.status(code);
  }

  setHeader(...args) {
    this.rep.header(...args);
  }

  writeHead(code, headers) {
    if (headers) this.rep.headers(headers);
    this.rep.status(code);
    return this;
  }

  getHeader(key) {
    return this.rep.getHeader(key);
  }

  getHeaderNames() {
    return Object.keys(this.rep.getHeaders());
  }

  removeHeader(key) {
    this.rep.removeHeader(key);
  }
}

export const plugin = fp(
  /**
   *
   * @param {import('fastify').FastifyInstance} instance
   * @param {{handler: (req: import('http').IncomingMessage, res: import('http').ServerResponse) => void}} param1
   */
  async (instance, { handler }) => {
    instance.addHook("onRequest", (req, rep, done) => {
      if (req.routeOptions.url) return done();

      const tunnel = new Tunnel(rep);
      handler(req.raw, tunnel, done);
      return rep.send(tunnel);
    });

    instance.get("/base/api/ping", async () => "pong");
  }
);

Do you think this is an acceptable solution?

@Uzlopak Uzlopak changed the title How to make friends with fustify and express like middleware? How to make friends with fastify and express like middleware? Nov 6, 2023
@Eomm
Copy link
Member

Eomm commented Nov 12, 2023

directly interacts with the response

I don't get the idea - the hook in the code manages the req.raw.. it seems an echo plugin to me.

@MrWaip
Copy link
Author

MrWaip commented Nov 16, 2023

Thanks to this fake response, I can use any express similar to the middleware and at the same time the following fastify hooks in the chain will work. for example, in onSend I can add a header with a trace. True, there is something wrong with the example, which I discovered later. If the middleware calls res.write and starts writing body, then the headers will be sent to the client and it will no longer be possible to set the headers in the onSend Hook. But it's easy to solve

@MrWaip
Copy link
Author

MrWaip commented Nov 16, 2023

I like this solution. it's a kind of express adapter. I can use one middleware, and the code written for fastify will always work. This is better than using a middle plugin, because as soon as you use one middleware, you have to use another, and fastify plugins will not work. and it turns out to be a mess

@Uzlopak
Copy link

Uzlopak commented Nov 16, 2023

Thanks to this fake response

Do you have a personal problem?

@MrWaip
Copy link
Author

MrWaip commented Nov 17, 2023

Fake response is the Tunnel class, that has Server response methods and It is Duplex Stream at the same time

@MrWaip
Copy link
Author

MrWaip commented Nov 17, 2023

Sry for misunderstanding :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants