Skip to content

Commit

Permalink
querystring: lazy loaded
Browse files Browse the repository at this point in the history
PR-URL: #20567
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
BridgeAR authored and MylesBorins committed May 22, 2018
1 parent 0ace8f9 commit 4a92da1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const {
CHAR_LOWERCASE_A,
CHAR_LOWERCASE_Z,
} = require('internal/constants');
const querystring = require('querystring');

// Lazy loaded for startup performance.
let querystring;

const { platform } = process;
const isWindows = platform === 'win32';
Expand Down Expand Up @@ -771,8 +773,10 @@ function parseParams(qs) {
} else if (encodeCheck > 0) {
// eslint-disable-next-line no-extra-boolean-cast
if (!!isHexTable[code]) {
if (++encodeCheck === 3)
if (++encodeCheck === 3) {
querystring = require('querystring');
encoded = true;
}
} else {
encodeCheck = 0;
}
Expand Down
10 changes: 8 additions & 2 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const slashedProtocol = {
'file': true,
'file:': true
};
const querystring = require('querystring');
const {
CHAR_SPACE,
CHAR_TAB,
Expand Down Expand Up @@ -133,6 +132,9 @@ const {
CHAR_AT,
} = require('internal/constants');

// Lazy loaded for startup performance.
let querystring;

function urlParse(url, parseQueryString, slashesDenoteHost) {
if (url instanceof Url) return url;

Expand Down Expand Up @@ -233,6 +235,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
if (simplePath[2]) {
this.search = simplePath[2];
if (parseQueryString) {
if (querystring === undefined) querystring = require('querystring');
this.query = querystring.parse(this.search.slice(1));
} else {
this.query = this.search.slice(1);
Expand Down Expand Up @@ -422,6 +425,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
this.query = rest.slice(questionIdx + 1, hashIdx);
}
if (parseQueryString) {
if (querystring === undefined) querystring = require('querystring');
this.query = querystring.parse(this.query);
}
} else if (parseQueryString) {
Expand Down Expand Up @@ -584,8 +588,10 @@ Url.prototype.format = function format() {
}
}

if (this.query !== null && typeof this.query === 'object')
if (this.query !== null && typeof this.query === 'object') {
if (querystring === undefined) querystring = require('querystring');
query = querystring.stringify(this.query);
}

var search = this.search || (query && ('?' + query)) || '';

Expand Down

0 comments on commit 4a92da1

Please sign in to comment.