{"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// eslint-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\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   * (Do not miss including the trailing slash)\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 = opts.basePath ?? '/';\n  const sliceLength = basePath.length;\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 leading slash\n      path = url.pathname.slice(sliceLength);\n\n      await nodeHTTPRequestHandler({\n        ...(opts as any),\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":";;;;;;;;;;;;;;AAsDA,SAAS,cAKP,MACyC;;CAEzC,MAAM,gBAAA,iBADW,KAAK,cAAA,QAAA,mBAAA,KAAA,IAAA,iBAAY,IAAA,CACL;CAE7B,QAAQ,KAAK,QAAQ;EACnB,IAAI,OAAO;EAEX,IAAI,YAAY;GAId,OAHY,UAAU,GAGb,CAAC,CAAC,SAAS,MAAM,WAAW;GAErC,MAAM,uBAAA,eAAA,eAAA,CAAA,GACA,IAAA,GAAA,CAAA,GAAA;IACJ;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"}