kit icon indicating copy to clipboard operation
kit copied to clipboard

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

Open veinnotnice opened this issue 1 year ago • 2 comments

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:

  • The error suggests an issue with converting a symbol to string within undici.
  • The issue seems related to the getRequest function in node_modules, specifically within @sveltejs/kit.
  • Adding https without a proxy object in the vite.config.ts enables HTTP/2, which seems to be triggering an unexpected code path.

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:

  • The development server should retain HTTP/2 support and not throw type errors.

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.

```
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/@[email protected]_@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/exports/node/index.js:109:9)
    at file:///C:/REPOS/CLOUD/solarsync/frontend/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]/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

veinnotnice avatar Oct 02 '24 17:10 veinnotnice