jimmywarting / FormData

HTML5 `FormData` polyfill for Browsers and nodejs
MIT License
360 stars 102 forks source link

`SyntaxError: Invalid regular expression` in Safari #142

Open robin-hartmann opened 2 years ago

robin-hartmann commented 2 years ago

Since version 4.0.5 or, to be more precise, since commit 6adcb7d03e2edcccf1d8c014ecff03a7a5c2547f, both formdata-to-blob.js and esm.min.js fail to load in Safari with the error SyntaxError: Invalid regular expression: invalid group specifier name. This is due to the fact that commit 6adcb7d03e2edcccf1d8c014ecff03a7a5c2547f introduced regular expressions for eol normalization, which use lookbehind (?<!).

Sadly, Safari still doesn't support lookbehind in regular expressions. And even worse, this error doesn't happen when the offending code is being executed, but instead, it already happens when the source file containing it is being parsed, which in turn prevents execution of any code in the same source file. I.e., the error happens, even if the offending code is not even being used.

robin-hartmann commented 2 years ago

I believe this could be fixed by using the regular expression from normalizeLinefeeds in FormData.js, which achieves the same result without lookbehind. I already tried it out and the tests still pass without problems.

jimmywarting commented 2 years ago

I already tried it out and the tests still pass without problems.

could you share what you have tried?


...wondering why you even use this package at all in the browser... all browser already have support for FormData already.

robin-hartmann commented 2 years ago

I already tried it out and the tests still pass without problems.

could you share what you have tried?

I replaced the offending regular expressions in formdata-to-blob.js and esm.min.js (replace(/\r(?!\n)|(?<!\r)\n/g, '\r\n')) with the one from normalizeLinefeeds in FormData.js (replace(/\r?\n|\r/g, '\r\n')).

...wondering why you even use this package at all in the browser... all browser already have support for FormData already.

I came across this problem more or less by accident and I thought I should at least open an issue, even if it isn't really relevant for my project. It might be relevant for someone else.

If you really want to know, my project is using node-fetch, which depends on this package. Because my project is a hybrid web app, which deploys the exact same code for Electron (macOS, Windows) and Cordova (Android, iOS), this package's code is not only loaded in Chrome (Android, Electron), but also in Safari. The code is only used on Electron, but as I described in the first post, the error happens even if the code isn't actually being used. For now, I've worked around it by splitting node-fetch and all of its dependencies into its own source file, which is only loaded on Electron.