66#include " inspector/network_resource_manager.h"
77#include " inspector/protocol_helper.h"
88#include " network_inspector.h"
9+ #include " node/inspector/protocol/Runtime.h"
910#include " node_metadata.h"
1011#include " util-inl.h"
1112#include " uv.h"
1516namespace node {
1617namespace inspector {
1718
19+ using v8::Array;
20+ using v8::Context;
1821using v8::HandleScope;
1922using v8::Isolate;
2023using v8::Local;
24+ using v8::MaybeLocal;
2125using v8::Object;
2226using v8::Uint8Array;
2327using v8::Value;
@@ -29,6 +33,76 @@ static void ThrowEventError(v8::Isolate* isolate, const std::string& message) {
2933 v8::String::NewFromUtf8 (isolate, message.c_str ()).ToLocalChecked ()));
3034}
3135
36+ static MaybeLocal<Value> GetProperty (Local<Context> context,
37+ Local<Object> object,
38+ Local<Value> key) {
39+ return object->Get (context, key);
40+ }
41+
42+ static std::unique_ptr<protocol::Value> V8ToProtocolValue (
43+ Local<Context> context,
44+ Local<Value> value) {
45+ Isolate* isolate = Isolate::GetCurrent ();
46+ if (value->IsNullOrUndefined ()) {
47+ return protocol::Value::null ();
48+ }
49+ if (value->IsBoolean ()) {
50+ return protocol::FundamentalValue::create (value.As <v8::Boolean>()->Value ());
51+ }
52+ if (value->IsInt32 ()) {
53+ return protocol::FundamentalValue::create (value.As <v8::Int32>()->Value ());
54+ }
55+ if (value->IsNumber ()) {
56+ return protocol::FundamentalValue::create (value.As <v8::Number>()->Value ());
57+ }
58+ if (value->IsString ()) {
59+ return protocol::StringValue::create (ToProtocolString (isolate, value));
60+ }
61+ if (value->IsArray ()) {
62+ Local<Array> array = value.As <Array>();
63+ std::unique_ptr<protocol::ListValue> list = protocol::ListValue::create ();
64+ list->reserve (array->Length ());
65+ for (uint32_t i = 0 ; i < array->Length (); i++) {
66+ Local<Value> element;
67+ if (!array->Get (context, i).ToLocal (&element)) {
68+ return nullptr ;
69+ }
70+ std::unique_ptr<protocol::Value> protocol_value =
71+ V8ToProtocolValue (context, element);
72+ if (!protocol_value) {
73+ return nullptr ;
74+ }
75+ list->pushValue (std::move (protocol_value));
76+ }
77+ return list;
78+ }
79+ if (value->IsObject ()) {
80+ Local<Object> object = value.As <Object>();
81+ Local<Array> property_names;
82+ if (!object->GetOwnPropertyNames (context).ToLocal (&property_names)) {
83+ return nullptr ;
84+ }
85+ std::unique_ptr<protocol::DictionaryValue> dict =
86+ protocol::DictionaryValue::create ();
87+ for (uint32_t i = 0 ; i < property_names->Length (); i++) {
88+ Local<Value> key;
89+ Local<Value> property;
90+ if (!property_names->Get (context, i).ToLocal (&key) ||
91+ !GetProperty (context, object, key).ToLocal (&property)) {
92+ return nullptr ;
93+ }
94+ std::unique_ptr<protocol::Value> protocol_value =
95+ V8ToProtocolValue (context, property);
96+ if (!protocol_value) {
97+ return nullptr ;
98+ }
99+ dict->setValue (ToProtocolString (isolate, key), std::move (protocol_value));
100+ }
101+ return dict;
102+ }
103+ return nullptr ;
104+ }
105+
32106// Create a protocol::Network::Headers from the v8 object.
33107std::unique_ptr<protocol::Network::Headers>
34108NetworkAgent::createHeadersFromObject (v8::Local<v8::Context> context,
@@ -65,6 +139,67 @@ NetworkAgent::createHeadersFromObject(v8::Local<v8::Context> context,
65139 return std::make_unique<protocol::Network::Headers>(std::move (dict));
66140}
67141
142+ std::unique_ptr<protocol::Network::Initiator>
143+ NetworkAgent::createInitiatorFromObject (v8::Local<v8::Context> context,
144+ Local<Object> initiator_obj) {
145+ HandleScope handle_scope (Isolate::GetCurrent ());
146+ Isolate* isolate = env_->isolate ();
147+
148+ protocol::String type;
149+ if (!ObjectGetProtocolString (context, initiator_obj, " type" ).To (&type)) {
150+ ThrowEventError (isolate, " Missing initiator.type in event" );
151+ return {};
152+ }
153+
154+ std::unique_ptr<protocol::Network::Initiator> initiator =
155+ protocol::Network::Initiator::create ().setType (type).build ();
156+
157+ Local<Object> stack_obj;
158+ if (ObjectGetObject (context, initiator_obj, " stack" ).ToLocal (&stack_obj)) {
159+ std::unique_ptr<protocol::Value> stack_value =
160+ V8ToProtocolValue (context, stack_obj);
161+ if (!stack_value) {
162+ ThrowEventError (isolate, " Invalid initiator.stack in event" );
163+ return {};
164+ }
165+
166+ protocol::ErrorSupport errors;
167+ std::unique_ptr<v8_inspector::protocol::Runtime::API::StackTrace> stack =
168+ protocol::ValueConversions<
169+ v8_inspector::protocol::Runtime::API::StackTrace>::fromValue (
170+ stack_value.get (), &errors);
171+ if (!stack) {
172+ ThrowEventError (isolate, " Invalid initiator.stack in event" );
173+ return {};
174+ }
175+ initiator->setStack (std::move (stack));
176+ }
177+
178+ protocol::String url;
179+ if (ObjectGetProtocolString (context, initiator_obj, " url" ).To (&url)) {
180+ initiator->setUrl (url);
181+ }
182+
183+ double line_number;
184+ if (ObjectGetDouble (context, initiator_obj, " lineNumber" ).To (&line_number)) {
185+ initiator->setLineNumber (line_number);
186+ }
187+
188+ double column_number;
189+ if (ObjectGetDouble (context, initiator_obj, " columnNumber" )
190+ .To (&column_number)) {
191+ initiator->setColumnNumber (column_number);
192+ }
193+
194+ protocol::String request_id;
195+ if (ObjectGetProtocolString (context, initiator_obj, " requestId" )
196+ .To (&request_id)) {
197+ initiator->setRequestId (request_id);
198+ }
199+
200+ return initiator;
201+ }
202+
68203// Create a protocol::Network::Request from the v8 object.
69204std::unique_ptr<protocol::Network::Request>
70205NetworkAgent::createRequestFromObject (v8::Local<v8::Context> context,
@@ -460,12 +595,20 @@ void NetworkAgent::requestWillBeSent(v8::Local<v8::Context> context,
460595 return ;
461596 }
462597
463- std::unique_ptr<protocol::Network::Initiator> initiator =
464- protocol::Network::Initiator::create ()
465- .setType (protocol::Network::Initiator::TypeEnum::Script)
466- .setStack (
467- v8_inspector_->captureStackTrace (true )->buildInspectorObject (0 ))
468- .build ();
598+ std::unique_ptr<protocol::Network::Initiator> initiator;
599+ Local<Object> initiator_obj;
600+ if (ObjectGetObject (context, params, " initiator" ).ToLocal (&initiator_obj)) {
601+ initiator = createInitiatorFromObject (context, initiator_obj);
602+ if (!initiator) {
603+ return ;
604+ }
605+ } else {
606+ initiator = protocol::Network::Initiator::create ()
607+ .setType (protocol::Network::Initiator::TypeEnum::Script)
608+ .setStack (v8_inspector_->captureStackTrace (true )
609+ ->buildInspectorObject (0 ))
610+ .build ();
611+ }
469612
470613 if (requests_.contains (request_id)) {
471614 // Duplicate entry, ignore it.
0 commit comments