1- import Url from 'url-parse' ;
2- import path from 'path' ;
31import * as request from 'superagent' ;
42import {
53 BaseHTTPClient ,
@@ -35,7 +33,7 @@ export type TokenHeader =
3533 * This is the default implementation of BaseHTTPClient.
3634 */
3735export class URLTokenBaseHTTPClient implements BaseHTTPClient {
38- private readonly baseURL : Url ;
36+ private readonly baseURL : URL ;
3937 private readonly tokenHeader : TokenHeader ;
4038
4139 constructor (
@@ -44,9 +42,15 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
4442 port ?: string | number ,
4543 private defaultHeaders : Record < string , any > = { }
4644 ) {
47- const baseServerURL = new Url ( baseServer , { } ) ;
45+ // Append a trailing slash so we can use relative paths. Without the trailing
46+ // slash, the last path segment will be replaced by the relative path. See
47+ // usage in `addressWithPath`.
48+ const fixedBaseServer = baseServer . endsWith ( '/' )
49+ ? baseServer
50+ : `${ baseServer } /` ;
51+ const baseServerURL = new URL ( fixedBaseServer ) ;
4852 if ( typeof port !== 'undefined' ) {
49- baseServerURL . set ( ' port' , port . toString ( ) ) ;
53+ baseServerURL . port = port . toString ( ) ;
5054 }
5155
5256 if ( baseServerURL . protocol . length === 0 ) {
@@ -63,10 +67,15 @@ export class URLTokenBaseHTTPClient implements BaseHTTPClient {
6367 * @returns A URL string
6468 */
6569 private addressWithPath ( relativePath : string ) {
66- const address = new Url (
67- path . posix . join ( this . baseURL . pathname , relativePath ) ,
68- this . baseURL
69- ) ;
70+ let fixedRelativePath : string ;
71+ if ( relativePath . startsWith ( './' ) ) {
72+ fixedRelativePath = relativePath ;
73+ } else if ( relativePath . startsWith ( '/' ) ) {
74+ fixedRelativePath = `.${ relativePath } ` ;
75+ } else {
76+ fixedRelativePath = `./${ relativePath } ` ;
77+ }
78+ const address = new URL ( fixedRelativePath , this . baseURL ) ;
7079 return address . toString ( ) ;
7180 }
7281
0 commit comments