1- /* We depend only on raw & vanilla libuv, V8 and OpenSSL.
2- * There is to be no dependencies on anything Node.js in here. */
3- #include < uv.h>
1+ /* This addon should depend on nothing but raw, vanilla Google V8 and µWebSockets. */
2+ #include " App.h"
43#include < v8.h>
4+ using namespace v8 ;
5+ Isolate *isolate;
6+ Persistent<Object> resTemplate;
7+ Persistent<Object> reqTemplate;
58
69#include < iostream>
7- using namespace v8 ;
810
911class NativeString {
1012 char *data;
1113 size_t length;
1214 char utf8ValueMemory[sizeof (String::Utf8Value)];
1315 String::Utf8Value *utf8Value = nullptr ;
1416public:
15- NativeString (const Local<Value> &value) {
17+ NativeString (Isolate *isolate, const Local<Value> &value) {
1618 if (value->IsUndefined ()) {
1719 data = nullptr ;
1820 length = 0 ;
1921 } else if (value->IsString ()) {
20- utf8Value = new (utf8ValueMemory) String::Utf8Value (value);
22+ utf8Value = new (utf8ValueMemory) String::Utf8Value (isolate, value);
2123 data = (**utf8Value);
2224 length = utf8Value->length ();
2325 } else if (value->IsTypedArray ()) {
@@ -46,17 +48,12 @@ class NativeString {
4648 }
4749};
4850
49- #include " App.h"
50- Isolate *isolate;
51- Persistent<Object> resTemplate;
52- Persistent<Object> reqTemplate;
53-
5451void res_end (const FunctionCallbackInfo<Value> &args) {
5552
5653 // you might want to do extra work here to swap to tryEnd if passed a Buffer?
5754 // or always use tryEnd and simply grab the object as persistent?
5855
59- NativeString data (args[0 ]);
56+ NativeString data (args. GetIsolate (), args [0 ]);
6057 ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->end (std::string_view (data.getData (), data.getLength ()));
6158
6259 // Return this
@@ -65,8 +62,8 @@ void res_end(const FunctionCallbackInfo<Value> &args) {
6562
6663void res_writeHeader (const FunctionCallbackInfo<Value> &args) {
6764 // get string
68- NativeString header (args[0 ]);
69- NativeString value (args[1 ]);
65+ NativeString header (args. GetIsolate (), args [0 ]);
66+ NativeString value (args. GetIsolate (), args [1 ]);
7067
7168 ((uWS::HttpResponse<false > *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->writeHeader (std::string_view (header.getData (), header.getLength ()), std::string_view (value.getData (), value.getLength ()));
7269
@@ -75,7 +72,7 @@ void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
7572
7673void req_getHeader (const FunctionCallbackInfo<Value> &args) {
7774 // get string
78- NativeString data (args[0 ]);
75+ NativeString data (args. GetIsolate (), args [0 ]);
7976 char *buf = data.getData (); int length = data.getLength ();
8077
8178 std::string_view header = ((uWS::HttpRequest *) args.Holder ()->GetAlignedPointerFromInternalField (0 ))->getHeader (std::string_view (buf, length));
@@ -86,7 +83,7 @@ void req_getHeader(const FunctionCallbackInfo<Value> &args) {
8683void uWS_App_get (const FunctionCallbackInfo<Value> &args) {
8784 uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
8885
89- NativeString nativeString (args[0 ]);
86+ NativeString nativeString (args. GetIsolate (), args [0 ]);
9087
9188 Persistent<Function> *pf = new Persistent<Function>();
9289 pf->Reset (args.GetIsolate (), Local<Function>::Cast (args[1 ]));
@@ -113,7 +110,7 @@ void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
113110void uWS_App_listen (const FunctionCallbackInfo<Value> &args) {
114111 uWS::App *app = (uWS::App *) args.Holder ()->GetAlignedPointerFromInternalField (0 );
115112
116- int port = args[0 ]->Uint32Value ();
113+ int port = args[0 ]->Uint32Value (args. GetIsolate ()-> GetCurrentContext ()). ToChecked ( );
117114
118115 app->listen (port, [&args](auto *token) {
119116 Local<Value> argv[] = {Boolean::New (isolate, token)};
@@ -124,7 +121,12 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
124121 args.GetReturnValue ().Set (args.Holder ());
125122}
126123
124+ // should absolutely not depend on libuv here!
125+ #ifndef ADDON_IS_HOST
126+ #include < uv.h>
127127uv_check_t check;
128+ #endif
129+
128130#include < vector>
129131std::vector<Persistent<Function, CopyablePersistentTraits<Function>>> nextTickQueue;
130132
@@ -156,10 +158,14 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
156158 args.GetReturnValue ().Set (localApp);
157159}
158160
161+ // we should absolutely not depend on libuv here
162+ // move this over to depend on µWS's loop post features
163+
159164void Main (Local<Object> exports) {
160165
161166 isolate = exports->GetIsolate ();
162167
168+ #ifndef ADDON_IS_HOST
163169 /* uWS.nextTick is executed in uv_check_t */
164170 uv_loop_t *loop = uv_default_loop ();
165171 uv_check_init (loop, &check);
@@ -179,6 +185,7 @@ void Main(Local<Object> exports) {
179185 }
180186 });
181187 uv_unref ((uv_handle_t *) &check);
188+ #endif
182189
183190 /* reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
184191 reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
@@ -210,5 +217,7 @@ void Main(Local<Object> exports) {
210217
211218/* This is the only part where we are allowed/forced to add Node.js specific code.
212219 * We do this because we are forced to, and we need a node module version added */
220+ #ifndef ADDON_IS_HOST
213221#include < node.h>
214222NODE_MODULE (uWS, Main)
223+ #endif
0 commit comments