/* * Authored by Alex Hultman, 2018-2026. * Intellectual property of third-party. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "App.h" #include "Utilities.h" #include #include "v8-fast-api-calls.h" using namespace v8; /* todo: probably isCorked, cork should be exposed? */ struct WebSocketWrapper { template static inline uWS::WebSocket *getWebSocket(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = (uWS::WebSocket *) getInternalPointer(args.This());//->GetAlignedPointerFromInternalField(0); if (!ws) { args.GetReturnValue().Set(isolate->ThrowException(v8::Exception::Error(String::NewFromUtf8(isolate, "Invalid access of closed uWS.WebSocket/SSLWebSocket.", NewStringType::kNormal).ToLocalChecked()))); } return ws; } static inline void invalidateWsObject(const FunctionCallbackInfo &args) { //args.This()->SetAlignedPointerInInternalField(0, nullptr); setInternalPointer(args.This(), nullptr); } /* Takes nothing returns holder (only used to fool TypeScript, as a conversion from WS to UserData) */ template static void uWS_WebSocket_getUserData(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(args.This()); } /* Takes string topic */ template static void uWS_WebSocket_subscribe(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { NativeString topic(isolate, args[0]); if (topic.isInvalid(args)) { return; } bool success = ws->subscribe(topic.getString()); args.GetReturnValue().Set(success); } } /* Takes string topic, returns boolean success */ template static void uWS_WebSocket_unsubscribe(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { NativeString topic(isolate, args[0]); if (topic.isInvalid(args)) { return; } bool success = ws->unsubscribe(topic.getString()); args.GetReturnValue().Set(success); } } /* Takes string topic, message, returns boolean success */ template static void uWS_WebSocket_publish(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { if (missingArguments(2, args)) { return; } bool isBinary = args[2]->BooleanValue(isolate); bool compress = args[3]->BooleanValue(isolate); NativeString topic(isolate, args[0]); if (topic.isInvalid(args)) { return; } NativeString message(isolate, args[1]); if (message.isInvalid(args)) { return; } bool success = ws->publish(topic.getString(), message.getString(), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress); args.GetReturnValue().Set(success); } } /* It would make sense to call terminate "close" and call close "end" to line up with HTTP */ /* That also makes sense seince close takes message and code -> you can end with a string message */ /* Takes nothing returns nothing */ template static void uWS_WebSocket_close(const FunctionCallbackInfo &args) { auto *ws = getWebSocket(args); if (ws) { invalidateWsObject(args); ws->close(); } } /* Takes code, message, returns undefined */ template static void uWS_WebSocket_end(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { int code = 0; if (args.Length() >= 1) { code = args[0]->Uint32Value(isolate->GetCurrentContext()).ToChecked(); } NativeString message(args.GetIsolate(), args[1]); if (message.isInvalid(args)) { return; } invalidateWsObject(args); ws->end(code, message.getString()); } } /* Takes nothing returns arraybuffer */ template static void uWS_WebSocket_getRemoteAddress(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { std::string_view ip = ws->getRemoteAddress(); args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } /* Takes nothing returns arraybuffer */ template static void uWS_WebSocket_getRemoteAddressAsText(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { std::string_view ip = ws->getRemoteAddressAsText(); args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } /* Takes nothing, returns integer */ template static void uWS_WebSocket_getRemotePort(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { unsigned int port = ws->getRemotePort(); args.GetReturnValue().Set(port); } } /* Takes nothing, returns integer */ template static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { unsigned int bufferedAmount = ws->getBufferedAmount(); args.GetReturnValue().Set(bufferedAmount); } } /* Takes message, isBinary, compressed. Returns true on success, false otherwise */ template static void uWS_WebSocket_sendFirstFragment(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { NativeString message(args.GetIsolate(), args[0]); if (message.isInvalid(args)) { return; } unsigned int sendStatus = ws->sendFirstFragment(message.getString(), args[1]->BooleanValue(isolate) ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, args[2]->BooleanValue(isolate)); args.GetReturnValue().Set(sendStatus); } } /* Takes message, compressed. Returns true on success, false otherwise */ template static void uWS_WebSocket_sendFragment(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { NativeString message(args.GetIsolate(), args[0]); if (message.isInvalid(args)) { return; } unsigned int sendStatus = ws->sendFragment(message.getString(), args[1]->BooleanValue(isolate)); args.GetReturnValue().Set(sendStatus); } } /* Takes message, compressed. Returns true on success, false otherwise */ template static void uWS_WebSocket_sendLastFragment(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { NativeString message(args.GetIsolate(), args[0]); if (message.isInvalid(args)) { return; } unsigned int sendStatus = ws->sendLastFragment(message.getString(), args[1]->BooleanValue(isolate)); args.GetReturnValue().Set(sendStatus); } } /* Takes message, isBinary, compressed. Returns true on success, false otherwise */ template static void uWS_WebSocket_send(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { bool isBinary = args[1]->BooleanValue(isolate); bool compress = args[2]->BooleanValue(isolate); NativeString message(args.GetIsolate(), args[0]); if (message.isInvalid(args)) { return; } unsigned int sendStatus = ws->send(message.getString(), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress); args.GetReturnValue().Set(sendStatus); } } /* Takes topic string, returns bool */ template static void uWS_WebSocket_isSubscribed(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { NativeString topic(args.GetIsolate(), args[0]); if (topic.isInvalid(args)) { return; } bool subscribed = ws->isSubscribed(topic.getString()); args.GetReturnValue().Set(subscribed); } } /* Takes message. Returns true on success, false otherwise */ template static void uWS_WebSocket_ping(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { NativeString message(args.GetIsolate(), args[0]); if (message.isInvalid(args)) { return; } /* This is a wrapper that does not exist in the C++ project */ unsigned int sendStatus = ws->send(message.getString(), uWS::OpCode::PING); args.GetReturnValue().Set(sendStatus); } } /* Takes function, returns this */ template static void uWS_WebSocket_cork(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { ws->cork([cb = Local::Cast(args[0]), isolate]() { /* No need for CallJS here */ cb->Call(isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 0, nullptr).IsEmpty(); }); args.GetReturnValue().Set(args.This()); } } /* This one is wrapped instead of iterateTopics as JS-people will put their hands in wood chipper for sure. */ template static void uWS_WebSocket_getTopics(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); auto *ws = getWebSocket(args); if (ws) { Local topicsArray = Array::New(isolate, 0); ws->iterateTopics([&topicsArray, isolate](std::string_view topic) { Local topicString = String::NewFromUtf8(isolate, topic.data(), NewStringType::kNormal, topic.length()).ToLocalChecked(); topicsArray->Set(isolate->GetCurrentContext(), topicsArray->Length(), topicString).IsNothing(); }); args.GetReturnValue().Set(topicsArray); } } /* V8 fast call fast path for send - called directly from JIT-optimised code. * Requirements: no JS heap allocation, no JS execution, Uint8Array arg only. * A null internal-field pointer (closed socket) sets options.fallback = true so V8 * re-invokes the slow path which throws the proper exception. */ // Version A: Handles Strings template static uint32_t uWS_WebSocket_send_fast_string(v8::Local receiver, const v8::FastOneByteString& message, bool isBinary, bool compress) { auto *ws = (uWS::WebSocket *) getInternalPointer(receiver);//->GetAlignedPointerFromInternalField(0); if (!ws) return 0; return ws->send(std::string_view(message.data, message.length), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress); } // Version B: Handles ArrayBuffer/TypedArray template static uint32_t uWS_WebSocket_send_fast_buffer(v8::Local receiver, v8::Local message, bool isBinary, bool compress) { auto *ws = (uWS::WebSocket *) getInternalPointer(receiver);//->GetAlignedPointerFromInternalField(0); if (!ws) return 0; char* data = nullptr; size_t length = 0; if (message->IsArrayBufferView()) { auto view = message.As(); length = view->ByteLength(); data = static_cast(view->Buffer()->GetBackingStore()->Data()) + view->ByteOffset(); } else if (message->IsArrayBuffer()) { auto ab = message.As(); length = ab->ByteLength(); data = static_cast(ab->GetBackingStore()->Data()); } else { // Not a buffer type we handle fast return 0; } return ws->send(std::string_view(data, length), isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress); } template static Local init(Isolate *isolate) { Local wsTemplateLocal = FunctionTemplate::New(isolate); if (SSL) { wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLWebSocket", NewStringType::kNormal).ToLocalChecked()); } else { wsTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.WebSocket", NewStringType::kNormal).ToLocalChecked()); } wsTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1); /* Register our functions */ wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "sendFirstFragment", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_sendFirstFragment)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "sendFragment", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_sendFragment)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "sendLastFragment", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_sendLastFragment)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getUserData", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getUserData)); static v8::CFunction fast_send = v8::CFunction::Make(uWS_WebSocket_send_fast_buffer); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "send", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_send/*, Local(), Local(), 0, ConstructorBehavior::kThrow, SideEffectType::kHasSideEffect, &fast_send*/)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_end)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_close)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getBufferedAmount", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getBufferedAmount)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddress)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "subscribe", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_subscribe)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "unsubscribe", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_unsubscribe)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "publish", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_publish)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_cork)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ping", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_ping)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemotePort)); wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed)); /* This one does not exist in C++ */ wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getTopics", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getTopics)); /* Create the template */ Local wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); return wsObjectLocal; } };