Skip to content

Commit 103f6ff

Browse files
Add binding for App.filter (#1010)
1 parent 3e1e899 commit 103f6ff

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎docs/index.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ export interface TemplatedApp {
325325
removeServerName(hostname: string) : TemplatedApp;
326326
/** Registers a synchronous callback on missing server names. See /examples/ServerName.js. */
327327
missingServerName(cb: (hostname: string) => void) : TemplatedApp;
328+
/** Attaches a "filter" function to track socket connections / disconnections */
329+
filter(cb: (res: HttpResponse, count: Number) => void | Promise<void>) : TemplatedApp;
328330
/** Closes all sockets including listen sockets. This will forcefully terminate all connections. */
329331
close() : TemplatedApp;
330332
}

‎src/AppWrapper.h‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,34 @@ void uWS_App_listen(const FunctionCallbackInfo<Value> &args) {
431431
args.GetReturnValue().Set(args.Holder());
432432
}
433433

434+
template <typename APP>
435+
void uWS_App_filter(const FunctionCallbackInfo<Value> &args) {
436+
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
437+
438+
/* Handler */
439+
Callback checkedCallback(args.GetIsolate(), args[0]);
440+
if (checkedCallback.isInvalid(args)) {
441+
return;
442+
}
443+
UniquePersistent<Function> cb = checkedCallback.getFunction();
444+
445+
/* This function requires perContextData */
446+
PerContextData *perContextData = (PerContextData *) Local<External>::Cast(args.Data())->Value();
447+
448+
app->filter([cb = std::move(cb), perContextData](auto *res, int count) {
449+
Isolate *isolate = perContextData->isolate;
450+
HandleScope hs(isolate);
451+
452+
Local<Object> resObject = perContextData->resTemplate[getAppTypeIndex<APP>()].Get(isolate)->Clone();
453+
resObject->SetAlignedPointerInInternalField(0, res);
454+
455+
Local<Value> argv[] = {resObject, Local<Value>::Cast(Integer::New(isolate, count))};
456+
CallJS(isolate, cb.Get(isolate), 2, argv);
457+
});
458+
459+
args.GetReturnValue().Set(args.Holder());
460+
}
461+
434462
template <typename APP>
435463
void uWS_App_domain(const FunctionCallbackInfo<Value> &args) {
436464
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
@@ -719,6 +747,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
719747

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

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

0 commit comments

Comments
 (0)