@@ -18,7 +18,8 @@ import { currentWorkspaceFolder, outputConsole, outputChannel } from "../utils";
18
18
const DEFAULT_API_VERSION = 1 ;
19
19
import * as Atelier from "./atelier" ;
20
20
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 > > ( ) ;
22
23
23
24
export interface ConnectionSettings {
24
25
serverName : string ;
@@ -260,13 +261,16 @@ export class AtelierAPI {
260
261
path = encodeURI ( `${ pathPrefix } /api/atelier/${ path || "" } ${ buildParams ( ) } ` ) ;
261
262
262
263
const cookies = this . cookies ;
263
- let auth ;
264
+ const target = `${ username } @${ host } :${ port } ` ;
265
+ let auth : Promise < any > ;
266
+ let authRequest = authRequestMap . get ( target ) ;
264
267
if ( cookies . length || method === "HEAD" ) {
265
268
auth = Promise . resolve ( cookies ) ;
266
269
headers [ "Authorization" ] = `Basic ${ Buffer . from ( `${ username } :${ password } ` ) . toString ( "base64" ) } ` ;
267
270
} else if ( ! cookies . length ) {
268
271
if ( ! authRequest ) {
269
272
authRequest = this . request ( 0 , "HEAD" ) ;
273
+ authRequestMap . set ( target , authRequest ) ;
270
274
}
271
275
auth = authRequest ;
272
276
}
@@ -287,6 +291,7 @@ export class AtelierAPI {
287
291
// simple: true,
288
292
} ) ;
289
293
if ( response . status === 401 ) {
294
+ authRequestMap . delete ( target ) ;
290
295
if ( this . wsOrFile ) {
291
296
setTimeout ( ( ) => {
292
297
checkConnection ( true , typeof this . wsOrFile === "object" ? this . wsOrFile : undefined ) ;
@@ -298,7 +303,7 @@ export class AtelierAPI {
298
303
panel . text = `${ connInfo } ` ;
299
304
panel . tooltip = `Connected as ${ username } ` ;
300
305
if ( method === "HEAD" ) {
301
- authRequest = null ;
306
+ authRequestMap . delete ( target ) ;
302
307
return this . cookies ;
303
308
}
304
309
@@ -309,7 +314,7 @@ export class AtelierAPI {
309
314
const buffer = await response . buffer ( ) ;
310
315
const data : Atelier . Response = JSON . parse ( buffer . toString ( "utf-8" ) ) ;
311
316
312
- /// deconde encoded content
317
+ /// decode encoded content
313
318
if ( data . result && data . result . enc && data . result . content ) {
314
319
data . result . enc = false ;
315
320
data . result . content = Buffer . from ( data . result . content . join ( "" ) , "base64" ) ;
0 commit comments