@@ -79,9 +79,8 @@ export interface HttpResponse<T = string | Buffer | object> {
79
79
*/
80
80
mime ?: string
81
81
/**
82
- * Redirect to this path. Will rewrite against the base path but NOT the
83
- * provider endpoint so you must include it. This allows redirecting outside
84
- * of your endpoint.
82
+ * Redirect to this path. This is constructed against the site base (not the
83
+ * provider's base).
85
84
*/
86
85
redirect ?: string
87
86
/**
@@ -133,12 +132,16 @@ export interface HttpServerOptions {
133
132
134
133
export interface Route {
135
134
/**
136
- * Base path part (in /test/path it would be "/test").
135
+ * Provider base path part (for /provider/base/path it would be /provider).
136
+ */
137
+ providerBase : string
138
+ /**
139
+ * Base path part (for /provider/base/path it would be /base).
137
140
*/
138
141
base : string
139
142
/**
140
- * Remaining part of the route (in /test/path it would be "/path"). It can be
141
- * blank.
143
+ * Remaining part of the route after factoring out the base and provider base
144
+ * (for /provider/base/path it would be /path). It can be blank.
142
145
*/
143
146
requestPath : string
144
147
/**
@@ -161,7 +164,6 @@ interface ProviderRoute extends Route {
161
164
162
165
export interface HttpProviderOptions {
163
166
readonly auth : AuthType
164
- readonly base : string
165
167
readonly commit : string
166
168
readonly password ?: string
167
169
}
@@ -518,41 +520,51 @@ export class HttpServer {
518
520
/**
519
521
* Register a provider for a top-level endpoint.
520
522
*/
521
- public registerHttpProvider < T extends HttpProvider > ( endpoint : string , provider : HttpProvider0 < T > ) : T
522
- public registerHttpProvider < A1 , T extends HttpProvider > ( endpoint : string , provider : HttpProvider1 < A1 , T > , a1 : A1 ) : T
523
+ public registerHttpProvider < T extends HttpProvider > ( endpoint : string | string [ ] , provider : HttpProvider0 < T > ) : T
524
+ public registerHttpProvider < A1 , T extends HttpProvider > (
525
+ endpoint : string | string [ ] ,
526
+ provider : HttpProvider1 < A1 , T > ,
527
+ a1 : A1 ,
528
+ ) : T
523
529
public registerHttpProvider < A1 , A2 , T extends HttpProvider > (
524
- endpoint : string ,
530
+ endpoint : string | string [ ] ,
525
531
provider : HttpProvider2 < A1 , A2 , T > ,
526
532
a1 : A1 ,
527
533
a2 : A2 ,
528
534
) : T
529
535
public registerHttpProvider < A1 , A2 , A3 , T extends HttpProvider > (
530
- endpoint : string ,
536
+ endpoint : string | string [ ] ,
531
537
provider : HttpProvider3 < A1 , A2 , A3 , T > ,
532
538
a1 : A1 ,
533
539
a2 : A2 ,
534
540
a3 : A3 ,
535
541
) : T
536
542
// eslint-disable-next-line @typescript-eslint/no-explicit-any
537
- public registerHttpProvider ( endpoint : string , provider : any , ...args : any [ ] ) : any {
538
- endpoint = endpoint . replace ( / ^ \/ + | \/ + $ / g, "" )
539
- if ( this . providers . has ( `/${ endpoint } ` ) ) {
540
- throw new Error ( `${ endpoint } is already registered` )
541
- }
542
- if ( / \/ / . test ( endpoint ) ) {
543
- throw new Error ( `Only top-level endpoints are supported (got ${ endpoint } )` )
544
- }
543
+ public registerHttpProvider ( endpoint : string | string [ ] , provider : any , ...args : any [ ] ) : void {
545
544
const p = new provider (
546
545
{
547
546
auth : this . options . auth || AuthType . None ,
548
- base : `/${ endpoint } ` ,
549
547
commit : this . options . commit ,
550
548
password : this . options . password ,
551
549
} ,
552
550
...args ,
553
551
)
554
- this . providers . set ( `/${ endpoint } ` , p )
555
- return p
552
+ const endpoints = ( typeof endpoint === "string" ? [ endpoint ] : endpoint ) . map ( ( e ) => e . replace ( / ^ \/ + | \/ + $ / g, "" ) )
553
+ endpoints . forEach ( ( endpoint ) => {
554
+ if ( / \/ / . test ( endpoint ) ) {
555
+ throw new Error ( `Only top-level endpoints are supported (got ${ endpoint } )` )
556
+ }
557
+ const existingProvider = this . providers . get ( `/${ endpoint } ` )
558
+ this . providers . set ( `/${ endpoint } ` , p )
559
+ if ( existingProvider ) {
560
+ logger . debug ( `Overridding existing /${ endpoint } provider` )
561
+ // If the existing provider isn't registered elsewhere we can dispose.
562
+ if ( ! Array . from ( this . providers . values ( ) ) . find ( ( p ) => p === existingProvider ) ) {
563
+ logger . debug ( `Disposing existing /${ endpoint } provider` )
564
+ existingProvider . dispose ( )
565
+ }
566
+ }
567
+ } )
556
568
}
557
569
558
570
/**
@@ -759,15 +771,15 @@ export class HttpServer {
759
771
// that by shifting the next base out of the request path.
760
772
let provider = this . providers . get ( base )
761
773
if ( base !== "/" && provider ) {
762
- return { ...parse ( requestPath ) , fullPath, query : parsedUrl . query , provider, originalPath }
774
+ return { ...parse ( requestPath ) , providerBase : base , fullPath, query : parsedUrl . query , provider, originalPath }
763
775
}
764
776
765
777
// Fall back to the top-level provider.
766
778
provider = this . providers . get ( "/" )
767
779
if ( ! provider ) {
768
780
throw new Error ( `No provider for ${ base } ` )
769
781
}
770
- return { base, fullPath, requestPath, query : parsedUrl . query , provider, originalPath }
782
+ return { base, providerBase : "/" , fullPath, requestPath, query : parsedUrl . query , provider, originalPath }
771
783
}
772
784
773
785
/**
0 commit comments