salesforce / tough-cookie

RFC6265 Cookies and CookieJar for Node.js
BSD 3-Clause "New" or "Revised" License
964 stars 207 forks source link

Checks structure instead of instanceof for URL test #431

Closed colincasey closed 2 months ago

colincasey commented 3 months ago

This is a follow up into my testing of our latest release candidate against the jsdom test suite. The callback issues have been sorted out but I also encountered a validation issue around the URL object that is used by jsdom. When we are passed a URL object, we test it using url instanceof URL but the URL object from jsdom is not the same as the global URL object.

Relying on instanceof is always tricky in JavaScript. We should instead favor checking for the structure of the object we expect which must have the following structure:

type UrlContext = {
  hostname: string
  pathname: string
  protocol: string
}

This PR replaces the instanceof check with a validation for the above structure. This will pass for unknown url values that are instances of URL since they will have that structure and is already represented by the following test: https://github.com/salesforce/tough-cookie/blob/master/lib/__tests__/cookieJar.spec.ts#L1252-L1265