Skip to content

Commit 8db35d4

Browse files
oveddanfacebook-github-bot-9
authored and
facebook-github-bot-9
committed
Have the chrome debugger run javascript within a web worker, to remove the global document.
Summary: To make the chrome debugger environment consisten with the JSC executer environment, where there is no `window.document`, make the chrome debugger run the javascript inside a web worker. This fixes #1473 Closes #1632 Reviewed By: @martinbigio Differential Revision: D2471710 Pulled By: @vjeux
1 parent 200d9af commit 8db35d4

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

packager/debugger.html

+17-28
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,28 @@
4242
document.getElementById('status').innerHTML = status;
4343
}
4444

45+
// This worker will run the application javascript code,
46+
// making sure that it's run in an environment without a global
47+
// document, to make it consistent with the JSC executor environment.
48+
var worker = new Worker('debuggerWorker.js');
49+
4550
var messageHandlers = {
4651
// This method is a bit hacky. Catalyst asks for a new clean JS runtime.
4752
// The easiest way to do this is to reload this page. That also means that
4853
// web socket connection will be lost. To send reply back we need to remember
49-
// message id
54+
// message id.
55+
// This message also needs to be handled outside of the worker, since the worker
56+
// doesn't have access to local storage.
5057
'prepareJSRuntime': function(message) {
5158
window.onbeforeunload = undefined;
5259
window.localStorage.setItem('sessionID', message.id);
5360
window.location.reload();
5461
},
55-
'executeApplicationScript': function(message, sendReply) {
56-
for (var key in message.inject) {
57-
window[key] = JSON.parse(message.inject[key]);
58-
}
59-
loadScript(message.url, sendReply.bind(null, null));
62+
'executeApplicationScript': function(message) {
63+
worker.postMessage(message);
6064
},
61-
'executeJSCall': function(message, sendReply) {
62-
var returnValue = null;
63-
try {
64-
if (window && window.require) {
65-
var module = window.require(message.moduleName);
66-
returnValue = module[message.moduleMethod].apply(module, message.arguments);
67-
}
68-
} finally {
69-
sendReply(JSON.stringify(returnValue));
70-
}
65+
'executeJSCall': function(message) {
66+
worker.postMessage(message);
7167
}
7268
};
7369

@@ -89,12 +85,9 @@
8985
return;
9086
}
9187

92-
var sendReply = function(result) {
93-
ws.send(JSON.stringify({replyID: object.id, result: result}));
94-
};
9588
var handler = messageHandlers[object.method];
9689
if (handler) {
97-
handler(object, sendReply);
90+
handler(object);
9891
} else {
9992
console.warn('Unknown method: ' + object.method);
10093
}
@@ -107,18 +100,14 @@
107100
window.localStorage.removeItem('sessionID');
108101
debuggerSetTimeout(connectToDebuggerProxy, 100);
109102
};
103+
104+
worker.onmessage = function(message) {
105+
ws.send(JSON.stringify(message.data));
106+
}
110107
}
111108

112109
connectToDebuggerProxy();
113110

114-
function loadScript(src, callback) {
115-
var script = document.createElement('script');
116-
script.type = 'text/javascript';
117-
script.src = src;
118-
script.onload = callback;
119-
document.head.appendChild(script);
120-
}
121-
122111
})();
123112
</script>
124113
<style type="text/css">

packager/debuggerWorker.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
/* global self, importScripts, postMessage, onmessage: true */
10+
/* eslint no-unused-vars: 0 */
11+
'use strict';
12+
13+
var messageHandlers = {
14+
'executeApplicationScript': function(message, sendReply) {
15+
for (var key in message.inject) {
16+
self[key] = JSON.parse(message.inject[key]);
17+
}
18+
importScripts(message.url);
19+
sendReply();
20+
},
21+
'executeJSCall': function(message, sendReply) {
22+
var returnValue = [[], [], [], [], []];
23+
try {
24+
if (require) {
25+
returnValue = require(message.moduleName)[message.moduleMethod].apply(null, message.arguments);
26+
}
27+
} finally {
28+
sendReply(JSON.stringify(returnValue));
29+
}
30+
}
31+
};
32+
33+
onmessage = function(message) {
34+
var object = message.data;
35+
36+
var sendReply = function(result) {
37+
postMessage({replyID: object.id, result: result});
38+
};
39+
40+
var handler = messageHandlers[object.method];
41+
if (handler) {
42+
handler(object, sendReply);
43+
} else {
44+
console.warn('Unknown method: ' + object.method);
45+
}
46+
};

packager/packager.js

+6
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ function getDevToolsLauncher(options) {
184184
var debuggerPath = path.join(__dirname, 'debugger.html');
185185
res.writeHead(200, {'Content-Type': 'text/html'});
186186
fs.createReadStream(debuggerPath).pipe(res);
187+
188+
} else if (req.url === '/debuggerWorker.js') {
189+
var workerPath = path.join(__dirname, 'debuggerWorker.js');
190+
res.writeHead(200, {'Content-Type': 'application/javascript'});
191+
fs.createReadStream(workerPath).pipe(res);
192+
187193
} else if (req.url === '/launch-chrome-devtools') {
188194
var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui';
189195
var script = 'launchChromeDevTools.applescript';

0 commit comments

Comments
 (0)