Skip to content

Add getRemotePort bindings #1162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export interface WebSocket<UserData> {
/** Returns the remote IP address as text. See RecognizedString. */
getRemoteAddressAsText() : ArrayBuffer;

/** Returns the remote port number. */
getRemotePort() : number;

/** Returns the UserData object. */
getUserData() : UserData;
}
Expand Down Expand Up @@ -172,12 +175,18 @@ export interface HttpResponse {
/** Returns the remote IP address as text. */
getRemoteAddressAsText() : ArrayBuffer;

/** Returns the remote port number. */
getRemotePort() : number;

/** Returns the remote IP address in binary format (4 or 16 bytes), as reported by the PROXY Protocol v2 compatible proxy. */
getProxiedRemoteAddress() : ArrayBuffer;

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

/** Returns the remote port number, as reported by the PROXY Protocol v2 compatible proxy. */
getProxiedRemotePort() : number;

/** Corking a response is a performance improvement in both CPU and network, as you ready the IO system for writing multiple chunks at once.
* By default, you're corked in the immediately executing top portion of the route handler. In all other cases, such as when returning from
* 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
Expand Down
26 changes: 26 additions & 0 deletions src/HttpResponseWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ struct HttpResponseWrapper {
}
}

/* Takes nothing, returns integer */
template <int SSL>
static void res_getRemotePort(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args);
if (res) {
unsigned int port = res->getRemotePort();

args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port));
}
}

/* Takes nothing, returns arraybuffer */
template <int SSL>
static void res_getProxiedRemoteAddress(const FunctionCallbackInfo<Value> &args) {
Expand All @@ -184,6 +196,18 @@ struct HttpResponseWrapper {
}
}

/* Takes nothing, returns number */
template <int SSL>
static void res_getProxiedRemotePort(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *res = getHttpResponse<SSL>(args);
if (res) {
unsigned int port = res->getProxiedRemotePort();

args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port));
}
}

template <int PROTOCOL>
static void res_getX509Certificate(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
Expand Down Expand Up @@ -472,8 +496,10 @@ struct HttpResponseWrapper {
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "collect", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_cork<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "upgrade", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_upgrade<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemoteAddressAsText<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getRemotePort<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemoteAddress", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemoteAddress<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemoteAddressAsText<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getProxiedRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_getProxiedRemotePort<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "pause", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_pause<SSL>));
resTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "resume", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, res_resume<SSL>));
}
Expand Down
12 changes: 12 additions & 0 deletions src/WebSocketWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ struct WebSocketWrapper {
}
}

/* Takes nothing, returns integer */
template <bool SSL>
static void uWS_WebSocket_getRemotePort(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
unsigned int port = ws->getRemotePort();
args.GetReturnValue().Set(Integer::NewFromUnsigned(isolate, port));
}
}

/* Takes nothing, returns integer */
template <bool SSL>
static void uWS_WebSocket_getBufferedAmount(const FunctionCallbackInfo<Value> &args) {
Expand Down Expand Up @@ -335,6 +346,7 @@ struct WebSocketWrapper {
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "cork", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_cork<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ping", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_ping<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemotePort", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemotePort<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed<SSL>));

/* This one does not exist in C++ */
Expand Down