INNER CODE UNIT · TypeScript

setupWebSocketUpgrade

im4codes/imcodes · server/src/index.ts:363

export function setupWebSocketUpgrade(server: import('node:http').Server, env: Env) {
  const wss = createServerWebSocketServer();
  // Compile trust function once — same proxy-addr library used by HTTP middleware
  const wsTrust = proxyAddr.compile(
    env.TRUSTED_PROXIES
      ? env.TRUSTED_PROXIES.split(',').map((s) => s.trim()).filter(Boolean)
      : [],
  );

  server.on('upgrade', async (req: IncomingMessage, socket, head) => {
    const url = new URL(req.url ?? '/', 'http://localhost');

    // Preview WebSocket tunnel: /api/server/:id/local-web/:previewId/*
    const previewWsMatch = url.pathname.match(/^\/api\/server\/([^/]+)\/local-web\/([^/]+)(\/.*)?$/);
    if (previewWsMatch) {
      await handlePreviewWsUpgrade(previewWsMatch[1]!, previewWsMatch[2]!, previewWsMatch[3] ?? '/', req, socket, head, url, wss, env);
      return;
    }

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…