uWebSockets.js/src/addon.cpp

215 lines
8.4 KiB
C++
Raw Normal View History

2018-11-03 11:35:42 +00:00
/* We depend only on raw & vanilla libuv, V8 and OpenSSL.
* There is to be no dependencies on anything Node.js in here. */
2018-10-28 15:07:21 +00:00
#include <uv.h>
2018-11-03 11:35:42 +00:00
#include <v8.h>
2018-10-28 15:07:21 +00:00
#include <iostream>
using namespace v8;
class NativeString {
char *data;
size_t length;
char utf8ValueMemory[sizeof(String::Utf8Value)];
String::Utf8Value *utf8Value = nullptr;
public:
NativeString(const Local<Value> &value) {
if (value->IsUndefined()) {
data = nullptr;
length = 0;
} else if (value->IsString()) {
utf8Value = new (utf8ValueMemory) String::Utf8Value(value);
data = (**utf8Value);
length = utf8Value->length();
} else if (value->IsTypedArray()) {
Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast(value);
ArrayBuffer::Contents contents = arrayBufferView->Buffer()->GetContents();
length = contents.ByteLength();
data = (char *) contents.Data();
} else if (value->IsArrayBuffer()) {
Local<ArrayBuffer> arrayBuffer = Local<ArrayBuffer>::Cast(value);
ArrayBuffer::Contents contents = arrayBuffer->GetContents();
length = contents.ByteLength();
data = (char *) contents.Data();
} else {
static char empty[] = "";
data = empty;
length = 0;
}
}
char *getData() {return data;}
size_t getLength() {return length;}
~NativeString() {
if (utf8Value) {
utf8Value->~Utf8Value();
}
}
};
#include "App.h"
Isolate *isolate;
Persistent<Object> resTemplate;
Persistent<Object> reqTemplate;
void res_end(const FunctionCallbackInfo<Value> &args) {
2018-11-03 11:35:42 +00:00
// 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?
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
NativeString data(args[0]);
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->end(std::string_view(data.getData(), data.getLength()));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
// Return this
args.GetReturnValue().Set(args.Holder());
2018-10-28 15:07:21 +00:00
}
void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
2018-11-03 11:35:42 +00:00
// get string
NativeString header(args[0]);
NativeString value(args[1]);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), std::string_view(value.getData(), value.getLength()));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
args.GetReturnValue().Set(args.Holder());
2018-10-28 15:07:21 +00:00
}
void req_getHeader(const FunctionCallbackInfo<Value> &args) {
2018-11-03 11:35:42 +00:00
// get string
NativeString data(args[0]);
char *buf = data.getData(); int length = data.getLength();
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
std::string_view header = ((uWS::HttpRequest *) args.Holder()->GetAlignedPointerFromInternalField(0))->getHeader(std::string_view(buf, length));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length()));
2018-10-28 15:07:21 +00:00
}
void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
2018-11-03 11:35:42 +00:00
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
NativeString nativeString(args[0]);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
Persistent<Function> *pf = new Persistent<Function>();
pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1]));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
//Persistent<Function, CopyablePersistentTraits<Function>> p(isolate, Local<Function>::Cast(args[1]));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
app->get(std::string(nativeString.getData(), nativeString.getLength()), [pf](auto *res, auto *req) {
HandleScope hs(isolate);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
Local<Object> resObject = Local<Object>::New(isolate, resTemplate)->Clone();
resObject->SetAlignedPointerInInternalField(0, res);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
Local<Object> reqObject = Local<Object>::New(isolate, reqTemplate)->Clone();
reqObject->SetAlignedPointerInInternalField(0, req);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
Local<Value> argv[] = {resObject, reqObject};
Local<Function>::New(isolate, *pf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
});
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
args.GetReturnValue().Set(args.Holder());
2018-10-28 15:07:21 +00:00
}
// port, callback?
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
2018-11-03 11:35:42 +00:00
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
int port = args[0]->Uint32Value();
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
app->listen(port, [&args](auto *token) {
Local<Value> argv[] = {Boolean::New(isolate, token)};
Local<Function>::Cast(args[1])->Call(isolate->GetCurrentContext()->Global(), 1, argv);
});
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
// Return this
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])));
2018-10-28 15:07:21 +00:00
}
// uWS.App() -> object
void uWS_App(const FunctionCallbackInfo<Value> &args) {
2018-11-03 11:35:42 +00:00
// uWS.App
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App"));
appTemplate->InstanceTemplate()->SetInternalFieldCount(1);
// Get
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
// Listen
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
// Instantiate and set intenal pointer
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
// Delete this boy
localApp->SetAlignedPointerInInternalField(0, new uWS::App());
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
// Return an instance of this shit
args.GetReturnValue().Set(localApp);
2018-10-28 15:07:21 +00:00
}
void Main(Local<Object> exports) {
2018-11-03 11:35:42 +00:00
isolate = exports->GetIsolate();
/* uWS.nextTick is executed in uv_check_t */
uv_loop_t *loop = uv_default_loop();
uv_check_init(loop, &check);
check.data = isolate;
uv_check_start(&check, [](uv_check_t *check) {
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
if (nextTickQueue.size()) {
Isolate *isolate = (Isolate *) check->data;
HandleScope hs(isolate);
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
for (Persistent<Function, CopyablePersistentTraits<Function>> &f : nextTickQueue) {
Local<Function>::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr);
f.Reset();
}
nextTickQueue.clear();
}
});
uv_unref((uv_handle_t *) &check);
/*reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "url"), Request::url);
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());
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
// 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));
2018-10-28 15:07:21 +00:00
2018-11-03 11:35:42 +00:00
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);
2018-10-28 15:07:21 +00:00
}
2018-11-03 11:35:42 +00:00
/* 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>
2018-10-28 15:07:21 +00:00
NODE_MODULE(uWS, Main)