Import packages/plugin/src/plugin-route-dispatcher.ts
This commit is contained in:
parent
31ff179490
commit
d72797c0fc
|
|
@ -16,14 +16,20 @@ export function createPluginRouteDispatcher(
|
||||||
signal.throwIfAborted()
|
signal.throwIfAborted()
|
||||||
const connection = readConnection()
|
const connection = readConnection()
|
||||||
const handler = connection?.createSharedFetchHandler?.('/api')
|
const handler = connection?.createSharedFetchHandler?.('/api')
|
||||||
|
const upload = route === 'api/session/uploadFileBinary'
|
||||||
|
? (body as { payload: { sessionId: string; mediaType?: string; name?: string; data: string } }).payload : undefined
|
||||||
|
const query = upload === undefined ? '' : `?${new URLSearchParams({ sessionId: upload.sessionId, ...(upload.mediaType === undefined ? {} : { mediaType: upload.mediaType }),
|
||||||
|
...(upload.name === undefined ? {} : { name: upload.name }) })}`
|
||||||
|
const contentType = upload === undefined ? 'application/json' : 'application/octet-stream'
|
||||||
|
const requestBody = upload === undefined ? JSON.stringify(body ?? {}) : Buffer.from(upload.data, 'base64')
|
||||||
let response: Response
|
let response: Response
|
||||||
if (handler !== undefined) {
|
if (handler !== undefined) {
|
||||||
// The authenticated Remote channel has already admitted this peer. Invoke
|
// The authenticated Remote channel has already admitted this peer. Invoke
|
||||||
// the official local Fetch carrier, including on Desktop without HTTP.
|
// the official local Fetch carrier, including on Desktop without HTTP.
|
||||||
response = await withSignal(handler.fetch(new Request(`http://127.0.0.1/${route}`, {
|
response = await withSignal(handler.fetch(new Request(`http://127.0.0.1/${route}${query}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'content-type': 'application/json' },
|
headers: { 'content-type': contentType },
|
||||||
body: JSON.stringify(body ?? {}),
|
body: requestBody,
|
||||||
signal,
|
signal,
|
||||||
}), peer), signal)
|
}), peer), signal)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -37,10 +43,10 @@ export function createPluginRouteDispatcher(
|
||||||
const setCookie = typeof launch.headers.getSetCookie === 'function' ? launch.headers.getSetCookie() : []
|
const setCookie = typeof launch.headers.getSetCookie === 'function' ? launch.headers.getSetCookie() : []
|
||||||
cookie = setCookie.map(item => item.split(';', 1)[0]).join('; ') || undefined
|
cookie = setCookie.map(item => item.split(';', 1)[0]).join('; ') || undefined
|
||||||
}
|
}
|
||||||
response = await fetch(`${origin}/${route}`, {
|
response = await fetch(`${origin}/${route}${query}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'content-type': 'application/json', ...(cookie === undefined ? {} : { cookie }) },
|
headers: { 'content-type': contentType, ...(cookie === undefined ? {} : { cookie }) },
|
||||||
body: JSON.stringify(body ?? {}),
|
body: requestBody,
|
||||||
signal,
|
signal,
|
||||||
})
|
})
|
||||||
if (response.status === 401) cookie = undefined
|
if (response.status === 401) cookie = undefined
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue