-
-
Notifications
You must be signed in to change notification settings - Fork 515
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
parseMultipartData don't work with File class in Node.js #947
Comments
I found a Maybe we should have something like this: if (!globalThis.File) {
globalThis.File = await import('fetch-blob/file.js')
} Note: It's a ESM only module. |
There is a cjs version of File class in this package: https://github.com/octet-stream/form-data A workaround by now: // TODO: use `File` from fetch-blob >= 3.x after upgrading to ESM.
import { File as FileClass } from 'formdata-node';
// for TS users:
declare global {
var File: typeof FileClass;
}
if (!globalThis.File) {
globalThis.File = FileClass;
} |
Another blocker is #929. |
We are running into the same issue. Any progress here? |
Hey, @chentsulin. Thanks for reporting this. I agree that we shouldn't rely on the That being said, I don't have the time capacity to look into this. I encourage anybody reading this (perhaps @delijah?) to debug this and see why the |
I've investigated some more here....
It looks like msw is currently converting |
In a way @chentsulin's issue is not limited to just Similar issues might occur for other body types: type BodyInit = Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string; There are three challenges
Instead of having For example in the issue description @chentsulin is already using the await axios.post('https://api-data.line.me/v2/bot/audienceGroup/upload/byFile', form, {
headers: form.getHeaders(),
}); ...and reuse them when creating the // parseWorkerRequest
const request: RestRequest = {
// parseIsomorphicRequest
const mockedRequest: MockedRequest = { This means I have written in more detail about this here: As I have written in the linked comment I don't really know what I am talking about here 😄 Usage exampleWithout headersawait axios.post('url', form); Browserrest.post("/post", async (req, res, ctx) => {
// req.body is the original body (native browser FormData). The modified body (with boundary value) returned from the service worker is ignored
// req.headers has the Content-Type header which was added by the browser (The header includes a now useless boundary value. Useless because msw is not using it to parse the modified body)
}) Noderest.post("/post", async (req, res, ctx) => {
// req.body is the original body (whatever FormData polyfill the user installed)
// req.headers is empty as Node does not add headers automatically like the browser does (?)
}) With headersawait axios.post('url', form, {headers: form.getHeaders()}); Browserrest.post("/post", async (req, res, ctx) => {
// req.body is the original body (native browser FormData). The modified body (with boundary value) returned from the service worker is ignored
// req.headers is the original headers
}) Noderest.post("/post", async (req, res, ctx) => {
// req.body is the original body (whatever FormData polyfill the user installed)
// req.headers is the original headers
}) |
Released: v2.0.0 🎉This has been released in v2.0.0! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Environment
Request handlers
Actual request
Current behavior
It throws
ReferenceError: File is not defined
in this line:msw/src/utils/internal/parseMultipartData.ts
Line 87 in 321d8c1
Expected behavior
I expect there is a File class polyfill or something that works in Node.js
Screenshots
The error catched in try/catch block:
The text was updated successfully, but these errors were encountered: