Skip to content

Commit f48a562

Browse files
legendecasaduh95
authored andcommitted
inspector: roll inspector_protocol
Roll the inspector_protocol library to match V8's inspector_protocol revision. Update the node inspector to use the new `crdtp` protocol library. PR-URL: #56649 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent ebf4527 commit f48a562

File tree

86 files changed

+10879
-6231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+10879
-6231
lines changed

src/inspector/network_agent.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ void NetworkAgent::Wire(UberDispatcher* dispatcher) {
5353

5454
DispatchResponse NetworkAgent::enable() {
5555
inspector_->Enable();
56-
return DispatchResponse::OK();
56+
return DispatchResponse::Success();
5757
}
5858

5959
DispatchResponse NetworkAgent::disable() {
6060
inspector_->Disable();
61-
return DispatchResponse::OK();
61+
return DispatchResponse::Success();
6262
}
6363

6464
void NetworkAgent::requestWillBeSent(
@@ -76,9 +76,11 @@ void NetworkAgent::requestWillBeSent(
7676
request->getString("method", &method);
7777

7878
ErrorSupport errors;
79+
errors.Push();
80+
errors.SetName("headers");
7981
auto headers =
8082
Network::Headers::fromValue(request->getObject("headers"), &errors);
81-
if (errors.hasErrors()) {
83+
if (!errors.Errors().empty()) {
8284
headers = std::make_unique<Network::Headers>(DictionaryValue::create());
8385
}
8486

@@ -105,9 +107,11 @@ void NetworkAgent::responseReceived(
105107
response->getString("statusText", &statusText);
106108

107109
ErrorSupport errors;
110+
errors.Push();
111+
errors.SetName("headers");
108112
auto headers =
109113
Network::Headers::fromValue(response->getObject("headers"), &errors);
110-
if (errors.hasErrors()) {
114+
if (!errors.Errors().empty()) {
111115
headers = std::make_unique<Network::Headers>(DictionaryValue::create());
112116
}
113117

src/inspector/node_inspector.gypi

+29-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
'src/inspector_socket_server.h',
1616
'src/inspector/main_thread_interface.cc',
1717
'src/inspector/main_thread_interface.h',
18+
'src/inspector/node_json.cc',
19+
'src/inspector/node_json.h',
1820
'src/inspector/node_string.cc',
1921
'src/inspector/node_string.h',
2022
'src/inspector/runtime_agent.cc',
@@ -29,6 +31,30 @@
2931
'src/inspector/network_agent.h',
3032
'src/inspector/worker_inspector.cc',
3133
'src/inspector/worker_inspector.h',
34+
35+
'<(protocol_tool_path)/crdtp/cbor.cc',
36+
'<(protocol_tool_path)/crdtp/cbor.h',
37+
'<(protocol_tool_path)/crdtp/dispatch.cc',
38+
'<(protocol_tool_path)/crdtp/dispatch.h',
39+
'<(protocol_tool_path)/crdtp/error_support.cc',
40+
'<(protocol_tool_path)/crdtp/error_support.h',
41+
'<(protocol_tool_path)/crdtp/export.h',
42+
'<(protocol_tool_path)/crdtp/find_by_first.h',
43+
'<(protocol_tool_path)/crdtp/frontend_channel.h',
44+
'<(protocol_tool_path)/crdtp/json.cc',
45+
'<(protocol_tool_path)/crdtp/json.h',
46+
'<(protocol_tool_path)/crdtp/json_platform.cc',
47+
'<(protocol_tool_path)/crdtp/json_platform.h',
48+
'<(protocol_tool_path)/crdtp/maybe.h',
49+
'<(protocol_tool_path)/crdtp/parser_handler.h',
50+
'<(protocol_tool_path)/crdtp/protocol_core.cc',
51+
'<(protocol_tool_path)/crdtp/protocol_core.h',
52+
'<(protocol_tool_path)/crdtp/serializable.cc',
53+
'<(protocol_tool_path)/crdtp/serializable.h',
54+
'<(protocol_tool_path)/crdtp/span.cc',
55+
'<(protocol_tool_path)/crdtp/span.h',
56+
'<(protocol_tool_path)/crdtp/status.cc',
57+
'<(protocol_tool_path)/crdtp/status.h',
3258
],
3359
'node_inspector_generated_sources': [
3460
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Forward.h',
@@ -44,23 +70,11 @@
4470
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Network.h',
4571
],
4672
'node_protocol_files': [
47-
'<(protocol_tool_path)/lib/Allocator_h.template',
48-
'<(protocol_tool_path)/lib/base_string_adapter_cc.template',
49-
'<(protocol_tool_path)/lib/base_string_adapter_h.template',
50-
'<(protocol_tool_path)/lib/DispatcherBase_cpp.template',
51-
'<(protocol_tool_path)/lib/DispatcherBase_h.template',
52-
'<(protocol_tool_path)/lib/encoding_cpp.template',
53-
'<(protocol_tool_path)/lib/encoding_h.template',
54-
'<(protocol_tool_path)/lib/ErrorSupport_cpp.template',
55-
'<(protocol_tool_path)/lib/ErrorSupport_h.template',
5673
'<(protocol_tool_path)/lib/Forward_h.template',
57-
'<(protocol_tool_path)/lib/FrontendChannel_h.template',
58-
'<(protocol_tool_path)/lib/Maybe_h.template',
5974
'<(protocol_tool_path)/lib/Object_cpp.template',
6075
'<(protocol_tool_path)/lib/Object_h.template',
61-
'<(protocol_tool_path)/lib/Parser_cpp.template',
62-
'<(protocol_tool_path)/lib/Parser_h.template',
6376
'<(protocol_tool_path)/lib/Protocol_cpp.template',
77+
'<(protocol_tool_path)/lib/ValueConversions_cpp.template',
6478
'<(protocol_tool_path)/lib/ValueConversions_h.template',
6579
'<(protocol_tool_path)/lib/Values_cpp.template',
6680
'<(protocol_tool_path)/lib/Values_h.template',
@@ -77,6 +91,7 @@
7791
'<@(node_inspector_sources)',
7892
],
7993
'include_dirs': [
94+
'<(protocol_tool_path)',
8095
'<(SHARED_INTERMEDIATE_DIR)/include', # for inspector
8196
'<(SHARED_INTERMEDIATE_DIR)',
8297
'<(SHARED_INTERMEDIATE_DIR)/src', # for inspector
@@ -112,6 +127,7 @@
112127
'action': [
113128
'<(python)',
114129
'tools/inspector_protocol/code_generator.py',
130+
'--inspector_protocol_dir', 'tools/inspector_protocol/',
115131
'--jinja_dir', '<@(protocol_tool_path)',
116132
'--output_base', '<(SHARED_INTERMEDIATE_DIR)/src/',
117133
'--config', 'src/inspector/node_protocol_config.json',

src/inspector/node_json.cc

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#include "node_json.h"
2+
3+
#include "crdtp/json.h"
4+
5+
namespace node {
6+
namespace inspector {
7+
8+
using crdtp::ParserHandler;
9+
using crdtp::span;
10+
using crdtp::Status;
11+
using protocol::Binary;
12+
using protocol::BinaryValue;
13+
using protocol::DictionaryValue;
14+
using protocol::FundamentalValue;
15+
using protocol::ListValue;
16+
using protocol::String;
17+
using protocol::StringUtil;
18+
using protocol::StringValue;
19+
using protocol::Value;
20+
21+
namespace {
22+
23+
// Uses the parsing events received from driver of `ParserHandler`
24+
// (e.g. crdtp::json::ParseJSON) into a protocol::Value instance.
25+
class ValueParserHandler : public ParserHandler {
26+
public:
27+
// Provides the parsed protocol::Value.
28+
std::unique_ptr<Value> ReleaseRoot() { return std::move(root_); }
29+
30+
// The first parsing error encountered; `status().ok()` is the default.
31+
Status status() const { return status_; }
32+
33+
private:
34+
// Implementation of `ParserHandler`.
35+
void HandleMapBegin() override {
36+
if (!status_.ok()) return;
37+
std::unique_ptr<DictionaryValue> dict = DictionaryValue::create();
38+
DictionaryValue* dict_ptr = dict.get();
39+
AddValueToParent(std::move(dict));
40+
stack_.emplace_back(dict_ptr);
41+
}
42+
43+
void HandleMapEnd() override {
44+
if (!status_.ok()) return;
45+
DCHECK(!stack_.empty());
46+
DCHECK(stack_.back().is_dict);
47+
stack_.pop_back();
48+
}
49+
50+
void HandleArrayBegin() override {
51+
if (!status_.ok()) return;
52+
std::unique_ptr<ListValue> list = ListValue::create();
53+
ListValue* list_ptr = list.get();
54+
AddValueToParent(std::move(list));
55+
stack_.emplace_back(list_ptr);
56+
}
57+
58+
void HandleArrayEnd() override {
59+
if (!status_.ok()) return;
60+
DCHECK(!stack_.empty());
61+
DCHECK(!stack_.back().is_dict);
62+
stack_.pop_back();
63+
}
64+
65+
void HandleString8(span<uint8_t> chars) override {
66+
AddStringToParent(StringUtil::fromUTF8(chars.data(), chars.size()));
67+
}
68+
69+
void HandleString16(span<uint16_t> chars) override {
70+
AddStringToParent(StringUtil::fromUTF16(chars.data(), chars.size()));
71+
}
72+
73+
void HandleBinary(span<uint8_t> bytes) override {
74+
AddValueToParent(
75+
BinaryValue::create(Binary::fromSpan(bytes.data(), bytes.size())));
76+
}
77+
78+
void HandleDouble(double value) override {
79+
AddValueToParent(FundamentalValue::create(value));
80+
}
81+
82+
void HandleInt32(int32_t value) override {
83+
AddValueToParent(FundamentalValue::create(value));
84+
}
85+
86+
void HandleBool(bool value) override {
87+
AddValueToParent(FundamentalValue::create(value));
88+
}
89+
90+
void HandleNull() override { AddValueToParent(Value::null()); }
91+
92+
void HandleError(Status error) override { status_ = error; }
93+
94+
// Adding strings and values to the parent value.
95+
// Strings are handled separately because they can be keys for
96+
// dictionary values.
97+
void AddStringToParent(String str) {
98+
if (!status_.ok()) return;
99+
if (!root_) {
100+
DCHECK(!key_is_pending_);
101+
root_ = StringValue::create(str);
102+
} else if (stack_.back().is_dict) {
103+
// If we already have a pending key, then this is the value of the
104+
// key/value pair. Otherwise, it's the new pending key.
105+
if (key_is_pending_) {
106+
stack_.back().dict->setString(pending_key_, str);
107+
key_is_pending_ = false;
108+
} else {
109+
pending_key_ = std::move(str);
110+
key_is_pending_ = true;
111+
}
112+
} else { // Top of the stack is a list.
113+
DCHECK(!key_is_pending_);
114+
stack_.back().list->pushValue(StringValue::create(str));
115+
}
116+
}
117+
118+
void AddValueToParent(std::unique_ptr<Value> value) {
119+
if (!status_.ok()) return;
120+
if (!root_) {
121+
DCHECK(!key_is_pending_);
122+
root_ = std::move(value);
123+
} else if (stack_.back().is_dict) {
124+
DCHECK(key_is_pending_);
125+
stack_.back().dict->setValue(pending_key_, std::move(value));
126+
key_is_pending_ = false;
127+
} else { // Top of the stack is a list.
128+
DCHECK(!key_is_pending_);
129+
stack_.back().list->pushValue(std::move(value));
130+
}
131+
}
132+
133+
// `status_.ok()` is the default; if we receive an error event
134+
// we keep the first one and stop modifying any other state.
135+
Status status_;
136+
137+
// The root of the parsed protocol::Value tree.
138+
std::unique_ptr<Value> root_;
139+
140+
// If root_ is a list or a dictionary, this stack keeps track of
141+
// the container we're currently parsing as well as its ancestors.
142+
struct ContainerState {
143+
explicit ContainerState(DictionaryValue* dict)
144+
: is_dict(true), dict(dict) {}
145+
explicit ContainerState(ListValue* list) : is_dict(false), list(list) {}
146+
147+
bool is_dict;
148+
union {
149+
DictionaryValue* dict;
150+
ListValue* list;
151+
};
152+
};
153+
std::vector<ContainerState> stack_;
154+
155+
// For maps, keys and values are alternating events, so we keep the
156+
// key around and process it when the value arrives.
157+
bool key_is_pending_ = false;
158+
String pending_key_;
159+
};
160+
} // anonymous namespace
161+
162+
std::unique_ptr<Value> JsonUtil::ParseJSON(const uint8_t* chars, size_t size) {
163+
ValueParserHandler handler;
164+
crdtp::json::ParseJSON(span<uint8_t>(chars, size), &handler);
165+
if (handler.status().ok()) return handler.ReleaseRoot();
166+
return nullptr;
167+
}
168+
169+
std::unique_ptr<Value> JsonUtil::ParseJSON(const uint16_t* chars, size_t size) {
170+
ValueParserHandler handler;
171+
crdtp::json::ParseJSON(span<uint16_t>(chars, size), &handler);
172+
if (handler.status().ok()) return handler.ReleaseRoot();
173+
return nullptr;
174+
}
175+
176+
std::unique_ptr<Value> JsonUtil::parseJSON(const std::string_view string) {
177+
if (string.empty()) return nullptr;
178+
179+
return ParseJSON(reinterpret_cast<const uint8_t*>(string.data()),
180+
string.size());
181+
}
182+
183+
std::unique_ptr<Value> JsonUtil::parseJSON(v8_inspector::StringView string) {
184+
if (string.length() == 0) return nullptr;
185+
if (string.is8Bit()) return ParseJSON(string.characters8(), string.length());
186+
return ParseJSON(string.characters16(), string.length());
187+
}
188+
189+
} // namespace inspector
190+
} // namespace node

src/inspector/node_json.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef SRC_INSPECTOR_NODE_JSON_H_
2+
#define SRC_INSPECTOR_NODE_JSON_H_
3+
4+
#include "node/inspector/protocol/Protocol.h"
5+
6+
namespace node {
7+
namespace inspector {
8+
9+
struct JsonUtil {
10+
// Parse s JSON string into protocol::Value.
11+
static std::unique_ptr<protocol::Value> ParseJSON(const uint8_t* chars,
12+
size_t size);
13+
static std::unique_ptr<protocol::Value> ParseJSON(const uint16_t* chars,
14+
size_t size);
15+
16+
static std::unique_ptr<protocol::Value> parseJSON(const std::string_view);
17+
static std::unique_ptr<protocol::Value> parseJSON(
18+
v8_inspector::StringView view);
19+
};
20+
21+
} // namespace inspector
22+
} // namespace node
23+
24+
#endif // SRC_INSPECTOR_NODE_JSON_H_

src/inspector/node_protocol_config.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"protocol": {
3-
"path": "node_protocol.json",
3+
"path": "node_protocol.pdl",
44
"package": "src/node/inspector/protocol",
55
"output": "node/inspector/protocol",
66
"namespace": ["node", "inspector", "protocol"]
@@ -18,5 +18,8 @@
1818
"package": "src/node/inspector/protocol",
1919
"output": "node/inspector/protocol",
2020
"string_header": "inspector/node_string.h"
21+
},
22+
"crdtp": {
23+
"dir": "crdtp"
2124
}
2225
}

0 commit comments

Comments
 (0)