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

Upload stream fetch request fails in SvelteKit #146

Closed
rexmondo opened this issue Feb 1, 2023 · 2 comments
Closed

Upload stream fetch request fails in SvelteKit #146

rexmondo opened this issue Feb 1, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@rexmondo
Copy link
Contributor

rexmondo commented Feb 1, 2023

Bug report

Not sure if this is the ideal place to file this bug, but I decided on here because my client implementation is the one recommended here.

Describe the bug

When trying to upload a ReadableStream to a bucket using SvelteKit 1.0.0-next.526 and the storage-js client like so:

const { data, error } = await supabase.storage.from('processed').upload('file.jpg', someReadableStream)

I got this error:

TypeError: RequestInit: duplex option is required when sending a body.
    at fetch (/Users/ben/projects/svelte-surfshots/node_modules/.pnpm/undici@5.14.0/node_modules/undici/index.js:105:13)

To Reproduce

  1. Follow supabase install instructions for a new SvelteKit project here
  2. Set up auth as per auth-helpers
  3. Set up a public bucket and add upload permission policy for any authenticated user
  4. Try to upload either a node:stream Readable or a node:stream/web ReadableStream from a +server.ts file on a route of your choosing (I tried both and they threw the same error)

Expected behavior

The file should be added to the bucket

System information

  • OS: macOS 12.6
  • Browser (if applies): Chrome 109.0.5414.119
  • Version of supabase-js: 2.2.3
  • Version of Node.js: 18.13.0 (lts)

Additional context

  • Recently the whatwg spec has been shifted to require a {duplex: 'half' } or { duplex: 'full' } option to be passed to the RequestInit when performing a fetch with a ReadableStream. See here.
  • Undici started following the spec and throwing a TypeError when the option isn't present during a streaming fetch call in v5.12.0. See here
  • SvelteKit pinned Undici at v5.12.0 in a recent release (now they are up to v5.14.0)
  • After digging around in storage-js and tracking the (rather labyrinthine) load sequence from auth-helper down to the fetch call the upload eventually flushes to, I determined that implementation was indeed calling undici and had no extra mechanism to pass in more options to the RequestInit. The lines below fix the request init to only contain method, body and header
     const res = await this.fetch(`${this.url}/object/${_path}`, {
         method,
         body: body as BodyInit,
         headers,
       })
  • I've confirmed that the error goes away by hotpatching the fetch function in the client instantiation as follows:
    export const supabaseClient = createClient(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY, {
      global: {
        // TODONT!!!!
        async fetch(path, opts) {
          return global.fetch(path, { ...opts, duplex: 'half' })
        }
      }
    })
    But I suspect this will break other things.
@ShantanuNair
Copy link

@rexmondo Shall we close this, since duplex option is allowed now?

@rexmondo
Copy link
Contributor Author

Thank you for your help! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants