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

See if Node 18 introducing a new FormData global could be of any use #1

Open
TomasHubelbauer opened this issue Apr 21, 2022 · 3 comments

Comments

@TomasHubelbauer
Copy link
Owner

https://nodejs.org/en/blog/announcements/v18-release-announce/

Once the Workers runtime runs on Node 18, presumably CloudFlare will either switch to Node's fetch or alias it to their current implementation. Depending on which they do, this repository could become obsolete.

@akoidan
Copy link

akoidan commented Jun 8, 2023

This is weird, I swear to God I ran the code below on CF worker and it worked:

  let body = await request.formData();
  let file = await body.get('file');
  let fileData = await file.arrayBuffer();

fileData was a type of ArrayBuffer. Then I recreated my worker and I get into the issue described in this repo. Either they randomly run their workers on different node versions (which would be really weird) either it's configurable somewhere and I missed it.

Full code:

const html = `
 <FORM action="/upload_file" method="post">
    <input type="file" name="file"/>
    <INPUT type="submit" value="Send"> <INPUT type="reset">
    </P>
 </FORM>`

async function ulpoadUrlAndReturnResponse(request, env) {
  let body = await request.formData();
  let file = await body.get('file');
  console.log(typeof file)
  let fileData = await file.arrayBuffer();
  return new Response(typeof fileData, {
    headers: {
      "content-type": "text/plain",
    },
  });
}

export default {
  async fetch(request, env) {
    if (request.method === 'POST') {
      return ulpoadUrlAndReturnResponse(request, env)
    } else {
      return new Response(html, {
        headers: {
          "content-type": "text/html;charset=UTF-8",
        },
      });
    }
  },
};

@TomasHubelbauer
Copy link
Owner Author

@akoidan Do you have this flag on in both cases you describe? https://developers.cloudflare.com/workers/platform/compatibility-dates/#formdata-parsing-supports-file

@akoidan
Copy link

akoidan commented Jun 8, 2023

Thank you, I should have specified compatibilityDate, this flag is enabled by default. It's weird that w/o specified compatibilityDate, it's defaulted to some old piece of Node version. 👎

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

2 participants