Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/hosted_mode/server.js |
| diff --git a/third_party/WebKit/Source/devtools/hosted_mode/server.js b/third_party/WebKit/Source/devtools/hosted_mode/server.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84100e988a0bd40de635f6db4a6a17d3b227d9d6 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/hosted_mode/server.js |
| @@ -0,0 +1,57 @@ |
| +var fs = require("fs"); |
| +var http = require("http"); |
| +var path = require("path"); |
| +var parseUrl = require("url").parse; |
| + |
| +var protocolFilesProxy = require("./protocol_files_proxy.js"); |
| + |
| +var port = parseInt(process.env.PORT, 10) || 9999; |
|
lushnikov
2016/07/22 02:56:21
|| 8090; (the default port atm)
chenwilliam
2016/07/22 17:35:25
OK, I can change it. I got the 9999 from the Chrom
|
| + |
| +http.createServer(requestHandler).listen(port); |
| +protocolFilesProxy.loadCache(); |
| +console.log("Starting hosted mode server at:\n http://localhost:" + port); |
| + |
| +function requestHandler(request, response) |
| +{ |
| + var url = parseUrl(request.url).pathname; |
| + |
| + if (url === "/front_end/InspectorBackendCommands.js") { |
| + sendResponse(200, " "); |
|
lushnikov
2016/07/22 02:56:21
can we pass just "" here?
chenwilliam
2016/07/22 17:35:25
I initially tried it with an empty string but then
|
| + return; |
| + } |
| + |
| + if (protocolFilesProxy.protocolFiles.indexOf(url) > -1) { |
| + protocolFilesProxy.getProtocolFile(url) |
| + .then(data => sendResponse(200, data)) |
| + .catch(err => console.log(`Error getting ${protocolFile}:`, err)); |
| + return; |
| + } |
| + |
| + var filePath = path.join(process.cwd(), url); |
| + fs.exists(filePath, fsExistsCallback); |
| + |
| + function fsExistsCallback(fileExists) |
| + { |
| + if (!fileExists) { |
| + sendResponse(404, "404 - File not found"); |
| + return; |
| + } |
| + fs.readFile(filePath, "binary", readFileCallback); |
| + } |
| + |
| + function readFileCallback(err, file) |
| + { |
| + if (err) { |
| + sendResponse(500, `500 - Can't read file: ${err}`); |
| + return; |
| + } |
| + sendResponse(200, file); |
| + } |
| + |
| + function sendResponse(statusCode, data) |
| + { |
| + response.writeHead(statusCode); |
| + response.write(data, "binary"); |
| + response.end(); |
| + } |
| +} |