MCPcopy Create free account
hub / github.com/langgenius/dify / proxyRequest

Function proxyRequest

packages/dev-proxy/src/server.ts:174–208  ·  view source on GitHub ↗
(
  context: Context,
  route: DevProxyRoute,
  fetchImpl: typeof globalThis.fetch,
  allowedOrigins: DevProxyCorsAllowedOrigins,
)

Source from the content-addressed store, hash-verified

172}
173
174const proxyRequest = async (
175 context: Context,
176 route: DevProxyRoute,
177 fetchImpl: typeof globalThis.fetch,
178 allowedOrigins: DevProxyCorsAllowedOrigins,
179) => {
180 const requestUrl = new URL(context.req.url)
181 const targetUrl = buildUpstreamUrl(route.target, requestUrl.pathname, requestUrl.search)
182 const requestHeaders = createProxyRequestHeaders(context.req.raw, targetUrl, route.cookieRewrite)
183 const requestInit: RequestInit & { duplex?: 'half' } = {
184 method: context.req.method,
185 headers: requestHeaders,
186 redirect: 'manual',
187 }
188
189 if (context.req.method !== 'GET' && context.req.method !== 'HEAD') {
190 requestInit.body = context.req.raw.body
191 requestInit.duplex = 'half'
192 }
193
194 const upstreamResponse = await fetchImpl(targetUrl, requestInit)
195 const responseHeaders = createUpstreamResponseHeaders(
196 upstreamResponse,
197 targetUrl,
198 context.req.header('origin'),
199 allowedOrigins,
200 route.cookieRewrite,
201 )
202
203 return new Response(upstreamResponse.body, {
204 status: upstreamResponse.status,
205 statusText: upstreamResponse.statusText,
206 headers: responseHeaders,
207 })
208}
209
210const normalizeRoutePaths = (paths: DevProxyRoute['paths']) => Array.isArray(paths) ? paths : [paths]
211

Callers 1

registerProxyRouteFunction · 0.85

Calls 4

buildUpstreamUrlFunction · 0.85
headerMethod · 0.80

Tested by

no test coverage detected