{"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 path the router is served at, such as `/trpc/`. It's cut from the\n   * start of each request's path.\n   * @default '/'\n   * @see https://trpc.io/docs/server/adapters/standalone#custom-basePath\n   */\n  basePath?: string;\n};\n\n// --- http1 ---\n/** The options of `createHTTPServer` and `createHTTPHandler`. */\nexport type CreateHTTPHandlerOptions<TRouter extends AnyRouter> =\n  StandaloneHandlerOptions<TRouter, http.IncomingMessage, http.ServerResponse>;\n\n/**\n * What `createContext` gets in the standalone adapter: `req`, `res` and\n * the request's `info`.\n * @see https://trpc.io/docs/server/adapters/standalone#2-use-the-standalone-adapter\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 * Creates a Node.js request listener for a router, to use with your own\n * `http.createServer()`.\n * @see https://trpc.io/docs/server/adapters/standalone#adding-a-handler-to-a-custom-http-server\n * @internal\n */\nexport function createHTTPHandler<TRouter extends AnyRouter>(\n  opts: CreateHTTPHandlerOptions<TRouter>,\n): http.RequestListener {\n  return createHandler(opts);\n}\n\n/**\n * Creates a Node.js HTTP server for a router. Call `.listen()` on it.\n * @example\n * ```ts\n * createHTTPServer({ router: appRouter, createContext }).listen(3000);\n * ```\n * @see https://trpc.io/docs/server/adapters/standalone#2-use-the-standalone-adapter\n */\nexport function createHTTPServer<TRouter extends AnyRouter>(\n  opts: CreateHTTPHandlerOptions<TRouter>,\n) {\n  return http.createServer(createHTTPHandler(opts));\n}\n\n// --- http2 ---\n/** The options of `createHTTP2Handler`. */\nexport type CreateHTTP2HandlerOptions<TRouter extends AnyRouter> =\n  StandaloneHandlerOptions<\n    TRouter,\n    http2.Http2ServerRequest,\n    http2.Http2ServerResponse\n  >;\n\n/**\n * What `createContext` gets in the HTTP/2 standalone adapter.\n * @see https://trpc.io/docs/server/adapters/standalone#http2\n */\nexport type CreateHTTP2ContextOptions = NodeHTTPCreateContextFnOptions<\n  http2.Http2ServerRequest,\n  http2.Http2ServerResponse\n>;\n\n/**\n * Creates a request listener for a router, to use with Node.js's\n * `http2.createSecureServer()`.\n * @see https://trpc.io/docs/server/adapters/standalone#http2\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;AA+BA,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;;;;;;;AAQA,SAAgB,kBACd,MACsB;CACtB,OAAO,cAAc,IAAI;AAC3B;;;;;;;;;AAUA,SAAgB,iBACd,MACA;CACA,OAAO,KAAK,aAAa,kBAAkB,IAAI,CAAC;AAClD;;;;;;AAyBA,SAAgB,mBAAmB,MAA4C;CAC7E,OAAO,cAAc,IAAI;AAC3B"}