uWebSockets.js/src/addon.cpp

118 lines
5.2 KiB
C++
Raw Normal View History

2018-11-15 15:01:49 +00:00
/*
2019-01-11 22:31:36 +00:00
* Authored by Alex Hultman, 2018-2019.
* Intellectual property of third-party.
2018-11-15 15:01:49 +00:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2018-11-04 09:56:51 +00:00
/* We are only allowed to depend on µWS and V8 in this layer. */
2018-11-04 07:55:35 +00:00
#include "App.h"
2018-11-03 11:35:42 +00:00
#include <v8.h>
2018-10-28 15:07:21 +00:00
#include <iostream>
2018-11-04 09:56:51 +00:00
#include <vector>
2018-12-26 12:50:12 +00:00
#include <type_traits>
2019-01-14 10:34:20 +00:00
using namespace v8;
2018-10-28 15:07:21 +00:00
2019-01-14 10:34:20 +00:00
/* These two are definitely static */
Isolate *isolate;
2019-03-02 03:38:01 +00:00
bool valid = true;
/* We hold all apps until free */
std::vector<std::unique_ptr<uWS::App>> apps;
std::vector<std::unique_ptr<uWS::SSLApp>> sslApps;
2018-10-28 15:07:21 +00:00
2019-11-08 12:49:40 +00:00
/* Compatibility for V8 7.0 and earlier */
#include <v8-version.h>
bool BooleanValue(Isolate *isolate, Local<Value> value) {
#if V8_MAJOR_VERSION < 7 || (V8_MAJOR_VERSION == 7 && V8_MINOR_VERSION == 0)
/* Old */
return value->BooleanValue(isolate->GetCurrentContext()).ToChecked();
#else
/* Node.js 12, 13 */
return value->BooleanValue(isolate);
#endif
}
2019-01-14 10:34:20 +00:00
#include "Utilities.h"
#include "WebSocketWrapper.h"
#include "HttpResponseWrapper.h"
#include "HttpRequestWrapper.h"
#include "AppWrapper.h"
2018-10-28 15:07:21 +00:00
2019-03-02 03:38:01 +00:00
/* Todo: Apps should be freed once the GC says so BUT ALWAYS before freeing the loop */
/* This has to be called in beforeExit, but exit also seems okay */
void uWS_free(const FunctionCallbackInfo<Value> &args) {
if (valid) {
/* Freeing apps here, it could be done earlier but not sooner */
apps.clear();
sslApps.clear();
/* Freeing the loop here means we give time for our timers to close, etc */
uWS::Loop::get()->free();
valid = false;
}
}
/* todo: Put this function and all inits of it in its own header */
void uWS_us_listen_socket_close(const FunctionCallbackInfo<Value> &args) {
2019-06-12 10:50:05 +00:00
// this should take int ssl first
us_listen_socket_close(0, (struct us_listen_socket_t *) External::Cast(*args[0])->Value());
}
2019-06-12 10:50:05 +00:00
#include <uv.h>
2018-10-28 15:07:21 +00:00
void Main(Local<Object> exports) {
2019-01-14 10:34:20 +00:00
/* I guess we store this statically */
2018-11-03 11:35:42 +00:00
isolate = exports->GetIsolate();
2019-03-01 18:47:27 +00:00
/* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */
2019-03-12 11:16:54 +00:00
// todo: setting this might be crashing nodejs?
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kAuto);
2019-03-01 18:04:13 +00:00
2019-03-02 03:38:01 +00:00
/* Integrate with existing libuv loop, we just pass a boolean basically */
2019-06-12 10:50:05 +00:00
uWS::Loop::get(uv_default_loop());
2018-12-26 18:19:26 +00:00
2018-11-03 11:35:42 +00:00
/* uWS namespace */
2019-11-08 12:49:40 +00:00
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "App", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::App>)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SSLApp", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "free", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_free)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
2018-10-28 15:07:21 +00:00
/* Expose some µSockets functions directly under uWS namespace */
2019-11-08 12:49:40 +00:00
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "us_listen_socket_close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
2019-01-14 10:34:20 +00:00
/* Compression enum */
2019-11-08 12:49:40 +00:00
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DISABLED", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DISABLED)).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SHARED_COMPRESSOR", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::SHARED_COMPRESSOR)).ToChecked();
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR)).ToChecked();
/* Listen options */
2019-11-08 12:49:40 +00:00
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "LIBUS_LISTEN_EXCLUSIVE_PORT", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, LIBUS_LISTEN_EXCLUSIVE_PORT)).ToChecked();
2018-12-26 14:23:14 +00:00
/* The template for websockets */
2019-01-14 10:54:37 +00:00
WebSocketWrapper::initWsTemplate<0>();
WebSocketWrapper::initWsTemplate<1>();
2018-12-26 14:23:14 +00:00
2019-01-08 21:04:56 +00:00
/* Initialize SSL and non-SSL templates */
2019-01-14 12:17:08 +00:00
HttpResponseWrapper::initResTemplate<0>();
HttpResponseWrapper::initResTemplate<1>();
2018-11-03 11:35:42 +00:00
2019-01-14 10:34:20 +00:00
/* Init a shared request object */
2019-01-14 11:44:29 +00:00
HttpRequestWrapper::initReqTemplate();
2018-10-28 15:07:21 +00:00
}
2018-11-04 09:56:51 +00:00
/* This is required when building as a Node.js addon */
2018-11-04 07:55:35 +00:00
#ifndef ADDON_IS_HOST
2018-11-03 11:35:42 +00:00
#include <node.h>
2018-10-28 15:07:21 +00:00
NODE_MODULE(uWS, Main)
2018-11-04 07:55:35 +00:00
#endif