Skip to content

Commit bb831f0

Browse files
authored
Add getRemotePort bindings (#1162)
1 parent b03dd9d commit bb831f0

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎docs/index.d.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ export interface WebSocket<UserData> {
104104
/** Returns the remote IP address as text. See RecognizedString. */
105105
getRemoteAddressAsText() : ArrayBuffer;
106106

107+
/** Returns the remote port number. */
108+
getRemotePort() : number;
109+
107110
/** Returns the UserData object. */
108111
getUserData() : UserData;
109112

@@ -195,12 +198,18 @@ export interface HttpResponse {
195198
/** Returns the remote IP address as text. */
196199
getRemoteAddressAsText() : ArrayBuffer;
197200

201+
/** Returns the remote port number. */
202+
getRemotePort() : number;
203+
198204
/** Returns the remote IP address in binary format (4 or 16 bytes), as reported by the PROXY Protocol v2 compatible proxy. */
199205
getProxiedRemoteAddress() : ArrayBuffer;
200206

201207
/** Returns the remote IP address as text, as reported by the PROXY Protocol v2 compatible proxy. */
202208
getProxiedRemoteAddressAsText() : ArrayBuffer;
203209

210+
/** Returns the remote port number, as reported by the PROXY Protocol v2 compatible proxy. */
211+
getProxiedRemotePort() : number;
212+
204213
/** Corking a response is a performance improvement in both CPU and network, as you ready the IO system for writing multiple chunks at once.
205214
* By default, you're corked in the immediately executing top portion of the route handler. In all other cases, such as when returning from
206215
* await, or when being called back from an async database request or anything that isn't directly executing in the route handler, you'll want

‎src/HttpResponseWrapper.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ struct HttpResponseWrapper {
242242
}
243243
}
244244

245+
/* Takes nothing, returns integer */
246+
template <int SSL>
247+
static void res_getRemotePort(const FunctionCallbackInfo<Value> &args) {
248+
Isolate *isolate = args.GetIsolate();
249+
auto *res = getHttpResponse<SSL>(args);
250+
if (res) {
251+
unsigned int port = res->getRemotePort();
252+
253+
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port));
254+
}
255+
}
256+
245257
/* Takes nothing, returns arraybuffer */
246258
template <int SSL>
247259
static void res_getProxiedRemoteAddress(const FunctionCallbackInfo<Value> &args) {
@@ -266,6 +278,18 @@ struct HttpResponseWrapper {
266278
}
267279
}
268280

281+
/* Takes nothing, returns number */
282+
template <int SSL>
283+
static void res_getProxiedRemotePort(const FunctionCallbackInfo<Value> &args) {
284+
Isolate *isolate = args.GetIsolate();
285+
auto *res = getHttpResponse<SSL>(args);
286+
if (res) {
287+
unsigned int port = res->getProxiedRemotePort();
288+
289+
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port));
290+
}
291+
}
292+
269293
template <int PROTOCOL>
270294
static void res_getX509Certificate(const FunctionCallbackInfo<Value> &args) {
271295
Isolate *isolate = args.GetIsolate();
@@ -599,8 +623,10 @@ struct HttpResponseWrapper {
599623
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "collect", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork<SSL>));
600624
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "upgrade", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_upgrade<SSL>));
601625
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemoteAddressAsText<SSL>));
626+
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemotePort<SSL>));
602627
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemoteAddress<SSL>));
603628
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemoteAddressAsText<SSL>));
629+
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemotePort<SSL>));
604630
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "pause", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_pause<SSL>));
605631
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "resume", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_resume<SSL>));
606632
}

‎src/WebSocketWrapper.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ struct WebSocketWrapper {
159159
}
160160
}
161161

162+
/* Takes nothing, returns integer */
163+
template <bool SSL>
164+
static void uWS_WebSocket_getRemotePort(const FunctionCallbackInfo<Value> &args) {
165+
Isolate *isolate = args.GetIsolate();
166+
auto *ws = getWebSocket<SSL>(args);
167+
if (ws) {
168+
unsigned int port = ws->getRemotePort();
169+
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port));
170+
}
171+
}
172+
162173
/* Takes nothing, returns integer */
163174
template <bool SSL>
164175
static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo<Value> &args) {
@@ -335,6 +346,7 @@ struct WebSocketWrapper {
335346
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_cork<SSL>));
336347
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ping", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_ping<SSL>));
337348
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText<SSL>));
349+
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemotePort<SSL>));
338350
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed<SSL>));
339351

340352
/* This one does not exist in C++ */

0 commit comments

Comments
 (0)