@@ -24,6 +24,7 @@ Persistent<Object> reqTemplate;
2424
2525#include < iostream>
2626#include < vector>
27+ #include < type_traits>
2728std::vector<Persistent<Function, CopyablePersistentTraits<Function>>> nextTickQueue;
2829
2930class NativeString {
@@ -83,7 +84,8 @@ void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
8384 NativeString header (args.GetIsolate (), args[0 ]);
8485 NativeString value (args.GetIsolate (), args[1 ]);
8586
86- ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->writeHeader (std::string_view (header.getData (), header.getLength ()), std::string_view (value.getData (), value.getLength ()));
87+ ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->writeHeader (std::string_view (header.getData (), header.getLength ()),
88+ std::string_view (value.getData (), value.getLength ()));
8789
8890 args.GetReturnValue ().Set (args.Holder ());
8991}
@@ -98,8 +100,64 @@ void req_getHeader(const FunctionCallbackInfo<Value> &args) {
98100 args.GetReturnValue ().Set (String::NewFromUtf8 (isolate, header.data (), v8::String::kNormalString , header.length ()));
99101}
100102
103+ /* WebSocket send */
104+ void uWS_WebSocket_send () {
105+
106+ }
107+
108+ /* uWS.App.ws('/pattern', options) */
109+ template <typename APP >
110+ void uWS_App_ws (const FunctionCallbackInfo<Value> &args) {
111+ APP *app = (APP *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
112+
113+ NativeString nativeString (args.GetIsolate (), args[0 ]);
114+
115+ Persistent<Function> *openPf = new Persistent<Function>();
116+ Persistent<Function> *messagePf = new Persistent<Function>();
117+
118+ /* Get the behavior object */
119+ if (args.Length () == 2 ) {
120+ Local<Object> behaviorObject = Local<Object>::Cast (args[1 ]);
121+
122+ /* Open */
123+ openPf->Reset (args.GetIsolate (), Local<Function>::Cast (behaviorObject->Get (String::NewFromUtf8 (isolate, " open" ))));
124+ /* Message */
125+ messagePf->Reset (args.GetIsolate (), Local<Function>::Cast (behaviorObject->Get (String::NewFromUtf8 (isolate, " message" ))));
126+ }
127+
128+ app->template ws <void >(std::string (nativeString.getData (), nativeString.getLength ()), {
129+
130+ /* .compression = uWS::SHARED_COMPRESSOR,
131+ .maxPayloadLength = 16 * 1024,*/
132+ .open = [openPf](auto *ws, auto *req) {
133+ HandleScope hs (isolate);
134+
135+ /* Attach a new V8 object with pointer to us, to us */
136+
137+ // Local<Value> argv[] = {resObject, reqObject};
138+ Local<Function>::New (isolate, *openPf)->Call (isolate->GetCurrentContext ()->Global (), 0 , nullptr );
139+
140+ },
141+ .message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
142+ HandleScope hs (isolate);
143+ /* ws's user data holds pointer to persistent object which is the V8 representation we pass here */
144+ Local<Value> argv[] = {}; // ws, message, opCode
145+ Local<Function>::New (isolate, *messagePf)->Call (isolate->GetCurrentContext ()->Global (), 1 , argv);
146+ }/*
147+ .drain = []() {},
148+ .ping = []() {},
149+ .pong = []() {},
150+ .close = []() {}*/
151+ });
152+
153+ // Return this
154+ args.GetReturnValue ().Set (args.Holder ());
155+ }
156+
157+ // todo: all other methods
158+ template <typename APP >
101159void uWS_App_get (const FunctionCallbackInfo<Value> &args) {
102- uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
160+ APP *app = (APP *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
103161
104162 NativeString nativeString (args.GetIsolate (), args[0 ]);
105163
@@ -124,9 +182,9 @@ void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
124182 args.GetReturnValue ().Set (args.Holder ());
125183}
126184
127- // port, callback?
185+ template < typename APP >
128186void uWS_App_listen (const FunctionCallbackInfo<Value> &args) {
129- uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
187+ APP *app = (APP *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
130188
131189 int port = args[0 ]->Uint32Value (args.GetIsolate ()->GetCurrentContext ()).ToChecked ();
132190
@@ -139,34 +197,79 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
139197 args.GetReturnValue ().Set (args.Holder ());
140198}
141199
142- // we need to override process.nextTick to avoid horrible loss of performance by Node.js
143- void nextTick (const FunctionCallbackInfo<Value> &args) {
144- nextTickQueue.push_back (Persistent<Function, CopyablePersistentTraits<Function>>(isolate, Local<Function>::Cast (args[0 ])));
145- }
146-
147- // uWS.App() -> object
200+ template <typename APP >
148201void uWS_App (const FunctionCallbackInfo<Value> &args) {
149- // uWS.App
150202 Local<FunctionTemplate> appTemplate = FunctionTemplate::New (isolate);
151- appTemplate->SetClassName (String::NewFromUtf8 (isolate, " uWS.App" ));
203+
204+ APP *app;
205+
206+ /* Name differs based on type */
207+ if (std::is_same<APP , uWS::SSLApp>::value) {
208+ appTemplate->SetClassName (String::NewFromUtf8 (isolate, " uWS.SSLApp" ));
209+
210+ /* We fill these below */
211+ us_ssl_socket_context_options ssl_options = {};
212+
213+ static std::string keyFileName;
214+ static std::string certFileName;
215+ static std::string passphrase;
216+
217+ /* Read the options object (SSL options) */
218+ if (args.Length () == 1 ) {
219+ /* Key file name */
220+ NativeString keyFileNameValue (isolate, Local<Object>::Cast (args[0 ])->Get (String::NewFromUtf8 (isolate, " key_file_name" )));
221+ if (keyFileNameValue.getLength ()) {
222+ keyFileName.append (keyFileNameValue.getData (), keyFileNameValue.getLength ());
223+ ssl_options.key_file_name = keyFileName.c_str ();
224+ }
225+
226+ /* Cert file name */
227+ NativeString certFileNameValue (isolate, Local<Object>::Cast (args[0 ])->Get (String::NewFromUtf8 (isolate, " cert_file_name" )));
228+ if (certFileNameValue.getLength ()) {
229+ certFileName.append (certFileNameValue.getData (), certFileNameValue.getLength ());
230+ ssl_options.cert_file_name = certFileName.c_str ();
231+ }
232+
233+ /* Passphrase */
234+ NativeString passphraseValue (isolate, Local<Object>::Cast (args[0 ])->Get (String::NewFromUtf8 (isolate, " passphrase" )));
235+ if (passphraseValue.getLength ()) {
236+ passphrase.append (passphraseValue.getData (), passphraseValue.getLength ());
237+ ssl_options.passphrase = passphrase.c_str ();
238+ }
239+ }
240+
241+ app = new APP (ssl_options);
242+ } else {
243+ appTemplate->SetClassName (String::NewFromUtf8 (isolate, " uWS.App" ));
244+ app = new APP ;
245+ }
246+
152247 appTemplate->InstanceTemplate ()->SetInternalFieldCount (1 );
153248
154- // Get
155- appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " get" ), FunctionTemplate::New (isolate, uWS_App_get));
249+ // Get and all the Http methods
250+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " get" ), FunctionTemplate::New (isolate, uWS_App_get<APP >));
251+
252+ // Ws
253+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " ws" ), FunctionTemplate::New (isolate, uWS_App_ws<APP >));
156254
157255 // Listen
158- appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " listen" ), FunctionTemplate::New (isolate, uWS_App_listen));
256+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " listen" ), FunctionTemplate::New (isolate, uWS_App_listen< APP > ));
159257
160258 // Instantiate and set intenal pointer
161259 Local<Object> localApp = appTemplate->GetFunction ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
162260
163261 // Delete this boy
164- localApp->SetAlignedPointerInInternalField (0 , new uWS::App () );
262+ localApp->SetAlignedPointerInInternalField (0 , app );
165263
166264 // Return an instance of this shit
167265 args.GetReturnValue ().Set (localApp);
168266}
169267
268+ // we need to override process.nextTick to avoid horrible loss of performance by Node.js
269+ void nextTick (const FunctionCallbackInfo<Value> &args) {
270+ nextTickQueue.push_back (Persistent<Function, CopyablePersistentTraits<Function>>(isolate, Local<Function>::Cast (args[0 ])));
271+ }
272+
170273void emptyNextTickQueue (Isolate *isolate) {
171274 if (nextTickQueue.size ()) {
172275 HandleScope hs (isolate);
@@ -194,11 +297,11 @@ void Main(Local<Object> exports) {
194297 reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
195298
196299 /* uWS namespace */
197- exports->Set (String::NewFromUtf8 (isolate, " App" ), FunctionTemplate::New (isolate, uWS_App)->GetFunction ());
300+ exports->Set (String::NewFromUtf8 (isolate, " App" ), FunctionTemplate::New (isolate, uWS_App<uWS::App>)->GetFunction ());
301+ exports->Set (String::NewFromUtf8 (isolate, " SSLApp" ), FunctionTemplate::New (isolate, uWS_App<uWS::SSLApp>)->GetFunction ());
198302 exports->Set (String::NewFromUtf8 (isolate, " nextTick" ), FunctionTemplate::New (isolate, nextTick)->GetFunction ());
199303
200-
201- // HttpResponse template
304+ // HttpResponse template (not templated)
202305 Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New (isolate);
203306 resTemplateLocal->SetClassName (String::NewFromUtf8 (isolate, " uWS.HttpResponse" ));
204307 resTemplateLocal->InstanceTemplate ()->SetInternalFieldCount (1 );
0 commit comments