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}`
|
||||
|
||||
const ws = new WebSocket(url)
|
||||
ws.binaryType = 'arraybuffer'
|
||||
|
||||
let resolve: (v: string) => void
|
||||
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 body = await res.text()
|
||||
const headers: Record<string, string> = {}
|
||||
res.headers.forEach((v, k) => (headers[k] = v))
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
id: msg.id,
|
||||
status: res.status,
|
||||
headers,
|
||||
body,
|
||||
}))
|
||||
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,
|
||||
status: res.status,
|
||||
headers,
|
||||
body,
|
||||
isBinary: false
|
||||
}
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify(responseData))
|
||||
} catch (err: any) {
|
||||
ws.send(JSON.stringify({
|
||||
id: msg.id,
|
||||
status: 500,
|
||||
headers: { "content-type": "text/plain" },
|
||||
body: "error: " + err.message,
|
||||
isBinary: false
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
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