Skip to content

Have the chrome debugger run javascript within a web worker, to remove the global document. #1632

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 1 commit 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
41 changes: 16 additions & 25 deletions packager/debugger.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,28 @@
document.getElementById('status').innerHTML = status;
}

// This worker will run the application javascript code,
// making sure that it's run in an environment without a global
// document, to make it consistent with the JSC executor environment.
var worker = new Worker('debuggerWorker.js');

var messageHandlers = {
// This method is a bit hacky. Catalyst asks for a new clean JS runtime.
// The easiest way to do this is to reload this page. That also means that
// web socket connection will be lost. To send reply back we need to remember
// message id
// message id.
// This message also needs to be handled outside of the worker, since the worker
// doesn't have access to local storage.
'prepareJSRuntime': function(message) {
window.onbeforeunload = undefined;
window.localStorage.setItem('sessionID', message.id);
window.location.reload();
},
'executeApplicationScript': function(message, sendReply) {
for (var key in message.inject) {
window[key] = JSON.parse(message.inject[key]);
}
loadScript(message.url, sendReply.bind(null, null));
'executeApplicationScript': function(message) {
worker.postMessage(message);
},
'executeJSCall': function(message, sendReply) {
var returnValue = [[], [], [], [], []];
try {
if (window && window.require) {
returnValue = window.require(message.moduleName)[message.moduleMethod].apply(null, message.arguments);
}
} finally {
sendReply(JSON.stringify(returnValue));
}
'executeJSCall': function(message){
worker.postMessage(message);
}
}

Expand All @@ -72,12 +69,10 @@

ws.onmessage = function(message) {
var object = JSON.parse(message.data);
var sendReply = function(result) {
ws.send(JSON.stringify({replyID: object.id, result: result}));
}
var handler = messageHandlers[object.method];

if (handler) {
handler(object, sendReply);
handler(object);
} else {
console.warn('Unknown method: ' + object.method);
}
Expand All @@ -87,12 +82,8 @@
setStatus('Disconnected from proxy. Is node server running?');
}

function loadScript(src, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.onload = callback;
document.head.appendChild(script);
worker.onmessage = function(message) {
ws.send(JSON.stringify(message.data));
}

})();
Expand Down
39 changes: 39 additions & 0 deletions packager/debuggerWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

var messageHandlers = {
'executeApplicationScript': function(message, sendReply) {
for (var key in message.inject) {
self[key] = JSON.parse(message.inject[key]);
}
loadScript(message.url, sendReply.bind(null, null));
},
'executeJSCall': function(message, sendReply) {
var returnValue = [[], [], [], [], []];
try {
if (require) {
returnValue = require(message.moduleName)[message.moduleMethod].apply(null, message.arguments);
}
} finally {
sendReply(JSON.stringify(returnValue));
}
}
}

onmessage = function(message) {
var object = message.data;

var sendReply = function(result) {
postMessage({replyID: object.id, result: result});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indent

}

var handler = messageHandlers[object.method];
if (handler) {
handler(object, sendReply);
} else {
console.warn('Unknown method: ' + object.method);
}
}

function loadScript(src, callback) {
importScripts(src);
callback();
}
4 changes: 4 additions & 0 deletions packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ function getDevToolsLauncher(options) {
var debuggerPath = path.join(__dirname, 'debugger.html');
res.writeHead(200, {'Content-Type': 'text/html'});
fs.createReadStream(debuggerPath).pipe(res);
} else if (req.url === '/debuggerWorker.js') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this belong here? The goal is to just serve a static js file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that seems like a decent way to do it

var workerPath = path.join(__dirname, 'debuggerWorker.js');
res.writeHead(200, {'Content-Type': 'application/javascript'});
fs.createReadStream(workerPath).pipe(res);
} else if (req.url === '/launch-chrome-devtools') {
var debuggerURL = 'http://localhost:' + options.port + '/debugger-ui';
var script = 'launchChromeDevTools.applescript';
Expand Down