diff --git a/src/AppWrapper.h b/src/AppWrapper.h index 64f2f26c..46e9ead6 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -179,30 +179,40 @@ template void uWS_App_listen(const FunctionCallbackInfo &args) { APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); - /* Invalid use */ - if (args.Length() != 2 && args.Length() != 3) { + /* Require at least two arguments */ + if (args.Length() < 2) { /* Throw here */ - args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "App.listen takes 2 or 3 arguments"))); + args.GetReturnValue().Set(isolate->ThrowException(String::NewFromUtf8(isolate, "App.listen requires port and callback"))); return; } + /* Callback is last */ auto cb = [&args](auto *token) { /* Return a false boolean if listen failed */ Local argv[] = {token ? Local::Cast(External::New(isolate, token)) : Local::Cast(Boolean::New(isolate, false))}; Local::Cast(args[args.Length() - 1])->Call(isolate->GetCurrentContext()->Global(), 1, argv); }; - if (args.Length() == 2) { - /* Port, callback */ - int port = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked(); - app->listen(port, std::move(cb)); - } else if (args.Length() == 3) { - /* Host, port, callback */ - NativeString host(isolate, args[0]); - int port = args[1]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked(); - app->listen(std::string(host.getString().data(), host.getString().length()), port, std::move(cb)); + /* Host is first, if present */ + std::string host; + if (!args[0]->IsNumber()) { + NativeString h(isolate, args[0]); + if (h.isInvalid(args)) { + return; + } + host = h.getString(); } + /* Port, options are in the middle, if present */ + std::vector numbers; + for (int i = std::min(1, host.length()); i < args.Length() - 1; i++) { + numbers.push_back(args[i]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked()); + } + + /* We only use the most complete overload */ + app->listen(host, numbers.size() ? numbers[0] : 0, + numbers.size() > 1 ? numbers[1] : 0, std::move(cb)); + args.GetReturnValue().Set(args.Holder()); } diff --git a/src/addon.cpp b/src/addon.cpp index 6d235403..116b57bd 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -89,6 +89,9 @@ void Main(Local exports) { exports->Set(String::NewFromUtf8(isolate, "SHARED_COMPRESSOR"), Integer::NewFromUnsigned(isolate, uWS::SHARED_COMPRESSOR)); exports->Set(String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR"), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR)); + /* Listen options */ + exports->Set(String::NewFromUtf8(isolate, "OPTION_DO_NOT_REUSE_PORT"), Integer::NewFromUnsigned(isolate, /*uWS::*/OPTION_DO_NOT_REUSE_PORT)); + /* The template for websockets */ WebSocketWrapper::initWsTemplate<0>(); WebSocketWrapper::initWsTemplate<1>(); diff --git a/uWebSockets b/uWebSockets index 31574ec6..ba5f6560 160000 --- a/uWebSockets +++ b/uWebSockets @@ -1 +1 @@ -Subproject commit 31574ec63ab36c6d6d0dcc8f35aba7200bc5d2e8 +Subproject commit ba5f6560419f78cca1b0f4f67f961039b7291718