@@ -29,6 +29,10 @@ import SsoCloudResponse = SsoCloud.SsoCloudResponse;
2929import { KeeperHttpResponse , RestCommand } from './commands'
3030import { AllowedEcKeyIds , AllowedMlKemKeyIds , isAllowedEcKeyId , isAllowedMlKemKeyId } from './transmissionKeys'
3131
32+ export type ExecuteRestOptions = {
33+ skipRegionRedirect ?: boolean
34+ }
35+
3236export class KeeperEndpoint {
3337 private _transmissionKey ?: TransmissionKey
3438 private locale ?: string
@@ -39,7 +43,6 @@ export class KeeperEndpoint {
3943 private onsitePublicKey : Uint8Array | null = null
4044
4145 private useHpkeForTransmissionKey : boolean = false
42- blockRegionRedirects : boolean = false
4346
4447 constructor ( private options : ClientConfigurationInternal ) {
4548 if ( options . deviceToken ) {
@@ -155,16 +158,16 @@ export class KeeperEndpoint {
155158 }
156159 }
157160
158- async executeRest < TIn , TOut > ( message : RestOutMessage < TOut > | RestMessage < TIn , TOut > , sessionToken ?: string ) : Promise < TOut > {
161+ async executeRest < TIn , TOut > ( message : RestOutMessage < TOut > | RestMessage < TIn , TOut > , sessionToken ?: string , options ?: ExecuteRestOptions ) : Promise < TOut > {
159162 // @ts -ignore
160- return this . executeRestInternal ( message , sessionToken )
163+ return this . executeRestInternal ( message , sessionToken , options )
161164 }
162165
163166 async executeRestAction < TIn > ( message : RestInMessage < TIn > | RestActionMessage , sessionToken ?: string ) : Promise < void > {
164167 return this . executeRestInternal ( message , sessionToken )
165168 }
166169
167- private async executeRestInternal < TIn , TOut > ( message : RestInMessage < TIn > | RestOutMessage < TOut > | RestMessage < TIn , TOut > | RestActionMessage , sessionToken ?: string ) : Promise < TOut | void > {
170+ private async executeRestInternal < TIn , TOut > ( message : RestInMessage < TIn > | RestOutMessage < TOut > | RestMessage < TIn , TOut > | RestActionMessage , sessionToken ?: string , options ?: ExecuteRestOptions ) : Promise < TOut | void > {
168171 this . _transmissionKey = await this . getTransmissionKey ( )
169172 while ( true ) {
170173 const payload = 'toBytes' in message ? message . toBytes ( ) : new Uint8Array ( )
@@ -197,7 +200,6 @@ export class KeeperEndpoint {
197200 return
198201 } catch {
199202 const errorMessage = platform . bytesToString ( response . data . slice ( 0 , 1000 ) )
200- let blockedError : Error | undefined
201203 try {
202204 const errorObj : KeeperError = JSON . parse ( errorMessage )
203205 switch ( errorObj . error ) {
@@ -235,13 +237,8 @@ export class KeeperEndpoint {
235237 await this . updateTransmissionKey ( newEcKeyId , newMlKemKeyId )
236238 continue
237239 case 'region_redirect' :
238- if ( this . blockRegionRedirects ) {
239- blockedError = new Error ( JSON . stringify ( {
240- error : 'region_redirect' ,
241- region_host : errorObj . region_host ,
242- blocked : true ,
243- } ) )
244- break
240+ if ( options ?. skipRegionRedirect ) {
241+ throw new Error ( 'region_redirect' )
245242 }
246243 this . options . host = errorObj . region_host !
247244 if ( this . options . onRegionChanged ) {
@@ -260,12 +257,11 @@ export class KeeperEndpoint {
260257 }
261258 }
262259 }
263- if ( ! blockedError && this . options . onCommandFailure ) {
260+ if ( this . options . onCommandFailure ) {
264261 this . options . onCommandFailure ( { ...errorObj , ...{ path : message . path } } )
265262 }
266263 } catch {
267264 }
268- if ( blockedError ) throw blockedError
269265 throw ( new Error ( errorMessage ) )
270266 }
271267 }
0 commit comments