Skip to content

Commit c61237f

Browse files
committed
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.
1 parent 1997b50 commit c61237f

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

lib/devtools.js

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,34 +151,71 @@ function fetchObject(transport, options, callback) {
151151
}
152152

153153
// callback(protocol)
154+
// XXX this function needs a proper refactor but the inconsistency of the
155+
// fetching process makes it useless for now
154156
function fetchFromChrome(options, info, callback) {
157+
function explodeVersion(v) {
158+
return v.split('.').map(function (x) {
159+
return parseInt(x);
160+
});
161+
}
155162
// attempt to fetch the protocol directly from the Chromium repository
156163
// according to the current version; fallback to the hardcoded version
157164
//
158165
// Thanks to Paul Irish.
159166
// (see https://github.com/cyrus-and/chrome-remote-interface/issues/10#issuecomment-146032907)
160-
var version = info['WebKit-Version'];
161-
var match = version.match(/\s\(@(\b[0-9a-f]{5,40}\b)/);
167+
var webKitVersion = info['WebKit-Version'];
168+
var match = webKitVersion.match(/\s\(@(\b[0-9a-f]{5,40}\b)/);
162169
var hash = match[1];
163170
var fromChromiumDotOrg = (hash <= 202666);
164-
var template = (fromChromiumDotOrg ?
165-
'https://src.chromium.org/blink/trunk/Source/devtools/protocol.json?p=%s':
166-
'https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/devtools/protocol.json?format=TEXT');
167-
var url = util.format(template, hash);
168-
fetchObject(https, url, function (err, data) {
169-
var descriptor;
170-
if (!err) {
171-
try {
172-
// the file is served base64 encoded from googlesource.com
173-
if (!fromChromiumDotOrg) {
174-
data = new Buffer(data, 'base64').toString();
171+
var templates;
172+
if (fromChromiumDotOrg) {
173+
templates = ['https://src.chromium.org/blink/trunk/Source/devtools/protocol.json?p=%s'];
174+
} else {
175+
var chromeVersion = explodeVersion(info.Browser.split('/')[1]);
176+
var lastChromeVersion = explodeVersion('53.0.2758.1'); // before the split (https://crbug.com/580337)
177+
// according to https://www.chromium.org/developers/version-numbers
178+
var beforeSplit = (chromeVersion[2] <= lastChromeVersion[2]); // patch not meaningful
179+
templates = (beforeSplit ?
180+
['https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/devtools/protocol.json?format=TEXT'] :
181+
['https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/core/inspector/browser_protocol.json?format=TEXT',
182+
'https://chromium.googlesource.com/chromium/src/+/%s/third_party/WebKit/Source/platform/v8_inspector/js_protocol.json?format=TEXT']);
183+
}
184+
var urls = templates.map(function (template) {
185+
return util.format(template, hash);
186+
});
187+
var descriptors = [];
188+
urls.forEach(function (url) {
189+
fetchObject(https, url, function (err, data) {
190+
var descriptor; // undefined == fallback
191+
if (!err) {
192+
try {
193+
// the file is served base64 encoded from googlesource.com
194+
if (!fromChromiumDotOrg) {
195+
data = new Buffer(data, 'base64').toString();
196+
}
197+
descriptor = JSON.parse(data);
198+
} catch (_) {
199+
// fall back
175200
}
176-
descriptor = JSON.parse(data);
177-
} catch (_) {
178-
// fall back
179201
}
180-
}
181-
callback(descriptor);
202+
descriptors.push(descriptor);
203+
if (descriptors.length === urls.length) {
204+
// all must be defined
205+
if (descriptors.indexOf(undefined) !== -1) {
206+
callback();
207+
return;
208+
}
209+
// merge the domains
210+
descriptors.forEach(function (descriptor, i) {
211+
if (i === 0) {
212+
return;
213+
}
214+
Array.prototype.push.apply(descriptors[0].domains, descriptor.domains);
215+
});
216+
callback(descriptors[0]);
217+
}
218+
});
182219
});
183220
}
184221

0 commit comments

Comments
 (0)