From 7fdba0bdbd468f02336d1ae643fd85f2b86b581a Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Wed, 26 Dec 2018 13:50:12 +0100 Subject: [PATCH] Wrap up SSL, initial WebSocket support --- Makefile | 6 +- examples/HelloWorld.js | 18 ++++-- src/addon.cpp | 141 +++++++++++++++++++++++++++++++++++------ 3 files changed, 138 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index e4934bc..163d0eb 100644 --- a/Makefile +++ b/Makefile @@ -16,9 +16,9 @@ targets: curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets curl https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz | tar xz -C targets Linux: - gcc $(C_SHARED) -I $$NODE/include/node - g++ $(CPP_SHARED) -I $$NODE/include/node - g++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node + clang $(C_SHARED) -I $$NODE/include/node + clang++ $(CPP_SHARED) -I $$NODE/include/node + clang++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node Darwin: gcc $(C_OSX) $(C_SHARED) -I $$NODE/include/node g++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node diff --git a/examples/HelloWorld.js b/examples/HelloWorld.js index 5556cde..c0d201d 100644 --- a/examples/HelloWorld.js +++ b/examples/HelloWorld.js @@ -1,13 +1,21 @@ /* The stand-alone runtime has uWS namespace already loaded. */ var uWS = uWS ? uWS : require('../dist/uws.js'); -const world = 'Strings are slower than ArrayBuffer but who cares for demo purose!'; const port = 3000; -uWS.App().get('/hello', (res, req) => { - res.end(world); -}).get('/*', (res, req) => { - res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!'); +const app = uWS./*SSL*/App({ + key_file_name: '/home/alexhultman/key.pem', + cert_file_name: '/home/alexhultman/cert.pem', + passphrase: '1234' +}).get('/hello', (res, req) => { + res.end('Hejdå!'); +}).ws('/*', { + open: (ws, req) => { + console.log('hello websocket!'); + }, + message: () => { + console.log('message from websocket yo'); + } }).listen(port, (token) => { if (token) { console.log('Listening to port ' + port); diff --git a/src/addon.cpp b/src/addon.cpp index 47ac7b8..fa3a701 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -24,6 +24,7 @@ Persistent reqTemplate; #include #include +#include std::vector>> nextTickQueue; class NativeString { @@ -83,7 +84,8 @@ void res_writeHeader(const FunctionCallbackInfo &args) { NativeString header(args.GetIsolate(), args[0]); NativeString value(args.GetIsolate(), args[1]); - ((uWS::HttpResponse *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), std::string_view(value.getData(), value.getLength())); + ((uWS::HttpResponse *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), + std::string_view(value.getData(), value.getLength())); args.GetReturnValue().Set(args.Holder()); } @@ -98,8 +100,64 @@ void req_getHeader(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length())); } +/* WebSocket send */ +void uWS_WebSocket_send() { + +} + +/* uWS.App.ws('/pattern', options) */ +template +void uWS_App_ws(const FunctionCallbackInfo &args) { + APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); + + NativeString nativeString(args.GetIsolate(), args[0]); + + Persistent *openPf = new Persistent(); + Persistent *messagePf = new Persistent(); + + /* Get the behavior object */ + if (args.Length() == 2) { + Local behaviorObject = Local::Cast(args[1]); + + /* Open */ + openPf->Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open")))); + /* Message */ + messagePf->Reset(args.GetIsolate(), Local::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "message")))); + } + + app->template ws(std::string(nativeString.getData(), nativeString.getLength()), { + + /*.compression = uWS::SHARED_COMPRESSOR, + .maxPayloadLength = 16 * 1024,*/ + .open = [openPf](auto *ws, auto *req) { + HandleScope hs(isolate); + + /* Attach a new V8 object with pointer to us, to us */ + + //Local argv[] = {resObject, reqObject}; + Local::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr); + + }, + .message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) { + HandleScope hs(isolate); + /* ws's user data holds pointer to persistent object which is the V8 representation we pass here */ + Local argv[] = {}; // ws, message, opCode + Local::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 1, argv); + }/* + .drain = []() {}, + .ping = []() {}, + .pong = []() {}, + .close = []() {}*/ + }); + + // Return this + args.GetReturnValue().Set(args.Holder()); +} + +// todo: all other methods +template void uWS_App_get(const FunctionCallbackInfo &args) { - uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0); + APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); NativeString nativeString(args.GetIsolate(), args[0]); @@ -124,9 +182,9 @@ void uWS_App_get(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(args.Holder()); } -// port, callback? +template void uWS_App_listen(const FunctionCallbackInfo &args) { - uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0); + APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0); int port = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked(); @@ -139,34 +197,79 @@ void uWS_App_listen(const FunctionCallbackInfo &args) { args.GetReturnValue().Set(args.Holder()); } -// we need to override process.nextTick to avoid horrible loss of performance by Node.js -void nextTick(const FunctionCallbackInfo &args) { - nextTickQueue.push_back(Persistent>(isolate, Local::Cast(args[0]))); -} - -// uWS.App() -> object +template void uWS_App(const FunctionCallbackInfo &args) { - // uWS.App Local appTemplate = FunctionTemplate::New(isolate); - appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App")); + + APP *app; + + /* Name differs based on type */ + if (std::is_same::value) { + appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLApp")); + + /* We fill these below */ + us_ssl_socket_context_options ssl_options = {}; + + static std::string keyFileName; + static std::string certFileName; + static std::string passphrase; + + /* Read the options object (SSL options) */ + if (args.Length() == 1) { + /* Key file name */ + NativeString keyFileNameValue(isolate, Local::Cast(args[0])->Get(String::NewFromUtf8(isolate, "key_file_name"))); + if (keyFileNameValue.getLength()) { + keyFileName.append(keyFileNameValue.getData(), keyFileNameValue.getLength()); + ssl_options.key_file_name = keyFileName.c_str(); + } + + /* Cert file name */ + NativeString certFileNameValue(isolate, Local::Cast(args[0])->Get(String::NewFromUtf8(isolate, "cert_file_name"))); + if (certFileNameValue.getLength()) { + certFileName.append(certFileNameValue.getData(), certFileNameValue.getLength()); + ssl_options.cert_file_name = certFileName.c_str(); + } + + /* Passphrase */ + NativeString passphraseValue(isolate, Local::Cast(args[0])->Get(String::NewFromUtf8(isolate, "passphrase"))); + if (passphraseValue.getLength()) { + passphrase.append(passphraseValue.getData(), passphraseValue.getLength()); + ssl_options.passphrase = passphrase.c_str(); + } + } + + app = new APP(ssl_options); + } else { + appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App")); + app = new APP; + } + appTemplate->InstanceTemplate()->SetInternalFieldCount(1); - // Get - appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get)); + // Get and all the Http methods + appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get)); + + // Ws + appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws)); // Listen - appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen)); + appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen)); // Instantiate and set intenal pointer Local localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked(); // Delete this boy - localApp->SetAlignedPointerInInternalField(0, new uWS::App()); + localApp->SetAlignedPointerInInternalField(0, app); // Return an instance of this shit args.GetReturnValue().Set(localApp); } +// we need to override process.nextTick to avoid horrible loss of performance by Node.js +void nextTick(const FunctionCallbackInfo &args) { + nextTickQueue.push_back(Persistent>(isolate, Local::Cast(args[0]))); +} + void emptyNextTickQueue(Isolate *isolate) { if (nextTickQueue.size()) { HandleScope hs(isolate); @@ -194,11 +297,11 @@ void Main(Local exports) { reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/ /* uWS namespace */ - exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App)->GetFunction()); + exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App)->GetFunction()); + exports->Set(String::NewFromUtf8(isolate, "SSLApp"), FunctionTemplate::New(isolate, uWS_App)->GetFunction()); exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction()); - - // HttpResponse template + // HttpResponse template (not templated) Local resTemplateLocal = FunctionTemplate::New(isolate); resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse")); resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);