trpc-uwebsockets
Version:
tRPC adapter for uWebSockets.js server
51 lines • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyRequestHandler = applyRequestHandler;
exports.uWsRequestHandler = uWsRequestHandler;
const http_1 = require("@trpc/server/http");
const fetchCompat_1 = require("./fetchCompat");
/**
* Applies tRPC request handler to uWebSockets app.
* @param app - The uWebSockets application.
* @param opts - Options for configuring the tRPC request handler.
*/
function applyRequestHandler(app, opts) {
const prefix = opts.prefix ?? '';
const handler = async (res, req) => {
const url = req.getUrl().substring(prefix.length + 1);
const resDecorated = (0, fetchCompat_1.decorateHttpResponse)(res, opts.ssl);
const reqFetch = (0, fetchCompat_1.uWsToRequest)(req, resDecorated, {
maxBodySize: opts.maxBodySize ?? null,
});
await uWsRequestHandler({
...opts.trpcOptions,
req: reqFetch,
res: resDecorated,
path: url,
});
};
app.get(prefix + '/*', handler);
app.post(prefix + '/*', handler);
}
async function uWsRequestHandler(opts) {
const createContext = async (innerOpts) => {
return await opts.createContext?.({
...opts,
...innerOpts,
});
};
const fetchRes = await (0, http_1.resolveResponse)({
...opts,
req: opts.req,
error: null,
createContext,
onError(o) {
opts?.onError?.({
...o,
req: opts.req,
});
},
});
await (0, fetchCompat_1.uWsSendResponseStreamed)(fetchRes, opts.res);
}
//# sourceMappingURL=requestHandler.js.map