Skip to content

Commit a3cd8b8

Browse files
authored
feat: allow access to Fastify request from Node request (#1385)
1 parent 3b88bf9 commit a3cd8b8

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

examples/servers/fastify3/rum-and-raisin.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,35 @@ import {
77
} from /*'postgraphile'*/ '../../../';
88
import { database, schemas, options, port } from '../common';
99

10-
const middleware = postgraphile(database, schemas, options);
10+
const middleware = postgraphile(database, schemas, {
11+
...options,
12+
pgSettings(req) {
13+
// Adding this to ensure that all servers pass through the request in a
14+
// good enough way that we can extract headers.
15+
// CREATE FUNCTION current_user_id() RETURNS text AS $$ SELECT current_setting('graphile.test.x-user-id', TRUE); $$ LANGUAGE sql STABLE;
16+
return {
17+
'graphile.test.x-user-id':
18+
// In GraphiQL, open console and enter `document.cookie = "userId=3"` to become user 3.
19+
req._fastifyRequest?.cookies.userId ||
20+
// `normalizedConnectionParams` comes from websocket connections, where
21+
// the headers often cannot be customized by the client.
22+
(req as any).normalizedConnectionParams?.['x-user-id'],
23+
};
24+
},
25+
});
1126

1227
/******************************************************************************/
1328
// These middlewares aren't needed; we just add them to make sure that
1429
// PostGraphile still works correctly with them in place.
1530

16-
import fastifyCompression from 'fastify-compress';
1731
const fastify = Fastify({ logger: true });
32+
33+
import fastifyCompression from 'fastify-compress';
1834
fastify.register(fastifyCompression, { threshold: 0, inflateIfDeflated: false });
1935

36+
import fastifyCookie from 'fastify-cookie';
37+
fastify.register(fastifyCookie, { secret: 'USE_A_SECURE_SECRET!' });
38+
2039
/******************************************************************************/
2140

2241
/**

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"express": "^4.14.0",
112112
"fastify": "^3.7.0",
113113
"fastify-compress": "^3.3.1",
114+
"fastify-cookie": "^4.1.0",
114115
"fastify-formbody": "^5.0.0",
115116
"fastify-v2": "npm:fastify@^2.2.0",
116117
"helmet": "^4.1.1",

src/postgraphile/http/frameworks.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { IncomingMessage, ServerResponse } from 'http';
22
import { PassThrough, Stream } from 'stream';
3-
import { Context as KoaContext } from 'koa';
4-
import { FastifyReply, FastifyRequest } from 'fastify';
3+
import type { Context as KoaContext } from 'koa';
4+
import type { FastifyReply, FastifyRequest } from 'fastify';
5+
6+
declare module 'http' {
7+
interface IncomingMessage {
8+
_koaCtx?: KoaContext;
9+
_fastifyRequest?: FastifyRequest;
10+
_body?: boolean;
11+
body?: any;
12+
originalUrl?: string;
13+
}
14+
}
515

616
type Headers = { [header: string]: string };
717

@@ -192,17 +202,18 @@ export class PostGraphileResponseKoa extends PostGraphileResponse {
192202
this._next = next;
193203
const req = this.getNodeServerRequest();
194204

195-
// For backwards compatibility (this is a documented interface)
196-
(req as any)._koaCtx = ctx;
205+
// For backwards compatibility, and to allow getting "back" to the Koa
206+
// context from pgSettings, etc (this is a documented interface)
207+
req._koaCtx = ctx;
197208

198209
// Make `koa-bodyparser` trigger skipping of our `body-parser`
199-
if ((ctx.request as any).body) {
200-
(req as any)._body = true;
201-
(req as any).body = (ctx.request as any).body;
210+
if (ctx.request.body) {
211+
req._body = true;
212+
req.body = ctx.request.body;
202213
}
203214

204215
// In case you're using koa-mount or similar
205-
(req as any).originalUrl = ctx.request.originalUrl;
216+
req.originalUrl = ctx.request.originalUrl;
206217
}
207218

208219
getNodeServerRequest() {
@@ -248,9 +259,13 @@ export class PostGraphileResponseFastify3 extends PostGraphileResponse {
248259
this._request = request;
249260
this._reply = reply;
250261

262+
// For backwards compatibility, and to allow getting "back" to the Fastify
263+
// request from pgSettings, etc
264+
const req = this.getNodeServerRequest();
265+
req._fastifyRequest = this._request;
266+
251267
// Make Fastify's body parsing trigger skipping of our `body-parser`
252268
if (this._request.body) {
253-
const req: any = this.getNodeServerRequest();
254269
req._body = true;
255270
req.body = this._request.body;
256271
}

yarn.lock

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3091,6 +3091,11 @@ [email protected]:
30913091
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c"
30923092
integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw=
30933093

3094+
cookie-signature@^1.1.0:
3095+
version "1.1.0"
3096+
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.1.0.tgz#cc94974f91fb9a9c1bb485e95fc2b7f4b120aff2"
3097+
integrity sha512-Alvs19Vgq07eunykd3Xy2jF0/qSNv2u7KDbAek9H5liV1UMijbqFs5cycZvv5dVsvseT/U4H8/7/w8Koh35C4A==
3098+
30943099
30953100
version "0.4.0"
30963101
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
@@ -4200,6 +4205,15 @@ fastify-compress@^3.3.1:
42004205
string-to-stream "^3.0.0"
42014206
unzipper "^0.10.8"
42024207

4208+
fastify-cookie@^4.1.0:
4209+
version "4.1.0"
4210+
resolved "https://registry.yarnpkg.com/fastify-cookie/-/fastify-cookie-4.1.0.tgz#b063f2b97cf9de7a33eb799a951b604ede48a915"
4211+
integrity sha512-+se+kNPFDE49JCiBYQrfPfchcW9s8BXjCqP5ijuYpbydTcMlo9pnyd8tyxLi43SIXBmeTBkB1CjklmD7nlHsXg==
4212+
dependencies:
4213+
cookie "^0.4.0"
4214+
cookie-signature "^1.1.0"
4215+
fastify-plugin "^2.0.0"
4216+
42034217
fastify-error@^0.2.0:
42044218
version "0.2.0"
42054219
resolved "https://registry.yarnpkg.com/fastify-error/-/fastify-error-0.2.0.tgz#9a1c28d4f42b6259e7a549671c8e5e2d85660634"
@@ -4212,7 +4226,7 @@ fastify-formbody@^5.0.0:
42124226
dependencies:
42134227
fastify-plugin "^2.3.2"
42144228

4215-
fastify-plugin@^2.2.0, fastify-plugin@^2.3.2:
4229+
fastify-plugin@^2.0.0, fastify-plugin@^2.2.0, fastify-plugin@^2.3.2:
42164230
version "2.3.4"
42174231
resolved "https://registry.yarnpkg.com/fastify-plugin/-/fastify-plugin-2.3.4.tgz#b17abdc36a97877d88101fb86ad8a07f2c07de87"
42184232
integrity sha512-I+Oaj6p9oiRozbam30sh39BiuiqBda7yK2nmSPVwDCfIBlKnT8YB3MY+pRQc2Fcd07bf6KPGklHJaQ2Qu81TYQ==

0 commit comments

Comments
 (0)