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

Update polyfills.js to include File #8925

Merged
merged 4 commits into from
Feb 9, 2023

Conversation

ConProgramming
Copy link
Contributor

Currently there is no global File object available. Usually this is fine, as any "file" returned by FormData still has the normal properties and methods available.

The problem became apparent when using zod-form-data, a library that relies on the global File object.

import { zfd } from "zod-form-data";

const schema = zfd.formData({
    pdf: zfd.file(),
});

Discussion here.

This is a simple fix, just adds another import from undici to include File.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@dummdidumm
Copy link
Member

Makes sense to me. Could you please do the File export from undici and provide a changeset?

@ConProgramming
Copy link
Contributor Author

ConProgramming commented Feb 7, 2023

Makes sense to me. Could you please do the File export from undici and provide a changeset?

That was my original solution, but actually this is the FormData of undici.

const { File: UndiciFile, FileLike, isFileLike } = require('./file')
const { Blob, File: NativeFile } = require('buffer')
/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile

In node environments like SvelteKit, it'll use the native node File from buffer.

I'll add the changeset though.

@dummdidumm
Copy link
Member

You have to use the import from undici. As you can see from all the CI test failures, File doesn't exist on buffer in Node16.

@ConProgramming
Copy link
Contributor Author

You have to use the import from undici. As you can see from all the CI test failures, File doesn't exist on buffer in Node16.

Hm well we'd have to do it conditionally like undici does right? Or else there'd be type mismatch between the global File and what is returned by the undici FormData.

const { File: UndiciFile, FileLike, isFileLike } = require('./file')
const { Blob, File: NativeFile } = require('buffer')
/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile

On Node16 unidici uses UndiciFile but on Node18 it usesNativeFile.

Is this possible currently, since installPolyfills is ESM?

Copy link
Contributor Author

@ConProgramming ConProgramming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for my uses. Thanks!

I guess then tree shaking doesn't matter for built in node modules?

Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

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

Successfully merging this pull request may close these issues.

3 participants