sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.52k stars 1.9k forks source link

TypeError in browser when adding empty proxy attribute to vite.config.ts disables HTTP/2 support #12745

Open veinnotnice opened 1 day ago

veinnotnice commented 1 day ago

Describe the bug

Issue Description:

It seems that adding the proxy attribute to your vite.config.ts file, even if left empty, causes Vite to disable HTTP/2 support for your development server. This is problematic because your project relies on HTTP/2 for certain functionalities. Even though you don't actually need a proxy, you're forced to include it to get the development server to start, but this inclusion then breaks a key part of your project.

Analysis:

Temporary Fix:

I modified the getRequest function within node_modules as follows:

export async function getRequest({ request, base, bodySizeLimit }) {
    const filteredHeaders = {};
    const isHttp2 = Object.keys(request.headers).some(key => key.startsWith(':'));
    for (const [key, value] of Object.entries(request.headers)) {
        if (typeof value === 'string') {
            // Remove pseudo-headers if it's an HTTP/2 request
            if (isHttp2 && key.startsWith(':')) {
                continue;
            }
            filteredHeaders[key] = value;
        }
    }
    return new Request(base + request.url, {
        // @ts-expect-error
        duplex: 'half',
        method: request.method,
        headers: filteredHeaders,
        body:
            request.method === 'GET' || request.method === 'HEAD'
                ? undefined
                : get_raw_body(request, bodySizeLimit)
    });
}

Expected Behavior:

Thank you for your help and consideration!

Reproduction

  1. Use @sveltejs/kit with Vite in vite.config.ts:

    import { sveltekit } from '@sveltejs/kit/vite';
    import { defineConfig } from 'vite';
    
    import fs from "fs"
    import dotenv from "dotenv"
    
    dotenv.config()
    
    export default defineConfig(({ mode }) => ({
        plugins: [
            sveltekit(),
        ],
        server: {
            https: {
                cert: fs.readFileSync(process.env.FRONTEND_SSL_CERT_PATH as string, "utf-8"),
                key: fs.readFileSync(process.env.FRONTEND_SSL_KEY_PATH as string, "utf-8")
            },
           // proxy: {} // Adding this disables HTTP/2 support, but removing it causes an error.
        }
    }));
  2. Run the project and open it in a browser.

  3. TypeError: Could not convert argument of type symbol to string.
        at webidl.converters.DOMString (node:internal/deps/undici/undici:1977:15)
        at webidl.converters.ByteString (node:internal/deps/undici/undici:1982:35)
        at Object.record<ByteString, ByteString> (node:internal/deps/undici/undici:1894:30)
        at webidl.converters.HeadersInit (node:internal/deps/undici/undici:3424:67)
        at Object.RequestInit (node:internal/deps/undici/undici:1951:21)
        at new Request (node:internal/deps/undici/undici:4835:34)
        at getRequest (file:///C:/REPOS/CLOUD/solarsync/frontend/node_modules/.pnpm/@sveltejs+kit@2.5.20_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.5/node_modules/@sveltejs/kit/src/exports/node/index.js:109:9)
        at file:///C:/REPOS/CLOUD/solarsync/frontend/node_modules/.pnpm/@sveltejs+kit@2.5.20_@sveltejs+vite-plugin-svelte@3.1.1_svelte@4.2.18_vite@5.3.5/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:497:27

Logs

No response

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 2.10 GB / 15.63 GB
  Binaries:
    Node: 20.14.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.22 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.5 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @sveltejs/adapter-node: ^5.2.2 => 5.2.2
    @sveltejs/adapter-vercel: ^5.4.4 => 5.4.4
    @sveltejs/kit: ^2.0.0 => 2.5.20
    @sveltejs/vite-plugin-svelte: ^3.1.1 => 3.1.1
    svelte: ^4.2.7 => 4.2.18
    vite: ^5.0.3 => 5.3.5

Severity

serious, but I can work around it

Additional Information

No response

LarryEitel commented 1 day ago

I confirmed this workaround.

I tried without success to monkeypatch using something like:

   import { getRequest as originalGetRequest } from '@sveltejs/kit/node';

   // Overwrite the original function
   export async function getRequest({ request, base, bodySizeLimit }) {
       const filteredHeaders = {};
       const isHttp2 = Object.keys(request.headers).some(key => key.startsWith(':'));
       for (const [key, value] of Object.entries(request.headers)) {
           if (typeof value === 'string') {
               // Remove pseudo-headers if it's an HTTP/2 request
               if (isHttp2 && key.startsWith(':')) {
                   continue;
               }
               filteredHeaders[key] = value;
           }
       }
       return new Request(base + request.url, {
           // @ts-expect-error
           duplex: 'half',
           method: request.method,
           headers: filteredHeaders,
           body:
               request.method === 'GET' || request.method === 'HEAD'
                   ? undefined
                   : get_raw_body(request, bodySizeLimit)
       });
   }

   // Patch the original module
   import * as originalModule from '@sveltejs/kit/node';
   originalModule.getRequest = getRequest;

Then in +layout.server.ts

   import '../../src/patchRequest.js';

Without this, I cannot run node v20.x.