@@ -18,7 +18,8 @@ import { currentWorkspaceFolder, outputConsole, outputChannel } from "../utils";
1818const DEFAULT_API_VERSION = 1 ;
1919import * as Atelier from "./atelier" ;
2020
21- let authRequest = null ;
21+ // Map of the authRequest promises for each username@host :port target to avoid concurrency issues
22+ const authRequestMap = new Map < string , Promise < any > > ( ) ;
2223
2324export interface ConnectionSettings {
2425 serverName : string ;
@@ -260,13 +261,16 @@ export class AtelierAPI {
260261 path = encodeURI ( `${ pathPrefix } /api/atelier/${ path || "" } ${ buildParams ( ) } ` ) ;
261262
262263 const cookies = this . cookies ;
263- let auth ;
264+ const target = `${ username } @${ host } :${ port } ` ;
265+ let auth : Promise < any > ;
266+ let authRequest = authRequestMap . get ( target ) ;
264267 if ( cookies . length || method === "HEAD" ) {
265268 auth = Promise . resolve ( cookies ) ;
266269 headers [ "Authorization" ] = `Basic ${ Buffer . from ( `${ username } :${ password } ` ) . toString ( "base64" ) } ` ;
267270 } else if ( ! cookies . length ) {
268271 if ( ! authRequest ) {
269272 authRequest = this . request ( 0 , "HEAD" ) ;
273+ authRequestMap . set ( target , authRequest ) ;
270274 }
271275 auth = authRequest ;
272276 }
@@ -287,6 +291,7 @@ export class AtelierAPI {
287291 // simple: true,
288292 } ) ;
289293 if ( response . status === 401 ) {
294+ authRequestMap . delete ( target ) ;
290295 if ( this . wsOrFile ) {
291296 setTimeout ( ( ) => {
292297 checkConnection ( true , typeof this . wsOrFile === "object" ? this . wsOrFile : undefined ) ;
@@ -298,7 +303,7 @@ export class AtelierAPI {
298303 panel . text = `${ connInfo } ` ;
299304 panel . tooltip = `Connected as ${ username } ` ;
300305 if ( method === "HEAD" ) {
301- authRequest = null ;
306+ authRequestMap . delete ( target ) ;
302307 return this . cookies ;
303308 }
304309
@@ -309,7 +314,7 @@ export class AtelierAPI {
309314 const buffer = await response . buffer ( ) ;
310315 const data : Atelier . Response = JSON . parse ( buffer . toString ( "utf-8" ) ) ;
311316
312- /// deconde encoded content
317+ /// decode encoded content
313318 if ( data . result && data . result . enc && data . result . content ) {
314319 data . result . enc = false ;
315320 data . result . content = Buffer . from ( data . result . content . join ( "" ) , "base64" ) ;
0 commit comments