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 */
|
|
|
|
std::vector<Persistent<Function, CopyablePersistentTraits<Function>>> nextTickQueue;
|
|
|
|
Isolate *isolate;
|
2018-10-28 15:07:21 +00:00
|
|
|
|
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-01-14 10:34:20 +00:00
|
|
|
/* We are not compatible with Node.js nextTick for performance (and standalone) reasons */
|
2018-12-26 12:50:12 +00:00
|
|
|
void nextTick(const FunctionCallbackInfo<Value> &args) {
|
|
|
|
nextTickQueue.push_back(Persistent<Function, CopyablePersistentTraits<Function>>(isolate, Local<Function>::Cast(args[0])));
|
|
|
|
}
|
|
|
|
|
2018-11-04 09:56:51 +00:00
|
|
|
void emptyNextTickQueue(Isolate *isolate) {
|
|
|
|
if (nextTickQueue.size()) {
|
|
|
|
HandleScope hs(isolate);
|
|
|
|
|
|
|
|
for (Persistent<Function, CopyablePersistentTraits<Function>> &f : nextTickQueue) {
|
|
|
|
Local<Function>::New(isolate, f)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr);
|
|
|
|
f.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTickQueue.clear();
|
|
|
|
}
|
|
|
|
}
|
2018-11-04 07:55:35 +00:00
|
|
|
|
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();
|
|
|
|
|
2018-11-04 09:56:51 +00:00
|
|
|
/* Register our own nextTick handler */
|
2018-11-05 00:42:54 +00:00
|
|
|
uWS::Loop::defaultLoop()->setPostHandler([](uWS::Loop *) {
|
2018-11-04 09:56:51 +00:00
|
|
|
emptyNextTickQueue(isolate);
|
2018-11-03 11:35:42 +00:00
|
|
|
});
|
|
|
|
|
2019-01-14 10:34:20 +00:00
|
|
|
/* Also empty in pre, it doesn't matter */
|
|
|
|
/*uWS::Loop::defaultLoop()->setPreHandler([]() {
|
|
|
|
emptyNextTickQueue(isolate);
|
|
|
|
});*/
|
|
|
|
|
2018-12-26 18:19:26 +00:00
|
|
|
/* Hook up our timers */
|
|
|
|
us_loop_integrate((us_loop *) uWS::Loop::defaultLoop());
|
|
|
|
|
2018-11-03 11:35:42 +00:00
|
|
|
/* uWS namespace */
|
2018-12-26 12:50:12 +00:00
|
|
|
exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App<uWS::App>)->GetFunction());
|
|
|
|
exports->Set(String::NewFromUtf8(isolate, "SSLApp"), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>)->GetFunction());
|
2018-11-03 11:35:42 +00:00
|
|
|
exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction());
|
2018-10-28 15:07:21 +00:00
|
|
|
|
2019-01-14 10:34:20 +00:00
|
|
|
// these inits should probably happen elsewhere (in their respective struct's constructor)?
|
|
|
|
|
2018-12-26 14:23:14 +00:00
|
|
|
/* The template for websockets */
|
2019-01-08 21:04:56 +00:00
|
|
|
initWsTemplate<0>();
|
|
|
|
initWsTemplate<1>();
|
2018-12-26 14:23:14 +00:00
|
|
|
|
2019-01-08 21:04:56 +00:00
|
|
|
/* Initialize SSL and non-SSL templates */
|
|
|
|
initResTemplate<0>();
|
|
|
|
initResTemplate<1>();
|
2018-11-03 11:35:42 +00:00
|
|
|
|
2019-01-14 10:34:20 +00:00
|
|
|
/* Init a shared request object */
|
|
|
|
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
|