Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: third_party/WebKit/Source/devtools/scripts/hosted_mode/server.js

Issue 2167413002: DevTools: implement proxy server for hosted mode (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: implement PR feedback Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/devtools/package.json ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
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;
10
11 var port = parseInt(process.env.PORT, 10) || 8090;
12
13 http.createServer(requestHandler).listen(port);
14 console.log("Started hosted mode server at http://localhost:" + port);
15
16 function requestHandler(request, response)
17 {
18 var filePath = parseURL(request.url).pathname;
19 if (filePath === "/front_end/InspectorBackendCommands.js") {
20 sendResponse(200, " ");
21 return;
22 }
23
24 var proxiedFile = proxy(filePath, sendResponse);
25 if (proxiedFile) {
26 proxiedFile
27 .then(data => sendResponse(200, data))
28 .catch(err => console.log(`Error getting ${filePath}:`, err));
lushnikov 2016/07/25 22:08:20 you might want to return 500 in case of error
chenwilliam 2016/07/26 01:23:58 Done.
29 return;
30 }
31
32 var absoluteFilePath = path.join(process.cwd(), filePath);
33 fs.exists(absoluteFilePath, fsExistsCallback);
34
35 function fsExistsCallback(fileExists)
36 {
37 if (!fileExists) {
38 sendResponse(404, "404 - File not found");
39 return;
40 }
41 fs.readFile(absoluteFilePath, "binary", readFileCallback);
42 }
43
44 function readFileCallback(err, file)
45 {
46 if (err) {
47 sendResponse(500, `500 - Can't read file: ${err}`);
48 return;
49 }
50 sendResponse(200, file);
51 }
52
53 function sendResponse(statusCode, data)
54 {
55 response.writeHead(statusCode);
56 response.write(data, "binary");
57 response.end();
58 }
59 }
60
61 var protocolFileToPath = {
lushnikov 2016/07/25 22:08:20 We can fetch SupportedCSSProperties from cloud, e.
chenwilliam 2016/07/26 01:23:58 Done.
62 "/front_end/sdk/protocol/js_protocol.json": "core/inspector/browser_protocol .json",
63 "/front_end/sdk/protocol/browser_protocol.json": "platform/v8_inspector/js_p rotocol.json"
64 };
65
66 var protocolFileCache = new Map();
67
68 function proxy(filePath)
69 {
70 if (!(filePath in protocolFileToPath))
71 return null;
72 return fetch("http://localhost:9222/json/version")
73 .then(onBrowserMetadata);
74
75 function onBrowserMetadata(metadata)
76 {
77 var metadataObject = JSON.parse(metadata);
78 var match = metadataObject["WebKit-Version"].match(/\s\(@(\b[0-9a-f]{5,4 0}\b)/);
79 var commitHash = match[1];
80 var protocolFileURL = getURL(filePath, commitHash);
81 if (protocolFileCache.has(protocolFileURL))
82 return protocolFileCache.get(protocolFileURL);
83 return fetch(protocolFileURL)
84 .then(text => new Buffer(text, "base64").toString("binary"))
85 .then(cacheProtocolFile.bind(null, protocolFileURL));
86 }
87
88 function getURL(protocolFile, commitHash)
89 {
90 var path = protocolFileToPath[protocolFile];
91 return `https://chromium.googlesource.com/chromium/src/+/${commitHash}/t hird_party/WebKit/Source/${path}?format=TEXT`;
92 }
93
94 function cacheProtocolFile(protocolFileURL, data)
95 {
96 protocolFileCache.set(protocolFileURL, data);
97 return data;
98 }
99 }
100
101 function fetch(url)
102 {
103 return new Promise(fetchPromise);
104
105 function fetchPromise(resolve, reject) {
106 var request;
107 var protocol = parseURL(url).protocol;
108 var handleResponse = getCallback.bind(null, resolve, reject);
109 if (protocol === "https:") {
110 request = https.get(url, handleResponse);
111 } else if (protocol === "http:") {
112 request = http.get(url, handleResponse);
113 } else {
114 reject(new Error(`Invalid protocol for url: ${url}`));
115 return;
116 }
117 request.on("error", err => reject(err));
118 }
119
120 function getCallback(resolve, reject, response)
121 {
122 if (response.statusCode !== 200) {
123 reject(new Error(`Request error: + ${response.statusCode}`));
124 return;
125 }
126 var body = "";
127 response.on("data", chunk => body += chunk);
128 response.on("end", () => resolve(body));
129 }
130 }
131
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/devtools/package.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698