| 738 | * Version: /version |
| 739 | */ |
| 740 | export async function createOrpcServer({ |
| 741 | host = "127.0.0.1", |
| 742 | port = 0, |
| 743 | authToken, |
| 744 | context, |
| 745 | serveStatic = false, |
| 746 | allowHttpOrigin = false, |
| 747 | // Default for non-bundled mode: from dist/node/orpc/, go up 2 levels to dist/. |
| 748 | // In bundled mode (dist/runtime/), serverService computes the static dir. |
| 749 | staticDir = path.join(__dirname, "../.."), |
| 750 | onOrpcError = (error, options) => { |
| 751 | // Auth failures are expected in browser mode while the user enters the token. |
| 752 | // Avoid spamming error logs with stack traces on every unauthenticated request. |
| 753 | if (error instanceof ORPCError && error.code === "UNAUTHORIZED") { |
| 754 | log.debug("ORPC unauthorized request"); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | const formatted = formatOrpcError(error, options); |
| 759 | log.error(formatted.message); |
| 760 | |
| 761 | if (log.isDebugMode()) { |
| 762 | const suffix = Math.random().toString(16).slice(2); |
| 763 | log.debug_obj(`orpc/${Date.now()}_${suffix}.json`, formatted.debugDump); |
| 764 | } |
| 765 | }, |
| 766 | router: existingRouter, |
| 767 | desktopBridgeServer = context.desktopBridgeServer, |
| 768 | browserBridgeServer = context.browserBridgeServer, |
| 769 | }: OrpcServerOptions): Promise<OrpcServer> { |
| 770 | // Express app setup |
| 771 | const app = express(); |
| 772 | app.use((req, res, next) => { |
| 773 | const requestPath = splitRequestUrlPathAndQuery(req.url); |
| 774 | if (requestPath && isValidUrlPathname(requestPath.pathname)) { |
| 775 | const { basePath, routePathname } = stripAppProxyBasePath(requestPath.pathname); |
| 776 | if (basePath) { |
| 777 | setResponsePublicBasePath(res, basePath); |
| 778 | req.url = `${routePathname}${requestPath.querySuffix}`; |
| 779 | next(); |
| 780 | return; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | const publicBasePath = getRequestPublicBasePath(req); |
| 785 | if (publicBasePath !== "/") { |
| 786 | setResponsePublicBasePath(res, publicBasePath); |
| 787 | } |
| 788 | |
| 789 | next(); |
| 790 | }); |
| 791 | |
| 792 | app.use((req, res, next) => { |
| 793 | if (!shouldEnforceOriginValidation(req)) { |
| 794 | next(); |
| 795 | return; |
| 796 | } |
| 797 | |