Skip to content

Commit 5f35ef7

Browse files
Add timeout to enumerateDevices call (#1982)
* Add timeout to enumerateDevices call * Clean up some boilerplate
1 parent 9048e42 commit 5f35ef7

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

injected/src/features/web-compat.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,17 @@ export class WebCompat extends ContentFeature {
867867
return deviceInfo;
868868
}
869869

870+
/**
871+
* Helper to wrap a promise with timeout
872+
* @param {Promise} promise - Promise to wrap
873+
* @param {number} timeoutMs - Timeout in milliseconds
874+
* @returns {Promise} Promise that rejects on timeout
875+
*/
876+
withTimeout(promise, timeoutMs) {
877+
const timeout = new Promise((_resolve, reject) => setTimeout(() => reject(new Error('Request timeout')), timeoutMs));
878+
return Promise.race([promise, timeout]);
879+
}
880+
870881
/**
871882
* Fixes device enumeration to handle permission prompts gracefully
872883
*/
@@ -883,10 +894,13 @@ export class WebCompat extends ContentFeature {
883894
* @returns {Promise<MediaDeviceInfo[]>}
884895
*/
885896
apply: async (target, thisArg, args) => {
897+
const settings = this.getFeatureSetting('enumerateDevices') || {};
898+
const timeoutEnabled = settings.timeoutEnabled !== false;
899+
const timeoutMs = settings.timeoutMs ?? 2000;
900+
886901
try {
887-
// Request device enumeration information from native
888-
/** @type {{willPrompt: boolean, videoInput: boolean, audioInput: boolean, audioOutput: boolean}} */
889-
const response = await this.messaging.request(MSG_DEVICE_ENUMERATION, {});
902+
const messagingPromise = this.messaging.request(MSG_DEVICE_ENUMERATION, {});
903+
const response = timeoutEnabled ? await this.withTimeout(messagingPromise, timeoutMs) : await messagingPromise;
890904

891905
// Check if native indicates that prompts would be required
892906
if (response.willPrompt) {
@@ -913,7 +927,7 @@ export class WebCompat extends ContentFeature {
913927
return DDGReflect.apply(target, thisArg, args);
914928
}
915929
} catch (err) {
916-
// If the native request fails, fall back to the original implementation
930+
// If the native request fails or times out, fall back to the original implementation
917931
return DDGReflect.apply(target, thisArg, args);
918932
}
919933
},

0 commit comments

Comments
 (0)