@@ -654,19 +654,38 @@ function createDeadline(
654654 }
655655}
656656
657- async function waitForRetry ( delayMs : number , signal : AbortSignal ) : Promise < void > {
658- if ( signal . aborted ) throw toError ( signal . reason )
659- let rejectAbort : ( ( reason ?: unknown ) => void ) | undefined
660- const aborted = new Promise < never > ( ( _ , reject ) => {
661- rejectAbort = reject
657+ function raceWithAbort < T > ( operation : ( ) => Promise < T > , signal : AbortSignal ) : Promise < T > {
658+ if ( signal . aborted ) return Promise . reject ( toError ( signal . reason ) )
659+ return new Promise < T > ( ( resolve , reject ) => {
660+ const cleanup = ( ) => signal . removeEventListener ( 'abort' , onAbort )
661+ const onAbort = ( ) => {
662+ cleanup ( )
663+ reject ( toError ( signal . reason ) )
664+ }
665+ signal . addEventListener ( 'abort' , onAbort , { once : true } )
666+ let pending : Promise < T >
667+ try {
668+ pending = operation ( )
669+ } catch ( error ) {
670+ cleanup ( )
671+ reject ( error )
672+ return
673+ }
674+ pending . then (
675+ ( value ) => {
676+ cleanup ( )
677+ resolve ( value )
678+ } ,
679+ ( error : unknown ) => {
680+ cleanup ( )
681+ reject ( error )
682+ }
683+ )
662684 } )
663- const onAbort = ( ) => rejectAbort ?.( signal . reason )
664- signal . addEventListener ( 'abort' , onAbort , { once : true } )
665- try {
666- await Promise . race ( [ sleep ( delayMs ) , aborted ] )
667- } finally {
668- signal . removeEventListener ( 'abort' , onAbort )
669- }
685+ }
686+
687+ async function waitForRetry ( delayMs : number , signal : AbortSignal ) : Promise < void > {
688+ await raceWithAbort ( ( ) => sleep ( delayMs ) , signal )
670689}
671690
672691/** Creates a lazily loaded OCI client bound to trusted workspace and service context. */
@@ -769,20 +788,24 @@ export async function createOciClient(params: CreateOciClientParams): Promise<Oc
769788
770789 let response : SecureFetchResponse
771790 try {
772- response = await secureFetchWithValidation (
773- signed . url ,
774- {
775- method : request . method ,
776- headers : { ...signed . headers } ,
777- ...( signed . body !== undefined ? { body : new Uint8Array ( signed . body ) } : { } ) ,
778- timeout : Math . max ( 1 , Math . floor ( remainingMs ) ) ,
779- maxResponseBytes : request . maxResponseBytes ,
780- maxRedirects : 0 ,
781- signal : deadline . signal ,
782- profile : 'configuredEndpoint' ,
783- logUrlValidationDetails : false ,
784- } ,
785- 'OCI destination'
791+ response = await raceWithAbort (
792+ ( ) =>
793+ secureFetchWithValidation (
794+ signed . url ,
795+ {
796+ method : request . method ,
797+ headers : { ...signed . headers } ,
798+ ...( signed . body !== undefined ? { body : new Uint8Array ( signed . body ) } : { } ) ,
799+ timeout : Math . max ( 1 , Math . floor ( remainingMs ) ) ,
800+ maxResponseBytes : request . maxResponseBytes ,
801+ maxRedirects : 0 ,
802+ signal : deadline . signal ,
803+ profile : 'configuredEndpoint' ,
804+ logUrlValidationDetails : false ,
805+ } ,
806+ 'OCI destination'
807+ ) ,
808+ deadline . signal
786809 )
787810 } catch ( error ) {
788811 if ( deadline . signal . aborted ) {
@@ -891,19 +914,23 @@ export async function verifyOciApiKeyCredentialForSetup(
891914 headers : { accept : 'application/json' } ,
892915 signingDate : new Date ( ) ,
893916 } )
894- const response = await secureFetchWithValidation (
895- signed . url ,
896- {
897- method : 'GET' ,
898- headers : { ...signed . headers } ,
899- timeout : SETUP_VERIFICATION_TIMEOUT_MS ,
900- maxResponseBytes : SETUP_VERIFICATION_RESPONSE_BYTES ,
901- maxRedirects : 0 ,
902- signal : deadline . signal ,
903- profile : 'configuredEndpoint' ,
904- logUrlValidationDetails : false ,
905- } ,
906- 'OCI credential verification destination'
917+ const response = await raceWithAbort (
918+ ( ) =>
919+ secureFetchWithValidation (
920+ signed . url ,
921+ {
922+ method : 'GET' ,
923+ headers : { ...signed . headers } ,
924+ timeout : SETUP_VERIFICATION_TIMEOUT_MS ,
925+ maxResponseBytes : SETUP_VERIFICATION_RESPONSE_BYTES ,
926+ maxRedirects : 0 ,
927+ signal : deadline . signal ,
928+ profile : 'configuredEndpoint' ,
929+ logUrlValidationDetails : false ,
930+ } ,
931+ 'OCI credential verification destination'
932+ ) ,
933+ deadline . signal
907934 )
908935 if ( ! response . ok ) {
909936 await readFailureCode ( response , deadline . signal )
0 commit comments