-
Notifications
You must be signed in to change notification settings - Fork 26
fix(node): await Promise returns from async JS hook handlers #61
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,14 +43,24 @@ impl HookHandler for JsHookHandlerBridge { | |
| "{}".to_string() | ||
| }); | ||
| Box::pin(async move { | ||
| let result_str: String = | ||
| // The JS handler may return either a bare string (synchronous) or a | ||
| // Promise<String> (async). `call_async` returns whatever the JS | ||
| // function returned — accept both via `Either`. | ||
| let ret: Either<String, napi::bindgen_prelude::Promise<String>> = | ||
| self.callback | ||
| .call_async((event, data_str)) | ||
| .await | ||
|
Comment on lines
+46
to
52
|
||
| .map_err(|e| HookError::HandlerFailed { | ||
| message: e.to_string(), | ||
| handler_name: None, | ||
| })?; | ||
| let result_str: String = match ret { | ||
| Either::A(s) => s, | ||
| Either::B(promise) => promise.await.map_err(|e| HookError::HandlerFailed { | ||
| message: e.to_string(), | ||
| handler_name: None, | ||
| })?, | ||
| }; | ||
|
Comment on lines
+57
to
+63
|
||
| let hook_result: HookResult = serde_json::from_str(&result_str).unwrap_or_else(|e| { | ||
| log::error!( | ||
| "SECURITY: Hook handler returned unparseable result — failing closed (Deny): {e} — json: {result_str}" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to avoid an extra closure (e.g., pass
resolvedirectly tosetImmediate) while keeping the same behavior. It also makes the intent of ‘await one tick’ a bit clearer.