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"
|
2019-12-05 03:07:18 +00:00
|
|
|
|
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>
|
2018-10-28 15:07:21 +00:00
|
|
|
|
2020-02-23 15:19:58 +00:00
|
|
|
/* This one can never change for the duration of this process, so never mind per context data:ing it, yes that is a word now */
|
|
|
|
bool experimental_fastcall = 0;
|
|
|
|
|
2019-12-05 03:07:18 +00:00
|
|
|
#include <v8.h>
|
|
|
|
using namespace v8;
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:57:38 +00:00
|
|
|
void NeuterArrayBuffer(Local<ArrayBuffer> ab) {
|
|
|
|
#if V8_MAJOR_VERSION < 7 || (V8_MAJOR_VERSION == 7 && V8_MINOR_VERSION == 0)
|
|
|
|
/* Old */
|
|
|
|
ab->Neuter();
|
|
|
|
#else
|
|
|
|
/* Node.js 12, 13 */
|
|
|
|
ab->Detach();
|
|
|
|
#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) {
|
2019-12-21 18:38:02 +00:00
|
|
|
/* Holder is exports */
|
|
|
|
Local<Object> exports = args.Holder();
|
2019-12-05 03:07:18 +00:00
|
|
|
|
2019-12-21 18:38:02 +00:00
|
|
|
/* See if we even have free anymore */
|
2019-12-21 21:59:25 +00:00
|
|
|
if (exports->Get(args.GetIsolate()->GetCurrentContext(), String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked() == Undefined(args.GetIsolate())) {
|
2019-12-21 18:38:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-12-05 03:07:18 +00:00
|
|
|
|
2019-12-21 18:38:02 +00:00
|
|
|
/* Once we free uWS we remove the uWS.free function from exports */
|
2019-12-21 21:59:25 +00:00
|
|
|
exports->Set(args.GetIsolate()->GetCurrentContext(), String::NewFromUtf8(args.GetIsolate(), "free", NewStringType::kNormal).ToLocalChecked(), Undefined(args.GetIsolate())).ToChecked();
|
2019-12-05 03:07:18 +00:00
|
|
|
|
2019-12-21 18:38:02 +00:00
|
|
|
/* We get the External holding perContextData */
|
|
|
|
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
|
2019-12-05 03:07:18 +00:00
|
|
|
|
2019-12-21 18:38:02 +00:00
|
|
|
/* Freeing apps here, it could be done earlier but not sooner */
|
|
|
|
perContextData->apps.clear();
|
|
|
|
perContextData->sslApps.clear();
|
|
|
|
/* Freeing the loop here means we give time for our timers to close, etc */
|
|
|
|
uWS::Loop::get()->free();
|
2019-12-05 03:07:18 +00:00
|
|
|
|
2019-12-21 18:38:02 +00:00
|
|
|
/* We can safely delete this since we no longer can call uWS.free */
|
|
|
|
delete perContextData;
|
2019-01-21 08:09:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 08:07:21 +00:00
|
|
|
/* 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-01-18 08:07:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 08:34:42 +00:00
|
|
|
/* Temporary KV store (doesn't belong here) */
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
|
|
|
#include <mutex>
|
|
|
|
|
2020-07-29 09:09:14 +00:00
|
|
|
std::unordered_map<std::string, std::string> kvStoreString;
|
|
|
|
std::unordered_map<std::string, uint32_t> kvStoreInteger;
|
2020-07-29 08:34:42 +00:00
|
|
|
std::mutex kvMutex;
|
|
|
|
|
2020-07-29 09:09:14 +00:00
|
|
|
void uWS_getString(const FunctionCallbackInfo<Value> &args) {
|
2020-07-29 08:34:42 +00:00
|
|
|
NativeString key(args.GetIsolate(), args[0]);
|
|
|
|
if (key.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:09:14 +00:00
|
|
|
std::string value = kvStoreString[std::string(key.getString())];
|
2020-07-29 08:34:42 +00:00
|
|
|
|
|
|
|
args.GetReturnValue().Set(String::NewFromUtf8(args.GetIsolate(), value.data(), NewStringType::kNormal, value.length()).ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:09:14 +00:00
|
|
|
void uWS_setString(const FunctionCallbackInfo<Value> &args) {
|
2020-07-29 08:34:42 +00:00
|
|
|
NativeString key(args.GetIsolate(), args[0]);
|
|
|
|
if (key.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NativeString value(args.GetIsolate(), args[1]);
|
|
|
|
if (value.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:09:14 +00:00
|
|
|
kvStoreString[std::string(key.getString())] = value.getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void uWS_getInteger(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
NativeString key(args.GetIsolate(), args[0]);
|
|
|
|
if (key.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t value = kvStoreInteger[std::string(key.getString())];
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(Integer::New(args.GetIsolate(), value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void uWS_setInteger(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
NativeString key(args.GetIsolate(), args[0]);
|
|
|
|
if (key.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t value = Local<Integer>::Cast(args[1])->Value();
|
|
|
|
|
|
|
|
kvStoreInteger[std::string(key.getString())] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uWS_incInteger(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
NativeString key(args.GetIsolate(), args[0]);
|
|
|
|
if (key.isInvalid(args)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t change = Local<Integer>::Cast(args[1])->Value();
|
|
|
|
|
|
|
|
uint32_t value = kvStoreInteger[std::string(key.getString())] += change;
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(Integer::New(args.GetIsolate(), value));
|
2020-07-29 08:34:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:12:59 +00:00
|
|
|
/* This one will spike memory usage for large stores */
|
|
|
|
void uWS_getStringKeys(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
|
|
|
|
Local<Array> stringKeys = Array::New(args.GetIsolate(), kvStoreString.size());
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
for (auto p : kvStoreString) {
|
|
|
|
stringKeys->Set(args.GetIsolate()->GetCurrentContext(), offset++, String::NewFromUtf8(args.GetIsolate(), p.first.data(), NewStringType::kNormal, p.first.length()).ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(stringKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uWS_getIntegerKeys(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
|
|
|
|
Local<Array> integerKeys = Array::New(args.GetIsolate(), kvStoreInteger.size());
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
for (auto p : kvStoreInteger) {
|
|
|
|
integerKeys->Set(args.GetIsolate()->GetCurrentContext(), offset++, String::NewFromUtf8(args.GetIsolate(), p.first.data(), NewStringType::kNormal, p.first.length()).ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
args.GetReturnValue().Set(integerKeys);
|
|
|
|
}
|
|
|
|
|
2020-07-29 08:34:42 +00:00
|
|
|
void uWS_lock(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
kvMutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void uWS_unlock(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
kvMutex.unlock();
|
|
|
|
}
|
|
|
|
|
2018-10-28 15:07:21 +00:00
|
|
|
void Main(Local<Object> exports) {
|
2019-12-05 03:07:18 +00:00
|
|
|
|
2020-02-23 15:19:58 +00:00
|
|
|
/* We only care if it is defined, not what it says */
|
|
|
|
experimental_fastcall = getenv("EXPERIMENTAL_FASTCALL") != nullptr;
|
|
|
|
|
2019-12-05 03:07:18 +00:00
|
|
|
/* We pass isolate everywhere */
|
|
|
|
Isolate *isolate = exports->GetIsolate();
|
2018-11-03 11:35:42 +00:00
|
|
|
|
2020-02-23 15:19:58 +00:00
|
|
|
if (experimental_fastcall) {
|
|
|
|
/* We want this so that we can redefine process.nextTick to using the V8 native microtask queue */
|
|
|
|
/* Settings this crashes Node.js while debugging with breakpoints */
|
|
|
|
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kAuto);
|
|
|
|
}
|
2019-03-01 18:04:13 +00:00
|
|
|
|
2019-12-05 03:07:18 +00:00
|
|
|
/* Init the template objects, SSL and non-SSL, store it in per context data */
|
|
|
|
PerContextData *perContextData = new PerContextData;
|
|
|
|
perContextData->isolate = isolate;
|
|
|
|
perContextData->reqTemplate.Reset(isolate, HttpRequestWrapper::init(isolate));
|
|
|
|
perContextData->resTemplate[0].Reset(isolate, HttpResponseWrapper::init<0>(isolate));
|
|
|
|
perContextData->resTemplate[1].Reset(isolate, HttpResponseWrapper::init<1>(isolate));
|
|
|
|
perContextData->wsTemplate[0].Reset(isolate, WebSocketWrapper::init<0>(isolate));
|
|
|
|
perContextData->wsTemplate[1].Reset(isolate, WebSocketWrapper::init<1>(isolate));
|
|
|
|
|
|
|
|
/* Refer to per context data via External */
|
|
|
|
Local<External> externalPerContextData = External::New(isolate, perContextData);
|
2018-12-26 18:19:26 +00:00
|
|
|
|
2018-11-03 11:35:42 +00:00
|
|
|
/* uWS namespace */
|
2019-12-05 03:07:18 +00:00
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "App", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::App>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "SSLApp", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "free", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_free, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
2018-10-28 15:07:21 +00:00
|
|
|
|
2020-07-29 08:34:42 +00:00
|
|
|
/* Temporary KV store */
|
2020-07-29 09:09:14 +00:00
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getString", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getString)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "setString", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_setString)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getInteger", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getInteger)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "setInteger", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_setInteger)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "incInteger", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_incInteger)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
2020-07-29 08:34:42 +00:00
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "lock", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_lock)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "unlock", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_unlock)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
2020-07-30 11:12:59 +00:00
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getIntegerKeys", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getIntegerKeys)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getStringKeys", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getStringKeys)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();
|
2020-07-29 08:34:42 +00:00
|
|
|
|
2019-01-18 08: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
|
|
|
|
2019-02-21 18:48:33 +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();
|
2020-03-01 00:26:30 +00:00
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_3KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_3KB)).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_4KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_4KB)).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_8KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_8KB)).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_16KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_16KB)).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_32KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_32KB)).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_64KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_64KB)).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_128KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_128KB)).ToChecked();
|
|
|
|
exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR_256KB", NewStringType::kNormal).ToLocalChecked(), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR_256KB)).ToChecked();
|
|
|
|
|
2019-04-17 20:09:11 +00:00
|
|
|
/* 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-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>
|
2019-12-05 03:07:18 +00:00
|
|
|
extern "C" NODE_MODULE_EXPORT void
|
|
|
|
NODE_MODULE_INITIALIZER(Local<Object> exports, Local<Value> module, Local<Context> context) {
|
|
|
|
/* Integrate uSockets with existing libuv loop */
|
|
|
|
uWS::Loop::get(node::GetCurrentEventLoop(context->GetIsolate()));
|
|
|
|
/* Register vanilla V8 addon */
|
|
|
|
Main(exports);
|
|
|
|
}
|
2020-02-23 15:19:58 +00:00
|
|
|
#endif
|