Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set property duplex of #<Request> which has only a getter #7468

Closed
probablykasper opened this issue Nov 1, 2022 · 6 comments
Closed

Comments

@probablykasper
Copy link

Describe the bug

As of SvelteKit 1.0.0-next.406, I started getting this issue in server endpoints that used ky for making web reqeusts:

Cannot set property duplex of #<Request> which has only a getter

The issue stems from it setting request.duplex like this:

    const request = new Request('https://example.com')
    if (supportsRequestStreams) {
        // @ts-expect-error - Types are outdated.
        request.duplex = 'half';
    }

In SvelteKit 1.0.0-next.406, supportsRequestStreams started evaluating to true. It's defined as this:

const supportsRequestStreams = (() => {
    let duplexAccessed = false;
    let hasContentType = false;
    const supportsReadableStream = typeof globalThis.ReadableStream === 'function';
    if (supportsReadableStream) {
        hasContentType = new globalThis.Request('https://a.com', {
            body: new globalThis.ReadableStream(),
            method: 'POST',
            // @ts-expect-error - Types are outdated.
            get duplex() {
                duplexAccessed = true;
                return 'half';
            },
        }).headers.has('Content-Type');
    }
    return duplexAccessed && !hasContentType;
})();

For context, setting request.duplex seems to be needed for compatibility with Chrome 105: sindresorhus/ky@316fffe

I'm not sure entirely sure if this is an incorrect implementation in SvelteKit, or if the issue is with ky.

Ky issue: sindresorhus/ky#467

Reproduction

https://github.com/probablykasper/sveltekit-fetch-duplex-bug

Logs

No response

System Info

System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
    Memory: 820.51 MB / 32.00 GB
    Shell: 3.5.1 - /usr/local/bin/fish
  Binaries:
    Node: 16.15.0 - ~/Library/Caches/fnm_multishells/70304_1667344002694/bin/node
    Yarn: 1.22.15 - ~/Library/Caches/fnm_multishells/70304_1667344002694/bin/yarn
    npm: 8.12.1 - ~/Library/Caches/fnm_multishells/70304_1667344002694/bin/npm
  Browsers:
    Brave Browser: 97.1.34.81
    Chrome: 107.0.5304.87
    Firefox: 106.0.1
    Firefox Developer Edition: 107.0
    Safari: 15.6.1
  npmPackages:
    @sveltejs/adapter-auto: 1.0.0-next.86 => 1.0.0-next.86 
    @sveltejs/kit: 1.0.0-next.531 => 1.0.0-next.531 
    svelte: ^3.52.0 => 3.52.0 
    vite: ^3.2.2 => 3.2.2

Severity

serious, but I can work around it

Additional Information

No response

@dummdidumm
Copy link
Member

dummdidumm commented Nov 4, 2022

This is probably a result of #7412 where we had to add the duplex property to prevent undici from erroring. From looking at the referenced Ky-code I don't see how this could have an impact on this though. Maybe it's also not related to our fix but the most recent undici version whose Response implementation throws this error? In that case Ky would need to work around that somehow.

@repsac-by
Copy link
Contributor

This behavior of undici@5.12.0 is fully consistent with the specification.

readonly attribute RequestDuplex duplex;

Note that the duplex attribute is marked readonly.
This attribute can only be set by passing RequestInit.

@dummdidumm
Copy link
Member

dummdidumm commented Nov 4, 2022

Then this is on Ky's end, because in the code that was linked they modify that property after initialising the request.

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Nov 4, 2022
@vhscom
Copy link

vhscom commented Nov 11, 2022

I'm seeing OP described behavior in SvelteKit and I'm not using Ky. Kit 529 + Node Adapter 100. Occurs on POST requests only.

@repsac-by
Copy link
Contributor

@vhscom please provide a minimal reproducible repo.

@vhscom
Copy link

vhscom commented Nov 11, 2022

For those who see this issue, ensure you're passing duplex: 'half' into any fetch calls using RequestInit like:

const res = await fetch('/api/listing', {
  method: 'POST',
  // @ts-ignore-next-line - Types outdated
  duplex: 'half',
  body: JSON.stringify(theBody)
});

If helpful, pin undici using overrides in the package manifest. 5.12.0 is the Unidici version which introduces duplex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants