mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
The text-like viewers read a whole file (up to 32 MB) and showed at most 1000 rows or 2048 names. Now only what is on the screen is fetched and only that is in the page (client/lib/attachmentWindow.js): - Text, .log, CSV/TSV, source code, and JSON and Markdown larger than one chunk are read through HTTP Range requests in 256 KB chunks, at most 16 kept. The scrollbar is the whole file: dragged to the middle it shows the middle, scrolled a little it moves row by row, at its end is the last row. A row ends at a newline or at the next multiple of 4096 bytes of the file, so rows are found the same from any byte, a 50 MB line of minified JSON is many rows, and no UTF-8 character is split. A server that ignores Range (a legacy CollectionFS attachment) is read only up to the bytes wanted. - ZIP lists its names from the central directory through Range requests, ZIP64 too, without downloading any entry. - tar and tar.gz, iCalendar and OpenDocument are read in order as far as they are scrolled - infinite scroll downwards - since compressed data cannot be read from its middle. An OpenDocument's content.xml is found in the central directory and only it is inflated and parsed as a stream; a sheet's rows repeated to the edge of the grid cost nothing. - Plain text no longer goes to an <object>, which loaded it whole. A footnote's paragraphs no longer end the OpenDocument paragraph they sit in. tests/attachmentWindow.test.cjs drives the rows and views against a fake Range server; the viewer and ZIP tests show no limit is left and that only windows are fetched; attachment-rich-preview.e2e.js opens a 150,000-row CSV in Chromium and finds only 206 ranges fetched, the middle and the last row shown, and only a screenful of rows in the page. Thanks to xet7 !
38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
'use strict';
|
|
// A fake attachment server for the slideshow's windowed viewers: 206 Partial
|
|
// Content for a Range request, as server/routes/universalFileServer.js
|
|
// answers (#6745), or - ignoreRange - the whole file as a stream, as it
|
|
// answers for a legacy CollectionFS attachment. log counts the requests and
|
|
// the bytes sent, so a test can show only the window was read.
|
|
|
|
function rangeServer(bytes, { ignoreRange = false, status = 200 } = {}) {
|
|
const log = { requests: 0, sent: 0, ranges: [] };
|
|
const fetchImpl = async (url, { headers = {} } = {}) => {
|
|
log.requests += 1;
|
|
if (status !== 200) return new Response('', { status });
|
|
const m = /^bytes=(\d+)-(\d+)$/.exec(headers.Range || '');
|
|
if (m && !ignoreRange) {
|
|
const a = Number(m[1]);
|
|
const b = Math.min(bytes.length - 1, Number(m[2]));
|
|
log.ranges.push([a, b]);
|
|
log.sent += b - a + 1;
|
|
return new Response(bytes.slice(a, b + 1), { status: 206,
|
|
headers: { 'Content-Range': `bytes ${a}-${b}/${bytes.length}` } });
|
|
}
|
|
let at = 0;
|
|
const stream = new ReadableStream({
|
|
pull(controller) {
|
|
if (at >= bytes.length) { controller.close(); return; }
|
|
const piece = bytes.slice(at, at + 65536);
|
|
at += piece.length;
|
|
log.sent += piece.length;
|
|
controller.enqueue(piece);
|
|
},
|
|
});
|
|
return new Response(stream, { status: 200, headers: { 'Content-Length': String(bytes.length) } });
|
|
};
|
|
return { fetchImpl, log };
|
|
}
|
|
|
|
module.exports = { rangeServer };
|