DEV Community

Discussion on: I made Express faster than Fastify (100x faster JSON, also NestJS)

Collapse
 
metcoder profile image
Carlos Fuentes

Nice work with typia, the job done has been amazing :)
Would you be willing to create a Fastify plugin for typia?
The results can be amazing! Happy to help you with it.

I’m part of Fastify team btw, in case you would like to share some thoughts or doubts about Fastify :)

Collapse
 
melroy89 profile image
Melroy van den Berg

So at this point I'm wondering if the benefits of nestia for Fastify can't be embedded upstream (as in making it fast by default when just using fastify)?

Collapse
 
samchon profile image
Jeongho Nam • Edited

Got it, I'll try at next Sunday (not tomorrow).

Anyway, where the plugin would be placed in? Just in my github account? Or as a fastify repo?

Collapse
 
metcoder profile image
Carlos Fuentes

Sure thing!

Yeah, made everything within a repo of your authorship and once done, we can incorporate it at the list of community plugins :)
Feel free to ping me out if you want a second pair eyes!

Thread Thread
 
samchon profile image
Jeongho Nam • Edited

What is your github accout name?

I will write an issue on fastify repo tagging you, and need to debate about below:

import fastify from "fastify";
import typia from "typia";

const plugin = fastify().addSomePlugin();
plugin.post("/some-path", {
    schema: {
        body: typia.application<[MyRequestDto]>(),
        response: typia.application<[MyResponseDto]>(),
    },
});
Enter fullscreen mode Exit fullscreen mode

At first, fastify provides plugin interfaces only through the JSON schema defition. As typia can generate JSON schema, it is possible to utilizing current plugin interface of fastify like above.

However, typia is a transformer library generating optimal code in the compilation time, utilizing pure TypeScript type. To maximize strength of typia and make users to be much convenient, I think below code would be should be possible.

import fastify from "fastify";
import typiaProvider from "@fastify/type-provider-typia";

const server = fastify();
const wrapper = typiaProvider.install(server);

wrapper.post<{
    query: SomeQueryDto;
    body: SomeRequestBodyDto;
    response: SomeResponseBodyDto;
}>("/some-path", (req, rep) => {
    req.query // -> type checked SomeQueryDto
    req.body // -> type checked SomeRequestBodyDto
    rep.send({ ... }); // -> faster stringify with type assertion (SomeResponseBodyDto)
    req.headers; // -> no type checking because not specified
});
Enter fullscreen mode Exit fullscreen mode

The typiaProvider.post<T>() function will generate both type assertion (+ JSON serialization when response) and JSON schema definition for Swagger at the same time. Of course, above code requires modifications from plugin APIs of fastify. Therefore, I need to debate about it detaily as an issue.

p.s) I'm developing protobuf features, therefore when fastify accepts typia as a plugin, you can easily support protobuf typed routers very easily.

Thread Thread
 
metcoder profile image
Carlos Fuentes

Hey! Please reach out in GH as metcoder95!

Wow, that sounds interesting. What modifications are you looking for exactly?
In theory, the fastify.register should provide you access to the encapsulated context where the plugin is being added, and therefore decorate any possible route being register through fastify.<post | get | ...>.
But please, open an issue at fastify repo and we can help you sorting things out :)

Thread Thread
 
samchon profile image
Jeongho Nam

Sorry for late.

I've been making guide documents of typia and nestia in nowaydays, and such documentation works had consumed much more time than what I'd expected. Therefore, could not develop the plugin in this weekend.

Anyway, stuyding fastify, I understood that there're enormous things to develop to adjust transform library typia in fastify. There are already a lot of elements to be developed, so I'm going to make a fastify plugin like nestia, which can build SDK library.

Please wait for one to two months, then I'll take interesting plugin.

Thread Thread
 
metcoder profile image
Carlos Fuentes

Please, do not rush it. Happy to wait for the results, please let me know if you need any support or similar. Happy to support you with anything!

Thread Thread
 
samchon profile image
Jeongho Nam • Edited

Currently, succeeded to support fastify through nestia in NestJS.

nestia.io/docs/
github.com/samchon/nestia/tree/mas...

Now, only "Swagger to NestJS" converter project is left for me. It may requires 2~3 weeks, and I'll start the pure fastify plugin project after that. Also, I'll run benchmark fastify + typia, too. Thanks for waiting.

Thread Thread
 
metcoder profile image
Carlos Fuentes

Hey! That's amazing! Don't worry about the plugin, take your time :)