i have no idea if this works or not

@Jack5079 can you please make file get work with honofile
This commit is contained in:
May 2023-11-06 01:10:39 +00:00 committed by GitHub
parent dddfd9dbcb
commit 460df7164a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -70,7 +70,21 @@ export interface StatusCodeError {
message: string message: string
} }
/* */ // this is probably really stupid. I'm sorry!
type ResolveType<T extends Promise<any>> = T extends Promise<infer U> ? U: never;
async function pushWebStream(stream: Readable, webStream: ReadableStream) {
const reader = await webStream.getReader()
let result: ResolveType<ReturnType<typeof reader.read>> = { done: false, value: undefined }
let last = true
while ( !result.done ) {
result = await reader.read()
last = stream.push(result.value)
}
return last
}
export default class Files { export default class Files {
config: Configuration config: Configuration
@ -366,6 +380,7 @@ export default class Files {
if (!file.sizeInBytes) if (!file.sizeInBytes)
file_updates.sizeInBytes = atSIB.reduce((a, b) => a + b, 0) file_updates.sizeInBytes = atSIB.reduce((a, b) => a + b, 0)
if (!file.chunkSize) file_updates.chunkSize = atSIB[0] if (!file.chunkSize) file_updates.chunkSize = atSIB[0]
if (Object.keys(file_updates).length) { if (Object.keys(file_updates).length) {
let valid_fp_keys = ["sizeInBytes", "chunkSize"] let valid_fp_keys = ["sizeInBytes", "chunkSize"]
let isValidFilePointerKey = ( let isValidFilePointerKey = (
@ -378,6 +393,8 @@ export default class Files {
} }
// The original was a callback so I don't think I'm supposed to `await` this -Jack // The original was a callback so I don't think I'm supposed to `await` this -Jack
// Jack you need to get more sleep man we're using fs/promises
// but also it would slow us down so maybe not
writeFile( writeFile(
process.cwd() + "/.data/files.json", process.cwd() + "/.data/files.json",
JSON.stringify( JSON.stringify(
@ -396,11 +413,8 @@ export default class Files {
return null return null
} }
let d = await axios let headers: HeadersInit =
.get(scanning_chunk.url, { useRanges
responseType: "arraybuffer",
headers: {
...(useRanges
? { ? {
Range: `bytes=${ Range: `bytes=${
position == 0 && position == 0 &&
@ -419,23 +433,17 @@ export default class Files {
: "" : ""
}`, }`,
} }
: {}), : {}
},
}) let d = await fetch(scanning_chunk.url, {headers})
.catch((e: Error) => { .catch((e: Error) => {
console.error(e) console.error(e)
return {body: "__ERR"}
}) })
position++ position++
if (d) { return d.body
return d.data
} else {
throw {
status: 500,
message: "internal server error",
}
}
} }
let ord: number[] = [] let ord: number[] = []
@ -447,17 +455,19 @@ export default class Files {
if (!lastChunkSent) return if (!lastChunkSent) return
lastChunkSent = false lastChunkSent = false
getNextChunk().then(async (nextChunk) => { getNextChunk().then(async (nextChunk) => {
if (nextChunk == "__ERR") { if (typeof nextChunk == "string") {
this.destroy(new Error("file read error")) this.destroy(new Error("file read error"))
return return
} }
let response = this.push(nextChunk)
if (!nextChunk) return // EOF if (!nextChunk) return // EOF
let response = await pushWebStream(this, nextChunk)
while (response) { while (response) {
let nextChunk = await getNextChunk() let nextChunk = await getNextChunk()
response = this.push(nextChunk) response = await pushWebStream(this, nextChunk)
if (!nextChunk) return if (!nextChunk) return
} }
lastChunkSent = true lastChunkSent = true