Default harness runs to unbounded and make the stream page reconnect itself

This commit is contained in:
flamingcow
2026-08-30 19:15:25 -07:00
parent 5a74fe0c76
commit 2cdeccc84d
2 changed files with 39 additions and 22 deletions
+24 -16
View File
@@ -24,23 +24,31 @@ const page = `<!doctype html>
<script>
(async () => {
const cv = document.querySelector('canvas'), g = cv.getContext('2d');
const rd = (await fetch('frames')).body.getReader();
let buf = new Uint8Array(0);
for (;;) {
const {value, done} = await rd.read();
if (done) return;
const b = new Uint8Array(buf.length + value.length);
b.set(buf); b.set(value, buf.length); buf = b;
for (;;) {
if (buf.length < 12) break;
const dv = new DataView(buf.buffer, buf.byteOffset);
const x = dv.getUint32(0, true), y = dv.getUint32(4, true), n = dv.getUint32(8, true);
if (buf.length < 12 + n) break;
const bmp = await createImageBitmap(new Blob([buf.slice(12, 12 + n)], {type: 'image/png'}));
if (x === 0 && y === 0 && cv.width !== bmp.width) { cv.width = bmp.width; cv.height = bmp.height; }
g.drawImage(bmp, x, y);
buf = buf.slice(12 + n);
}
try {
const res = await fetch('frames');
if (res.ok) {
const rd = res.body.getReader();
let buf = new Uint8Array(0);
for (;;) {
const {value, done} = await rd.read();
if (done) break;
const b = new Uint8Array(buf.length + value.length);
b.set(buf); b.set(value, buf.length); buf = b;
for (;;) {
if (buf.length < 12) break;
const dv = new DataView(buf.buffer, buf.byteOffset);
const x = dv.getUint32(0, true), y = dv.getUint32(4, true), n = dv.getUint32(8, true);
if (buf.length < 12 + n) break;
const bmp = await createImageBitmap(new Blob([buf.slice(12, 12 + n)], {type: 'image/png'}));
if (x === 0 && y === 0 && cv.width !== bmp.width) { cv.width = bmp.width; cv.height = bmp.height; }
g.drawImage(bmp, x, y);
buf = buf.slice(12 + n);
}
}
}
} catch (e) {}
await new Promise(r => setTimeout(r, 1000));
}
})();
</script>