mirror of
https://github.com/BradNut/awesome-uses
synced 2025-09-08 17:40:31 +00:00
tee streams
This commit is contained in:
parent
acfef2b3f7
commit
9c2e3c5926
1 changed files with 22 additions and 13 deletions
|
|
@ -4,6 +4,20 @@ import { renderToReadableStream } from 'react-dom/server';
|
||||||
|
|
||||||
const ABORT_DELAY = 5000;
|
const ABORT_DELAY = 5000;
|
||||||
|
|
||||||
|
export async function streamToText(stream: ReadableStream<Uint8Array>): Promise<string> {
|
||||||
|
let result = '';
|
||||||
|
const reader = stream.pipeThrough(new TextDecoderStream()).getReader();
|
||||||
|
while (true) { // eslint-disable-line no-constant-condition
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result += value;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
type CachedResponse = {
|
type CachedResponse = {
|
||||||
html: string;
|
html: string;
|
||||||
date: Date;
|
date: Date;
|
||||||
|
|
@ -33,7 +47,6 @@ export default async function handleRequest(
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
let didError = false;
|
let didError = false;
|
||||||
const chunks: Uint8Array[] = [];
|
const chunks: Uint8Array[] = [];
|
||||||
|
|
||||||
|
|
@ -47,21 +60,17 @@ export default async function handleRequest(
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// body.pip
|
const [toReponse, toCache] = body.tee();
|
||||||
// .on('data', (data) => {
|
|
||||||
// console.log('data', data);
|
streamToText(toCache).then(html => {
|
||||||
// chunks.push(data);
|
console.log('I have the HTML!', html.length);
|
||||||
// })
|
});
|
||||||
// .on('end', () => {
|
|
||||||
// const html = Buffer.concat(chunks).toString('utf8');
|
|
||||||
// cache.set(request.url, { html: html.replace('Rendered Fresh', `Served from Cache ${new Date().toString()}`), date: new Date() });
|
|
||||||
// })
|
|
||||||
|
|
||||||
const headers = new Headers(responseHeaders);
|
const headers = new Headers(responseHeaders);
|
||||||
headers.set("Content-Type", "text/html");
|
headers.set("Content-Type", "text/html");
|
||||||
return new Response(body, {
|
const response = new Response(toReponse, {
|
||||||
headers,
|
headers,
|
||||||
status: didError ? 500 : responseStatusCode,
|
status: didError ? 500 : responseStatusCode,
|
||||||
})
|
});
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue