refactor: use structural typing instead of instanceof for AbortSignal check
Address P1 review feedback from Greptile: instanceof AbortSignal may be unreliable across different realms (VM, iframe, etc.) where the AbortSignal constructor may differ. Use structural typing (checking for aborted property and addEventListener method) for more robust cross-realm compatibility.
This commit is contained in:
@@ -6,6 +6,19 @@ function throwAbortError(): never {
|
||||
throw err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an object is a valid AbortSignal using structural typing.
|
||||
* This is more reliable than `instanceof` across different realms (VM, iframe, etc.)
|
||||
* where the AbortSignal constructor may differ.
|
||||
*/
|
||||
function isAbortSignal(obj: unknown): obj is AbortSignal {
|
||||
if (!obj || typeof obj !== "object") {
|
||||
return false;
|
||||
}
|
||||
const signal = obj as Record<string, unknown>;
|
||||
return typeof signal.aborted === "boolean" && typeof signal.addEventListener === "function";
|
||||
}
|
||||
|
||||
function combineAbortSignals(a?: AbortSignal, b?: AbortSignal): AbortSignal | undefined {
|
||||
if (!a && !b) {
|
||||
return undefined;
|
||||
@@ -22,11 +35,7 @@ function combineAbortSignals(a?: AbortSignal, b?: AbortSignal): AbortSignal | un
|
||||
if (b?.aborted) {
|
||||
return b;
|
||||
}
|
||||
if (
|
||||
typeof AbortSignal.any === "function" &&
|
||||
a instanceof AbortSignal &&
|
||||
b instanceof AbortSignal
|
||||
) {
|
||||
if (typeof AbortSignal.any === "function" && isAbortSignal(a) && isAbortSignal(b)) {
|
||||
return AbortSignal.any([a, b]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user