added streaming bullshit

performance still isn't good enough for long af tracks, maybe i'll try bf decryption in wasm
This commit is contained in:
uh wot 2021-08-01 01:57:30 +02:00
parent 026edf3808
commit 3fe1482da5
Signed by: uhwot
GPG Key ID: CB2454984587B781
2 changed files with 30 additions and 14 deletions

6
dist/worker.js vendored

File diff suppressed because one or more lines are too long

View File

@ -180,35 +180,51 @@ async function track(id, format, access_token, tagging) {
let { readable, writable } = new TransformStream()
const writer = writable.getWriter()
if (tagging) {
if (id3) {
writer.write(id3.arrayBuffer)
}
const bfKey = await bf_key(id)
console.log(bfKey)
const length = Number(track.headers.get('Content-Length'))
pipeDecryptedStream(writer, track.body, length, bfKey, id3)
return new Response(readable, { status: 200, headers: { 'content-type': 'audio/mpeg' } })
}
async function pipeDecryptedStream(writer, body, length, bfKey) {
const cipher = new Blowfish(bfKey, Blowfish.MODE.CBC, Blowfish.PADDING.NULL)
cipher.setIv('\x00\x01\x02\x03\x04\x05\x06\x07')
const buffer = await track.arrayBuffer()
const length = buffer.byteLength
const reader = body.getReader({ mode: 'byob' })
let byteCount = 0
let end = false
while (!end) {
let chunkEnd = byteCount + 2048
if (chunkEnd > length) {
chunkEnd = length
end = true
end = byteCount + 2048 > length
let chunk
if (!end) {
chunk = new Int8Array(2048)
} else {
chunk = new Int8Array(length - byteCount)
}
let chunk = buffer.slice(byteCount, byteCount+2048)
// if read chunk isn't 2048 bytes, read until it is
// cause of retarded readable streams not having an option to specify min bytes
let tempLength = 0
while (tempLength !== chunk.length) {
let read = (await reader.read(new Int8Array(chunk.length - tempLength))).value
chunk.set(read, tempLength)
tempLength += read.length
}
if (byteCount % 6144 === 0 && !end) {
// encrypted chunk
chunk = cipher.decode(chunk, Blowfish.TYPE.UINT8_ARRAY)
}
writer.write(chunk)
byteCount += 2048
}
return new Response(readable, { status: 200, headers: { 'content-type': 'audio/mpeg' } })
}
async function track_url(json, format) {