11import axios from "axios" ;
22import * as httpsModule from "https" ;
33import * as vscode from "vscode" ;
4- import * as Cache from "vscode-cache" ;
54import * as semver from "semver" ;
65import {
76 getResolvedConnectionSpec ,
@@ -21,9 +20,12 @@ const DEFAULT_SERVER_VERSION = "2016.2.0";
2120import * as Atelier from "./atelier" ;
2221import { isfsConfig } from "../utils/FileProviderUtil" ;
2322
24- // Map of the authRequest promises for each username@host :port target to avoid concurrency issues
23+ // Map of the authRequest promises for each username@host :port/pathPrefix target to avoid concurrency issues
2524const authRequestMap = new Map < string , Promise < any > > ( ) ;
2625
26+ /** Map of `username@host:port/pathPrefix` to cookies */
27+ const cookiesMap = new Map < string , string [ ] > ( ) ;
28+
2729interface ConnectionSettings {
2830 serverName : string ;
2931 active : boolean ;
@@ -170,12 +172,11 @@ export class AtelierAPI {
170172 }
171173
172174 public get cookies ( ) : string [ ] {
173- const cookies = this . cache . get ( "cookies" , [ ] ) ;
174- return cookies ;
175+ return cookiesMap . get ( this . mapKey ( ) ) ?? [ ] ;
175176 }
176177
177- public async clearCookies ( ) : Promise < void > {
178- await this . cache . put ( "cookies" , [ ] ) ;
178+ public clearCookies ( ) : void {
179+ cookiesMap . delete ( this . mapKey ( ) ) ;
179180 }
180181
181182 public xdebugUrl ( ) : string {
@@ -191,8 +192,9 @@ export class AtelierAPI {
191192 : "" ;
192193 }
193194
194- public async updateCookies ( newCookies : string [ ] ) : Promise < void > {
195- const cookies = this . cache . get ( "cookies" , [ ] ) ;
195+ public updateCookies ( newCookies : string [ ] ) : void {
196+ const mapKey = this . mapKey ( ) ;
197+ const cookies = cookiesMap . get ( mapKey ) ?? [ ] ;
196198 newCookies . forEach ( ( cookie ) => {
197199 const [ cookieName ] = cookie . split ( "=" ) ;
198200 const index = cookies . findIndex ( ( el ) => el . startsWith ( cookieName ) ) ;
@@ -202,7 +204,17 @@ export class AtelierAPI {
202204 cookies . push ( cookie ) ;
203205 }
204206 } ) ;
205- await this . cache . put ( "cookies" , cookies ) ;
207+ cookiesMap . set ( mapKey , cookies ) ;
208+ }
209+
210+ /** Return the key for getting values from connection-specific Maps for this connection */
211+ private mapKey ( ) : string {
212+ const { host, port, username } = this . config ;
213+ let pathPrefix = this . _config . pathPrefix || "" ;
214+ if ( pathPrefix . length && ! pathPrefix . startsWith ( "/" ) ) {
215+ pathPrefix = "/" + pathPrefix ;
216+ }
217+ return `${ username } @${ host } :${ port } ${ pathPrefix } ` ;
206218 }
207219
208220 private setConnection ( workspaceFolderName : string , namespace ?: string ) : void {
@@ -284,11 +296,6 @@ export class AtelierAPI {
284296 }
285297 }
286298
287- private get cache ( ) : Cache {
288- const { host, port } = this . config ;
289- return new Cache ( extensionContext , `API:${ host } :${ port } ` ) ;
290- }
291-
292299 public get connInfo ( ) : string {
293300 const { port, docker, dockerService } = this . config ;
294301 return ( docker ? "docker" + ( dockerService ? `:${ dockerService } :${ port } ` : "" ) : this . serverId ) + `[${ this . ns } ]` ;
@@ -358,9 +365,9 @@ export class AtelierAPI {
358365 path = encodeURI ( `${ pathPrefix } /api/atelier/${ path || "" } ` ) + buildParams ( ) ;
359366
360367 const cookies = this . cookies ;
361- const target = ` ${ username } @ ${ host } : ${ port } ` ;
368+ const mapKey = this . mapKey ( ) ;
362369 let auth : Promise < any > ;
363- let authRequest = authRequestMap . get ( target ) ;
370+ let authRequest = authRequestMap . get ( mapKey ) ;
364371 if ( cookies . length || ( method === "HEAD" && ! originalPath ) ) {
365372 auth = Promise . resolve ( cookies ) ;
366373
@@ -372,7 +379,7 @@ export class AtelierAPI {
372379 if ( ! authRequest ) {
373380 // Recursion point
374381 authRequest = this . request ( 0 , "HEAD" , undefined , undefined , undefined , undefined , options ) ;
375- authRequestMap . set ( target , authRequest ) ;
382+ authRequestMap . set ( mapKey , authRequest ) ;
376383 }
377384 auth = authRequest ;
378385 }
@@ -438,7 +445,7 @@ export class AtelierAPI {
438445 } ;
439446 }
440447 if ( response . status === 401 ) {
441- authRequestMap . delete ( target ) ;
448+ authRequestMap . delete ( mapKey ) ;
442449 if ( this . wsOrFile && ! checkingConnection ) {
443450 setTimeout ( ( ) => {
444451 checkConnection (
@@ -453,7 +460,7 @@ export class AtelierAPI {
453460 await this . updateCookies ( response . headers [ "set-cookie" ] || [ ] ) ;
454461 if ( method === "HEAD" ) {
455462 if ( ! originalPath ) {
456- authRequestMap . delete ( target ) ;
463+ authRequestMap . delete ( mapKey ) ;
457464 return this . cookies ;
458465 } else if ( response . status >= 400 ) {
459466 // The HEAD /doc request errored out
@@ -552,7 +559,7 @@ export class AtelierAPI {
552559 outputChannel . appendLine ( `+- END ----------------------------------------------` ) ;
553560 }
554561 // always discard the cached authentication promise
555- authRequestMap . delete ( target ) ;
562+ authRequestMap . delete ( mapKey ) ;
556563
557564 // In some cases schedule an automatic retry.
558565 // ENOTFOUND occurs if, say, the VPN to the server's network goes down.
0 commit comments