-
Notifications
You must be signed in to change notification settings - Fork 42
/
demo.js
143 lines (132 loc) · 3.85 KB
/
demo.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
const inspector = require('inspector');
const http = require('http');
const query = require('querystring');
const fs = require('fs');
inspector.Session.prototype.postAsync = function(...args) {
let session = this;
return new Promise(
function(resolve, reject) {
session.post(...args,
function(error, result) {
if (error !== null) {
reject(error);
} else if (result.exceptionDetails !== undefined) {
reject(result.exceptionDetails.exception.description);
} else {
resolve(result);
}
});
});
};
async function ReadFile(file_name) {
return new Promise(
function(resolve, reject) {
fs.readFile(file_name, "utf8", function(error, result) {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
}
// Reformat string for HTML.
function Escape(string) {
console.log(string);
return [
["&", "&"],
[" ", " "],
["<", "<"],
[">", ">"],
["\r\n", "<br/>"],
["\n", "<br/>"],
["\"", """],
].reduce(
function(string, [pattern, replacement]) {
return string.replace(new RegExp(pattern, "g"), replacement);
}, string);
}
async function CollectTypeProfile(source) {
// Open a new inspector session.
const session = new inspector.Session();
let typeProfile = "";
try {
session.connect();
// Enable relevant inspector domains.
await session.postAsync('Runtime.enable');
await session.postAsync('Profiler.enable');
await session.postAsync('Profiler.startTypeProfile');
// Compile script.
let { scriptId } = await session.postAsync('Runtime.compileScript', {
expression: source,
sourceURL: "test",
persistScript: true
});
// Execute script.
await session.postAsync('Runtime.runScript', { scriptId });
let { result } = await session.postAsync('Profiler.takeTypeProfile');
[{ entries: typeProfile }] = result.filter(x => x.scriptId == scriptId);
} finally {
// Close session and return.
session.disconnect();
}
return typeProfile;
}
function MarkUpCode(entries, source) {
// Sort in reverse order so we can replace entries without invalidating
// the other offsets.
entries = entries.sort((a, b) => b.offset - a.offset);
for (let entry of entries) {
source = source.slice(0, entry.offset) + typeAnnotation(entry.types) +
source.slice(entry.offset);
}
return source;
function typeAnnotation(types) {
return `<font color="red"><b>/*${types.map(t => t.name).join(', ')}*/</b></font>`;
}
}
async function GetPostBody(request) {
return new Promise(function(resolve) {
let body = "";
request.on('data', data => body += data);
request.on('end', end => resolve(query.parse(body)));
});
}
async function Server(request, response) {
let script = "";
let result = "";
let message_log = "";
let detailed = false;
let count = false;
if (request.method == 'POST') {
// Collect type profile on the script from input form.
try {
let post = await GetPostBody(request);
script = post.script;
let typeProfile = await CollectTypeProfile(script);
result = MarkUpCode(typeProfile, script);
} catch (e) {
console.log(e);
message_log = Escape(e.toString());
console.log(message_log);
}
} else {
// Use example file.
script = await ReadFile("example.js");
}
let template = await ReadFile("template.html");
let html = [
["SCRIPT", script],
["RESULT", result],
["CONSOLE", message_log],
].reduce(function(template, [pattern, replacement]) {
return template.replace(pattern, replacement);
}, template);
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.end(html);
}
http.createServer(Server).listen(8080);
console.log("Listening on localhost:8080");