-
Notifications
You must be signed in to change notification settings - Fork 82
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
Cannot get fastify context in request handler. #708
Comments
Hi @acrazing can you elaborate on what details of the Fastify request you're looking for? Is it the logging instance or are there other bits of info you need? |
logger is the most urgently needed. |
But other information is also very important, such as ip address? I think a mechanism is needed to allow users to inject context, similar to grpc's interceptor. |
Here is my current patch file: diff --git a/node_modules/@bufbuild/connect-fastify/dist/cjs/fastify-connect-plugin.js b/node_modules/@bufbuild/connect-fastify/dist/cjs/fastify-connect-plugin.js
index 505235f..1983a89 100644
--- a/node_modules/@bufbuild/connect-fastify/dist/cjs/fastify-connect-plugin.js
+++ b/node_modules/@bufbuild/connect-fastify/dist/cjs/fastify-connect-plugin.js
@@ -43,7 +43,7 @@ function fastifyConnectPlugin(instance, opts, done) {
for (const uHandler of uHandlers) {
instance.all(uHandler.requestPath, {}, async function handleFastifyRequest(req, reply) {
try {
- const uRes = await uHandler((0, connect_node_1.universalRequestFromNodeRequest)(req.raw, req.body));
+ const uRes = await uHandler(Object.assign((0, connect_node_1.universalRequestFromNodeRequest)(req.raw, req.body), { extraContext: { req, reply, log: req.log } }));
// Fastify maintains response headers on the reply object and only moves them to
// the raw response during reply.send, but we are not using reply.send with this plugin.
// So we need to manually copy them to the raw response before handing off to vanilla Node.
diff --git a/node_modules/@bufbuild/connect-fastify/dist/types/fastify-connect-plugin.d.ts b/node_modules/@bufbuild/connect-fastify/dist/types/fastify-connect-plugin.d.ts
index 675ac8a..fdb473a 100644
--- a/node_modules/@bufbuild/connect-fastify/dist/types/fastify-connect-plugin.d.ts
+++ b/node_modules/@bufbuild/connect-fastify/dist/types/fastify-connect-plugin.d.ts
@@ -1,5 +1,7 @@
import type { ConnectRouter, ConnectRouterOptions } from "@bufbuild/connect";
import type { FastifyInstance } from "fastify/types/instance";
+import { FastifyReply, FastifyRequest } from 'fastify';
+import { Logger } from 'pino';
interface FastifyConnectPluginOptions extends ConnectRouterOptions {
/**
* Route definitions. We recommend the following pattern:
@@ -23,3 +25,11 @@ interface FastifyConnectPluginOptions extends ConnectRouterOptions {
*/
export declare function fastifyConnectPlugin(instance: FastifyInstance, opts: FastifyConnectPluginOptions, done: (err?: Error) => void): void;
export {};
+
+declare module '@bufbuild/connect' {
+ export interface HandlerContext {
+ req: FastifyRequest;
+ reply: FastifyReply;
+ log: Logger;
+ }
+}
diff --git a/node_modules/@bufbuild/connect/dist/cjs/protocol-grpc/handler-factory.js b/node_modules/@bufbuild/connect/dist/cjs/protocol-grpc/handler-factory.js
index e606c02..26d8f81 100644
--- a/node_modules/@bufbuild/connect/dist/cjs/protocol-grpc/handler-factory.js
+++ b/node_modules/@bufbuild/connect/dist/cjs/protocol-grpc/handler-factory.js
@@ -67,7 +67,7 @@ function createHandler(opt, spec) {
[headers_js_1.headerContentType]: type.binary ? content_type_js_1.contentTypeProto : content_type_js_1.contentTypeJson,
}, responseTrailer: {
[headers_js_1.headerGrpcStatus]: trailer_status_js_1.grpcStatusOk,
- } }));
+ } }, req.extraContext));
const compression = (0, compression_js_1.compressionNegotiate)(opt.acceptCompression, req.header.get(headers_js_1.headerEncoding), req.header.get(headers_js_1.headerAcceptEncoding), headers_js_1.headerAcceptEncoding);
if (compression.response) {
context.responseHeader.set(headers_js_1.headerEncoding, compression.response.name); |
Fastify has a strong plugin system. If we can't access the request instance, we can't utilize it. |
Understood. We are currently preparing for an upcoming v1.0 release and once that is complete, we are going to focus on enhancements such as this, so stay tuned. |
Hi @smaye81 any ETA for this? Would you accept a PR? |
No definitive ETA, but we are nearing our v1.0 release. We have an idea of how to accomplish this that will help with Express and Fastify, so I would say hold off on the PR for now. Hopefully we get to this very soon. |
Alright, thank you! |
Realize +1's aren't always helpful, but I just wanted to add that there is basically no way to work around this issue without patching the source. We'd happily own and maintain our own fastify connect plugin implementation if that solved this, but unfortunately (as the patch above makes clear) that isn't enough. Also happy to contribute a PR if the internal timeline gets pushed out. This blocks a proper implementation of request logging (and therefore proper request tracing in prod). |
@joemckenney is it opensource? We would love to contribute. Unfortunately, we can't wait until it is fixed here which seems will take a while. The alternative is we roll out our own too. Besides access to fastify inside buf handlers, it would be also very beneficial if the integration would honor fastify request/response lifecycle instead of hijacking the response. That would provide compatible error / response hooks handling as well. |
In original fastify handler, we can access the fastify request context handy.
Bug it is impossible in connect-es.
The text was updated successfully, but these errors were encountered: