tunnel binary files
This commit is contained in:
parent
d1e2e7d7a4
commit
94b0eb4dad
|
|
@ -84,6 +84,8 @@ export async function connectSneaker(app: string, subdomain = ""): Promise<strin
|
||||||
if (subdomain) url += `&subdomain=${subdomain}`
|
if (subdomain) url += `&subdomain=${subdomain}`
|
||||||
|
|
||||||
const ws = new WebSocket(url)
|
const ws = new WebSocket(url)
|
||||||
|
ws.binaryType = 'arraybuffer'
|
||||||
|
|
||||||
let resolve: (v: string) => void
|
let resolve: (v: string) => void
|
||||||
let promise = new Promise<string>(res => resolve = res)
|
let promise = new Promise<string>(res => resolve = res)
|
||||||
|
|
||||||
|
|
@ -115,25 +117,57 @@ export async function connectSneaker(app: string, subdomain = ""): Promise<strin
|
||||||
|
|
||||||
const res = await nose.fetch(req)
|
const res = await nose.fetch(req)
|
||||||
|
|
||||||
const body = await res.text()
|
|
||||||
const headers: Record<string, string> = {}
|
const headers: Record<string, string> = {}
|
||||||
res.headers.forEach((v, k) => (headers[k] = v))
|
res.headers.forEach((v, k) => (headers[k] = v))
|
||||||
|
|
||||||
ws.send(JSON.stringify({
|
const contentType = res.headers.get('content-type') || ''
|
||||||
|
const isBinary = isBinaryContentType(contentType)
|
||||||
|
|
||||||
|
let body: string | ArrayBuffer
|
||||||
|
let responseData: any
|
||||||
|
|
||||||
|
if (isBinary) {
|
||||||
|
const arrayBuffer = await res.arrayBuffer()
|
||||||
|
const base64 = Buffer.from(arrayBuffer).toString('base64')
|
||||||
|
|
||||||
|
responseData = {
|
||||||
|
id: msg.id,
|
||||||
|
status: res.status,
|
||||||
|
headers,
|
||||||
|
body: base64,
|
||||||
|
isBinary: true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
body = await res.text()
|
||||||
|
|
||||||
|
responseData = {
|
||||||
id: msg.id,
|
id: msg.id,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
headers,
|
headers,
|
||||||
body,
|
body,
|
||||||
}))
|
isBinary: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.send(JSON.stringify(responseData))
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
id: msg.id,
|
id: msg.id,
|
||||||
status: 500,
|
status: 500,
|
||||||
headers: { "content-type": "text/plain" },
|
headers: { "content-type": "text/plain" },
|
||||||
body: "error: " + err.message,
|
body: "error: " + err.message,
|
||||||
|
isBinary: false
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return promise
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isBinaryContentType(contentType: string): boolean {
|
||||||
|
const binaryTypes = [
|
||||||
|
'image/', 'audio/', 'video/', 'application/octet-stream',
|
||||||
|
'application/pdf', 'application/zip', 'font/'
|
||||||
|
]
|
||||||
|
return binaryTypes.some(type => contentType.startsWith(type))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user