Skip to content

Commit 53f05d4

Browse files
tromeyredeyes2015
authored andcommitted
Replace URL parser with real URL
Fixes mozilla#275
1 parent ac518d2 commit 53f05d4

14 files changed

+18807
-475
lines changed

dist/source-map.debug.js

Lines changed: 1552 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/source-map.js

Lines changed: 1551 additions & 44 deletions
Large diffs are not rendered by default.

dist/source-map.min.js

Lines changed: 4740 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/source-map.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/test_api.js

Lines changed: 1552 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/test_array_set.js

Lines changed: 1538 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/test_dog_fooding.js

Lines changed: 1557 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/test_source_map_consumer.js

Lines changed: 1559 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/test_source_map_generator.js

Lines changed: 1554 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/test_source_node.js

Lines changed: 1560 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/test_util.js

Lines changed: 1560 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/source-map-consumer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,12 +731,12 @@ BasicSourceMapConsumer.prototype.sourceContentFor =
731731
// behave like it would if they were running a local HTTP server. See
732732
// https://bugzilla.mozilla.org/show_bug.cgi?id=885597.
733733
var fileUriAbsPath = relativeSource.replace(/^file:\/\//, "");
734-
if (url.scheme == "file"
734+
if (url.protocol == "file:"
735735
&& this._sources.has(fileUriAbsPath)) {
736736
return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)]
737737
}
738738

739-
if ((!url.path || url.path == "/")
739+
if ((!url.pathname || url.pathname == "/")
740740
&& this._sources.has("/" + relativeSource)) {
741741
return this.sourcesContent[this._sources.indexOf("/" + relativeSource)];
742742
}

lib/util.js

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* http://opensource.org/licenses/BSD-3-Clause
66
*/
77

8+
89
/**
910
* This is a helper function for getting values from parameter/options
1011
* objects.
@@ -26,21 +27,15 @@ function getArg(aArgs, aName, aDefaultValue) {
2627
}
2728
exports.getArg = getArg;
2829

29-
var urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.-]*)(?::(\d+))?(.*)$/;
3030
var dataUrlRegexp = /^data:.+\,.+$/;
31+
var URLConstructor = (typeof URL == 'function') ? URL : require('url').URL
3132

3233
function urlParse(aUrl) {
33-
var match = aUrl.match(urlRegexp);
34-
if (!match) {
34+
try {
35+
return new URLConstructor(aUrl);
36+
} catch (e) {
3537
return null;
3638
}
37-
return {
38-
scheme: match[1],
39-
auth: match[2],
40-
host: match[3],
41-
port: match[4],
42-
path: match[5]
43-
};
4439
}
4540
exports.urlParse = urlParse;
4641

@@ -66,6 +61,43 @@ function urlGenerate(aParsedUrl) {
6661
}
6762
exports.urlGenerate = urlGenerate;
6863

64+
const MAX_CACHED_INPUTS = 32;
65+
66+
/**
67+
* Takes some function `f(input) -> result` and returns a memoized version of
68+
* `f`.
69+
*
70+
* We keep at most `MAX_CACHED_INPUTS` memoized results of `f` alive. The
71+
* memoization is a dumb-simple, linear least-recently-used cache.
72+
*/
73+
function lruMemoize(f) {
74+
const cache = [];
75+
76+
return function (input) {
77+
for (var i = 0; i < cache.length; i++) {
78+
if (cache[i].input === input) {
79+
var temp = cache[0];
80+
cache[0] = cache[i];
81+
cache[i] = temp;
82+
return cache[0].result;
83+
}
84+
}
85+
86+
var result = f(input);
87+
88+
cache.unshift({
89+
input,
90+
result,
91+
});
92+
93+
if (cache.length > MAX_CACHED_INPUTS) {
94+
cache.pop();
95+
}
96+
97+
return result;
98+
};
99+
}
100+
69101
/**
70102
* Normalizes a path, or the path portion of a URL:
71103
*
@@ -81,10 +113,10 @@ function normalize(aPath) {
81113
var path = aPath;
82114
var url = urlParse(aPath);
83115
if (url) {
84-
if (!url.path) {
116+
if (!url.pathname) {
85117
return aPath;
86118
}
87-
path = url.path;
119+
path = url.pathname;
88120
}
89121
var isAbsolute = exports.isAbsolute(path);
90122

@@ -115,8 +147,8 @@ function normalize(aPath) {
115147
}
116148

117149
if (url) {
118-
url.path = path;
119-
return urlGenerate(url);
150+
url.pathname = path;
151+
return url.toString();
120152
}
121153
return path;
122154
}
@@ -148,41 +180,41 @@ function join(aRoot, aPath) {
148180
var aPathUrl = urlParse(aPath);
149181
var aRootUrl = urlParse(aRoot);
150182
if (aRootUrl) {
151-
aRoot = aRootUrl.path || '/';
183+
aRoot = aRootUrl.pathname || '/';
152184
}
153185

154186
// `join(foo, '//www.example.org')`
155-
if (aPathUrl && !aPathUrl.scheme) {
187+
if (aPathUrl && !aPathUrl.protocol) {
156188
if (aRootUrl) {
157-
aPathUrl.scheme = aRootUrl.scheme;
189+
aPathUrl.protocol = aRootUrl.protocol;
158190
}
159-
return urlGenerate(aPathUrl);
191+
return aPathUrl.toString();
160192
}
161193

162194
if (aPathUrl || aPath.match(dataUrlRegexp)) {
163195
return aPath;
164196
}
165197

166198
// `join('http://', 'www.example.com')`
167-
if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {
199+
if (aRootUrl && !aRootUrl.host && !aRootUrl.pathname) {
168200
aRootUrl.host = aPath;
169-
return urlGenerate(aRootUrl);
201+
return aRootUrl.toString();
170202
}
171203

172204
var joined = aPath.charAt(0) === '/'
173205
? aPath
174206
: normalize(aRoot.replace(/\/+$/, '') + '/' + aPath);
175207

176208
if (aRootUrl) {
177-
aRootUrl.path = joined;
178-
return urlGenerate(aRootUrl);
209+
aRootUrl.pathname = joined;
210+
return aRootUrl.toString();
179211
}
180212
return joined;
181213
}
182214
exports.join = join;
183215

184216
exports.isAbsolute = function (aPath) {
185-
return aPath.charAt(0) === '/' || urlRegexp.test(aPath);
217+
return aPath.charAt(0) === '/' || !!urlParse(aPath);
186218
};
187219

188220
/**
@@ -461,8 +493,6 @@ function computeSourceURL(sourceRoot, sourceURL, sourceMapURL) {
461493
// can implement the source URL resolution algorithm as outlined in
462494
// the spec. This block is basically the equivalent of:
463495
// new URL(sourceURL, sourceMapURL).toString()
464-
// ... except it avoids using URL, which wasn't available in the
465-
// older releases of node still supported by this library.
466496
//
467497
// The spec says:
468498
// If the sources are not absolute URLs after prepending of the
@@ -473,14 +503,14 @@ function computeSourceURL(sourceRoot, sourceURL, sourceMapURL) {
473503
if (!parsed) {
474504
throw new Error("sourceMapURL could not be parsed");
475505
}
476-
if (parsed.path) {
506+
if (parsed.pathname) {
477507
// Strip the last path component, but keep the "/".
478-
var index = parsed.path.lastIndexOf('/');
508+
var index = parsed.pathname.lastIndexOf('/');
479509
if (index >= 0) {
480-
parsed.path = parsed.path.substring(0, index + 1);
510+
parsed.pathname = parsed.pathname.substring(0, index + 1);
481511
}
482512
}
483-
sourceURL = join(urlGenerate(parsed), sourceURL);
513+
sourceURL = join(parsed.toString(), sourceURL);
484514
}
485515

486516
return normalize(sourceURL);

test/test-util.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@
88
var libUtil = require('../lib/util');
99

1010
exports['test urls'] = function (assert) {
11-
var assertUrl = function (url) {
12-
assert.equal(url, libUtil.urlGenerate(libUtil.urlParse(url)));
11+
var assertUrl = function (url, expect) {
12+
expect = expect || url;
13+
assert.equal(expect, libUtil.urlParse(url).toString());
1314
};
14-
assertUrl('http://');
15-
assertUrl('http://www.example.com');
16-
assertUrl('http://user:[email protected]');
17-
assertUrl('http://www.example.com:80');
15+
assertUrl('http://www.example.com', 'http://www.example.com/');
16+
assertUrl('http://user:[email protected]', 'http://user:[email protected]/');
17+
assertUrl('http://www.example.com:80', 'http://www.example.com/');
1818
assertUrl('http://www.example.com/');
1919
assertUrl('http://www.example.com/foo/bar');
2020
assertUrl('http://www.example.com/foo/bar/');
21-
assertUrl('http://user:[email protected]:80/foo/bar/');
21+
assertUrl('http://user:[email protected]:80/foo/bar/',
22+
'http://user:[email protected]/foo/bar/');
23+
24+
// From https://bugzilla.mozilla.org/show_bug.cgi?id=1451274
25+
assertUrl('data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9Vc2Vycy9rdWQvUHJvamVjdHMvX2NvbnRleHRlL2xvaXMtd2ViYXBwL3NyYy9zdHlsZXMvc2VsZWN0aW9uLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLHVDQUF1QztFQUN2QyxlQUFlO0NBQ2hCOztBQUVEO0VBQ0UsdUNBQXVDO0VBQ3ZDLGVBQWU7Q0FDaEIiLCJmaWxlIjoic2VsZWN0aW9uLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIjo6LW1vei1zZWxlY3Rpb24ge1xuICBiYWNrZ3JvdW5kLWNvbG9yOiB2YXIoLS1jb2xvci0tYWN0aW9uKTtcbiAgY29sb3I6ICNmZmZmZmY7XG59XG5cbjo6c2VsZWN0aW9uIHtcbiAgYmFja2dyb3VuZC1jb2xvcjogdmFyKC0tY29sb3ItLWFjdGlvbik7XG4gIGNvbG9yOiAjZmZmZmZmO1xufVxuIl0sInNvdXJjZVJvb3QiOiIifQ==');
2226

23-
assertUrl('//');
24-
assertUrl('//www.example.com');
2527
assertUrl('file:///www.example.com');
2628

2729
assert.equal(libUtil.urlParse(''), null);
@@ -31,19 +33,19 @@ exports['test urls'] = function (assert) {
3133
assert.equal(libUtil.urlParse('a/b'), null);
3234
assert.equal(libUtil.urlParse('a//b'), null);
3335
assert.equal(libUtil.urlParse('/a'), null);
34-
assert.equal(libUtil.urlParse('data:foo,bar'), null);
36+
assertUrl('data:foo,bar');
3537

3638
var parsed = libUtil.urlParse('http://x-y.com/bar');
37-
assert.equal(parsed.scheme, 'http');
39+
assert.equal(parsed.protocol, 'http:');
3840
assert.equal(parsed.host, 'x-y.com');
39-
assert.equal(parsed.path, '/bar');
41+
assert.equal(parsed.pathname, '/bar');
4042

4143
var webpackURL = 'webpack:///webpack/bootstrap 67e184f9679733298d44'
4244
parsed = libUtil.urlParse(webpackURL);
43-
assert.equal(parsed.scheme, 'webpack');
45+
assert.equal(parsed.protocol, 'webpack:');
4446
assert.equal(parsed.host, '');
45-
assert.equal(parsed.path, '/webpack/bootstrap 67e184f9679733298d44');
46-
assert.equal(webpackURL, libUtil.urlGenerate(parsed));
47+
assert.equal(parsed.pathname, '/webpack/bootstrap%2067e184f9679733298d44');
48+
assert.equal(webpackURL, parsed.toString().replace(/%20/, " "));
4749
};
4850

4951
exports['test normalize()'] = function (assert) {
@@ -73,12 +75,12 @@ exports['test normalize()'] = function (assert) {
7375
assert.equal(libUtil.normalize('a/././.'), 'a');
7476

7577
assert.equal(libUtil.normalize('/a/b//c////d/////'), '/a/b/c/d/');
76-
assert.equal(libUtil.normalize('///a/b//c////d/////'), '///a/b/c/d/');
78+
assert.equal(libUtil.normalize('///a/b//c////d/////'), '/a/b/c/d/');
7779
assert.equal(libUtil.normalize('a/b//c////d'), 'a/b/c/d');
7880

7981
assert.equal(libUtil.normalize('.///.././../a/b//./..'), '../../a')
8082

81-
assert.equal(libUtil.normalize('http://www.example.com'), 'http://www.example.com');
83+
assert.equal(libUtil.normalize('http://www.example.com'), 'http://www.example.com/');
8284
assert.equal(libUtil.normalize('http://www.example.com/'), 'http://www.example.com/');
8385
assert.equal(libUtil.normalize('http://www.example.com/./..//a/b/c/.././d//'), 'http://www.example.com/a/b/d/');
8486
};
@@ -167,8 +169,8 @@ exports['test join()'] = function (assert) {
167169
assert.equal(libUtil.join('http://foo.org/', '.'), 'http://foo.org/');
168170
assert.equal(libUtil.join('http://foo.org//', ''), 'http://foo.org/');
169171
assert.equal(libUtil.join('http://foo.org//', '.'), 'http://foo.org/');
170-
assert.equal(libUtil.join('//www.example.com', ''), '//www.example.com/');
171-
assert.equal(libUtil.join('//www.example.com', '.'), '//www.example.com/');
172+
assert.equal(libUtil.join('//www.example.com', ''), '/www.example.com');
173+
assert.equal(libUtil.join('//www.example.com', '.'), '/www.example.com');
172174

173175

174176
assert.equal(libUtil.join('http://foo.org/a', 'b'), 'http://foo.org/a/b');
@@ -177,7 +179,6 @@ exports['test join()'] = function (assert) {
177179
assert.equal(libUtil.join('http://foo.org/a', 'b/'), 'http://foo.org/a/b/');
178180
assert.equal(libUtil.join('http://foo.org/a', 'b//'), 'http://foo.org/a/b/');
179181
assert.equal(libUtil.join('http://foo.org/a/', '/b'), 'http://foo.org/b');
180-
assert.equal(libUtil.join('http://foo.org/a//', '//b'), 'http://b');
181182

182183
assert.equal(libUtil.join('http://foo.org/a', '..'), 'http://foo.org/');
183184
assert.equal(libUtil.join('http://foo.org/a', '../b'), 'http://foo.org/b');
@@ -199,11 +200,10 @@ exports['test join()'] = function (assert) {
199200
assert.equal(libUtil.join('http://foo.org//', '/a'), 'http://foo.org/a');
200201

201202

202-
assert.equal(libUtil.join('http://', 'www.example.com'), 'http://www.example.com');
203+
assert.equal(libUtil.join('http://', 'www.example.com'), 'http://www.example.com/');
203204
assert.equal(libUtil.join('file:///', 'www.example.com'), 'file:///www.example.com');
204205
assert.equal(libUtil.join('http://', 'ftp://example.com'), 'ftp://example.com');
205206

206-
assert.equal(libUtil.join('http://www.example.com', '//foo.org/bar'), 'http://foo.org/bar');
207207
assert.equal(libUtil.join('//www.example.com', '//foo.org/bar'), '//foo.org/bar');
208208
};
209209

0 commit comments

Comments
 (0)