@@ -6,17 +6,19 @@ import {
66 providerDestinationConfigError ,
77 resolvePublicAddresses ,
88} from "./destination-policy" ;
9- import { pinnedHttpGet } from "./pinned-http" ;
9+ import { pinnedHttpGet , pinnedHttpPost } from "./pinned-http" ;
1010import { outboundProxyConfigured } from "./proxy-env" ;
1111import { publicProviderBaseUrl } from "./provider-url" ;
1212
1313type ProviderGetInit = Omit < RequestInit , "body" | "method" | "redirect" > ;
14+ type ProviderPostInit = ProviderGetInit & { body : string } ;
1415type ProviderOutboundConfig = Pick < OcxProviderConfig , "baseUrl" | "allowPrivateNetwork" > & {
1516 fetch ?: typeof globalThis . fetch ;
1617} ;
1718export interface ProviderOutboundDependencies {
1819 resolveAddresses ?: typeof resolvePublicAddresses ;
1920 pinnedGet ?: typeof pinnedHttpGet ;
21+ pinnedPost ?: typeof pinnedHttpPost ;
2022}
2123
2224export class ProviderOutboundPolicyError extends Error {
@@ -102,13 +104,18 @@ export async function providerRedirectError(response: Response, requestUrl: stri
102104 return `provider returned ${ response . status } redirect to ${ target } ; configure the final provider URL directly` ;
103105}
104106
105- export async function providerOutboundGet (
107+ async function providerOutboundRequest (
106108 name : string ,
107109 provider : ProviderOutboundConfig ,
108110 url : string ,
109- init : ProviderGetInit = { } ,
111+ method : "GET" | "POST" ,
112+ init : ProviderGetInit | ProviderPostInit ,
110113 dependencies : ProviderOutboundDependencies = { } ,
111114) : Promise < Response > {
115+ const postUrl = method === "POST" ? new URL ( url ) : undefined ;
116+ if ( postUrl ?. protocol !== undefined && postUrl . protocol !== "https:" ) {
117+ throw new ProviderOutboundPolicyError ( "provider POST URL must use HTTPS" ) ;
118+ }
112119 if ( provider . fetch ) {
113120 // A caller-owned executor cannot be peer-pinned here. This branch keeps literal/config
114121 // checks and redirect blocking, but does not provide the resolved-address guarantees of
@@ -128,12 +135,13 @@ export async function providerOutboundGet(
128135 } ) ;
129136 if ( destinationError ) throw new ProviderOutboundPolicyError ( destinationError ) ;
130137 }
131- return provider . fetch ( url , { ...init , method : "GET" , redirect : "manual" } ) ;
138+ return provider . fetch ( url , { ...init , method, redirect : "manual" } ) ;
132139 }
133- const parsed = new URL ( url ) ;
140+ const parsed = postUrl ?? new URL ( url ) ;
134141 const proxyConfigured = configuredProxyFor ( ) ;
135142 const resolveAddresses = dependencies . resolveAddresses ?? resolvePublicAddresses ;
136143 const pinnedGet = dependencies . pinnedGet ?? pinnedHttpGet ;
144+ const pinnedPost = dependencies . pinnedPost ?? pinnedHttpPost ;
137145 const allowPrivate = providerAllowsPrivateNetwork ( name , provider ) ;
138146 let resolved : Awaited < ReturnType < typeof resolvePublicAddresses > > ;
139147 try {
@@ -150,21 +158,46 @@ export async function providerOutboundGet(
150158 if ( ! proxyConfigured ) throw error ;
151159 warnProxyBoundaryOnce ( ) ;
152160 warnProxyDnsDegradationOnce ( ) ;
153- return globalThis . fetch ( url , { ...init , method : "GET" , redirect : "manual" } ) ;
161+ return globalThis . fetch ( url , { ...init , method, redirect : "manual" } ) ;
154162 }
155163 if ( proxyConfigured && ! resolved . privateNetwork ) {
156164 warnProxyBoundaryOnce ( ) ;
157- return globalThis . fetch ( url , { ...init , method : "GET" , redirect : "manual" } ) ;
165+ return globalThis . fetch ( url , { ...init , method, redirect : "manual" } ) ;
158166 }
159167 if ( proxyConfigured && resolved . privateNetwork && ! noProxyMatches ( parsed ) ) {
160168 const hostname = normalizeProxyHostname ( parsed . hostname ) ;
161169 throw new Error (
162170 `provider URL resolves to a private-network destination; add ${ hostname } to NO_PROXY before using allowPrivateNetwork with an outbound proxy` ,
163171 ) ;
164172 }
165- return pinnedGet ( url , pickPinnedAddress ( resolved . addresses ) , init . signal ?? undefined , {
173+ const requestOptions = {
166174 headers : init . headers ,
167175 rejectUnauthorized : true ,
168176 context : "provider response" ,
169- } ) ;
177+ } ;
178+ const pinned = pickPinnedAddress ( resolved . addresses ) ;
179+ if ( method === "POST" ) {
180+ return pinnedPost ( url , pinned , ( init as ProviderPostInit ) . body , init . signal ?? undefined , requestOptions ) ;
181+ }
182+ return pinnedGet ( url , pinned , init . signal ?? undefined , requestOptions ) ;
183+ }
184+
185+ export async function providerOutboundGet (
186+ name : string ,
187+ provider : ProviderOutboundConfig ,
188+ url : string ,
189+ init : ProviderGetInit = { } ,
190+ dependencies : ProviderOutboundDependencies = { } ,
191+ ) : Promise < Response > {
192+ return providerOutboundRequest ( name , provider , url , "GET" , init , dependencies ) ;
193+ }
194+
195+ export async function providerOutboundPost (
196+ name : string ,
197+ provider : ProviderOutboundConfig ,
198+ url : string ,
199+ init : ProviderPostInit ,
200+ dependencies : ProviderOutboundDependencies = { } ,
201+ ) : Promise < Response > {
202+ return providerOutboundRequest ( name , provider , url , "POST" , init , dependencies ) ;
170203}
0 commit comments