From b1c2f640412f05fd3e307a5a1e20a107c810d69a Mon Sep 17 00:00:00 2001 From: coomdev Date: Thu, 28 Apr 2022 12:35:22 +0200 Subject: [PATCH] slightly optimize requests --- src/main.ts | 26 ++++++++++++-------------- src/platform.ts | 10 +++++++--- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/main.ts b/src/main.ts index 47c6642..dde0c34 100644 --- a/src/main.ts +++ b/src/main.ts @@ -105,16 +105,15 @@ const processImage = async (src: string, fn: string, hex: string, prevurl: strin let found: boolean | undefined; let chunk: ReadableStreamDefaultReadResult = { done: true }; do { - const { value, done } = await iter.next(found === false); + const { value, done } = await iter.next(typeof found === "boolean"); if (done) { chunk = { done: true } as ReadableStreamDefaultReadDoneResult; } else { chunk = { done: false, value } as ReadableStreamDefaultReadValueResult; - } - if (!done) cumul = Buffer.concat([cumul, value!]); - found = await proc.has_embed(cumul); - } while (found !== false && !chunk.done); + found = await proc.has_embed(cumul); + } + } while (found !== false && !chunk.done /* Because we only embed links now, it's safe to assume we get everything we need in the first chunk */); await iter.next(true); if (found === false) { //console.log(`Gave up on ${src} after downloading ${cumul.byteLength} bytes...`); @@ -258,15 +257,14 @@ const scrapeBoard = async (self: HTMLButtonElement) => { let found: boolean | undefined; let chunk: ReadableStreamDefaultReadResult = { done: true }; do { - const { value, done } = await iter.next(found === false); + const { value, done } = await iter.next(typeof found === "boolean"); if (done) { chunk = { done: true } as ReadableStreamDefaultReadDoneResult; } else { chunk = { done: false, value } as ReadableStreamDefaultReadValueResult; - } - if (!done) cumul = Buffer.concat([cumul, value!]); - found = await proc.has_embed(cumul); + found = await proc.has_embed(cumul); + } } while (found !== false && !chunk.done); await iter.next(true); return found === true; @@ -415,11 +413,11 @@ const startup = async (is4chanX = true) => { }); scts?.appendChild(button); - const appHost = textToElement(`
`); + const appHost = textToElement(`
`); const appInstance = new App({ target: appHost }); document.body.append(appHost); - const scrollHost = textToElement(`
`); + const scrollHost = textToElement(`
`); new ScrollHighlighter({ target: scrollHost }); document.body.append(scrollHost); @@ -546,8 +544,8 @@ function processAttachments(post: HTMLDivElement, ress: [EmbeddedFile, boolean][ const quot = qp.getTextBox(post); const textInsertCursor = document.createElement('div'); quot?.appendChild(textInsertCursor); - const filehost: HTMLElement | null = ft.querySelector('.filehost'); - const eyehost: HTMLElement | null = info.querySelector('.eyehost'); + const filehost: HTMLElement | null = ft.querySelector('.fiilehost'); + const eyehost: HTMLElement | null = info.querySelector('.eyeehost'); const imgcont = filehost || document.createElement('div'); const eyecont = eyehost || document.createElement('span'); @@ -560,7 +558,7 @@ function processAttachments(post: HTMLDivElement, ress: [EmbeddedFile, boolean][ } if (!eyehost) { info.append(eyecont); - eyecont.classList.add("eyehost"); + eyecont.classList.add("eyeehost"); } else { eyecont.innerHTML = ''; } diff --git a/src/platform.ts b/src/platform.ts index bda1f97..f199c90 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -104,8 +104,8 @@ export async function* streamRemote(url: string, chunkSize = 72 * 1024, fetchRes stream?.releaseLock(); return; } - const headers = await getHeaders(url); - const size = +headers['content-length']; + //const headers = await getHeaders(url); + let size = Number.POSITIVE_INFINITY; let ptr = 0; let fetchSize = chunkSize; while (ptr != size) { @@ -115,7 +115,11 @@ export async function* streamRemote(url: string, chunkSize = 72 * 1024, fetchRes if (!('content-length' in obj)) { console.warn("no content lenght???", url); break; - } const len = +obj['content-length']; + } + if ('content-range' in obj) { + size = +obj['content-range'].split('/')[1]; + } + const len = +obj['content-length']; ptr += len; if (fetchRestOnNonCanceled) fetchSize = size;