Browse Source

url: use "empty" object for empty query strings

This makes things consistent with the way that the querystring
module creates parsed results.

PR-URL: https://github.com/nodejs/node/pull/6289
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <mic.besace@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Minwoo Jung <jmwsoft@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
process-exit-stdio-flushing
Brian White 9 years ago
committed by James M Snell
parent
commit
e9dc6306ca
  1. 10
      lib/url.js
  2. 4
      test/parallel/test-url.js

10
lib/url.js

@ -60,6 +60,12 @@ const slashedProtocol = {
}; };
const querystring = require('querystring'); const querystring = require('querystring');
// This constructor is used to store parsed query string values. Instantiating
// this is faster than explicitly calling `Object.create(null)` to get a
// "clean" empty object (tested with v8 v4.9).
function ParsedQueryString() {}
ParsedQueryString.prototype = Object.create(null);
function urlParse(url, parseQueryString, slashesDenoteHost) { function urlParse(url, parseQueryString, slashesDenoteHost) {
if (url instanceof Url) return url; if (url instanceof Url) return url;
@ -168,7 +174,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
} }
} else if (parseQueryString) { } else if (parseQueryString) {
this.search = ''; this.search = '';
this.query = {}; this.query = new ParsedQueryString();
} }
return this; return this;
} }
@ -358,7 +364,7 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
} else if (parseQueryString) { } else if (parseQueryString) {
// no query string, but parseQueryString still requested // no query string, but parseQueryString still requested
this.search = ''; this.search = '';
this.query = {}; this.query = new ParsedQueryString();
} }
var firstIdx = (questionIdx !== -1 && var firstIdx = (questionIdx !== -1 &&

4
test/parallel/test-url.js

@ -927,7 +927,7 @@ var parseTestsWithQueryString = {
path: '/example', path: '/example',
href: '/example' href: '/example'
}, },
'/example?query=value':{ '/example?query=value': {
protocol: null, protocol: null,
slashes: null, slashes: null,
auth: null, auth: null,
@ -951,6 +951,8 @@ for (const u in parseTestsWithQueryString) {
} }
} }
assert.notStrictEqual(Object.getPrototypeOf(actual.query), Object.prototype);
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
} }

Loading…
Cancel
Save