| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 var fs = require("fs"); | 4 var fs = require("fs"); |
| 5 var http = require("http"); | 5 var http = require("http"); |
| 6 var https = require("https"); | 6 var https = require("https"); |
| 7 var path = require("path"); | 7 var path = require("path"); |
| 8 var parseURL = require("url").parse; | 8 var parseURL = require("url").parse; |
| 9 var Stream = require("stream").Transform; | 9 var Stream = require("stream").Transform; |
| 10 | 10 |
| 11 var remoteDebuggingPort = parseInt(process.env.REMOTE_DEBUGGING_PORT, 10) || 922
2; | 11 var remoteDebuggingPort = parseInt(process.env.REMOTE_DEBUGGING_PORT, 10) || 922
2; |
| 12 var serverPort = parseInt(process.env.PORT, 10) || 8090; | 12 var serverPort = parseInt(process.env.PORT, 10) || 8090; |
| 13 var devtoolsFolder = path.resolve(path.join(__dirname, "../..")); | 13 var devtoolsFolder = path.resolve(path.join(__dirname, "../..")); |
| 14 | 14 |
| 15 http.createServer(requestHandler).listen(serverPort); | 15 http.createServer(requestHandler).listen(serverPort); |
| 16 console.log("Started hosted mode server at http://localhost:" + serverPort); | 16 console.log(`Started hosted mode server at http://localhost:${serverPort}\n`); |
| 17 console.log("For info on using the hosted mode server, see our contributing docs
:"); |
| 18 console.log("https://bit.ly/devtools-contribution-guide"); |
| 19 console.log("Tip: Look for the 'Hosted Mode Server Options' section"); |
| 17 | 20 |
| 18 function requestHandler(request, response) | 21 function requestHandler(request, response) |
| 19 { | 22 { |
| 20 var filePath = parseURL(request.url).pathname; | 23 var filePath = parseURL(request.url).pathname; |
| 21 if (filePath === "/") { | 24 if (filePath === "/") { |
| 22 var landingURL = `http://localhost:${remoteDebuggingPort}#http://localho
st:${serverPort}/front_end/inspector.html?experiments=true`; | 25 var landingURL = `http://localhost:${remoteDebuggingPort}#http://localho
st:${serverPort}/front_end/inspector.html?experiments=true`; |
| 23 sendResponse(200, `<html>Please go to <a href="${landingURL}">${landingU
RL}</a></html>`); | 26 sendResponse(200, `<html>Please go to <a href="${landingURL}">${landingU
RL}</a></html>`); |
| 24 return; | 27 return; |
| 25 } | 28 } |
| 26 | 29 |
| 27 var proxiedFile = proxy(filePath, sendResponse); | 30 var proxiedFile = proxy(filePath, sendResponse); |
| 28 if (proxiedFile) { | 31 if (proxiedFile) { |
| 29 proxiedFile | 32 proxiedFile |
| 30 .then(data => sendResponse(200, data)) | 33 .then(data => sendResponse(200, data)) |
| 31 .catch(handleProxyError); | 34 .catch(handleProxyError); |
| 32 return; | 35 return; |
| 33 } | 36 } |
| 34 | 37 |
| 35 function handleProxyError(err) | 38 function handleProxyError(err) |
| 36 { | 39 { |
| 37 console.log(`Error fetching over the internet file ${filePath}:`, err); | 40 console.log(`Error fetching over the internet file ${filePath}:`, err); |
| 38 console.log(`Make sure you opened Chrome with the flag "--remote-debuggi
ng-port=${remoteDebuggingPort}"`); | 41 console.log(`Make sure you opened Chrome with the flag "--remote-debuggi
ng-port=${remoteDebuggingPort}"`); |
| 39 sendResponse(500, "500 - Internal Server Error"); | 42 sendResponse(500, "500 - Internal Server Error"); |
| 40 } | 43 } |
| 41 | 44 |
| 42 var absoluteFilePath = path.join(process.cwd(), filePath); | 45 var absoluteFilePath = path.join(process.cwd(), filePath); |
| 43 if (!path.resolve(absoluteFilePath).startsWith(devtoolsFolder)) { | 46 if (!path.resolve(absoluteFilePath).startsWith(devtoolsFolder)) { |
| 44 console.log(`File requested is outside of devtools folder: ${devtoolsFol
der}`); | 47 console.log(`File requested is outside of devtools folder: ${devtoolsFol
der}`); |
| 45 sendResponse(403, "`403 - Access denied. File requested is outside of de
vtools folder: ${devtoolsFolder}`"); | 48 sendResponse(403, `403 - Access denied. File requested is outside of dev
tools folder: ${devtoolsFolder}`); |
| 46 return; | 49 return; |
| 47 } | 50 } |
| 48 | 51 |
| 49 fs.exists(absoluteFilePath, fsExistsCallback); | 52 fs.exists(absoluteFilePath, fsExistsCallback); |
| 50 | 53 |
| 51 function fsExistsCallback(fileExists) | 54 function fsExistsCallback(fileExists) |
| 52 { | 55 { |
| 53 if (!fileExists) { | 56 if (!fileExists) { |
| 54 console.log(`Cannot find file ${absoluteFilePath}`); | 57 console.log(`Cannot find file ${absoluteFilePath}`); |
| 55 sendResponse(404, "404 - File not found"); | 58 sendResponse(404, "404 - File not found"); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 86 { | 89 { |
| 87 return `https://chrome-devtools-frontend.appspot.com/serve_file/@${commitHas
h}/${path}`; | 90 return `https://chrome-devtools-frontend.appspot.com/serve_file/@${commitHas
h}/${path}`; |
| 88 } | 91 } |
| 89 | 92 |
| 90 var proxyFileCache = new Map(); | 93 var proxyFileCache = new Map(); |
| 91 | 94 |
| 92 function proxy(filePath) | 95 function proxy(filePath) |
| 93 { | 96 { |
| 94 if (!(filePath in proxyFilePathToURL)) | 97 if (!(filePath in proxyFilePathToURL)) |
| 95 return null; | 98 return null; |
| 99 if (process.env.CHROMIUM_COMMIT) |
| 100 return onProxyFileURL(proxyFilePathToURL[filePath](process.env.CHROMIUM_
COMMIT)); |
| 96 return fetch(`http://localhost:${remoteDebuggingPort}/json/version`) | 101 return fetch(`http://localhost:${remoteDebuggingPort}/json/version`) |
| 97 .then(onBrowserMetadata); | 102 .then(onBrowserMetadata) |
| 103 .then(onProxyFileURL); |
| 98 | 104 |
| 99 function onBrowserMetadata(metadata) | 105 function onBrowserMetadata(metadata) |
| 100 { | 106 { |
| 101 var metadataObject = JSON.parse(metadata); | 107 var metadataObject = JSON.parse(metadata); |
| 102 var match = metadataObject["WebKit-Version"].match(/\s\(@(\b[0-9a-f]{5,4
0}\b)/); | 108 var match = metadataObject["WebKit-Version"].match(/\s\(@(\b[0-9a-f]{5,4
0}\b)/); |
| 103 var commitHash = match[1]; | 109 var commitHash = match[1]; |
| 104 var proxyFileURL = proxyFilePathToURL[filePath](commitHash); | 110 var proxyFileURL = proxyFilePathToURL[filePath](commitHash); |
| 111 return proxyFileURL; |
| 112 } |
| 113 |
| 114 function onProxyFileURL(proxyFileURL) |
| 115 { |
| 105 if (proxyFileCache.has(proxyFileURL)) | 116 if (proxyFileCache.has(proxyFileURL)) |
| 106 return proxyFileCache.get(proxyFileURL); | 117 return Promise.resolve(proxyFileCache.get(proxyFileURL)); |
| 107 return fetch(proxyFileURL) | 118 return fetch(proxyFileURL) |
| 108 .then(cacheProxyFile.bind(null, proxyFileURL)); | 119 .then(cacheProxyFile.bind(null, proxyFileURL)); |
| 109 } | 120 } |
| 110 | 121 |
| 111 function cacheProxyFile(proxyFileURL, data) | 122 function cacheProxyFile(proxyFileURL, data) |
| 112 { | 123 { |
| 113 proxyFileCache.set(proxyFileURL, data); | 124 proxyFileCache.set(proxyFileURL, data); |
| 114 return data; | 125 return data; |
| 115 } | 126 } |
| 116 } | 127 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 140 if (response.statusCode !== 200) { | 151 if (response.statusCode !== 200) { |
| 141 reject(new Error(`Request error: + ${response.statusCode}`)); | 152 reject(new Error(`Request error: + ${response.statusCode}`)); |
| 142 return; | 153 return; |
| 143 } | 154 } |
| 144 var body = new Stream(); | 155 var body = new Stream(); |
| 145 response.on("data", chunk => body.push(chunk)); | 156 response.on("data", chunk => body.push(chunk)); |
| 146 response.on("end", () => resolve(body.read())); | 157 response.on("end", () => resolve(body.read())); |
| 147 } | 158 } |
| 148 } | 159 } |
| 149 | 160 |
| OLD | NEW |