Skip to content

Commit 9e1b056

Browse files
committed
Node.js 11, uWS.nextTick, purely V8
1 parent 100c93f commit 9e1b056

4 files changed

Lines changed: 124 additions & 83 deletions

File tree

‎Makefile‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
C_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I ../v0.15/uSockets/src ../v0.15/uSockets/src/*.c ../v0.15/uSockets/src/eventing/*.c
1+
C_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I ../uWebSockets/uSockets/src ../uWebSockets/uSockets/src/*.c ../uWebSockets/uSockets/src/eventing/*.c
22

3-
CPP_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I ../v0.15/uSockets/src -I ../v0.15/src src/addon.cpp
3+
CPP_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I ../uWebSockets/uSockets/src -I ../uWebSockets/src src/addon.cpp
44

55
CPP_OSX := -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup
66

@@ -9,12 +9,14 @@ default:
99
NODE=targets/node-v8.1.2 ABI=57 make `(uname -s)`
1010
NODE=targets/node-v9.2.0 ABI=59 make `(uname -s)`
1111
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
12+
NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)`
1213
for f in dist/*.node; do chmod +x $$f; done
1314
targets:
1415
mkdir targets
1516
curl https://nodejs.org/dist/v8.1.2/node-v8.1.2-headers.tar.gz | tar xz -C targets
1617
curl https://nodejs.org/dist/v9.2.0/node-v9.2.0-headers.tar.gz | tar xz -C targets
1718
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets
19+
curl https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz | tar xz -C targets
1820
Linux:
1921
gcc $(C_SHARED)
2022
g++ $(CPP_SHARED) -I $$NODE/include/node

‎dist/uws.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module.exports = (() => {
22
try {
3-
return require(`./uws_${process.platform}_${process.versions.modules}.node`);
3+
const uWS = require(`./uws_${process.platform}_${process.versions.modules}.node`);
4+
/* We are not compatible with Node.js domain */
5+
process.nextTick = (f, ...args) => uWS.nextTick(f.apply(...args));
6+
return uWS;
47
} catch (e) {
58
throw new Error('This version of µWS is not compatible with your Node.js build.');
69
}

‎examples/HelloWorld.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ const uWS = require('../dist/uws.js');
22

33
const port = 3000;
44

5+
const world = new (require('util').TextEncoder)().encode('World!');
6+
57
uWS.App().get('/hello', (res, req) => {
6-
res.end('world!');
8+
res.end(world);
79
}).get('/*', (res, req) => {
810
res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!');
911
}).listen(port, (token) => {

‎src/addon.cpp‎

Lines changed: 113 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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>
57
using 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

5554
void 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

6766
void 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

7776
void 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

8786
void 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?
114113
void 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
129137
void 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

151159
void 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>
180214
NODE_MODULE(uWS, Main)

0 commit comments

Comments
 (0)