@@ -867,6 +867,17 @@ export class WebCompat extends ContentFeature {
867
867
return deviceInfo ;
868
868
}
869
869
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
+
870
881
/**
871
882
* Fixes device enumeration to handle permission prompts gracefully
872
883
*/
@@ -883,10 +894,13 @@ export class WebCompat extends ContentFeature {
883
894
* @returns {Promise<MediaDeviceInfo[]> }
884
895
*/
885
896
apply : async ( target , thisArg , args ) => {
897
+ const settings = this . getFeatureSetting ( 'enumerateDevices' ) || { } ;
898
+ const timeoutEnabled = settings . timeoutEnabled !== false ;
899
+ const timeoutMs = settings . timeoutMs ?? 2000 ;
900
+
886
901
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 ;
890
904
891
905
// Check if native indicates that prompts would be required
892
906
if ( response . willPrompt ) {
@@ -913,7 +927,7 @@ export class WebCompat extends ContentFeature {
913
927
return DDGReflect . apply ( target , thisArg , args ) ;
914
928
}
915
929
} 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
917
931
return DDGReflect . apply ( target , thisArg , args ) ;
918
932
}
919
933
} ,
0 commit comments