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

URIError: Malformed encodeURI input #41

Closed
bradzickafoose opened this issue Jan 4, 2022 · 6 comments
Closed

URIError: Malformed encodeURI input #41

bradzickafoose opened this issue Jan 4, 2022 · 6 comments

Comments

@bradzickafoose
Copy link

bradzickafoose commented Jan 4, 2022

I'm seeing a "URIError: Malformed encodeURI input" error at encodeURIComponent when emoji are passed in as the value to encode(value).

It looks like the error is coming from line 7, .replace(/[^ !'()~*]/gu, encodeURIComponent), when encode(value) is called within nest on line 34, f = encode(name) + '=' + encode(value);.

The error doesn't occur on all emoji.

Here are a few emoji that have triggered the error in React Native on iOS and Android: 😎, 😅, 😍, 👀

@iambumblehead
Copy link
Owner

@bradzickafoose would you check and see if your toolchain is preserving the 'u' in the '/gu' part of the regular expression?

this runs for me

['😎', '😅', '😍', '👀'].map(value => String(value)
    .replace(/[^ !'()~*]/gu, encodeURIComponent)
    .replace(/ /g, '+')
    .replace(/[!'()~*]/g, ch =>
      `%${ch.charCodeAt().toString(16).slice(-2).toUpperCase()}`))

@iambumblehead
Copy link
Owner

@bradzickafoose if you are using babel, and if babel is removing the unicode 'u' from the regular expression, you might try using this plugin https://babeljs.io/docs/en/babel-plugin-proposal-unicode-property-regex

@iambumblehead
Copy link
Owner

iambumblehead commented Jan 6, 2022

I'm closing this because I can't reproduce it and there is no reply here. Feel free to re-open or send a message if you feel there is an issue.

@bradzickafoose
Copy link
Author

Thank you for your help.

The only way I could get it to not throw a URIError was to replace the encode logic with something like, const encode = (value) => encodeURIComponent(value);

I tried the solution you suggested but that didn't work, in this case.

@iambumblehead
Copy link
Owner

@bradzickafoose I'm interested to update this package if there is an issue with iOS and Android environments.

Would you try this 'encode' variant which does not use the unicode regex flag? do react native generated sources using this regexp run correctly in iOS and Android?

  var encode = function encode(value) {
    return String(value).replace(/(?:[\0-\x1F"-&\+-\}\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g, encodeURIComponent).replace(/ /g, '+').replace(/[!'()~*]/g, function (ch) {
      return "%".concat(ch.charCodeAt().toString(16).slice(-2).toUpperCase());
    });
  };

@iambumblehead
Copy link
Owner

perhaps this issue is related facebook/react-native#29807

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