Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 request = require("./request"); | |
| 6 | |
| 7 var protocolFileToPath = { | |
| 8 "/front_end/sdk/protocol/js_protocol.json": "core/inspector/browser_protocol .json", | |
| 9 "/front_end/sdk/protocol/browser_protocol.json": "platform/v8_inspector/js_p rotocol.json" | |
| 10 }; | |
| 11 var protocolFiles = Object.keys(protocolFileToPath); | |
| 12 | |
| 13 function proxy(filePath, sendResponse) | |
| 14 { | |
| 15 if (protocolFiles.indexOf(filePath) === -1) | |
|
lushnikov
2016/07/22 21:06:04
if (!(filePath in protocolFileToPath))
return
chenwilliam
2016/07/22 23:20:31
Done.
| |
| 16 return null; | |
| 17 return request("http://localhost:9222/json/version") | |
| 18 .then(onBrowserMetadata) | |
| 19 .then(data => sendResponse(200, data)) | |
| 20 .catch(err => console.log(`Error getting ${protocolFile}:`, err)); | |
| 21 | |
| 22 function onBrowserMetadata(metadata) | |
| 23 { | |
| 24 metadata = JSON.parse(metadata); | |
|
lushnikov
2016/07/22 21:06:04
let's make a separate variable for the parsed json
chenwilliam
2016/07/22 23:20:31
Done.
| |
| 25 var match = metadata["WebKit-Version"].match(/\s\(@(\b[0-9a-f]{5,40}\b)/ ); | |
| 26 var commitHash = match[1]; | |
| 27 protocolFileCache.setVersion(commitHash); | |
| 28 if (protocolFileCache.hasFile(filePath)) | |
| 29 return protocolFileCache.getFile(filePath); | |
| 30 return request(getURL(filePath, commitHash)) | |
|
lushnikov
2016/07/22 21:06:04
I would use this url as a cache keys
chenwilliam
2016/07/22 23:20:31
Done.
| |
| 31 .then(text => new Buffer(text, "base64").toString("binary")) | |
| 32 .then(data => { | |
|
lushnikov
2016/07/22 21:06:04
style: we don't use multiline arrow functions, let
chenwilliam
2016/07/22 23:20:31
Done.
| |
| 33 protocolFileCache.cacheFile(filePath, data); | |
| 34 return data; | |
| 35 }); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 function getURL(protocolFile, commitHash) | |
| 40 { | |
| 41 var path = protocolFileToPath[protocolFile]; | |
| 42 return `https://chromium.googlesource.com/chromium/src/+/${commitHash}/third _party/WebKit/Source/${path}?format=TEXT`; | |
| 43 } | |
| 44 | |
| 45 var Cache = function() | |
|
lushnikov
2016/07/22 21:06:04
you don't need this Cache at all - just inline it
chenwilliam
2016/07/22 23:20:31
Done.
| |
| 46 { | |
| 47 this._data = new Map(); | |
| 48 this._version = null; | |
| 49 }; | |
| 50 | |
| 51 Cache.prototype = { | |
| 52 | |
| 53 setVersion: function(version) | |
| 54 { | |
| 55 this._version = version; | |
| 56 }, | |
| 57 | |
| 58 hasFile: function(file) | |
| 59 { | |
| 60 return this._data.has(this._getKey(file)); | |
| 61 }, | |
| 62 | |
| 63 getFile: function(file) | |
| 64 { | |
| 65 return this._data.get(this._getKey(file)); | |
| 66 }, | |
| 67 | |
| 68 cacheFile: function(file, data) | |
| 69 { | |
| 70 this._data.set(this._getKey(file), data); | |
| 71 }, | |
| 72 | |
| 73 _getKey: function(file) | |
| 74 { | |
| 75 return `${file}__version:${this._version}`; | |
| 76 } | |
| 77 }; | |
| 78 | |
| 79 var protocolFileCache = new Cache(); | |
| 80 | |
| 81 module.exports = { | |
| 82 proxy | |
| 83 }; | |
| OLD | NEW |