2019-01-17 05:53:30 +00:00
|
|
|
#include "App.h"
|
2019-01-14 10:45:25 +00:00
|
|
|
#include <v8.h>
|
|
|
|
#include "Utilities.h"
|
|
|
|
using namespace v8;
|
2019-01-14 10:34:20 +00:00
|
|
|
|
2019-01-18 15:57:54 +00:00
|
|
|
/* uWS.App.ws('/pattern', behavior) */
|
2019-01-14 10:34:20 +00:00
|
|
|
template <typename APP>
|
|
|
|
void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
2019-01-16 20:36:35 +00:00
|
|
|
typename APP::WebSocketBehavior behavior = {};
|
2019-01-14 10:34:20 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
NativeString pattern(args.GetIsolate(), args[0]);
|
|
|
|
if (pattern.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 10:34:20 +00:00
|
|
|
|
2019-01-18 15:57:54 +00:00
|
|
|
/* We don't need to care for these just yet, since we do
|
|
|
|
* not have a way to free the app itself */
|
2019-01-14 10:34:20 +00:00
|
|
|
Persistent<Function> *openPf = new Persistent<Function>();
|
|
|
|
Persistent<Function> *messagePf = new Persistent<Function>();
|
|
|
|
Persistent<Function> *drainPf = new Persistent<Function>();
|
|
|
|
Persistent<Function> *closePf = new Persistent<Function>();
|
|
|
|
|
|
|
|
struct PerSocketData {
|
|
|
|
Persistent<Object> *socketPf;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Get the behavior object */
|
|
|
|
if (args.Length() == 2) {
|
|
|
|
Local<Object> behaviorObject = Local<Object>::Cast(args[1]);
|
|
|
|
|
|
|
|
/* maxPayloadLength */
|
2019-01-16 20:31:17 +00:00
|
|
|
behavior.maxPayloadLength = behaviorObject->Get(String::NewFromUtf8(isolate, "maxPayloadLength"))->Int32Value();
|
|
|
|
|
|
|
|
/* Compression, map from 0, 1, 2 to disabled, shared, dedicated */
|
|
|
|
int compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
|
|
|
|
if (compression == 0) {
|
|
|
|
behavior.compression = uWS::CompressOptions::DISABLED;
|
|
|
|
} else if (compression == 1) {
|
|
|
|
behavior.compression = uWS::CompressOptions::SHARED_COMPRESSOR;
|
|
|
|
} else if (compression == 2) {
|
|
|
|
behavior.compression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
|
|
|
|
}
|
2019-01-14 10:34:20 +00:00
|
|
|
|
|
|
|
/* Open */
|
|
|
|
openPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
|
|
|
|
/* Message */
|
|
|
|
messagePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "message"))));
|
|
|
|
/* Drain */
|
|
|
|
drainPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "drain"))));
|
|
|
|
/* Close */
|
|
|
|
closePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
|
|
|
|
}
|
|
|
|
|
2019-01-16 20:31:17 +00:00
|
|
|
behavior.open = [openPf](auto *ws, auto *req) {
|
|
|
|
HandleScope hs(isolate);
|
2019-01-14 10:34:20 +00:00
|
|
|
|
2019-01-16 20:31:17 +00:00
|
|
|
/* Create a new websocket object */
|
|
|
|
Local<Object> wsObject = WebSocketWrapper::getWsInstance<APP>();
|
|
|
|
wsObject->SetAlignedPointerInInternalField(0, ws);
|
|
|
|
|
2019-01-17 06:33:31 +00:00
|
|
|
/* Create the HttpRequest wrapper */
|
|
|
|
Local<Object> reqObject = HttpRequestWrapper::getReqInstance();
|
|
|
|
reqObject->SetAlignedPointerInInternalField(0, req);
|
|
|
|
|
2019-01-16 20:31:17 +00:00
|
|
|
/* Attach a new V8 object with pointer to us, to us */
|
|
|
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
|
|
|
perSocketData->socketPf = new Persistent<Object>;
|
|
|
|
perSocketData->socketPf->Reset(isolate, wsObject);
|
|
|
|
|
2019-01-17 06:33:31 +00:00
|
|
|
Local<Value> argv[] = {wsObject, reqObject};
|
|
|
|
Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
2019-01-16 20:31:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
behavior.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
|
|
|
|
HandleScope hs(isolate);
|
|
|
|
|
|
|
|
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
|
|
|
|
|
|
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
|
|
|
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
|
2019-01-18 15:57:54 +00:00
|
|
|
messageArrayBuffer,
|
|
|
|
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)};
|
2019-01-16 20:31:17 +00:00
|
|
|
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
|
|
|
|
|
|
|
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
|
|
|
|
messageArrayBuffer->Neuter();
|
|
|
|
};
|
|
|
|
|
|
|
|
behavior.drain = [drainPf](auto *ws) {
|
|
|
|
HandleScope hs(isolate);
|
|
|
|
|
|
|
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
|
|
|
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
|
|
|
|
};
|
|
|
|
Local<Function>::New(isolate, *drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
|
|
|
};
|
|
|
|
|
|
|
|
behavior.ping = [](auto *ws) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
behavior.pong = [](auto *ws) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
behavior.close = [closePf](auto *ws, int code, std::string_view message) {
|
|
|
|
HandleScope hs(isolate);
|
|
|
|
|
|
|
|
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
|
|
|
|
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
|
2019-01-18 15:57:54 +00:00
|
|
|
Local<Object> wsObject = Local<Object>::New(isolate, *(perSocketData->socketPf));
|
|
|
|
Local<Value> argv[3] = {wsObject,
|
2019-01-16 20:31:17 +00:00
|
|
|
Integer::New(isolate, code),
|
2019-01-18 15:57:54 +00:00
|
|
|
messageArrayBuffer};
|
|
|
|
|
|
|
|
/* Invalidate this wsObject */
|
|
|
|
wsObject->SetAlignedPointerInInternalField(0, nullptr);
|
2019-01-16 20:31:17 +00:00
|
|
|
Local<Function>::New(isolate, *closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
|
|
|
|
|
2019-01-18 15:57:54 +00:00
|
|
|
delete perSocketData->socketPf;
|
|
|
|
|
2019-01-16 20:31:17 +00:00
|
|
|
/* Again, here we clear the buffer to avoid strange bugs */
|
|
|
|
messageArrayBuffer->Neuter();
|
|
|
|
};
|
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
app->template ws<PerSocketData>(std::string(pattern.getString()), std::move(behavior));
|
2019-01-14 10:34:20 +00:00
|
|
|
|
|
|
|
/* Return this */
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
|
|
|
|
2019-01-16 21:59:53 +00:00
|
|
|
/* This method wraps get, post and all http methods */
|
|
|
|
template <typename APP, typename F>
|
|
|
|
void uWS_App_get(F f, const FunctionCallbackInfo<Value> &args) {
|
2019-01-14 10:34:20 +00:00
|
|
|
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
NativeString pattern(args.GetIsolate(), args[0]);
|
|
|
|
if (pattern.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 10:34:20 +00:00
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
/* todo: make it UniquePersistent */
|
|
|
|
std::shared_ptr<Persistent<Function>> pf(new Persistent<Function>);
|
2019-01-14 10:34:20 +00:00
|
|
|
pf->Reset(args.GetIsolate(), Local<Function>::Cast(args[1]));
|
|
|
|
|
2019-01-18 15:33:56 +00:00
|
|
|
(app->*f)(std::string(pattern.getString()), [pf](auto *res, auto *req) {
|
2019-01-14 10:34:20 +00:00
|
|
|
HandleScope hs(isolate);
|
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
Local<Object> resObject = HttpResponseWrapper::getResInstance<APP>();
|
2019-01-14 10:34:20 +00:00
|
|
|
resObject->SetAlignedPointerInInternalField(0, res);
|
|
|
|
|
2019-01-14 12:17:08 +00:00
|
|
|
Local<Object> reqObject = HttpRequestWrapper::getReqInstance();
|
2019-01-14 10:34:20 +00:00
|
|
|
reqObject->SetAlignedPointerInInternalField(0, req);
|
|
|
|
|
|
|
|
Local<Value> argv[] = {resObject, reqObject};
|
|
|
|
Local<Function>::New(isolate, *pf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
|
2019-01-18 15:33:56 +00:00
|
|
|
|
|
|
|
/* Properly invalidate req */
|
|
|
|
reqObject->SetAlignedPointerInInternalField(0, nullptr);
|
|
|
|
|
|
|
|
/* µWS itself will terminate if not responded and not attached
|
|
|
|
* onAborted handler, so we can assume it's done */
|
2019-01-14 10:34:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename APP>
|
|
|
|
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
|
|
|
|
|
|
|
|
int port = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
|
|
|
|
|
|
|
|
app->listen(port, [&args](auto *token) {
|
2019-01-18 08:07:21 +00:00
|
|
|
Local<Value> argv[] = {External::New(isolate, token)};
|
2019-01-14 10:34:20 +00:00
|
|
|
Local<Function>::Cast(args[1])->Call(isolate->GetCurrentContext()->Global(), 1, argv);
|
|
|
|
});
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(args.Holder());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename APP>
|
|
|
|
void uWS_App(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
|
|
|
|
|
|
|
|
APP *app;
|
|
|
|
|
|
|
|
/* Name differs based on type */
|
|
|
|
if (std::is_same<APP, uWS::SSLApp>::value) {
|
|
|
|
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLApp"));
|
|
|
|
|
|
|
|
/* We fill these below */
|
|
|
|
us_ssl_socket_context_options ssl_options = {};
|
|
|
|
|
|
|
|
static std::string keyFileName;
|
|
|
|
static std::string certFileName;
|
|
|
|
static std::string passphrase;
|
2019-01-18 14:56:08 +00:00
|
|
|
static std::string dhParamsFileName;
|
2019-01-14 10:34:20 +00:00
|
|
|
|
|
|
|
/* Read the options object (SSL options) */
|
|
|
|
if (args.Length() == 1) {
|
|
|
|
/* Key file name */
|
|
|
|
NativeString keyFileNameValue(isolate, Local<Object>::Cast(args[0])->Get(String::NewFromUtf8(isolate, "key_file_name")));
|
2019-01-18 15:33:56 +00:00
|
|
|
if (keyFileNameValue.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (keyFileNameValue.getString().length()) {
|
|
|
|
keyFileName.append(keyFileNameValue.getString());
|
2019-01-14 10:34:20 +00:00
|
|
|
ssl_options.key_file_name = keyFileName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cert file name */
|
|
|
|
NativeString certFileNameValue(isolate, Local<Object>::Cast(args[0])->Get(String::NewFromUtf8(isolate, "cert_file_name")));
|
2019-01-18 15:33:56 +00:00
|
|
|
if (certFileNameValue.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (certFileNameValue.getString().length()) {
|
|
|
|
certFileName.append(certFileNameValue.getString());
|
2019-01-14 10:34:20 +00:00
|
|
|
ssl_options.cert_file_name = certFileName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Passphrase */
|
|
|
|
NativeString passphraseValue(isolate, Local<Object>::Cast(args[0])->Get(String::NewFromUtf8(isolate, "passphrase")));
|
2019-01-18 15:33:56 +00:00
|
|
|
if (passphraseValue.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (passphraseValue.getString().length()) {
|
|
|
|
passphrase.append(passphraseValue.getString());
|
2019-01-14 10:34:20 +00:00
|
|
|
ssl_options.passphrase = passphrase.c_str();
|
|
|
|
}
|
2019-01-18 14:56:08 +00:00
|
|
|
|
|
|
|
/* DH params file name */
|
|
|
|
NativeString dhParamsFileNameValue(isolate, Local<Object>::Cast(args[0])->Get(String::NewFromUtf8(isolate, "dh_params_file_name")));
|
2019-01-18 15:33:56 +00:00
|
|
|
if (dhParamsFileNameValue.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dhParamsFileNameValue.getString().length()) {
|
|
|
|
dhParamsFileName.append(dhParamsFileNameValue.getString());
|
2019-01-18 14:56:08 +00:00
|
|
|
ssl_options.dh_params_file_name = dhParamsFileName.c_str();
|
|
|
|
}
|
2019-01-14 10:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app = new APP(ssl_options);
|
|
|
|
} else {
|
|
|
|
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App"));
|
|
|
|
app = new APP;
|
|
|
|
}
|
|
|
|
|
|
|
|
appTemplate->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
|
2019-01-18 14:56:08 +00:00
|
|
|
/* All the http methods */
|
2019-01-16 21:59:53 +00:00
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::get, args);
|
|
|
|
}));
|
2019-01-14 10:34:20 +00:00
|
|
|
|
2019-01-16 21:59:53 +00:00
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "post"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::post, args);
|
|
|
|
}));
|
2019-01-14 10:34:20 +00:00
|
|
|
|
2019-01-18 14:56:08 +00:00
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "options"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::options, args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "del"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::del, args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "patch"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::patch, args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "put"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::put, args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "head"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::head, args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "connect"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::connect, args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "trace"), FunctionTemplate::New(isolate, [](auto &args) {
|
|
|
|
uWS_App_get<APP>(&APP::trace, args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
/* What about unhandled? */
|
|
|
|
|
2019-01-16 21:59:53 +00:00
|
|
|
/* ws, listen */
|
2019-01-14 10:34:20 +00:00
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws<APP>));
|
|
|
|
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen<APP>));
|
|
|
|
|
|
|
|
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
|
|
|
|
localApp->SetAlignedPointerInInternalField(0, app);
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(localApp);
|
|
|
|
}
|