@@ -513,6 +513,36 @@ static void RegisterDestroyHook(const FunctionCallbackInfo<Value>& args) {
513513 p->env ->AddCleanupHook (DestroyParamCleanupHook, p);
514514}
515515
516+ static void GetActiveRequests (const FunctionCallbackInfo<Value>& args) {
517+ Environment* env = Environment::GetCurrent (args);
518+
519+ std::vector<Local<Value>> request_v;
520+ for (ReqWrapBase* req_wrap : *env->req_wrap_queue ()) {
521+ AsyncWrap* w = req_wrap->GetAsyncWrap ();
522+ if (w->persistent ().IsEmpty ())
523+ continue ;
524+ request_v.emplace_back (w->GetOwner ());
525+ }
526+
527+ args.GetReturnValue ().Set (
528+ Array::New (env->isolate (), request_v.data (), request_v.size ()));
529+ }
530+
531+ // Non-static, friend of HandleWrap. Could have been a HandleWrap method but
532+ // implemented here for consistency with GetActiveRequests().
533+ void GetActiveHandles (const FunctionCallbackInfo<Value>& args) {
534+ Environment* env = Environment::GetCurrent (args);
535+
536+ std::vector<Local<Value>> handle_v;
537+ for (auto w : *env->handle_wrap_queue ()) {
538+ if (!HandleWrap::HasRef (w))
539+ continue ;
540+ handle_v.emplace_back (w->GetOwner ());
541+ }
542+ args.GetReturnValue ().Set (
543+ Array::New (env->isolate (), handle_v.data (), handle_v.size ()));
544+ }
545+
516546void AsyncWrap::GetAsyncId (const FunctionCallbackInfo<Value>& args) {
517547 AsyncWrap* wrap;
518548 args.GetReturnValue ().Set (kInvalidAsyncId );
@@ -634,6 +664,9 @@ void AsyncWrap::Initialize(Local<Object> target,
634664 env->SetMethod (target, " disablePromiseHook" , DisablePromiseHook);
635665 env->SetMethod (target, " registerDestroyHook" , RegisterDestroyHook);
636666
667+ env->SetMethod (target, " getActiveRequests" , GetActiveRequests);
668+ env->SetMethod (target, " getActiveHandles" , GetActiveHandles);
669+
637670 PropertyAttribute ReadOnlyDontDelete =
638671 static_cast <PropertyAttribute>(ReadOnly | DontDelete);
639672
@@ -732,6 +765,8 @@ void AsyncWrap::RegisterExternalReferences(
732765 registry->Register (AsyncWrap::GetProviderType);
733766 registry->Register (PromiseWrap::GetAsyncId);
734767 registry->Register (PromiseWrap::GetTriggerAsyncId);
768+ registry->Register (GetActiveRequests);
769+ registry->Register (GetActiveHandles);
735770}
736771
737772AsyncWrap::AsyncWrap (Environment* env,
0 commit comments