@@ -28,6 +28,8 @@ export function registerPluginHttpRoute(params: {
2828 gatewayRuntimeScopeSurface ?: PluginHttpRouteRegistration [ "gatewayRuntimeScopeSurface" ] ;
2929 /** Replace an existing canonical route owned by the same plugin and compatible route source. */
3030 replaceExisting ?: boolean ;
31+ /** Reuse an existing canonical route only when its nonempty plugin and source owners match. */
32+ reuseExistingSameOwner ?: boolean ;
3133 /** Throw when the route cannot be registered instead of returning a no-op cleanup. */
3234 throwOnFailure ?: boolean ;
3335 pluginId ?: string ;
@@ -83,24 +85,40 @@ export function registerPluginHttpRoute(params: {
8385 `plugin: route conflict at ${ normalizedPath } (${ routeMatch } )${ suffix } ` ,
8486 ) ;
8587 }
88+ const requestedOwner = normalizeOptionalString ( params . pluginId ) ;
89+ const requestedSource = normalizeOptionalString ( params . source ) ;
90+ const mismatchedOwner = canonicalMatches . find (
91+ ( route ) =>
92+ normalizeOptionalString ( route . pluginId ) !== requestedOwner ||
93+ normalizeOptionalString ( route . source ) !== requestedSource ,
94+ ) ;
95+ if ( ! params . replaceExisting && params . reuseExistingSameOwner ) {
96+ if ( requestedOwner !== undefined && requestedSource !== undefined && ! mismatchedOwner ) {
97+ params . log ?.(
98+ `plugin: reusing existing webhook path ${ normalizedPath } (${ routeMatch } ) (${ requestedOwner } /${ requestedSource } )` ,
99+ ) ;
100+ return noopUnregister ;
101+ }
102+ const conflictingOwner = mismatchedOwner ?? existing ;
103+ return rejectRegistration (
104+ `plugin: route reuse denied for ${ normalizedPath } (${ routeMatch } )${ suffix } ; owned by ${ conflictingOwner . pluginId ?? "unknown-plugin" } (${ conflictingOwner . source ?? "unknown-source" } )` ,
105+ ) ;
106+ }
86107 if ( ! params . replaceExisting ) {
87108 return rejectRegistration (
88109 `plugin: route conflict at ${ normalizedPath } (${ routeMatch } )${ suffix } ; owned by ${ existing . pluginId ?? "unknown-plugin" } (${ existing . source ?? "unknown-source" } )` ,
89110 ) ;
90111 }
91- const replacementOwner = normalizeOptionalString ( params . pluginId ) ;
92- const replacementSource = normalizeOptionalString ( params . source ) ;
93112 // Source-less same-plugin replacement shipped before route-source ownership.
94113 // Preserve it only when both sides omit source; otherwise require an exact source match.
95- const mismatchedOwner = canonicalMatches . find (
114+ const incompatibleReplacement = canonicalMatches . find (
96115 ( route ) =>
97- normalizeOptionalString ( route . pluginId ) !== replacementOwner ||
98- ( replacementOwner !== undefined &&
99- normalizeOptionalString ( route . source ) !== replacementSource ) ,
116+ normalizeOptionalString ( route . pluginId ) !== requestedOwner ||
117+ ( requestedOwner !== undefined && normalizeOptionalString ( route . source ) !== requestedSource ) ,
100118 ) ;
101- if ( mismatchedOwner ) {
119+ if ( incompatibleReplacement ) {
102120 return rejectRegistration (
103- `plugin: route replacement denied for ${ normalizedPath } (${ routeMatch } )${ suffix } ; owned by ${ mismatchedOwner . pluginId ?? "unknown-plugin" } (${ mismatchedOwner . source ?? "unknown-source" } )` ,
121+ `plugin: route replacement denied for ${ normalizedPath } (${ routeMatch } )${ suffix } ; owned by ${ incompatibleReplacement . pluginId ?? "unknown-plugin" } (${ incompatibleReplacement . source ?? "unknown-source" } )` ,
104122 ) ;
105123 }
106124 const pluginHint = params . pluginId ? ` (${ params . pluginId } )` : "" ;
0 commit comments