Skip to content

Commit dcfbd23

Browse files
committed
Don't require all websocket handlers, fix up Autobahn test
1 parent d8937c6 commit dcfbd23

3 files changed

Lines changed: 52 additions & 40 deletions

File tree

‎src/AppWrapper.h‎

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,8 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
3333
/* idleTimeout */
3434
behavior.idleTimeout = behaviorObject->Get(String::NewFromUtf8(isolate, "idleTimeout"))->Int32Value();
3535

36-
/* Compression, map from 0, 1, 2 to disabled, shared, dedicated */
37-
int compression = behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
38-
if (compression == 0) {
39-
behavior.compression = uWS::CompressOptions::DISABLED;
40-
} else if (compression == 1) {
41-
behavior.compression = uWS::CompressOptions::SHARED_COMPRESSOR;
42-
} else if (compression == 2) {
43-
behavior.compression = uWS::CompressOptions::DEDICATED_COMPRESSOR;
44-
}
36+
/* Compression, map from 0, 1, 2 to disabled, shared, dedicated. This is actually the enum */
37+
behavior.compression = (uWS::CompressOptions) behaviorObject->Get(String::NewFromUtf8(isolate, "compression"))->Int32Value();
4538

4639
/* Open */
4740
openPf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "open"))));
@@ -53,6 +46,7 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
5346
closePf.Reset(args.GetIsolate(), Local<Function>::Cast(behaviorObject->Get(String::NewFromUtf8(isolate, "close"))));
5447
}
5548

49+
/* Open handler is NOT optional for the wrapper */
5650
behavior.open = [openPf = std::move(openPf)](auto *ws, auto *req) {
5751
HandleScope hs(isolate);
5852

@@ -69,34 +63,44 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
6963
perSocketData->socketPf = new Persistent<Object>;
7064
perSocketData->socketPf->Reset(isolate, wsObject);
7165

72-
Local<Value> argv[] = {wsObject, reqObject};
73-
Local<Function>::New(isolate, openPf)->Call(isolate->GetCurrentContext()->Global(), 2, argv);
66+
Local<Function> openLf = Local<Function>::New(isolate, openPf);
67+
if (!openLf->IsUndefined()) {
68+
Local<Value> argv[] = {wsObject, reqObject};
69+
openLf->Call(isolate->GetCurrentContext()->Global(), 2, argv);
70+
}
7471
};
7572

76-
behavior.message = [messagePf = std::move(messagePf)](auto *ws, std::string_view message, uWS::OpCode opCode) {
77-
HandleScope hs(isolate);
73+
/* Message handler is always optional */
74+
if (messagePf != Undefined(isolate)) {
75+
behavior.message = [messagePf = std::move(messagePf)](auto *ws, std::string_view message, uWS::OpCode opCode) {
76+
HandleScope hs(isolate);
7877

79-
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
78+
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
8079

81-
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
82-
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
83-
messageArrayBuffer,
84-
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)};
85-
Local<Function>::New(isolate, messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
80+
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
81+
Local<Value> argv[3] = {Local<Object>::New(isolate, *(perSocketData->socketPf)),
82+
messageArrayBuffer,
83+
Boolean::New(isolate, opCode == uWS::OpCode::BINARY)};
84+
Local<Function>::New(isolate, messagePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
8685

87-
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
88-
messageArrayBuffer->Neuter();
89-
};
86+
/* Important: we clear the ArrayBuffer to make sure it is not invalidly used after return */
87+
messageArrayBuffer->Neuter();
88+
};
89+
}
9090

91-
behavior.drain = [drainPf = std::move(drainPf)](auto *ws) {
92-
HandleScope hs(isolate);
91+
/* Drain handler is always optional */
92+
if (drainPf != Undefined(isolate)) {
93+
behavior.drain = [drainPf = std::move(drainPf)](auto *ws) {
94+
HandleScope hs(isolate);
9395

94-
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
95-
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
96-
};
97-
Local<Function>::New(isolate, drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
98-
};
96+
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
97+
Local<Value> argv[1] = {Local<Object>::New(isolate, *(perSocketData->socketPf))
98+
};
99+
Local<Function>::New(isolate, drainPf)->Call(isolate->GetCurrentContext()->Global(), 1, argv);
100+
};
101+
}
99102

103+
/* These are not hooked in */
100104
behavior.ping = [](auto *ws) {
101105

102106
};
@@ -105,19 +109,23 @@ void uWS_App_ws(const FunctionCallbackInfo<Value> &args) {
105109

106110
};
107111

112+
/* Close handler is NOT optional for the wrapper */
108113
behavior.close = [closePf = std::move(closePf)](auto *ws, int code, std::string_view message) {
109114
HandleScope hs(isolate);
110115

111116
Local<ArrayBuffer> messageArrayBuffer = ArrayBuffer::New(isolate, (void *) message.data(), message.length());
112117
PerSocketData *perSocketData = (PerSocketData *) ws->getUserData();
113118
Local<Object> wsObject = Local<Object>::New(isolate, *(perSocketData->socketPf));
114-
Local<Value> argv[3] = {wsObject,
115-
Integer::New(isolate, code),
116-
messageArrayBuffer};
117119

118120
/* Invalidate this wsObject */
119121
wsObject->SetAlignedPointerInInternalField(0, nullptr);
120-
Local<Function>::New(isolate, closePf)->Call(isolate->GetCurrentContext()->Global(), 3, argv);
122+
123+
/* Only call close handler if we have one set */
124+
Local<Function> closeLf = Local<Function>::New(isolate, closePf);
125+
if (!closeLf->IsUndefined()) {
126+
Local<Value> argv[3] = {wsObject, Integer::New(isolate, code), messageArrayBuffer};
127+
closeLf->Call(isolate->GetCurrentContext()->Global(), 3, argv);
128+
}
121129

122130
delete perSocketData->socketPf;
123131

‎src/addon.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ void Main(Local<Object> exports) {
108108
/* Expose some µSockets functions directly under uWS namespace */
109109
exports->Set(String::NewFromUtf8(isolate, "us_listen_socket_close"), FunctionTemplate::New(isolate, uWS_us_listen_socket_close)->GetFunction());
110110

111+
/* Compression enum */
112+
exports->Set(String::NewFromUtf8(isolate, "DISABLED"), Integer::NewFromUnsigned(isolate, uWS::DISABLED));
113+
exports->Set(String::NewFromUtf8(isolate, "SHARED_COMPRESSOR"), Integer::NewFromUnsigned(isolate, uWS::SHARED_COMPRESSOR));
114+
exports->Set(String::NewFromUtf8(isolate, "DEDICATED_COMPRESSOR"), Integer::NewFromUnsigned(isolate, uWS::DEDICATED_COMPRESSOR));
115+
111116
/* The template for websockets */
112117
WebSocketWrapper::initWsTemplate<0>();
113118
WebSocketWrapper::initWsTemplate<1>();

‎tests/Autobahn.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ function listenWithSettings(settings) {
1414
};
1515

1616
/* Create the app */
17-
let app = settings.ssl ? uWS.App(sslOptions) : uWS.SSLApp(sslOptions);
17+
let app = settings.ssl ? uWS.SSLApp(sslOptions) : uWS.App(sslOptions);
1818

1919
/* Attach our behavior from settings */
2020
app.ws('/*', {
2121
compression: settings.compression,
2222
maxPayloadLength: 16 * 1024 * 1024,
2323
idleTimeout: 60,
24-
2524
message: (ws, message, isBinary) => {
26-
ws.send(message, isBinary);
25+
ws.send(message, isBinary, true);
2726
}
2827
}).any('/exit', (res, req) => {
2928
/* Shut down everything on this route */
@@ -56,26 +55,26 @@ function listenWithSettings(settings) {
5655
listenWithSettings({
5756
port: 9001,
5857
ssl: false,
59-
compression: 0
58+
compression: uWS.DISABLED
6059
});
6160

6261
/* SSL, shared compressor */
6362
listenWithSettings({
6463
port: 9002,
6564
ssl: true,
66-
compression: 1
65+
compression: uWS.SHARED_COMPRESSOR
6766
});
6867

6968
/* non-SSL, dedicated compressor */
7069
listenWithSettings({
7170
port: 9003,
7271
ssl: false,
73-
compression: 2
72+
compression: uWS.DEDICATED_COMPRESSOR
7473
});
7574

7675
/* This is required to check for memory leaks */
7776
process.on('exit', () => {
7877
apps.forEach((a) => {
7978
a.app.forcefully_free();
8079
});
81-
});
80+
});

0 commit comments

Comments
 (0)