|
| 1 | +import type { HttpRequest, HttpResponse } from 'uWebSockets.js'; |
| 2 | +import { |
| 3 | + createHandler as createRawHandler, |
| 4 | + HandlerOptions as RawHandlerOptions, |
| 5 | + OperationContext, |
| 6 | +} from '../handler'; |
| 7 | + |
| 8 | +/** |
| 9 | + * The context in the request for the handler. |
| 10 | + * |
| 11 | + * @category Server/uWebSockets |
| 12 | + */ |
| 13 | +export interface RequestContext { |
| 14 | + res: HttpResponse; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Handler options when using the http adapter. |
| 19 | + * |
| 20 | + * @category Server/uWebSockets |
| 21 | + */ |
| 22 | +export type HandlerOptions<Context extends OperationContext = undefined> = |
| 23 | + RawHandlerOptions<HttpRequest, RequestContext, Context>; |
| 24 | + |
| 25 | +/** |
| 26 | + * Create a GraphQL over HTTP spec compliant request handler for |
| 27 | + * the Node environment [uWebSockets.js module](https://github.com/uNetworking/uWebSockets.js/). |
| 28 | + * |
| 29 | + * ```js |
| 30 | + * import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<version> |
| 31 | + * import { createHandler } from 'graphql-http/lib/use/uWebSockets'; |
| 32 | + * import { schema } from './my-graphql-schema'; |
| 33 | + * |
| 34 | + * uWS |
| 35 | + * .App() |
| 36 | + * .any('/graphql', createHandler({ schema })) |
| 37 | + * .listen(4000, () => { |
| 38 | + * console.log('Listening to port 4000'); |
| 39 | + * }); |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * @category Server/uWebSockets |
| 43 | + */ |
| 44 | +export function createHandler<Context extends OperationContext = undefined>( |
| 45 | + options: HandlerOptions<Context>, |
| 46 | +): (res: HttpResponse, req: HttpRequest) => Promise<void> { |
| 47 | + const handle = createRawHandler(options); |
| 48 | + return async function requestListener(res, req) { |
| 49 | + let aborted = false; |
| 50 | + res.onAborted(() => (aborted = true)); |
| 51 | + try { |
| 52 | + let url = req.getUrl(); |
| 53 | + const query = req.getQuery(); |
| 54 | + if (query) { |
| 55 | + url += '?' + query; |
| 56 | + } |
| 57 | + const [body, init] = await handle({ |
| 58 | + url, |
| 59 | + method: req.getMethod().toUpperCase(), |
| 60 | + headers: { get: (key) => req.getHeader(key) }, |
| 61 | + body: () => |
| 62 | + new Promise<string>((resolve) => { |
| 63 | + let body = ''; |
| 64 | + if (aborted) { |
| 65 | + resolve(body); |
| 66 | + } else { |
| 67 | + res.onData((chunk, isLast) => { |
| 68 | + body += Buffer.from(chunk, 0, chunk.byteLength).toString(); |
| 69 | + if (isLast) { |
| 70 | + resolve(body); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + }), |
| 75 | + raw: req, |
| 76 | + context: { res }, |
| 77 | + }); |
| 78 | + if (!aborted) { |
| 79 | + res.writeStatus(`${init.status} ${init.statusText}`); |
| 80 | + for (const [key, val] of Object.entries(init.headers || {})) { |
| 81 | + res.writeHeader(key, val); |
| 82 | + } |
| 83 | + if (body) { |
| 84 | + res.end(body); |
| 85 | + } else { |
| 86 | + res.endWithoutBody(); |
| 87 | + } |
| 88 | + } |
| 89 | + } catch (err) { |
| 90 | + // The handler shouldnt throw errors. |
| 91 | + // If you wish to handle them differently, consider implementing your own request handler. |
| 92 | + console.error( |
| 93 | + 'Internal error occurred during request handling. ' + |
| 94 | + 'Please check your implementation.', |
| 95 | + err, |
| 96 | + ); |
| 97 | + if (!aborted) { |
| 98 | + res.writeStatus('500 Internal Server Error').endWithoutBody(); |
| 99 | + } |
| 100 | + } |
| 101 | + }; |
| 102 | +} |
0 commit comments