Skip to content

Use standard-conforming URL #360

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

Closed
wants to merge 4 commits 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
2 changes: 1 addition & 1 deletion dist/source-map.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,12 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
// behave like it would if they were running a local HTTP server. See
// https://bugzilla.mozilla.org/show_bug.cgi?id=885597.
const fileUriAbsPath = relativeSource.replace(/^file:\/\//, "");
if (url.scheme == "file"
if (url.protocol == "file:"
&& this._sources.has(fileUriAbsPath)) {
return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)];
}

if ((!url.path || url.path == "/")
if ((!url.pathname || url.pathname == "/")
&& this._sources.has("/" + relativeSource)) {
return this.sourcesContent[this._sources.indexOf("/" + relativeSource)];
}
Expand Down
81 changes: 21 additions & 60 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,18 @@ function getArg(aArgs, aName, aDefaultValue) {
}
exports.getArg = getArg;

const urlRegexp = /^(?:([\w+\-.]+):)?\/\/(?:(\w+:\w+)@)?([\w.-]*)(?::(\d+))?(.*)$/;
const dataUrlRegexp = /^data:.+\,.+$/;
const URLConstructor = (typeof URL == "function") ? URL : require("url").URL;

function urlParse(aUrl) {
const match = aUrl.match(urlRegexp);
if (!match) {
try {
return new URLConstructor(aUrl);
} catch (e) {
return null;
}
return {
scheme: match[1],
auth: match[2],
host: match[3],
port: match[4],
path: match[5]
};
}
exports.urlParse = urlParse;

function urlGenerate(aParsedUrl) {
let url = "";
if (aParsedUrl.scheme) {
url += aParsedUrl.scheme + ":";
}
url += "//";
if (aParsedUrl.auth) {
url += aParsedUrl.auth + "@";
}
if (aParsedUrl.host) {
url += aParsedUrl.host;
}
if (aParsedUrl.port) {
url += ":" + aParsedUrl.port;
}
if (aParsedUrl.path) {
url += aParsedUrl.path;
}
return url;
}
exports.urlGenerate = urlGenerate;

const MAX_CACHED_INPUTS = 32;

/**
Expand Down Expand Up @@ -118,10 +90,10 @@ const normalize = lruMemoize(function normalize(aPath) {
let path = aPath;
const url = urlParse(aPath);
if (url) {
if (!url.path) {
if (!url.pathname) {
return aPath;
}
path = url.path;
path = url.pathname;
}
const isAbsolute = exports.isAbsolute(path);

Expand Down Expand Up @@ -171,8 +143,8 @@ const normalize = lruMemoize(function normalize(aPath) {
}

if (url) {
url.path = path;
return urlGenerate(url);
url.pathname = path;
return url.toString();
}
return path;
});
Expand Down Expand Up @@ -204,41 +176,41 @@ function join(aRoot, aPath) {
const aPathUrl = urlParse(aPath);
const aRootUrl = urlParse(aRoot);
if (aRootUrl) {
aRoot = aRootUrl.path || "/";
aRoot = aRootUrl.pathname || "/";
}

// `join(foo, '//www.example.org')`
if (aPathUrl && !aPathUrl.scheme) {
if (aPathUrl && !aPathUrl.protocol) {
if (aRootUrl) {
aPathUrl.scheme = aRootUrl.scheme;
aPathUrl.protocol = aRootUrl.protocol;
}
return urlGenerate(aPathUrl);
return aPathUrl.toString();
}

if (aPathUrl || aPath.match(dataUrlRegexp)) {
return aPath;
}

// `join('http://', 'www.example.com')`
if (aRootUrl && !aRootUrl.host && !aRootUrl.path) {
if (aRootUrl && !aRootUrl.host && !aRootUrl.pathname) {
aRootUrl.host = aPath;
return urlGenerate(aRootUrl);
return aRootUrl.toString();
}

const joined = aPath.charAt(0) === "/"
? aPath
: normalize(aRoot.replace(/\/+$/, "") + "/" + aPath);

if (aRootUrl) {
aRootUrl.path = joined;
return urlGenerate(aRootUrl);
aRootUrl.pathname = joined;
return aRootUrl.toString();
}
return joined;
}
exports.join = join;

exports.isAbsolute = function(aPath) {
return aPath.charAt(0) === "/" || urlRegexp.test(aPath);
return aPath.charAt(0) === "/" || !!urlParse(aPath);
};

/**
Expand Down Expand Up @@ -517,28 +489,17 @@ function computeSourceURL(sourceRoot, sourceURL, sourceMapURL) {
// this code block is conditional. However, it's preferable to pass
// the source map URL to SourceMapConsumer, so that this function
// can implement the source URL resolution algorithm as outlined in
// the spec. This block is basically the equivalent of:
// new URL(sourceURL, sourceMapURL).toString()
// ... except it avoids using URL, which wasn't available in the
// older releases of node still supported by this library.
// the spec.
//
// The spec says:
// If the sources are not absolute URLs after prepending of the
// “sourceRoot”, the sources are resolved relative to the
// SourceMap (like resolving script src in a html document).
if (sourceMapURL) {
const parsed = urlParse(sourceMapURL);
if (!parsed) {
throw new Error("sourceMapURL could not be parsed");
}
if (parsed.path) {
// Strip the last path component, but keep the "/".
const index = parsed.path.lastIndexOf("/");
if (index >= 0) {
parsed.path = parsed.path.substring(0, index + 1);
}
try {
sourceURL = (new URLConstructor(sourceURL, sourceMapURL)).toString();
} catch (e) {
}
sourceURL = join(urlGenerate(parsed), sourceURL);
}

return normalize(sourceURL);
Expand Down
20 changes: 18 additions & 2 deletions test/test-source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ exports["test webpack URL resolution"] = async function(assert) {
const consumer = await new SourceMapConsumer(map);

assert.equal(consumer.sources.length, 1);
assert.equal(consumer.sources[0], "webpack:///webpack/bootstrap 67e184f9679733298d44");
assert.equal(consumer.sources[0], "webpack:///webpack/bootstrap%2067e184f9679733298d44");

consumer.destroy();
};
Expand All @@ -1312,7 +1312,7 @@ exports["test webpack URL resolution with sourceMapURL"] = async function(assert
const consumer = await new SourceMapConsumer(map, "http://www.example.com/q.js.map");

assert.equal(consumer.sources.length, 1);
assert.equal(consumer.sources[0], "webpack:///webpack/bootstrap 67e184f9679733298d44");
assert.equal(consumer.sources[0], "webpack:///webpack/bootstrap%2067e184f9679733298d44");

consumer.destroy();
};
Expand Down Expand Up @@ -1368,6 +1368,22 @@ exports["test absolute sourceURL resolution with sourceMapURL"] = async function
consumer.destroy();
};

exports["test sourceURL resolution with null sourceRoot and blob sourceMapURL"] = async function(assert) {
const map = {
version: 3,
sources: ["something.js"],
names: [],
mappings: "CAAS",
file: "static/js/manifest.b7cf97680f7a50fa150f.js",
sourceRoot: null,
};
const consumer = await new SourceMapConsumer(map, "blob:http://exmaple.com/12345-12345-12345");

assert.equal(consumer.sources.length, 1);
assert.equal(consumer.sources[0], "something.js");

consumer.destroy();
};
exports["test line numbers > 2**32"] = async function(assert) {
const map = await new SourceMapConsumer({
version: 3,
Expand Down
44 changes: 22 additions & 22 deletions test/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
const libUtil = require("../lib/util");

exports["test urls"] = function(assert) {
const assertUrl = function(url) {
assert.equal(url, libUtil.urlGenerate(libUtil.urlParse(url)));
const assertUrl = function(url, expect) {
expect = expect || url;
assert.equal(expect, libUtil.urlParse(url).toString());
};
assertUrl("http://");
assertUrl("http://www.example.com");
assertUrl("http://user:pass@www.example.com");
assertUrl("http://www.example.com:80");
assertUrl("http://www.example.com", "http://www.example.com/");
assertUrl("http://user:pass@www.example.com", "http://user:pass@www.example.com/");
assertUrl("http://www.example.com:80", "http://www.example.com/");
assertUrl("http://www.example.com/");
assertUrl("http://www.example.com/foo/bar");
assertUrl("http://www.example.com/foo/bar/");
assertUrl("http://user:pass@www.example.com:80/foo/bar/");
assertUrl("http://user:pass@www.example.com:80/foo/bar/",
"http://user:pass@www.example.com/foo/bar/");

// From https://bugzilla.mozilla.org/show_bug.cgi?id=1451274
assertUrl("data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9Vc2Vycy9rdWQvUHJvamVjdHMvX2NvbnRleHRlL2xvaXMtd2ViYXBwL3NyYy9zdHlsZXMvc2VsZWN0aW9uLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtFQUNFLHVDQUF1QztFQUN2QyxlQUFlO0NBQ2hCOztBQUVEO0VBQ0UsdUNBQXVDO0VBQ3ZDLGVBQWU7Q0FDaEIiLCJmaWxlIjoic2VsZWN0aW9uLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIjo6LW1vei1zZWxlY3Rpb24ge1xuICBiYWNrZ3JvdW5kLWNvbG9yOiB2YXIoLS1jb2xvci0tYWN0aW9uKTtcbiAgY29sb3I6ICNmZmZmZmY7XG59XG5cbjo6c2VsZWN0aW9uIHtcbiAgYmFja2dyb3VuZC1jb2xvcjogdmFyKC0tY29sb3ItLWFjdGlvbik7XG4gIGNvbG9yOiAjZmZmZmZmO1xufVxuIl0sInNvdXJjZVJvb3QiOiIifQ==");

assertUrl("//");
assertUrl("//www.example.com");
assertUrl("file:///www.example.com");

assert.equal(libUtil.urlParse(""), null);
Expand All @@ -31,19 +33,19 @@ exports["test urls"] = function(assert) {
assert.equal(libUtil.urlParse("a/b"), null);
assert.equal(libUtil.urlParse("a//b"), null);
assert.equal(libUtil.urlParse("/a"), null);
assert.equal(libUtil.urlParse("data:foo,bar"), null);
assertUrl("data:foo,bar");

let parsed = libUtil.urlParse("http://x-y.com/bar");
assert.equal(parsed.scheme, "http");
assert.equal(parsed.protocol, "http:");
assert.equal(parsed.host, "x-y.com");
assert.equal(parsed.path, "/bar");
assert.equal(parsed.pathname, "/bar");

const webpackURL = "webpack:///webpack/bootstrap 67e184f9679733298d44";
parsed = libUtil.urlParse(webpackURL);
assert.equal(parsed.scheme, "webpack");
assert.equal(parsed.protocol, "webpack:");
assert.equal(parsed.host, "");
assert.equal(parsed.path, "/webpack/bootstrap 67e184f9679733298d44");
assert.equal(webpackURL, libUtil.urlGenerate(parsed));
assert.equal(parsed.pathname, "/webpack/bootstrap%2067e184f9679733298d44");
assert.equal(webpackURL, parsed.toString().replace(/%20/, " "));
};

exports["test normalize()"] = function(assert) {
Expand Down Expand Up @@ -73,12 +75,12 @@ exports["test normalize()"] = function(assert) {
assert.equal(libUtil.normalize("a/././."), "a");

assert.equal(libUtil.normalize("/a/b//c////d/////"), "/a/b/c/d/");
assert.equal(libUtil.normalize("///a/b//c////d/////"), "///a/b/c/d/");
assert.equal(libUtil.normalize("///a/b//c////d/////"), "/a/b/c/d/");
assert.equal(libUtil.normalize("a/b//c////d"), "a/b/c/d");

assert.equal(libUtil.normalize(".///.././../a/b//./.."), "../../a");

assert.equal(libUtil.normalize("http://www.example.com"), "http://www.example.com");
assert.equal(libUtil.normalize("http://www.example.com"), "http://www.example.com/");
assert.equal(libUtil.normalize("http://www.example.com/"), "http://www.example.com/");
assert.equal(libUtil.normalize("http://www.example.com/./..//a/b/c/.././d//"), "http://www.example.com/a/b/d/");
};
Expand Down Expand Up @@ -167,8 +169,8 @@ exports["test join()"] = function(assert) {
assert.equal(libUtil.join("http://foo.org/", "."), "http://foo.org/");
assert.equal(libUtil.join("http://foo.org//", ""), "http://foo.org/");
assert.equal(libUtil.join("http://foo.org//", "."), "http://foo.org/");
assert.equal(libUtil.join("//www.example.com", ""), "//www.example.com/");
assert.equal(libUtil.join("//www.example.com", "."), "//www.example.com/");
assert.equal(libUtil.join("//www.example.com", ""), "/www.example.com");
assert.equal(libUtil.join("//www.example.com", "."), "/www.example.com");


assert.equal(libUtil.join("http://foo.org/a", "b"), "http://foo.org/a/b");
Expand All @@ -177,7 +179,6 @@ exports["test join()"] = function(assert) {
assert.equal(libUtil.join("http://foo.org/a", "b/"), "http://foo.org/a/b/");
assert.equal(libUtil.join("http://foo.org/a", "b//"), "http://foo.org/a/b/");
assert.equal(libUtil.join("http://foo.org/a/", "/b"), "http://foo.org/b");
assert.equal(libUtil.join("http://foo.org/a//", "//b"), "http://b");

assert.equal(libUtil.join("http://foo.org/a", ".."), "http://foo.org/");
assert.equal(libUtil.join("http://foo.org/a", "../b"), "http://foo.org/b");
Expand All @@ -199,11 +200,10 @@ exports["test join()"] = function(assert) {
assert.equal(libUtil.join("http://foo.org//", "/a"), "http://foo.org/a");


assert.equal(libUtil.join("http://", "www.example.com"), "http://www.example.com");
assert.equal(libUtil.join("http://", "www.example.com"), "http://www.example.com/");
assert.equal(libUtil.join("file:///", "www.example.com"), "file:///www.example.com");
assert.equal(libUtil.join("http://", "ftp://example.com"), "ftp://example.com");

assert.equal(libUtil.join("http://www.example.com", "//foo.org/bar"), "http://foo.org/bar");
assert.equal(libUtil.join("//www.example.com", "//foo.org/bar"), "//foo.org/bar");
};

Expand Down