Skip to content

Commit

Permalink
Fixes the 'protocol.json' fetch URL
Browse files Browse the repository at this point in the history
Since Chromium version 53.0.2759.0 the file `protocol.json` has been split
(broswer and JavaScript runtime, see https://crbug.com/580337) and hence a merge
is needed.

Closes #40.
  • Loading branch information
cyrus-and committed Aug 2, 2016
1 parent 1997b50 commit c61237f
Showing 1 changed file with 55 additions and 18 deletions.
73 changes: 55 additions & 18 deletions lib/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,34 +151,71 @@ function fetchObject(transport, options, callback) {
}

// callback(protocol)
// XXX this function needs a proper refactor but the inconsistency of the
// fetching process makes it useless for now
function fetchFromChrome(options, info, callback) {
function explodeVersion(v) {
return v.split('.').map(function (x) {
return parseInt(x);
});
}
// attempt to fetch the protocol directly from the Chromium repository
// according to the current version; fallback to the hardcoded version
//
// Thanks to Paul Irish.
// (see https://github.com/cyrus-and/chrome-remote-interface/issues/10#issuecomment-146032907)
var version = info['WebKit-Version'];
var match = version.match(/\s\(@(\b[0-9a-f]{5,40}\b)/);
var webKitVersion = info['WebKit-Version'];
var match = webKitVersion.match(/\s\(@(\b[0-9a-f]{5,40}\b)/);
var hash = match[1];
var fromChromiumDotOrg = (hash <= 202666);
var template = (fromChromiumDotOrg ?
'https://src.chromium.org/blink/trunk/Source/devtools/protocol.json?p=%s':
'https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/devtools/protocol.json?format=TEXT');
var url = util.format(template, hash);
fetchObject(https, url, function (err, data) {
var descriptor;
if (!err) {
try {
// the file is served base64 encoded from googlesource.com
if (!fromChromiumDotOrg) {
data = new Buffer(data, 'base64').toString();
var templates;
if (fromChromiumDotOrg) {
templates = ['https://src.chromium.org/blink/trunk/Source/devtools/protocol.json?p=%s'];
} else {
var chromeVersion = explodeVersion(info.Browser.split('/')[1]);
var lastChromeVersion = explodeVersion('53.0.2758.1'); // before the split (https://crbug.com/580337)
// according to https://www.chromium.org/developers/version-numbers
var beforeSplit = (chromeVersion[2] <= lastChromeVersion[2]); // patch not meaningful
templates = (beforeSplit ?
['https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/devtools/protocol.json?format=TEXT'] :
['https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/core/inspector/browser_protocol.json?format=TEXT',
'https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/platform/v8_inspector/js_protocol.json?format=TEXT']);
}
var urls = templates.map(function (template) {
return util.format(template, hash);
});
var descriptors = [];
urls.forEach(function (url) {
fetchObject(https, url, function (err, data) {
var descriptor; // undefined == fallback
if (!err) {
try {
// the file is served base64 encoded from googlesource.com
if (!fromChromiumDotOrg) {
data = new Buffer(data, 'base64').toString();
}
descriptor = JSON.parse(data);
} catch (_) {
// fall back
}
descriptor = JSON.parse(data);
} catch (_) {
// fall back
}
}
callback(descriptor);
descriptors.push(descriptor);
if (descriptors.length === urls.length) {
// all must be defined
if (descriptors.indexOf(undefined) !== -1) {
callback();
return;
}
// merge the domains
descriptors.forEach(function (descriptor, i) {
if (i === 0) {
return;
}
Array.prototype.push.apply(descriptors[0].domains, descriptor.domains);
});
callback(descriptors[0]);
}
});
});
}

Expand Down

0 comments on commit c61237f

Please sign in to comment.