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