/
fastify.ts
124 lines (117 loc) · 3.65 KB
/
fastify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type { FastifyRequest, FastifyReply } from 'fastify';
import {
createHandler as createRawHandler,
HandlerOptions as RawHandlerOptions,
OperationContext,
} from '../handler';
/**
* @category Server/fastify
*/
export interface RequestContext {
reply: FastifyReply;
}
/**
* @category Server/fastify
*/
export type HandlerOptions<Context extends OperationContext = undefined> =
RawHandlerOptions<FastifyRequest, RequestContext, Context>;
/**
* The ready-to-use handler for [fastify](https://www.fastify.io).
*
* Errors thrown from the provided options or callbacks (or even due to
* library misuse or potential bugs) will reject the handler or bubble to the
* returned iterator. They are considered internal errors and you should take care
* of them accordingly.
*
* For production environments, its recommended not to transmit the exact internal
* error details to the client, but instead report to an error logging tool or simply
* the console.
*
* ```ts
* import Fastify from 'fastify'; // yarn add fastify
* import { createHandler } from 'graphql-sse/lib/use/fastify';
*
* const handler = createHandler({ schema });
*
* const fastify = Fastify();
*
* fastify.all('/graphql/stream', async (req, reply) => {
* try {
* await handler(req, reply);
* } catch (err) {
* console.error(err);
* reply.code(500).send();
* }
* });
*
* fastify.listen({ port: 4000 });
* console.log('Listening to port 4000');
* ```
*
* @category Server/fastify
*/
export function createHandler<Context extends OperationContext = undefined>(
options: HandlerOptions<Context>,
): (req: FastifyRequest, reply: FastifyReply) => Promise<void> {
const handler = createRawHandler(options);
return async function handleRequest(req, reply) {
const [body, init] = await handler({
method: req.method,
url: req.url,
headers: {
get(key) {
const header = reply.getHeader(key) ?? req.headers[key];
return Array.isArray(header) ? header.join('\n') : String(header);
},
},
body: () =>
new Promise((resolve, reject) => {
if (req.body) {
// body was parsed by middleware
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- even if fastify incorrectly parsed the body, we cannot re-read it
return resolve(req.body as any);
}
let body = '';
req.raw.on('data', (chunk) => (body += chunk));
req.raw.once('error', reject);
req.raw.once('end', () => {
req.raw.off('error', reject);
resolve(body);
});
}),
raw: req,
context: { reply },
});
const middlewareHeaders: Record<string, string> = {};
for (const [key, val] of Object.entries(reply.getHeaders())) {
middlewareHeaders[key] = Array.isArray(val)
? val.join('\n')
: String(val);
}
reply.raw.writeHead(init.status, init.statusText, {
...middlewareHeaders,
...init.headers,
});
if (!body || typeof body === 'string') {
return new Promise<void>((resolve) =>
reply.raw.end(body, () => resolve()),
);
}
reply.raw.once('close', body.return);
for await (const value of body) {
const closed = await new Promise((resolve, reject) => {
if (!reply.raw.writable) {
// response's close event might be late
resolve(true);
} else {
reply.raw.write(value, (err) => (err ? reject(err) : resolve(false)));
}
});
if (closed) {
break;
}
}
reply.raw.off('close', body.return);
return new Promise((resolve) => reply.raw.end(resolve));
};
}