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

use url-parse package as parsing fallback #267

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions iife-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var Pretender = (function(self) {
var FakeXMLHttpRequest = appearsBrowserified
? getModuleDefault(require('fake-xml-http-request'))
: self.FakeXMLHttpRequest;
var ParsedUrl = appearsBrowserified
? getModuleDefault(require('url-parse'))
: self.ParsedUrl;

// fetch related ponyfills
var FakeFetch = appearsBrowserified
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"dependencies": {
"fake-xml-http-request": "^2.0.0",
"route-recognizer": "^0.3.3",
"url-parse": "^1.4.7",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To bundle url-parse in the output file, url-parse should lives in devDependencies.

"whatwg-fetch": "^3.0.0"
},
"files": [
Expand All @@ -65,7 +66,8 @@
"deps": [
"route-recognizer",
"fake-xml-http-request",
"whatwg-fetch"
"whatwg-fetch",
"url-parse"
],
"exports": "Pretender"
}
Expand Down
7 changes: 4 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ const fs = require('fs');
const globals = {
'whatwg-fetch': 'FakeFetch',
'fake-xml-http-request': 'FakeXMLHttpRequest',
'route-recognizer': 'RouteRecognizer'
'route-recognizer': 'RouteRecognizer',
'url-parse': 'ParsedUrl'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global in output file seems to be urlParse rather than ParsedUrl.

};

const rollupTemplate = fs.readFileSync('./iife-wrapper.js').toString();
const [ banner, footer ] = rollupTemplate.split('/*==ROLLUP_CONTENT==*/');

module.exports = {
input: 'src/index.ts',
external: Object.keys(pkg.dependencies),
// don't exclude url-parse because it does *not* provide a UMD bundle and needs to be included
external: Object.keys(pkg.dependencies).filter(x => x !== 'url-parse'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This need to be changed when updating how this package is being bundled.

output: [
{
name: 'Pretender',
Expand Down
28 changes: 14 additions & 14 deletions src/parse-url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { default as ParsedUrl } from 'url-parse';

/**
* parseURL - decompose a URL into its parts
* @param {String} url a URL
Expand All @@ -16,32 +18,30 @@
* }
*/
export default function parseURL(url: string) {
// TODO: something for when document isn't present... #yolo
var anchor = document.createElement('a');
anchor.href = url;
var parsedUrl = new ParsedUrl(url);

if (!anchor.host) {
if (!parsedUrl.host) {
// eslint-disable-next-line no-self-assign
anchor.href = anchor.href; // IE: load the host and protocol
parsedUrl.href = parsedUrl.href; // IE: load the host and protocol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the comment was for IE quirks. This should be changed when moving away from anchor tag method.

}

var pathname = anchor.pathname;
var pathname = parsedUrl.pathname;
if (pathname.charAt(0) !== '/') {
pathname = '/' + pathname; // IE: prepend leading slash
}

var host = anchor.host;
if (anchor.port === '80' || anchor.port === '443') {
host = anchor.hostname; // IE: remove default port
var host = parsedUrl.host;
if (parsedUrl.port === '80' || parsedUrl.port === '443') {
host = parsedUrl.hostname; // IE: remove default port
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my comment above. These seem to be addressing IE issue, we should have test coverage on the mentioned cases when moving to url-parse.
Although there's no cross browser setup yet, but it's still worth adding test for this.

}

return {
host: host,
protocol: anchor.protocol,
search: anchor.search,
hash: anchor.hash,
href: anchor.href,
protocol: parsedUrl.protocol,
search: parsedUrl.query,
hash: parsedUrl.hash,
href: parsedUrl.href,
pathname: pathname,
fullpath: pathname + (anchor.search || '') + (anchor.hash || '')
fullpath: pathname + (parsedUrl.query || '') + (parsedUrl.hash || '')
};
}
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3510,6 +3510,11 @@ qs@~6.3.0:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.3.2.tgz#e75bd5f6e268122a2a0e0bda630b2550c166502c"
integrity sha1-51vV9uJoEioqDgvaYwslUMFmUCw=

querystringify@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.1.1.tgz#60e5a5fd64a7f8bfa4d2ab2ed6fdf4c85bad154e"
integrity sha512-w7fLxIRCRT7U8Qu53jQnJyPkYZIaR4n5151KMfcJlO/A9397Wxb1amJvROTK6TOnp7PfoAmg/qXiNHI+08jRfA==

qunit@^2.6.1:
version "2.8.0"
resolved "https://registry.yarnpkg.com/qunit/-/qunit-2.8.0.tgz#007474727cdba323c35f9526e21c0687f8ea04b3"
Expand Down Expand Up @@ -4610,6 +4615,14 @@ urix@^0.1.0:
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=

url-parse@^1.4.7:
version "1.4.7"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.4.7.tgz#a8a83535e8c00a316e403a5db4ac1b9b853ae278"
integrity sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==
dependencies:
querystringify "^2.1.1"
requires-port "^1.0.0"

use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
Expand Down