@trpc/server
Version:
76 lines (74 loc) • 2.63 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_rolldown_runtime = require("../rolldown-runtime-CFW-PwTP.cjs");
const require_codes = require("../codes-Be-DrYMh.cjs");
const require_getErrorShape = require("../getErrorShape-BAuHhNuh.cjs");
const require_node_http = require("../node-http-vS2eeV-2.cjs");
let http = require("http");
http = require_rolldown_runtime.__toESM(http, 1);
//#region src/adapters/standalone.ts
/**
* If you're making an adapter for tRPC and looking at this file for reference, you should import types and functions from `@trpc/server` and `@trpc/server/http`
*
* @example
* ```ts
* import type { AnyTRPCRouter } from '@trpc/server'
* import type { HTTPBaseHandlerOptions } from '@trpc/server/http'
* ```
*/
const trimSlashes = (path) => {
path = path.startsWith("/") ? path.slice(1) : path;
path = path.endsWith("/") ? path.slice(0, -1) : path;
return path;
};
function createHandler(opts) {
var _opts$basePath;
const basePath = trimSlashes((_opts$basePath = opts.basePath) !== null && _opts$basePath !== void 0 ? _opts$basePath : "/");
return (req, res) => {
let path = "";
require_codes.run(async () => {
const url = require_node_http.createURL(req);
path = trimSlashes(trimSlashes(url.pathname).slice(basePath.length));
await require_node_http.nodeHTTPRequestHandler(require_getErrorShape._objectSpread2(require_getErrorShape._objectSpread2({}, opts), {}, {
req,
res,
path
}));
}).catch(require_node_http.internal_exceptionHandler(require_getErrorShape._objectSpread2({
req,
res,
path
}, opts)));
};
}
/**
* Creates a Node.js request listener for a router, to use with your own
* `http.createServer()`.
* @see https://trpc.io/docs/server/adapters/standalone#adding-a-handler-to-a-custom-http-server
* @internal
*/
function createHTTPHandler(opts) {
return createHandler(opts);
}
/**
* Creates a Node.js HTTP server for a router. Call `.listen()` on it.
* @example
* ```ts
* createHTTPServer({ router: appRouter, createContext }).listen(3000);
* ```
* @see https://trpc.io/docs/server/adapters/standalone#2-use-the-standalone-adapter
*/
function createHTTPServer(opts) {
return http.default.createServer(createHTTPHandler(opts));
}
/**
* Creates a request listener for a router, to use with Node.js's
* `http2.createSecureServer()`.
* @see https://trpc.io/docs/server/adapters/standalone#http2
*/
function createHTTP2Handler(opts) {
return createHandler(opts);
}
//#endregion
exports.createHTTP2Handler = createHTTP2Handler;
exports.createHTTPHandler = createHTTPHandler;
exports.createHTTPServer = createHTTPServer;