@trpc/server
Version:
1 lines • 4.25 kB
Source Map (JSON)
{"version":3,"file":"standalone.mjs","names":[],"sources":["../../src/adapters/standalone.ts"],"sourcesContent":["/**\n * 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`\n *\n * @example\n * ```ts\n * import type { AnyTRPCRouter } from '@trpc/server'\n * import type { HTTPBaseHandlerOptions } from '@trpc/server/http'\n * ```\n */\n\nimport http from 'http';\n// --- http2 ---\nimport type * as http2 from 'http2';\n// @trpc/server\nimport { type AnyRouter } from '../@trpc/server';\n// oxlint-disable-next-line no-restricted-imports\nimport { run } from '../unstable-core-do-not-import';\nimport type {\n NodeHTTPCreateContextFnOptions,\n NodeHTTPHandlerOptions,\n NodeHTTPRequest,\n NodeHTTPResponse,\n} from './node-http';\nimport {\n createURL,\n internal_exceptionHandler,\n nodeHTTPRequestHandler,\n} from './node-http';\n\n// same as in the fetch adapter\nconst trimSlashes = (path: string): string => {\n path = path.startsWith('/') ? path.slice(1) : path;\n path = path.endsWith('/') ? path.slice(0, -1) : path;\n\n return path;\n};\n\ntype StandaloneHandlerOptions<\n TRouter extends AnyRouter,\n TRequest extends NodeHTTPRequest,\n TResponse extends NodeHTTPResponse,\n> = NodeHTTPHandlerOptions<TRouter, TRequest, TResponse> & {\n /**\n * The base path to handle requests for.\n * This will be sliced from the beginning of the request path\n * @default '/'\n * @example '/trpc/'\n * @example '/trpc/api/'\n */\n basePath?: string;\n};\n\n// --- http1 ---\nexport type CreateHTTPHandlerOptions<TRouter extends AnyRouter> =\n StandaloneHandlerOptions<TRouter, http.IncomingMessage, http.ServerResponse>;\n\nexport type CreateHTTPContextOptions = NodeHTTPCreateContextFnOptions<\n http.IncomingMessage,\n http.ServerResponse\n>;\n\nfunction createHandler<\n TRouter extends AnyRouter,\n TRequest extends NodeHTTPRequest,\n TResponse extends NodeHTTPResponse,\n>(\n opts: StandaloneHandlerOptions<TRouter, TRequest, TResponse>,\n): (req: TRequest, res: TResponse) => void {\n const basePath = trimSlashes(opts.basePath ?? '/');\n\n return (req, res) => {\n let path = '';\n\n run(async () => {\n const url = createURL(req);\n\n // get procedure(s) path and remove the slashes around it\n path = trimSlashes(trimSlashes(url.pathname).slice(basePath.length));\n\n await nodeHTTPRequestHandler({\n ...opts,\n req,\n res,\n path,\n });\n }).catch(\n internal_exceptionHandler({\n req,\n res,\n path,\n ...opts,\n }),\n );\n };\n}\n\n/**\n * @internal\n */\nexport function createHTTPHandler<TRouter extends AnyRouter>(\n opts: CreateHTTPHandlerOptions<TRouter>,\n): http.RequestListener {\n return createHandler(opts);\n}\n\nexport function createHTTPServer<TRouter extends AnyRouter>(\n opts: CreateHTTPHandlerOptions<TRouter>,\n) {\n return http.createServer(createHTTPHandler(opts));\n}\n\n// --- http2 ---\nexport type CreateHTTP2HandlerOptions<TRouter extends AnyRouter> =\n StandaloneHandlerOptions<\n TRouter,\n http2.Http2ServerRequest,\n http2.Http2ServerResponse\n >;\n\nexport type CreateHTTP2ContextOptions = NodeHTTPCreateContextFnOptions<\n http2.Http2ServerRequest,\n http2.Http2ServerResponse\n>;\n\nexport function createHTTP2Handler(opts: CreateHTTP2HandlerOptions<AnyRouter>) {\n return createHandler(opts);\n}\n"],"mappings":";;;;;;;;;;;;;;AA8BA,MAAM,eAAe,SAAyB;CAC5C,OAAO,KAAK,WAAW,GAAG,IAAI,KAAK,MAAM,CAAC,IAAI;CAC9C,OAAO,KAAK,SAAS,GAAG,IAAI,KAAK,MAAM,GAAG,EAAE,IAAI;CAEhD,OAAO;AACT;AA0BA,SAAS,cAKP,MACyC;;CACzC,MAAM,WAAW,aAAA,iBAAY,KAAK,cAAA,QAAA,mBAAA,KAAA,IAAA,iBAAY,GAAG;CAEjD,QAAQ,KAAK,QAAQ;EACnB,IAAI,OAAO;EAEX,IAAI,YAAY;GACd,MAAM,MAAM,UAAU,GAAG;GAGzB,OAAO,YAAY,YAAY,IAAI,QAAQ,CAAC,CAAC,MAAM,SAAS,MAAM,CAAC;GAEnE,MAAM,uBAAA,eAAA,eAAA,CAAA,GACD,IAAA,GAAA,CAAA,GAAA;IACH;IACA;IACA;GACF,CAAA,CAAC;EACH,CAAC,CAAC,CAAC,MACD,0BAAA,eAAA;GACE;GACA;GACA;EACG,GAAA,IACL,CAAC,CACH;CACF;AACF;;;;AAKA,SAAgB,kBACd,MACsB;CACtB,OAAO,cAAc,IAAI;AAC3B;AAEA,SAAgB,iBACd,MACA;CACA,OAAO,KAAK,aAAa,kBAAkB,IAAI,CAAC;AAClD;AAeA,SAAgB,mBAAmB,MAA4C;CAC7E,OAAO,cAAc,IAAI;AAC3B"}