Node.js 11, uWS.nextTick, purely V8
This commit is contained in:
parent
100c93ffe4
commit
9e1b05608e
6
Makefile
6
Makefile
@ -1,6 +1,6 @@
|
|||||||
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
|
C_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -I ../uWebSockets/uSockets/src ../uWebSockets/uSockets/src/*.c ../uWebSockets/uSockets/src/eventing/*.c
|
||||||
|
|
||||||
CPP_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I ../v0.15/uSockets/src -I ../v0.15/src src/addon.cpp
|
CPP_SHARED := -DLIBUS_USE_LIBUV -flto -O3 -c -fPIC -std=c++17 -I ../uWebSockets/uSockets/src -I ../uWebSockets/src src/addon.cpp
|
||||||
|
|
||||||
CPP_OSX := -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup
|
CPP_OSX := -stdlib=libc++ -mmacosx-version-min=10.7 -undefined dynamic_lookup
|
||||||
|
|
||||||
@ -9,12 +9,14 @@ default:
|
|||||||
NODE=targets/node-v8.1.2 ABI=57 make `(uname -s)`
|
NODE=targets/node-v8.1.2 ABI=57 make `(uname -s)`
|
||||||
NODE=targets/node-v9.2.0 ABI=59 make `(uname -s)`
|
NODE=targets/node-v9.2.0 ABI=59 make `(uname -s)`
|
||||||
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
|
NODE=targets/node-v10.0.0 ABI=64 make `(uname -s)`
|
||||||
|
NODE=targets/node-v11.1.0 ABI=67 make `(uname -s)`
|
||||||
for f in dist/*.node; do chmod +x $$f; done
|
for f in dist/*.node; do chmod +x $$f; done
|
||||||
targets:
|
targets:
|
||||||
mkdir targets
|
mkdir targets
|
||||||
curl https://nodejs.org/dist/v8.1.2/node-v8.1.2-headers.tar.gz | tar xz -C targets
|
curl https://nodejs.org/dist/v8.1.2/node-v8.1.2-headers.tar.gz | tar xz -C targets
|
||||||
curl https://nodejs.org/dist/v9.2.0/node-v9.2.0-headers.tar.gz | tar xz -C targets
|
curl https://nodejs.org/dist/v9.2.0/node-v9.2.0-headers.tar.gz | tar xz -C 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/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:
|
Linux:
|
||||||
gcc $(C_SHARED)
|
gcc $(C_SHARED)
|
||||||
g++ $(CPP_SHARED) -I $$NODE/include/node
|
g++ $(CPP_SHARED) -I $$NODE/include/node
|
||||||
|
5
dist/uws.js
vendored
5
dist/uws.js
vendored
@ -1,6 +1,9 @@
|
|||||||
module.exports = (() => {
|
module.exports = (() => {
|
||||||
try {
|
try {
|
||||||
return require(`./uws_${process.platform}_${process.versions.modules}.node`);
|
const uWS = require(`./uws_${process.platform}_${process.versions.modules}.node`);
|
||||||
|
/* We are not compatible with Node.js domain */
|
||||||
|
process.nextTick = (f, ...args) => uWS.nextTick(f.apply(...args));
|
||||||
|
return uWS;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error('This version of µWS is not compatible with your Node.js build.');
|
throw new Error('This version of µWS is not compatible with your Node.js build.');
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@ const uWS = require('../dist/uws.js');
|
|||||||
|
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
|
||||||
|
const world = new (require('util').TextEncoder)().encode('World!');
|
||||||
|
|
||||||
uWS.App().get('/hello', (res, req) => {
|
uWS.App().get('/hello', (res, req) => {
|
||||||
res.end('world!');
|
res.end(world);
|
||||||
}).get('/*', (res, req) => {
|
}).get('/*', (res, req) => {
|
||||||
res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!');
|
res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!');
|
||||||
}).listen(port, (token) => {
|
}).listen(port, (token) => {
|
||||||
|
190
src/addon.cpp
190
src/addon.cpp
@ -1,6 +1,8 @@
|
|||||||
|
/* We depend only on raw & vanilla libuv, V8 and OpenSSL.
|
||||||
|
* There is to be no dependencies on anything Node.js in here. */
|
||||||
#include <uv.h>
|
#include <uv.h>
|
||||||
#include <node.h>
|
#include <v8.h>
|
||||||
#include <node_buffer.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
|
|
||||||
@ -18,9 +20,6 @@ public:
|
|||||||
utf8Value = new (utf8ValueMemory) String::Utf8Value(value);
|
utf8Value = new (utf8ValueMemory) String::Utf8Value(value);
|
||||||
data = (**utf8Value);
|
data = (**utf8Value);
|
||||||
length = utf8Value->length();
|
length = utf8Value->length();
|
||||||
} else if (node::Buffer::HasInstance(value)) {
|
|
||||||
data = node::Buffer::Data(value);
|
|
||||||
length = node::Buffer::Length(value);
|
|
||||||
} else if (value->IsTypedArray()) {
|
} else if (value->IsTypedArray()) {
|
||||||
Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast(value);
|
Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast(value);
|
||||||
ArrayBuffer::Contents contents = arrayBufferView->Buffer()->GetContents();
|
ArrayBuffer::Contents contents = arrayBufferView->Buffer()->GetContents();
|
||||||
@ -54,127 +53,162 @@ Persistent<Object> reqTemplate;
|
|||||||
|
|
||||||
void res_end(const FunctionCallbackInfo<Value> &args) {
|
void res_end(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
|
||||||
// you might want to do extra work here to swap to tryEnd if passed a Buffer?
|
// you might want to do extra work here to swap to tryEnd if passed a Buffer?
|
||||||
// or always use tryEnd and simply grab the object as persistent?
|
// or always use tryEnd and simply grab the object as persistent?
|
||||||
|
|
||||||
NativeString data(args[0]);
|
NativeString data(args[0]);
|
||||||
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength()));
|
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength()));
|
||||||
|
|
||||||
// Return this
|
// Return this
|
||||||
args.GetReturnValue().Set(args.Holder());
|
args.GetReturnValue().Set(args.Holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
|
void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
|
||||||
// get string
|
// get string
|
||||||
NativeString header(args[0]);
|
NativeString header(args[0]);
|
||||||
NativeString value(args[1]);
|
NativeString value(args[1]);
|
||||||
|
|
||||||
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), std::string_view(value.getData(), value.getLength()));
|
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), std::string_view(value.getData(), value.getLength()));
|
||||||
|
|
||||||
args.GetReturnValue().Set(args.Holder());
|
args.GetReturnValue().Set(args.Holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
void req_getHeader(const FunctionCallbackInfo<Value> &args) {
|
void req_getHeader(const FunctionCallbackInfo<Value> &args) {
|
||||||
// get string
|
// get string
|
||||||
NativeString data(args[0]);
|
NativeString data(args[0]);
|
||||||
char *buf = data.getData(); int length = data.getLength();
|
char *buf = data.getData(); int length = data.getLength();
|
||||||
|
|
||||||
std::string_view header = ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0))->getHeader(std::string_view(buf, length));
|
std::string_view header = ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0))->getHeader(std::string_view(buf, length));
|
||||||
|
|
||||||
args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length()));
|
args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
|
void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
|
||||||
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
||||||
|
|
||||||
NativeString nativeString(args[0]);
|
NativeString nativeString(args[0]);
|
||||||
|
|
||||||
Persistent<Function> *pf = new Persistent<Function>();
|
Persistent<Function> *pf = new Persistent<Function>();
|
||||||
pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1]));
|
pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1]));
|
||||||
|
|
||||||
app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) {
|
//Persistent<Function, CopyablePersistentTraits<Function>> p(isolate, Local<Function>::Cast(args[1]));
|
||||||
HandleScope hs(isolate);
|
|
||||||
|
|
||||||
Local<Object> resObject = Local<Object>::New(isolate, resTemplate)->Clone();
|
app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) {
|
||||||
resObject->SetAlignedPointerInInternalField(0, res);
|
HandleScope hs(isolate);
|
||||||
|
|
||||||
Local<Object> reqObject = Local<Object>::New(isolate, reqTemplate)->Clone();
|
Local<Object> resObject = Local<Object>::New(isolate, resTemplate)->Clone();
|
||||||
reqObject->SetAlignedPointerInInternalField(0, req);
|
resObject->SetAlignedPointerInInternalField(0, res);
|
||||||
|
|
||||||
// node:: is horrible but
|
Local<Object> reqObject = Local<Object>::New(isolate, reqTemplate)->Clone();
|
||||||
Local<Value> argv[] = {resObject, reqObject};
|
reqObject->SetAlignedPointerInInternalField(0, req);
|
||||||
node::MakeCallback(isolate, isolate->GetCurrentContext()->Global(), Local<Function>::New(isolate, *pf), 2, argv);
|
|
||||||
|
|
||||||
});
|
Local<Value> argv[] = {resObject, reqObject};
|
||||||
|
Local<Function>::New(isolate, *pf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
||||||
|
});
|
||||||
|
|
||||||
args.GetReturnValue().Set(args.Holder());
|
args.GetReturnValue().Set(args.Holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
// port, callback?
|
// port, callback?
|
||||||
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
|
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
|
||||||
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
||||||
|
|
||||||
int port = args[0]->Uint32Value();
|
int port = args[0]->Uint32Value();
|
||||||
|
|
||||||
app->listen(port, [&args](auto *token) {
|
app->listen(port, [&args](auto *token) {
|
||||||
Local<Value> argv[] = {Boolean::New(isolate, token)};
|
Local<Value> argv[] = {Boolean::New(isolate, token)};
|
||||||
Local<Function>::Cast(args[1])->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
Local<Function>::Cast(args[1])->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return this
|
// Return this
|
||||||
args.GetReturnValue().Set(args.Holder());
|
args.GetReturnValue().Set(args.Holder());
|
||||||
|
}
|
||||||
|
|
||||||
|
uv_check_t check;
|
||||||
|
#include <vector>
|
||||||
|
std::vector<Persistent<Function, CopyablePersistentTraits<Function>>> nextTickQueue;
|
||||||
|
|
||||||
|
// we need to override process.nextTick to avoid horrible loss of performance by Node.js
|
||||||
|
void nextTick(const FunctionCallbackInfo<Value> &args) {
|
||||||
|
nextTickQueue.push_back(Persistent<Function, CopyablePersistentTraits<Function>>(isolate, Local<Function>::Cast(args[0])));
|
||||||
}
|
}
|
||||||
|
|
||||||
// uWS.App() -> object
|
// uWS.App() -> object
|
||||||
void uWS_App(const FunctionCallbackInfo<Value> &args) {
|
void uWS_App(const FunctionCallbackInfo<Value> &args) {
|
||||||
// uWS.App
|
// uWS.App
|
||||||
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
|
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
|
||||||
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App"));
|
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App"));
|
||||||
appTemplate->InstanceTemplate()->SetInternalFieldCount(1);
|
appTemplate->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
|
||||||
// Get
|
// Get
|
||||||
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get));
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get));
|
||||||
|
|
||||||
// Listen
|
// 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
|
// Instantiate and set intenal pointer
|
||||||
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||||
|
|
||||||
// Delete this boy
|
// Delete this boy
|
||||||
localApp->SetAlignedPointerInInternalField(0, new uWS::App());
|
localApp->SetAlignedPointerInInternalField(0, new uWS::App());
|
||||||
|
|
||||||
// Return an instance of this shit
|
// Return an instance of this shit
|
||||||
args.GetReturnValue().Set(localApp);
|
args.GetReturnValue().Set(localApp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Main(Local<Object> exports) {
|
void Main(Local<Object> exports) {
|
||||||
isolate = exports->GetIsolate();
|
|
||||||
|
|
||||||
/*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
|
isolate = exports->GetIsolate();
|
||||||
reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
|
|
||||||
|
|
||||||
// App is a function returning an object
|
/* uWS.nextTick is executed in uv_check_t */
|
||||||
NODE_SET_METHOD(exports, "App", uWS_App);
|
uv_loop_t *loop = uv_default_loop();
|
||||||
|
uv_check_init(loop, &check);
|
||||||
|
check.data = isolate;
|
||||||
|
uv_check_start(&check, [](uv_check_t *check) {
|
||||||
|
|
||||||
// HttpResponse template
|
if (nextTickQueue.size()) {
|
||||||
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
|
Isolate *isolate = (Isolate *) check->data;
|
||||||
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse"));
|
HandleScope hs(isolate);
|
||||||
resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
|
||||||
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, res_end));
|
|
||||||
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader));
|
|
||||||
|
|
||||||
Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
for (Persistent<Function, CopyablePersistentTraits<Function>> &f : nextTickQueue) {
|
||||||
resTemplate.Reset(isolate, resObjectLocal);
|
Local<Function>::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr);
|
||||||
|
f.Reset();
|
||||||
|
}
|
||||||
|
|
||||||
// Request template
|
nextTickQueue.clear();
|
||||||
Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate);
|
}
|
||||||
reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest"));
|
});
|
||||||
reqTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
uv_unref((uv_handle_t *) &check);
|
||||||
reqTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getHeader"), FunctionTemplate::New(isolate, req_getHeader));
|
|
||||||
|
|
||||||
Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
/*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
|
||||||
reqTemplate.Reset(isolate, reqObjectLocal);
|
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, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction());
|
||||||
|
|
||||||
|
|
||||||
|
// HttpResponse template
|
||||||
|
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
|
||||||
|
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse"));
|
||||||
|
resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "end"), FunctionTemplate::New(isolate, res_end));
|
||||||
|
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "writeHeader"), FunctionTemplate::New(isolate, res_writeHeader));
|
||||||
|
|
||||||
|
Local<Object> resObjectLocal = resTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||||
|
resTemplate.Reset(isolate, resObjectLocal);
|
||||||
|
|
||||||
|
// Request template (do we need to clone this?)
|
||||||
|
Local<FunctionTemplate> reqTemplateLocal = FunctionTemplate::New(isolate);
|
||||||
|
reqTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpRequest"));
|
||||||
|
reqTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
reqTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getHeader"), FunctionTemplate::New(isolate, req_getHeader));
|
||||||
|
|
||||||
|
Local<Object> reqObjectLocal = reqTemplateLocal->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
||||||
|
reqTemplate.Reset(isolate, reqObjectLocal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* This is the only part where we are allowed/forced to add Node.js specific code.
|
||||||
|
* We do this because we are forced to, and we need a node module version added */
|
||||||
|
#include <node.h>
|
||||||
NODE_MODULE(uWS, Main)
|
NODE_MODULE(uWS, Main)
|
||||||
|
Loading…
Reference in New Issue
Block a user