Skip to content

Pass in empty string to URL constructor if no opts.uri defined #190

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chriswiggins
Copy link

During some routine package updates to our React Native application (which uses m3u8-parser internally - thanks for the package!), @timardex found that the minor bump broke a part of our app. We found that the bundled URL implementation behaves differently to browsers when it receives a URL with an undefined first argument.

The URL spec says that whatever value is parsed in should be stringified, but undefined can't be stringified. Browsers / node must just not hit a code path where this is a problem.

However in our bundled react-native, the URL constructor looks like this: (look at the // !!!!!PROBLEM HERE!!! line)

var URL = /*#__PURE__*/function () {
  // $FlowFixMe[missing-local-annot]
  function URL(url, base) {
    _classCallCheck(this, URL);
    this._searchParamsInstance = null;
    var baseUrl = null;
    if (!base || validateBaseUrl(url)) {
      this._url = url;
      if (!this._url.endsWith('/')) {
        this._url += '/';
      }
    } else {
      if (typeof base === 'string') {
        baseUrl = base;
        if (!validateBaseUrl(baseUrl)) {
          throw new TypeError(`Invalid base URL: ${baseUrl}`);
        }
      } else {
        baseUrl = base.toString();
      }
      if (baseUrl.endsWith('/')) {
        baseUrl = baseUrl.slice(0, baseUrl.length - 1);
      }
      // !!!!!PROBLEM HERE!!!!
      if (!url.startsWith('/')) {
        url = `/${url}`;
      }
      if (baseUrl.endsWith(url)) {
        url = '';
      }
      this._url = `${baseUrl}${url}`;
    }
  },
 // Snipped....
}

Of course, you can't call .startsWith on undefined, so this small fix just passes an empty string into the URL constructor if opts.uri is undefined

Thanks again for the package, we've worked around it by pinning ourselves to 7.1.0 until this is patched 😄

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.

2 participants