Skip to content

Commit

Permalink
Add binding for App.filter (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesdewinter authored Jan 16, 2024
1 parent 3e1e899 commit 103f6ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ export interface TemplatedApp {
removeServerName(hostname: string) : TemplatedApp;
/** Registers a synchronous callback on missing server names. See /examples/ServerName.js. */
missingServerName(cb: (hostname: string) => void) : TemplatedApp;
/** Attaches a "filter" function to track socket connections / disconnections */
filter(cb: (res: HttpResponse, count: Number) => void | Promise<void>) : TemplatedApp;
/** Closes all sockets including listen sockets. This will forcefully terminate all connections. */
close() : TemplatedApp;
}
Expand Down
29 changes: 29 additions & 0 deletions src/AppWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,34 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
args.GetReturnValue().Set(args.Holder());
}

template <typename APP>
void uWS_App_filter(const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);

/* Handler */
Callback checkedCallback(args.GetIsolate(), args[0]);
if (checkedCallback.isInvalid(args)) {
return;
}
UniquePersistent<Function> cb = checkedCallback.getFunction();

/* This function requires perContextData */
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();

app->filter([cb = std::move(cb), perContextData](auto *res, int count) {
Isolate *isolate = perContextData->isolate;
HandleScope hs(isolate);

Local<Object> resObject = perContextData->resTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
resObject->SetAlignedPointerInInternalField(0, res);

Local<Value> argv[] = {resObject, Local<Value>::Cast(Integer::New(isolate, count))};
CallJS(isolate, cb.Get(isolate), 2, argv);
});

args.GetReturnValue().Set(args.Holder());
}

template <typename APP>
void uWS_App_domain(const FunctionCallbackInfo<Value> &args) {
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
Expand Down Expand Up @@ -719,6 +747,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {

appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "close", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_close<APP>, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen_unix", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_listen_unix<APP>, args.Data()));
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "filter", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_filter<APP>, args.Data()));

/* ws, listen */
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "ws", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_ws<APP>, args.Data()));
Expand Down

0 comments on commit 103f6ff

Please sign in to comment.