@@ -370,6 +370,16 @@ bool IsFilePath(const std::string& path) {
370
370
}
371
371
#endif // __POSIX__
372
372
373
+ void ThrowUninitializedInspectorError (Environment* env) {
374
+ HandleScope scope (env->isolate ());
375
+
376
+ const char * msg = " This Environment was initialized without a V8::Inspector" ;
377
+ Local<Value> exception =
378
+ v8::String::NewFromUtf8 (env->isolate (), msg).ToLocalChecked ();
379
+
380
+ env->isolate ()->ThrowException (exception );
381
+ }
382
+
373
383
} // namespace
374
384
375
385
class NodeInspectorClient : public V8InspectorClient {
@@ -730,6 +740,11 @@ bool Agent::StartIoThread() {
730
740
if (io_ != nullptr )
731
741
return true ;
732
742
743
+ if (!parent_env_->should_create_inspector () && !client_) {
744
+ ThrowUninitializedInspectorError (parent_env_);
745
+ return false ;
746
+ }
747
+
733
748
CHECK_NOT_NULL (client_);
734
749
735
750
io_ = InspectorIo::Start (client_->getThreadHandle (),
@@ -750,7 +765,13 @@ void Agent::Stop() {
750
765
std::unique_ptr<InspectorSession> Agent::Connect (
751
766
std::unique_ptr<InspectorSessionDelegate> delegate,
752
767
bool prevent_shutdown) {
768
+ if (!parent_env_->should_create_inspector () && !client_) {
769
+ ThrowUninitializedInspectorError (parent_env_);
770
+ return std::unique_ptr<InspectorSession>{};
771
+ }
772
+
753
773
CHECK_NOT_NULL (client_);
774
+
754
775
int session_id = client_->connectFrontend (std::move (delegate),
755
776
prevent_shutdown);
756
777
return std::unique_ptr<InspectorSession>(
@@ -760,6 +781,11 @@ std::unique_ptr<InspectorSession> Agent::Connect(
760
781
std::unique_ptr<InspectorSession> Agent::ConnectToMainThread (
761
782
std::unique_ptr<InspectorSessionDelegate> delegate,
762
783
bool prevent_shutdown) {
784
+ if (!parent_env_->should_create_inspector () && !client_) {
785
+ ThrowUninitializedInspectorError (parent_env_);
786
+ return std::unique_ptr<InspectorSession>{};
787
+ }
788
+
763
789
CHECK_NOT_NULL (parent_handle_);
764
790
CHECK_NOT_NULL (client_);
765
791
auto thread_safe_delegate =
@@ -769,6 +795,11 @@ std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
769
795
}
770
796
771
797
void Agent::WaitForDisconnect () {
798
+ if (!parent_env_->should_create_inspector () && !client_) {
799
+ ThrowUninitializedInspectorError (parent_env_);
800
+ return ;
801
+ }
802
+
772
803
CHECK_NOT_NULL (client_);
773
804
bool is_worker = parent_handle_ != nullptr ;
774
805
parent_handle_.reset ();
@@ -918,6 +949,12 @@ void Agent::SetParentHandle(
918
949
919
950
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle (
920
951
int thread_id, const std::string& url) {
952
+ if (!parent_env_->should_create_inspector () && !client_) {
953
+ ThrowUninitializedInspectorError (parent_env_);
954
+ return std::unique_ptr<ParentInspectorHandle>{};
955
+ }
956
+
957
+ CHECK_NOT_NULL (client_);
921
958
if (!parent_handle_) {
922
959
return client_->getWorkerManager ()->NewParentHandle (thread_id, url);
923
960
} else {
@@ -926,11 +963,21 @@ std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
926
963
}
927
964
928
965
void Agent::WaitForConnect () {
966
+ if (!parent_env_->should_create_inspector () && !client_) {
967
+ ThrowUninitializedInspectorError (parent_env_);
968
+ return ;
969
+ }
970
+
929
971
CHECK_NOT_NULL (client_);
930
972
client_->waitForFrontend ();
931
973
}
932
974
933
975
std::shared_ptr<WorkerManager> Agent::GetWorkerManager () {
976
+ if (!parent_env_->should_create_inspector () && !client_) {
977
+ ThrowUninitializedInspectorError (parent_env_);
978
+ return std::unique_ptr<WorkerManager>{};
979
+ }
980
+
934
981
CHECK_NOT_NULL (client_);
935
982
return client_->getWorkerManager ();
936
983
}
0 commit comments