1+ /* We depend only on raw & vanilla libuv, V8 and OpenSSL.
2+ * There is to be no dependencies on anything Node.js in here. */
13#include < uv.h>
2- #include < node .h>
3- # include < node_buffer.h >
4+ #include < v8 .h>
5+
46#include < iostream>
57using namespace v8 ;
68
@@ -18,9 +20,6 @@ class NativeString {
1820 utf8Value = new (utf8ValueMemory) String::Utf8Value (value);
1921 data = (**utf8Value);
2022 length = utf8Value->length ();
21- } else if (node::Buffer::HasInstance (value)) {
22- data = node::Buffer::Data (value);
23- length = node::Buffer::Length (value);
2423 } else if (value->IsTypedArray ()) {
2524 Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast (value);
2625 ArrayBuffer::Contents contents = arrayBufferView->Buffer ()->GetContents ();
@@ -54,127 +53,162 @@ Persistent<Object> reqTemplate;
5453
5554void res_end (const FunctionCallbackInfo<Value> &args) {
5655
57- // you might want to do extra work here to swap to tryEnd if passed a Buffer?
58- // or always use tryEnd and simply grab the object as persistent?
56+ // you might want to do extra work here to swap to tryEnd if passed a Buffer?
57+ // or always use tryEnd and simply grab the object as persistent?
5958
60- NativeString data (args[0 ]);
61- ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->end (std::string_view (data.getData (), data.getLength ()));
59+ NativeString data (args[0 ]);
60+ ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->end (std::string_view (data.getData (), data.getLength ()));
6261
63- // Return this
64- args.GetReturnValue ().Set (args.Holder ());
62+ // Return this
63+ args.GetReturnValue ().Set (args.Holder ());
6564}
6665
6766void res_writeHeader (const FunctionCallbackInfo<Value> &args) {
68- // get string
69- NativeString header (args[0 ]);
70- NativeString value (args[1 ]);
67+ // get string
68+ NativeString header (args[0 ]);
69+ NativeString value (args[1 ]);
7170
72- ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->writeHeader (std::string_view (header.getData (), header.getLength ()), std::string_view (value.getData (), value.getLength ()));
71+ ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->writeHeader (std::string_view (header.getData (), header.getLength ()), std::string_view (value.getData (), value.getLength ()));
7372
74- args.GetReturnValue ().Set (args.Holder ());
73+ args.GetReturnValue ().Set (args.Holder ());
7574}
7675
7776void req_getHeader (const FunctionCallbackInfo<Value> &args) {
78- // get string
79- NativeString data (args[0 ]);
80- char *buf = data.getData (); int length = data.getLength ();
77+ // get string
78+ NativeString data (args[0 ]);
79+ char *buf = data.getData (); int length = data.getLength ();
8180
82- std::string_view header = ((uWS::HttpRequest *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->getHeader (std::string_view (buf, length));
81+ std::string_view header = ((uWS::HttpRequest *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->getHeader (std::string_view (buf, length));
8382
84- args.GetReturnValue ().Set (String::NewFromUtf8 (isolate, header.data (), v8::String::kNormalString , header.length ()));
83+ args.GetReturnValue ().Set (String::NewFromUtf8 (isolate, header.data (), v8::String::kNormalString , header.length ()));
8584}
8685
8786void uWS_App_get (const FunctionCallbackInfo<Value> &args) {
88- uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
87+ uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
8988
90- NativeString nativeString (args[0 ]);
89+ NativeString nativeString (args[0 ]);
9190
92- Persistent<Function> *pf = new Persistent<Function>();
93- pf->Reset (args.GetIsolate (), Local<Function>::Cast (args[1 ]));
91+ Persistent<Function> *pf = new Persistent<Function>();
92+ pf->Reset (args.GetIsolate (), Local<Function>::Cast (args[1 ]));
9493
95- app->get (std::string (nativeString.getData (), nativeString.getLength ()), [pf](auto *res, auto *req) {
96- HandleScope hs (isolate);
94+ // Persistent<Function, CopyablePersistentTraits<Function>> p(isolate, Local<Function>::Cast(args[1]));
9795
98- Local<Object> resObject = Local<Object>:: New (isolate, resTemplate)-> Clone ();
99- resObject-> SetAlignedPointerInInternalField ( 0 , res );
96+ app-> get ( std::string (nativeString. getData (), nativeString. getLength ()), [pf]( auto *res, auto *req) {
97+ HandleScope hs (isolate );
10098
101- Local<Object> reqObject = Local<Object>::New (isolate, reqTemplate )->Clone ();
102- reqObject ->SetAlignedPointerInInternalField (0 , req );
99+ Local<Object> resObject = Local<Object>::New (isolate, resTemplate )->Clone ();
100+ resObject ->SetAlignedPointerInInternalField (0 , res );
103101
104- // node:: is horrible but
105- Local<Value> argv[] = {resObject, reqObject};
106- node::MakeCallback (isolate, isolate->GetCurrentContext ()->Global (), Local<Function>::New (isolate, *pf), 2 , argv);
102+ Local<Object> reqObject = Local<Object>::New (isolate, reqTemplate)->Clone ();
103+ reqObject->SetAlignedPointerInInternalField (0 , req);
107104
108- });
105+ Local<Value> argv[] = {resObject, reqObject};
106+ Local<Function>::New (isolate, *pf)->Call (isolate->GetCurrentContext ()->Global (), 2 , argv);
107+ });
109108
110- args.GetReturnValue ().Set (args.Holder ());
109+ args.GetReturnValue ().Set (args.Holder ());
111110}
112111
113112// port, callback?
114113void uWS_App_listen (const FunctionCallbackInfo<Value> &args) {
115- uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
114+ uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
116115
117- int port = args[0 ]->Uint32Value ();
116+ int port = args[0 ]->Uint32Value ();
118117
119- app->listen (port, [&args](auto *token) {
120- Local<Value> argv[] = {Boolean::New (isolate, token)};
121- Local<Function>::Cast (args[1 ])->Call (isolate->GetCurrentContext ()->Global (), 1 , argv);
122- });
118+ app->listen (port, [&args](auto *token) {
119+ Local<Value> argv[] = {Boolean::New (isolate, token)};
120+ Local<Function>::Cast (args[1 ])->Call (isolate->GetCurrentContext ()->Global (), 1 , argv);
121+ });
123122
124- // Return this
125- args.GetReturnValue ().Set (args.Holder ());
123+ // Return this
124+ args.GetReturnValue ().Set (args.Holder ());
125+ }
126+
127+ uv_check_t check;
128+ #include < vector>
129+ std::vector<Persistent<Function, CopyablePersistentTraits<Function>>> nextTickQueue;
130+
131+ // we need to override process.nextTick to avoid horrible loss of performance by Node.js
132+ void nextTick (const FunctionCallbackInfo<Value> &args) {
133+ nextTickQueue.push_back (Persistent<Function, CopyablePersistentTraits<Function>>(isolate, Local<Function>::Cast (args[0 ])));
126134}
127135
128136// uWS.App() -> object
129137void uWS_App (const FunctionCallbackInfo<Value> &args) {
130- // uWS.App
131- Local<FunctionTemplate> appTemplate = FunctionTemplate::New (isolate);
132- appTemplate->SetClassName (String::NewFromUtf8 (isolate, " uWS.App" ));
133- appTemplate->InstanceTemplate ()->SetInternalFieldCount (1 );
134-
135- // Get
136- appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " get" ), FunctionTemplate::New (isolate, uWS_App_get));
138+ // uWS.App
139+ Local<FunctionTemplate> appTemplate = FunctionTemplate::New (isolate);
140+ appTemplate->SetClassName (String::NewFromUtf8 (isolate, " uWS.App" ));
141+ appTemplate->InstanceTemplate ()->SetInternalFieldCount (1 );
142+
143+ // Get
144+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " get" ), FunctionTemplate::New (isolate, uWS_App_get));
137145
138- // Listen
139- appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " listen" ), FunctionTemplate::New (isolate, uWS_App_listen));
146+ // Listen
147+ appTemplate->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " listen" ), FunctionTemplate::New (isolate, uWS_App_listen));
140148
141- // Instantiate and set intenal pointer
142- Local<Object> localApp = appTemplate->GetFunction ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
149+ // Instantiate and set intenal pointer
150+ Local<Object> localApp = appTemplate->GetFunction ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
143151
144- // Delete this boy
145- localApp->SetAlignedPointerInInternalField (0 , new uWS::App ());
152+ // Delete this boy
153+ localApp->SetAlignedPointerInInternalField (0 , new uWS::App ());
146154
147- // Return an instance of this shit
148- args.GetReturnValue ().Set (localApp);
155+ // Return an instance of this shit
156+ args.GetReturnValue ().Set (localApp);
149157}
150158
151159void Main (Local<Object> exports) {
152- isolate = exports->GetIsolate ();
153160
154- /* reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
155- reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
161+ isolate = exports->GetIsolate ();
162+
163+ /* uWS.nextTick is executed in uv_check_t */
164+ uv_loop_t *loop = uv_default_loop ();
165+ uv_check_init (loop, &check);
166+ check.data = isolate;
167+ uv_check_start (&check, [](uv_check_t *check) {
156168
157- // App is a function returning an object
158- NODE_SET_METHOD (exports, " App" , uWS_App);
169+ if (nextTickQueue.size ()) {
170+ Isolate *isolate = (Isolate *) check->data ;
171+ HandleScope hs (isolate);
159172
160- // HttpResponse template
161- Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New (isolate);
162- resTemplateLocal->SetClassName (String::NewFromUtf8 (isolate, " uWS.HttpResponse" ));
163- resTemplateLocal->InstanceTemplate ()->SetInternalFieldCount (1 );
164- resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " end" ), FunctionTemplate::New (isolate, res_end));
165- resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " writeHeader" ), FunctionTemplate::New (isolate, res_writeHeader));
173+ for (Persistent<Function, CopyablePersistentTraits<Function>> &f : nextTickQueue) {
174+ Local<Function>::New (isolate, f)->Call (isolate->GetCurrentContext ()->Global (), 0 , nullptr );
175+ f.Reset ();
176+ }
177+
178+ nextTickQueue.clear ();
179+ }
180+ });
181+ uv_unref ((uv_handle_t *) &check);
182+
183+ /* reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
184+ reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
185+
186+ /* uWS namespace */
187+ exports->Set (String::NewFromUtf8 (isolate, " App" ), FunctionTemplate::New (isolate, uWS_App)->GetFunction ());
188+ exports->Set (String::NewFromUtf8 (isolate, " nextTick" ), FunctionTemplate::New (isolate, nextTick)->GetFunction ());
166189
167- Local<Object> resObjectLocal = resTemplateLocal->GetFunction ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
168- resTemplate.Reset (isolate, resObjectLocal);
169190
170- // Request template
171- Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New (isolate);
172- reqTemplateLocal->SetClassName (String::NewFromUtf8 (isolate, " uWS.HttpRequest" ));
173- reqTemplateLocal->InstanceTemplate ()->SetInternalFieldCount (1 );
174- reqTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " getHeader" ), FunctionTemplate::New (isolate, req_getHeader));
191+ // HttpResponse template
192+ Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New (isolate);
193+ resTemplateLocal->SetClassName (String::NewFromUtf8 (isolate, " uWS.HttpResponse" ));
194+ resTemplateLocal->InstanceTemplate ()->SetInternalFieldCount (1 );
195+ resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " end" ), FunctionTemplate::New (isolate, res_end));
196+ resTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " writeHeader" ), FunctionTemplate::New (isolate, res_writeHeader));
175197
176- Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
177- reqTemplate.Reset (isolate, reqObjectLocal);
198+ Local<Object> resObjectLocal = resTemplateLocal->GetFunction ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
199+ resTemplate.Reset (isolate, resObjectLocal);
200+
201+ // Request template (do we need to clone this?)
202+ Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New (isolate);
203+ reqTemplateLocal->SetClassName (String::NewFromUtf8 (isolate, " uWS.HttpRequest" ));
204+ reqTemplateLocal->InstanceTemplate ()->SetInternalFieldCount (1 );
205+ reqTemplateLocal->PrototypeTemplate ()->Set (String::NewFromUtf8 (isolate, " getHeader" ), FunctionTemplate::New (isolate, req_getHeader));
206+
207+ Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction ()->NewInstance (isolate->GetCurrentContext ()).ToLocalChecked ();
208+ reqTemplate.Reset (isolate, reqObjectLocal);
178209}
179210
211+ /* This is the only part where we are allowed/forced to add Node.js specific code.
212+ * We do this because we are forced to, and we need a node module version added */
213+ #include < node.h>
180214NODE_MODULE (uWS, Main)
0 commit comments