*Wrap handleBuffer in filterPromise/streamPromise chain to ensure it is called when the stream is ready

*Use an empty uint8array on reduce to handle edge cases
This commit is contained in:
Derrick Hammer 2022-08-22 03:09:32 -04:00
parent 0d5d3fa1e3
commit eb65cc6b10
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 25 additions and 36 deletions

View File

@ -282,42 +282,30 @@ export default class IpfsProvider extends BaseProvider {
const handleBuffer = () => { const handleBuffer = () => {
if (buffer.length) { if (buffer.length) {
let mode = contentModes[contentType as string]; filterPromise.then(() => {
buffer = buffer.map((item: Uint8Array | ArrayBuffer) => { streamPromise = streamPromise.then(() => {
if (item instanceof ArrayBuffer) { let mode = contentModes[contentType as string];
return new Uint8Array(item); buffer = buffer.map((item: Uint8Array | ArrayBuffer) => {
} if (item instanceof ArrayBuffer) {
return item; return new Uint8Array(item);
});
if (mode === CONTENT_MODE_BUFFERED) {
let data: string | Uint8Array = Uint8Array.from(
buffer.reduce(
(previousValue: Uint8Array, currentValue: Uint8Array) => {
return Uint8Array.from([...previousValue, ...currentValue]);
} }
) return item;
); });
/* if (contentType === "text/html") { if (mode === CONTENT_MODE_BUFFERED) {
data = new TextDecoder("utf-8", { fatal: true }).decode(data); let data: string | Uint8Array = Uint8Array.from(
let htmlDoc = new DOMParser().parseFromString( buffer.reduce(
data as string, (previousValue: Uint8Array, currentValue: Uint8Array) => {
contentType return Uint8Array.from([...previousValue, ...currentValue]);
); },
let found = htmlDoc.documentElement.querySelectorAll( new Uint8Array()
'meta[http-equiv="Content-Security-Policy"]' )
); );
filter.write(data);
if (found.length) { } else if (mode == CONTENT_MODE_CHUNKED) {
found.forEach((item) => item.remove()); buffer.forEach((data) => filter.write(data));
data = htmlDoc.documentElement.outerHTML; }
} });
});
data = new TextEncoder().encode(data);
}*/
filter.write(data);
} else if (mode == CONTENT_MODE_CHUNKED) {
buffer.forEach((data) => filter.write(data));
}
} }
}; };
if (cachedPage) { if (cachedPage) {
@ -345,7 +333,8 @@ export default class IpfsProvider extends BaseProvider {
(cacheBuffer as Uint8Array[]).reduce( (cacheBuffer as Uint8Array[]).reduce(
(previousValue: Uint8Array, currentValue: Uint8Array) => { (previousValue: Uint8Array, currentValue: Uint8Array) => {
return Uint8Array.from([...previousValue, ...currentValue]); return Uint8Array.from([...previousValue, ...currentValue]);
} },
new Uint8Array()
) )
); );