Skip to content

Commit 7fdba0b

Browse files
committed
Wrap up SSL, initial WebSocket support
1 parent 94a7c96 commit 7fdba0b

3 files changed

Lines changed: 138 additions & 27 deletions

File tree

‎Makefile‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ targets:
1616
curl https://nodejs.org/dist/v10.0.0/node-v10.0.0-headers.tar.gz | tar xz -C targets
1717
curl https://nodejs.org/dist/v11.1.0/node-v11.1.0-headers.tar.gz | tar xz -C targets
1818
Linux:
19-
gcc $(C_SHARED) -I $$NODE/include/node
20-
g++ $(CPP_SHARED) -I $$NODE/include/node
21-
g++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node
19+
clang $(C_SHARED) -I $$NODE/include/node
20+
clang++ $(CPP_SHARED) -I $$NODE/include/node
21+
clang++ -flto -O3 *.o -std=c++17 -shared -static-libstdc++ -static-libgcc -s -o dist/uws_linux_$$ABI.node
2222
Darwin:
2323
gcc $(C_OSX) $(C_SHARED) -I $$NODE/include/node
2424
g++ $(CPP_OSX) $(CPP_SHARED) -I $$NODE/include/node

‎examples/HelloWorld.js‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
/* The stand-alone runtime has uWS namespace already loaded. */
22
var uWS = uWS ? uWS : require('../dist/uws.js');
33

4-
const world = 'Strings are slower than ArrayBuffer but who cares for demo purose!';
54
const port = 3000;
65

7-
uWS.App().get('/hello', (res, req) => {
8-
res.end(world);
9-
}).get('/*', (res, req) => {
10-
res.writeHeader('content-type', 'text/html; charset= utf-8').end(req.getHeader('user-agent') + ' är din user agent, biatch!');
6+
const app = uWS./*SSL*/App({
7+
key_file_name: '/home/alexhultman/key.pem',
8+
cert_file_name: '/home/alexhultman/cert.pem',
9+
passphrase: '1234'
10+
}).get('/hello', (res, req) => {
11+
res.end('Hejdå!');
12+
}).ws('/*', {
13+
open: (ws, req) => {
14+
console.log('hello websocket!');
15+
},
16+
message: () => {
17+
console.log('message from websocket yo');
18+
}
1119
}).listen(port, (token) => {
1220
if (token) {
1321
console.log('Listening to port ' + port);

‎src/addon.cpp‎

Lines changed: 122 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Persistent<Object> reqTemplate;
2424

2525
#include <iostream>
2626
#include <vector>
27+
#include <type_traits>
2728
std::vector<Persistent<Function, CopyablePersistentTraits<Function>>> nextTickQueue;
2829

2930
class NativeString {
@@ -83,7 +84,8 @@ void res_writeHeader(const FunctionCallbackInfo<Value> &args) {
8384
NativeString header(args.GetIsolate(), args[0]);
8485
NativeString value(args.GetIsolate(), args[1]);
8586

86-
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()), std::string_view(value.getData(), value.getLength()));
87+
((uWS::HttpResponse<false> *) args.Holder()->GetAlignedPointerFromInternalField(0))->writeHeader(std::string_view(header.getData(), header.getLength()),
88+
std::string_view(value.getData(), value.getLength()));
8789

8890
args.GetReturnValue().Set(args.Holder());
8991
}
@@ -98,8 +100,64 @@ void req_getHeader(const FunctionCallbackInfo<Value> &args) {
98100
args.GetReturnValue().Set(String::NewFromUtf8(isolate, header.data(), v8::String::kNormalString, header.length()));
99101
}
100102

103+
/* WebSocket send */
104+
void uWS_WebSocket_send() {
105+
106+
}
107+
108+
/* uWS.App.ws('/pattern', options) */
109+
template <typename APP>
110+
void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
111+
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
112+
113+
NativeString nativeString(args.GetIsolate(), args[0]);
114+
115+
Persistent<Function> *openPf = new Persistent<Function>();
116+
Persistent<Function> *messagePf = new Persistent<Function>();
117+
118+
/* Get the behavior object */
119+
if (args.Length() == 2) {
120+
Local<Object> behaviorObject = Local<Object>::Cast(args[1]);
121+
122+
/* Open */
123+
openPf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
124+
/* Message */
125+
messagePf->Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "message"))));
126+
}
127+
128+
app->template ws<void>(std::string(nativeString.getData(), nativeString.getLength()), {
129+
130+
/*.compression = uWS::SHARED_COMPRESSOR,
131+
.maxPayloadLength = 16 * 1024,*/
132+
.open = [openPf](auto *ws, auto *req) {
133+
HandleScope hs(isolate);
134+
135+
/* Attach a new V8 object with pointer to us, to us */
136+
137+
//Local<Value> argv[] = {resObject, reqObject};
138+
Local<Function>::New(isolate, *openPf)->Call(isolate->GetCurrentContext()->Global(), 0, nullptr);
139+
140+
},
141+
.message = [messagePf](auto *ws, std::string_view message, uWS::OpCode opCode) {
142+
HandleScope hs(isolate);
143+
/* ws's user data holds pointer to persistent object which is the V8 representation we pass here */
144+
Local<Value> argv[] = {}; // ws, message, opCode
145+
Local<Function>::New(isolate, *messagePf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
146+
}/*
147+
.drain = []() {},
148+
.ping = []() {},
149+
.pong = []() {},
150+
.close = []() {}*/
151+
});
152+
153+
// Return this
154+
args.GetReturnValue().Set(args.Holder());
155+
}
156+
157+
// todo: all other methods
158+
template <typename APP>
101159
void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
102-
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
160+
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
103161

104162
NativeString nativeString(args.GetIsolate(), args[0]);
105163

@@ -124,9 +182,9 @@ void uWS_App_get(const FunctionCallbackInfo<Value> &args) {
124182
args.GetReturnValue().Set(args.Holder());
125183
}
126184

127-
// port, callback?
185+
template <typename APP>
128186
void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
129-
uWS::App *app = (uWS::App *) args.Holder()->GetAlignedPointerFromInternalField(0);
187+
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
130188

131189
int port = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
132190

@@ -139,34 +197,79 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
139197
args.GetReturnValue().Set(args.Holder());
140198
}
141199

142-
// we need to override process.nextTick to avoid horrible loss of performance by Node.js
143-
void nextTick(const FunctionCallbackInfo<Value> &args) {
144-
nextTickQueue.push_back(Persistent<Function, CopyablePersistentTraits<Function>>(isolate, Local<Function>::Cast(args[0])));
145-
}
146-
147-
// uWS.App() -> object
200+
template <typename APP>
148201
void uWS_App(const FunctionCallbackInfo<Value> &args) {
149-
// uWS.App
150202
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
151-
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App"));
203+
204+
APP *app;
205+
206+
/* Name differs based on type */
207+
if (std::is_same<APP, uWS::SSLApp>::value) {
208+
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLApp"));
209+
210+
/* We fill these below */
211+
us_ssl_socket_context_options ssl_options = {};
212+
213+
static std::string keyFileName;
214+
static std::string certFileName;
215+
static std::string passphrase;
216+
217+
/* Read the options object (SSL options) */
218+
if (args.Length() == 1) {
219+
/* Key file name */
220+
NativeString keyFileNameValue(isolate, Local<Object>::Cast(args[0])->Get(String::NewFromUtf8(isolate, "key_file_name")));
221+
if (keyFileNameValue.getLength()) {
222+
keyFileName.append(keyFileNameValue.getData(), keyFileNameValue.getLength());
223+
ssl_options.key_file_name = keyFileName.c_str();
224+
}
225+
226+
/* Cert file name */
227+
NativeString certFileNameValue(isolate, Local<Object>::Cast(args[0])->Get(String::NewFromUtf8(isolate, "cert_file_name")));
228+
if (certFileNameValue.getLength()) {
229+
certFileName.append(certFileNameValue.getData(), certFileNameValue.getLength());
230+
ssl_options.cert_file_name = certFileName.c_str();
231+
}
232+
233+
/* Passphrase */
234+
NativeString passphraseValue(isolate, Local<Object>::Cast(args[0])->Get(String::NewFromUtf8(isolate, "passphrase")));
235+
if (passphraseValue.getLength()) {
236+
passphrase.append(passphraseValue.getData(), passphraseValue.getLength());
237+
ssl_options.passphrase = passphrase.c_str();
238+
}
239+
}
240+
241+
app = new APP(ssl_options);
242+
} else {
243+
appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App"));
244+
app = new APP;
245+
}
246+
152247
appTemplate->InstanceTemplate()->SetInternalFieldCount(1);
153248

154-
// Get
155-
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get));
249+
// Get and all the Http methods
250+
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "get"), FunctionTemplate::New(isolate, uWS_App_get<APP>));
251+
252+
// Ws
253+
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws"), FunctionTemplate::New(isolate, uWS_App_ws<APP>));
156254

157255
// Listen
158-
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen));
256+
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen"), FunctionTemplate::New(isolate, uWS_App_listen<APP>));
159257

160258
// Instantiate and set intenal pointer
161259
Local<Object> localApp = appTemplate->GetFunction()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
162260

163261
// Delete this boy
164-
localApp->SetAlignedPointerInInternalField(0, new uWS::App());
262+
localApp->SetAlignedPointerInInternalField(0, app);
165263

166264
// Return an instance of this shit
167265
args.GetReturnValue().Set(localApp);
168266
}
169267

268+
// we need to override process.nextTick to avoid horrible loss of performance by Node.js
269+
void nextTick(const FunctionCallbackInfo<Value> &args) {
270+
nextTickQueue.push_back(Persistent<Function, CopyablePersistentTraits<Function>>(isolate, Local<Function>::Cast(args[0])));
271+
}
272+
170273
void emptyNextTickQueue(Isolate *isolate) {
171274
if (nextTickQueue.size()) {
172275
HandleScope hs(isolate);
@@ -194,11 +297,11 @@ void Main(Local<Object> exports) {
194297
reqTemplateLocal->PrototypeTemplate()->SetAccessor(String::NewFromUtf8(isolate, "method"), Request::method);*/
195298

196299
/* uWS namespace */
197-
exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App)->GetFunction());
300+
exports->Set(String::NewFromUtf8(isolate, "App"), FunctionTemplate::New(isolate, uWS_App<uWS::App>)->GetFunction());
301+
exports->Set(String::NewFromUtf8(isolate, "SSLApp"), FunctionTemplate::New(isolate, uWS_App<uWS::SSLApp>)->GetFunction());
198302
exports->Set(String::NewFromUtf8(isolate, "nextTick"), FunctionTemplate::New(isolate, nextTick)->GetFunction());
199303

200-
201-
// HttpResponse template
304+
// HttpResponse template (not templated)
202305
Local<FunctionTemplate> resTemplateLocal = FunctionTemplate::New(isolate);
203306
resTemplateLocal->SetClassName(String::NewFromUtf8(isolate, "uWS.HttpResponse"));
204307
resTemplateLocal->InstanceTemplate()->SetInternalFieldCount(1);

0 commit comments

Comments
 (0)