Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
|
lushnikov
2016/07/25 19:33:44
Let's create hosted_mode under scripts/ folder
chenwilliam
2016/07/25 21:58:45
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 var fs = require("fs"); | |
| 6 var http = require("http"); | |
| 7 var https = require("https"); | |
| 8 var path = require("path"); | |
| 9 var parseUrl = require("url").parse; | |
|
lushnikov
2016/07/25 19:33:44
nit: parseURL (abbreviation should be capitalized)
chenwilliam
2016/07/25 21:58:46
Done.
| |
| 10 | |
| 11 var port = parseInt(process.env.PORT, 10) || 8090; | |
| 12 | |
| 13 http.createServer(requestHandler).listen(port); | |
| 14 console.log("Starting hosted mode server at:\n http://localhost:" + port); | |
|
lushnikov
2016/07/25 19:33:44
nit: "Started hosted mode server at http://localho
chenwilliam
2016/07/25 21:58:46
Done.
| |
| 15 | |
| 16 function requestHandler(request, response) | |
| 17 { | |
| 18 var filePath = parseUrl(request.url).pathname; | |
| 19 | |
|
lushnikov
2016/07/25 19:33:44
nit: let's kill newline
chenwilliam
2016/07/25 21:58:46
Done.
| |
| 20 if (filePath === "/front_end/InspectorBackendCommands.js") { | |
| 21 sendResponse(200, " "); | |
| 22 return; | |
| 23 } | |
| 24 | |
| 25 if (proxy(filePath, sendResponse)) | |
| 26 return; | |
| 27 | |
| 28 var absoluteFilePath = path.join(process.cwd(), filePath); | |
| 29 fs.exists(absoluteFilePath, fsExistsCallback); | |
| 30 | |
| 31 function fsExistsCallback(fileExists) | |
| 32 { | |
| 33 if (!fileExists) { | |
| 34 sendResponse(404, "404 - File not found"); | |
| 35 return; | |
| 36 } | |
| 37 fs.readFile(absoluteFilePath, "binary", readFileCallback); | |
| 38 } | |
| 39 | |
| 40 function readFileCallback(err, file) | |
| 41 { | |
| 42 if (err) { | |
| 43 sendResponse(500, `500 - Can't read file: ${err}`); | |
| 44 return; | |
| 45 } | |
| 46 sendResponse(200, file); | |
| 47 } | |
| 48 | |
| 49 function sendResponse(statusCode, data) | |
| 50 { | |
| 51 response.writeHead(statusCode); | |
| 52 response.write(data, "binary"); | |
| 53 response.end(); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 var protocolFileToPath = { | |
|
lushnikov
2016/07/25 19:33:44
can we proxy SupportedCSSProperties.js in the same
chenwilliam
2016/07/25 21:58:45
I don't think we can because SupportedCSSPropertie
| |
| 58 "/front_end/sdk/protocol/js_protocol.json": "core/inspector/browser_protocol .json", | |
| 59 "/front_end/sdk/protocol/browser_protocol.json": "platform/v8_inspector/js_p rotocol.json" | |
| 60 }; | |
| 61 | |
| 62 var protocolFileCache = new Map(); | |
| 63 | |
| 64 function proxy(filePath, sendResponse) | |
|
lushnikov
2016/07/25 19:33:44
let's not pass sendResponse in proxy
chenwilliam
2016/07/25 21:58:45
Done.
| |
| 65 { | |
| 66 if (!(filePath in protocolFileToPath)) | |
| 67 return null; | |
| 68 return fetch("http://localhost:9222/json/version") | |
| 69 .then(onBrowserMetadata) | |
| 70 .then(data => sendResponse(200, data)) | |
| 71 .catch(err => console.log(`Error getting ${filePath}:`, err)); | |
| 72 | |
| 73 function onBrowserMetadata(metadata) | |
| 74 { | |
| 75 var metadataObject = JSON.parse(metadata); | |
| 76 var match = metadataObject["WebKit-Version"].match(/\s\(@(\b[0-9a-f]{5,4 0}\b)/); | |
| 77 var commitHash = match[1]; | |
| 78 var protocolFileURL = getURL(filePath, commitHash); | |
| 79 if (protocolFileCache.has(protocolFileURL)) | |
| 80 return protocolFileCache.get(protocolFileURL); | |
| 81 return fetch(protocolFileURL) | |
| 82 .then(text => new Buffer(text, "base64").toString("binary")) | |
| 83 .then(cacheProtocolFile.bind(null, protocolFileURL)); | |
| 84 } | |
| 85 | |
| 86 function getURL(protocolFile, commitHash) | |
| 87 { | |
| 88 var path = protocolFileToPath[protocolFile]; | |
| 89 return `https://chromium.googlesource.com/chromium/src/+/${commitHash}/t hird_party/WebKit/Source/${path}?format=TEXT`; | |
| 90 } | |
| 91 | |
| 92 function cacheProtocolFile(protocolFileURL, data) | |
| 93 { | |
| 94 protocolFileCache.set(protocolFileURL, data); | |
| 95 return data; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 function fetch(url) | |
| 100 { | |
| 101 return new Promise((resolve, reject) => { | |
|
lushnikov
2016/07/25 19:33:44
style: we don't use multiline arrow functions, let
chenwilliam
2016/07/25 21:58:46
Done.
| |
| 102 var request; | |
| 103 var protocol = parseUrl(url).protocol; | |
| 104 | |
| 105 if (protocol === "https:") { | |
| 106 request = https.get(url, handleResponse); | |
| 107 } else if (protocol === "http:") { | |
| 108 request = http.get(url, handleResponse); | |
| 109 } else { | |
| 110 reject(new Error(`Invalid protocol for url: ${url}`)); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 function handleResponse(response) | |
|
lushnikov
2016/07/25 19:33:44
let's unnest this (the less nesting, the better!)
chenwilliam
2016/07/25 21:58:46
Done.
| |
| 115 { | |
| 116 if (response.statusCode !== 200) { | |
| 117 reject(new Error(`Request error: + ${response.statusCode}`)); | |
| 118 return; | |
| 119 } | |
| 120 var body = ""; | |
| 121 response.on("data", chunk => body += chunk); | |
| 122 response.on("end", () => resolve(body)); | |
| 123 } | |
| 124 | |
| 125 request.on("error", (err) => reject(err)); | |
| 126 }); | |
| 127 } | |
| OLD | NEW |