🐛 fix: Header seek error

This commit is contained in:
2024-07-02 10:34:29 +07:00
parent 9514a3d527
commit b338319f80

View File

@@ -49,11 +49,11 @@ export class SeekableStream {
}, 2000); }, 2000);
this.timer.start(); this.timer.start();
this.tick(); this.tick(seekTime);
if (seekTime !== 0) this.seek(); //if (seekTime !== 0) this.seek();
} }
private async tick() { private async tick(seekTime?: number) {
if (this.destroyed) { if (this.destroyed) {
console.debug( console.debug(
`[${this.id}] > Stream already destroyed, not ticking`, `[${this.id}] > Stream already destroyed, not ticking`,
@@ -69,17 +69,16 @@ export class SeekableStream {
this.locked = true; this.locked = true;
// Get header // Get header
const rangeHeader = `bytes=${this.bytesReceived}-${this.information.indexRange.end}`; console.debug(
const request = await getStream(this.information.url, { `[${this.id}] > Requesting range | 0-${this.information.indexRange.end}`,
);
let request = await getStream(this.information.url, {
headers: { headers: {
range: rangeHeader, range: `bytes=0-${this.information.indexRange.end}`,
}, },
}).catch((err: Error) => err); }).catch((err: Error) => err);
console.debug(
`[${this.id}] > Request first tick header range | ${rangeHeader}`,
);
if (request instanceof Error) { if (request instanceof Error) {
console.debug( console.debug(
`[${this.id}] > Request first tick error: ${request.message}`, `[${this.id}] > Request first tick error: ${request.message}`,
@@ -127,15 +126,28 @@ export class SeekableStream {
this.bytesReceived += chunk.length; this.bytesReceived += chunk.length;
}); });
incomingStream.on("end", async () => { incomingStream.pipe(this.stream, { end: false });
console.debug(`[${this.id}] > Header received, unlocking`);
this.locked = false; this.stream.once("headComplete", () => {
console.debug(`[${this.id}] > Header parsed, unpiping...`);
incomingStream.unpipe(this.stream);
incomingStream.destroy(); incomingStream.destroy();
this.stream.state = WebmSeekerState.READING_DATA;
this.stream.headerparsed = true;
this.debugLog(); this.debugLog();
if (seekTime !== 0) this.seek();
this.locked = false; this.locked = false;
}); });
// incomingStream.on("end", async () => {
// // console.debug(`[${this.id}] > Header received, unlocking`);
// //this.locked = false;
// //incomingStream.destroy();
// //this.debugLog();
// //this.locked = false;
// });
return; return;
} }
@@ -251,6 +263,7 @@ export class SeekableStream {
}).catch((err: Error) => err); }).catch((err: Error) => err);
if (req instanceof Error || req.status >= 400) { if (req instanceof Error || req.status >= 400) {
console.error(`[${this.id}] > Request error: ${req}`);
reject(false); reject(false);
return; return;
} }
@@ -285,6 +298,7 @@ export class SeekableStream {
const bytes = this.stream.seek(this.information.fileSize); const bytes = this.stream.seek(this.information.fileSize);
if (bytes instanceof Error) { if (bytes instanceof Error) {
console.error(`[${this.id}] > Seek error: ${bytes.message}`);
// TODO: Handle seek error // TODO: Handle seek error
this.destroy(); this.destroy();
return false; return false;
@@ -350,6 +364,7 @@ export class SeekableStream {
`Buffer Remaining: ${(this.stream.readableLength / (1024 * 1024)).toFixed(3)} MB | ` + `Buffer Remaining: ${(this.stream.readableLength / (1024 * 1024)).toFixed(3)} MB | ` +
`${!false ? `Buffer Sufficient: ${isBufferSufficient} | ` : ``}` + `${!false ? `Buffer Sufficient: ${isBufferSufficient} | ` : ``}` +
`Locked: ${this.locked} | `, // + `Locked: ${this.locked} | `, // +
//`Stream URL: ${this.information.url} | `,
//`Fetch Completed: ${this.fetchCompleted}`, //`Fetch Completed: ${this.fetchCompleted}`,
); );
} }