From c61237fdb3fe256c823fc1d045a1c9f1ff887ece Mon Sep 17 00:00:00 2001 From: Andrea Cardaci Date: Tue, 2 Aug 2016 14:54:15 +0200 Subject: [PATCH] Fixes the 'protocol.json' fetch URL 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. --- lib/devtools.js | 73 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/lib/devtools.js b/lib/devtools.js index 10358a1..cbd0b90 100644 --- a/lib/devtools.js +++ b/lib/devtools.js @@ -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]); + } + }); }); }