-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathDevTools.js
108 lines (95 loc) · 2.66 KB
/
DevTools.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const url = require('url');
const http = require('http');
const {Buffer} = global;
const htermRepl = require('hterm-repl');
const DOM = require('./DOM');
const {HTMLIframeElement} = DOM;
// const DEVTOOLS_PORT = 9223;
const _getReplServer = (() => {
let replServer = null;
return () => new Promise((accept, reject) => {
if (!replServer) {
htermRepl(null, (err, newReplServer) => {
if (!err) {
replServer = newReplServer;
accept(replServer);
} else {
reject(err);
}
});
} else {
accept(replServer);
}
});
})();
let id = 0;
class DevTools {
constructor(context, document, replServer) {
this.context = context;
this.document = document;
this.replServer = replServer;
this.id = (++id) + '';
this.repls = [];
this.iframe = (() => {
const iframe = document.createElement('iframe');
iframe.d = 2;
iframe.onload = () => {
const socket = this.replServer.createConnection(this.getUrl());
socket.on('data', d => {
this.iframe.runJs(`window.dispatchEvent(new MessageEvent('message', {data: '${d.toString('base64')}'}));`);
});
iframe.onconsole = m => {
const match = m.match(/^POSTMESSAGE:([\s\S]+)$/);
if (match) {
const bMessage = Buffer.from(match[1], 'utf8');
const b = Buffer.from(new ArrayBuffer(Uint32Array.BYTES_PER_ELEMENT + bMessage.byteLength));
new Uint32Array(b.buffer, b.byteOffset, 1)[0] = bMessage.byteLength;
b.set(bMessage, Uint32Array.BYTES_PER_ELEMENT);
socket.write(b);
}
};
};
iframe.src = this.getUrl();
return iframe;
})();
this.onRepl = this.onRepl.bind(this);
this.replServer.on('repl', this.onRepl);
}
getUrl() {
return `${this.replServer.url}&id=${this.id}`;
}
onRepl(r) {
if (r.id === this.id) {
r.setEval((s, context, filename, cb) => {
let err = null, result;
try {
result = this.ownerDocument.defaultView.runRepl(s);
// result = this.context.vm.run(s);
} catch (e) {
err = e;
}
if (!err) {
cb(null, result);
} else {
cb(err);
}
});
}
}
getIframe() {
return this.iframe;
}
destroy() {
this.replServer.removeListener('repl', this.onRepl);
for (let i = 0; i < this.repls.length; i++) {
this.repls[i].close();
}
this.repls.length = 0;
}
}
module.exports = {
async requestDevTools(context, document) {
const replServer = await _getReplServer();
return new DevTools(context, document, replServer);
},
};