| 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"); | |
| 7 var path = require("path"); | 6 var path = require("path"); |
| 8 var parseURL = require("url").parse; | 7 var parseURL = require("url").parse; |
| 9 var Stream = require("stream").Transform; | 8 |
| 9 var utils = require("../utils"); |
| 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}\n`); | 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
:"); | 17 console.log("For info on using the hosted mode server, see our contributing docs
:"); |
| 18 console.log("https://bit.ly/devtools-contribution-guide"); | 18 console.log("https://bit.ly/devtools-contribution-guide"); |
| 19 console.log("Tip: Look for the 'Hosted Mode Server Options' section\n"); | 19 console.log("Tip: Look for the 'Hosted Mode Server Options' section\n"); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 91 } |
| 92 | 92 |
| 93 var proxyFileCache = new Map(); | 93 var proxyFileCache = new Map(); |
| 94 | 94 |
| 95 function proxy(filePath) | 95 function proxy(filePath) |
| 96 { | 96 { |
| 97 if (!(filePath in proxyFilePathToURL)) | 97 if (!(filePath in proxyFilePathToURL)) |
| 98 return null; | 98 return null; |
| 99 if (process.env.CHROMIUM_COMMIT) | 99 if (process.env.CHROMIUM_COMMIT) |
| 100 return onProxyFileURL(proxyFilePathToURL[filePath](process.env.CHROMIUM_
COMMIT)); | 100 return onProxyFileURL(proxyFilePathToURL[filePath](process.env.CHROMIUM_
COMMIT)); |
| 101 return fetch(`http://localhost:${remoteDebuggingPort}/json/version`) | 101 return utils.fetch(`http://localhost:${remoteDebuggingPort}/json/version`) |
| 102 .then(onBrowserMetadata) | 102 .then(onBrowserMetadata) |
| 103 .then(onProxyFileURL); | 103 .then(onProxyFileURL); |
| 104 | 104 |
| 105 function onBrowserMetadata(metadata) | 105 function onBrowserMetadata(metadata) |
| 106 { | 106 { |
| 107 var metadataObject = JSON.parse(metadata); | 107 var metadataObject = JSON.parse(metadata); |
| 108 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)/); |
| 109 var commitHash = match[1]; | 109 var commitHash = match[1]; |
| 110 var proxyFileURL = proxyFilePathToURL[filePath](commitHash); | 110 var proxyFileURL = proxyFilePathToURL[filePath](commitHash); |
| 111 return proxyFileURL; | 111 return proxyFileURL; |
| 112 } | 112 } |
| 113 | 113 |
| 114 function onProxyFileURL(proxyFileURL) | 114 function onProxyFileURL(proxyFileURL) |
| 115 { | 115 { |
| 116 if (proxyFileCache.has(proxyFileURL)) | 116 if (proxyFileCache.has(proxyFileURL)) |
| 117 return Promise.resolve(proxyFileCache.get(proxyFileURL)); | 117 return Promise.resolve(proxyFileCache.get(proxyFileURL)); |
| 118 return fetch(proxyFileURL) | 118 return utils.fetch(proxyFileURL) |
| 119 .then(cacheProxyFile.bind(null, proxyFileURL)); | 119 .then(cacheProxyFile.bind(null, proxyFileURL)); |
| 120 } | 120 } |
| 121 | 121 |
| 122 function cacheProxyFile(proxyFileURL, data) | 122 function cacheProxyFile(proxyFileURL, data) |
| 123 { | 123 { |
| 124 proxyFileCache.set(proxyFileURL, data); | 124 proxyFileCache.set(proxyFileURL, data); |
| 125 return data; | 125 return data; |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | |
| 129 function fetch(url) | |
| 130 { | |
| 131 return new Promise(fetchPromise); | |
| 132 | |
| 133 function fetchPromise(resolve, reject) | |
| 134 { | |
| 135 var request; | |
| 136 var protocol = parseURL(url).protocol; | |
| 137 var handleResponse = getCallback.bind(null, resolve, reject); | |
| 138 if (protocol === "https:") { | |
| 139 request = https.get(url, handleResponse); | |
| 140 } else if (protocol === "http:") { | |
| 141 request = http.get(url, handleResponse); | |
| 142 } else { | |
| 143 reject(new Error(`Invalid protocol for url: ${url}`)); | |
| 144 return; | |
| 145 } | |
| 146 request.on("error", err => reject(err)); | |
| 147 } | |
| 148 | |
| 149 function getCallback(resolve, reject, response) | |
| 150 { | |
| 151 if (response.statusCode !== 200) { | |
| 152 reject(new Error(`Request error: + ${response.statusCode}`)); | |
| 153 return; | |
| 154 } | |
| 155 var body = new Stream(); | |
| 156 response.on("data", chunk => body.push(chunk)); | |
| 157 response.on("end", () => resolve(body.read())); | |
| 158 } | |
| 159 } | |
| 160 | |
| OLD | NEW |