From d0bb9b12051fa3540387a4c23d1f004b9d9a5a01 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 6 May 2018 23:08:42 +0200 Subject: [PATCH] querystring: lazy loaded PR-URL: https://github.com/nodejs/node/pull/20567 Reviewed-By: Gus Caplan Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/internal/url.js | 8 ++++++-- lib/url.js | 10 ++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 7a67fe2f15e..ffd8f10edae 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -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'; @@ -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; } diff --git a/lib/url.js b/lib/url.js index e4326e80b5d..c517ba7ab3d 100644 --- a/lib/url.js +++ b/lib/url.js @@ -94,7 +94,6 @@ const slashedProtocol = { 'file': true, 'file:': true }; -const querystring = require('querystring'); const { CHAR_SPACE, CHAR_TAB, @@ -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; @@ -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); @@ -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) { @@ -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)) || '';