-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inspector: add initial support for network inspection
PR-URL: #53593 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
- Loading branch information
Showing
20 changed files
with
789 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
const { | ||
DateNow, | ||
} = primordials; | ||
|
||
let dc; | ||
let Network; | ||
|
||
let requestId = 0; | ||
const getNextRequestId = () => `node-network-event-${++requestId}`; | ||
|
||
function onClientRequestStart({ request }) { | ||
const url = `${request.protocol}//${request.host}${request.path}`; | ||
const wallTime = DateNow(); | ||
const timestamp = wallTime / 1000; | ||
request._inspectorRequestId = getNextRequestId(); | ||
Network.requestWillBeSent({ | ||
requestId: request._inspectorRequestId, | ||
timestamp, | ||
wallTime, | ||
request: { | ||
url, | ||
method: request.method, | ||
}, | ||
}); | ||
} | ||
|
||
function onClientResponseFinish({ request }) { | ||
if (typeof request._inspectorRequestId !== 'string') { | ||
return; | ||
} | ||
const timestamp = DateNow() / 1000; | ||
Network.responseReceived({ | ||
requestId: request._inspectorRequestId, | ||
timestamp, | ||
}); | ||
Network.loadingFinished({ | ||
requestId: request._inspectorRequestId, | ||
timestamp, | ||
}); | ||
} | ||
|
||
function enable() { | ||
if (!dc) { | ||
dc = require('diagnostics_channel'); | ||
} | ||
if (!Network) { | ||
Network = require('inspector').Network; | ||
} | ||
dc.subscribe('http.client.request.start', onClientRequestStart); | ||
dc.subscribe('http.client.response.finish', onClientResponseFinish); | ||
} | ||
|
||
function disable() { | ||
dc.unsubscribe('http.client.request.start', onClientRequestStart); | ||
dc.unsubscribe('http.client.response.finish', onClientResponseFinish); | ||
} | ||
|
||
module.exports = { | ||
enable, | ||
disable, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "network_agent.h" | ||
#include "network_inspector.h" | ||
|
||
namespace node { | ||
namespace inspector { | ||
namespace protocol { | ||
|
||
std::unique_ptr<Network::Request> Request(const String& url, | ||
const String& method) { | ||
return Network::Request::create().setUrl(url).setMethod(method).build(); | ||
} | ||
|
||
NetworkAgent::NetworkAgent(NetworkInspector* inspector) | ||
: inspector_(inspector) { | ||
event_notifier_map_["requestWillBeSent"] = &NetworkAgent::requestWillBeSent; | ||
event_notifier_map_["responseReceived"] = &NetworkAgent::responseReceived; | ||
event_notifier_map_["loadingFinished"] = &NetworkAgent::loadingFinished; | ||
} | ||
|
||
void NetworkAgent::emitNotification( | ||
const String& event, std::unique_ptr<protocol::DictionaryValue> params) { | ||
if (!inspector_->IsEnabled()) return; | ||
auto it = event_notifier_map_.find(event); | ||
if (it != event_notifier_map_.end()) { | ||
(this->*(it->second))(std::move(params)); | ||
} | ||
} | ||
|
||
void NetworkAgent::Wire(UberDispatcher* dispatcher) { | ||
frontend_ = std::make_unique<Network::Frontend>(dispatcher->channel()); | ||
Network::Dispatcher::wire(dispatcher, this); | ||
} | ||
|
||
DispatchResponse NetworkAgent::enable() { | ||
inspector_->Enable(); | ||
return DispatchResponse::OK(); | ||
} | ||
|
||
DispatchResponse NetworkAgent::disable() { | ||
inspector_->Disable(); | ||
return DispatchResponse::OK(); | ||
} | ||
|
||
void NetworkAgent::requestWillBeSent( | ||
std::unique_ptr<protocol::DictionaryValue> params) { | ||
String request_id; | ||
params->getString("requestId", &request_id); | ||
double timestamp; | ||
params->getDouble("timestamp", ×tamp); | ||
double wall_time; | ||
params->getDouble("wallTime", &wall_time); | ||
auto request = params->getObject("request"); | ||
String url; | ||
request->getString("url", &url); | ||
String method; | ||
request->getString("method", &method); | ||
|
||
frontend_->requestWillBeSent( | ||
request_id, Request(url, method), timestamp, wall_time); | ||
} | ||
|
||
void NetworkAgent::responseReceived( | ||
std::unique_ptr<protocol::DictionaryValue> params) { | ||
String request_id; | ||
params->getString("requestId", &request_id); | ||
double timestamp; | ||
params->getDouble("timestamp", ×tamp); | ||
|
||
frontend_->responseReceived(request_id, timestamp); | ||
} | ||
|
||
void NetworkAgent::loadingFinished( | ||
std::unique_ptr<protocol::DictionaryValue> params) { | ||
String request_id; | ||
params->getString("requestId", &request_id); | ||
double timestamp; | ||
params->getDouble("timestamp", ×tamp); | ||
|
||
frontend_->loadingFinished(request_id, timestamp); | ||
} | ||
|
||
} // namespace protocol | ||
} // namespace inspector | ||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef SRC_INSPECTOR_NETWORK_AGENT_H_ | ||
#define SRC_INSPECTOR_NETWORK_AGENT_H_ | ||
|
||
#include "node/inspector/protocol/Network.h" | ||
|
||
#include <unordered_map> | ||
|
||
namespace node { | ||
|
||
namespace inspector { | ||
class NetworkInspector; | ||
|
||
namespace protocol { | ||
|
||
std::unique_ptr<Network::Request> Request(const String& url, | ||
const String& method); | ||
|
||
class NetworkAgent : public Network::Backend { | ||
public: | ||
explicit NetworkAgent(NetworkInspector* inspector); | ||
|
||
void Wire(UberDispatcher* dispatcher); | ||
|
||
DispatchResponse enable() override; | ||
|
||
DispatchResponse disable() override; | ||
|
||
void emitNotification(const String& event, | ||
std::unique_ptr<protocol::DictionaryValue> params); | ||
|
||
void requestWillBeSent(std::unique_ptr<protocol::DictionaryValue> params); | ||
|
||
void responseReceived(std::unique_ptr<protocol::DictionaryValue> params); | ||
|
||
void loadingFinished(std::unique_ptr<protocol::DictionaryValue> params); | ||
|
||
private: | ||
NetworkInspector* inspector_; | ||
std::shared_ptr<Network::Frontend> frontend_; | ||
using EventNotifier = | ||
void (NetworkAgent::*)(std::unique_ptr<protocol::DictionaryValue>); | ||
std::unordered_map<String, EventNotifier> event_notifier_map_; | ||
}; | ||
|
||
} // namespace protocol | ||
} // namespace inspector | ||
} // namespace node | ||
|
||
#endif // SRC_INSPECTOR_NETWORK_AGENT_H_ |
Oops, something went wrong.