v2.0.4: fetch() вместо XHR, чанки 55KB, задержка 500ms — fix ERR_HTTP2_PROTOCOL_ERROR

This commit is contained in:
2026-06-18 06:46:35 +04:00
parent 5e9e754af0
commit 6c296f4f22
+27 -34
View File
@@ -60,7 +60,7 @@
<body> <body>
<div class="topbar"> <div class="topbar">
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes"> <img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v2.0.3</span></span> <span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v2.0.4</span></span>
</div> </div>
<div class="content"> <div class="content">
@@ -172,51 +172,44 @@
} }
var b64 = btoa(b64Chunks.join('')); var b64 = btoa(b64Chunks.join(''));
var checksum = SparkMD5.ArrayBuffer.hash(buffer); var checksum = SparkMD5.ArrayBuffer.hash(buffer);
var CHUNK = 30 * 1024; var CHUNK = 55 * 1024; // меньше запросов → меньше HTTP/2 ошибок
var totalChunks = Math.ceil(b64.length / CHUNK); var totalChunks = Math.ceil(b64.length / CHUNK);
var uploadId = 'u' + Date.now().toString(36) + Math.random().toString(36).substr(2, 7); var uploadId = 'u' + Date.now().toString(36) + Math.random().toString(36).substr(2, 7);
var done = 0; var done = 0;
console.log('uploadFileChunked: ' + file.name + ' b64=' + b64.length + ' chunks=' + totalChunks + ' uploadId=' + uploadId); console.log('uploadFileChunked: ' + file.name + ' b64=' + b64.length + ' chunks=' + totalChunks + ' uploadId=' + uploadId);
function sendChunk(i) { function sendChunk(i) {
if (i >= totalChunks) { if (i >= totalChunks) {
console.log('finalize: ' + uploadId); console.log('finalize: ' + uploadId);
var xhr = new XMLHttpRequest(); fetch('/v2/finalize', {
xhr.open('POST', '/v2/finalize'); method: 'POST',
xhr.setRequestHeader('Content-Type', 'application/json'); headers: {'Content-Type': 'application/json'},
xhr.timeout = 60000; body: JSON.stringify({upload_id: uploadId, filename: file.name, total: totalChunks, checksum: checksum})
xhr.onload = function() { }).then(function(r) {
try { if (!r.ok) throw new Error('HTTP ' + r.status);
var r = JSON.parse(xhr.responseText); return r.json();
if (r.ok) resolve({contract_id: r.contract_id}); }).then(function(data) {
else reject(new Error(r.error)); if (data.ok) resolve({contract_id: data.contract_id});
} catch(e) { reject(new Error('Bad JSON')); } else reject(new Error(data.error));
}; }).catch(function(e) { reject(e); });
xhr.onerror = function() { reject(new Error('Сеть')); };
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
xhr.send(JSON.stringify({upload_id: uploadId, filename: file.name, total: totalChunks, checksum: checksum}));
return; return;
} }
var piece = b64.substring(i * CHUNK, (i + 1) * CHUNK); var piece = b64.substring(i * CHUNK, (i + 1) * CHUNK);
console.log('chunk ' + i + '/' + totalChunks + ' send ' + piece.length + ' bytes'); console.log('chunk ' + i + '/' + totalChunks + ' send ' + piece.length + ' bytes');
var xhr = new XMLHttpRequest(); fetch('/v2/chunk', {
xhr.open('POST', '/v2/chunk'); method: 'POST',
xhr.setRequestHeader('Content-Type', 'application/json'); headers: {'Content-Type': 'application/json'},
xhr.timeout = 30000; body: JSON.stringify({upload_id: uploadId, index: i, total: totalChunks, data: piece})
xhr.onload = function() { }).then(function(r) {
if (xhr.status === 200) { if (!r.ok) throw new Error('HTTP ' + r.status);
try { return r.json();
var r = JSON.parse(xhr.responseText); }).then(function(data) {
if (!r.ok) { reject(new Error(r.error)); return; } if (!data.ok) { reject(new Error(data.error)); return; }
done++; done++;
if (onProgress) onProgress(Math.round(done / totalChunks * 100)); if (onProgress) onProgress(Math.round(done / totalChunks * 100));
setTimeout(function() { sendChunk(i + 1); }, 50); setTimeout(function() { sendChunk(i + 1); }, 500); // дать HTTP/2 стримам закрыться
} catch(e) { reject(new Error('Bad JSON')); } }).catch(function(e) { reject(e); });
} else { reject(new Error('HTTP ' + xhr.status)); }
};
xhr.onerror = function() { reject(new Error('Сеть')); };
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
xhr.send(JSON.stringify({upload_id: uploadId, index: i, total: totalChunks, data: piece}));
} }
sendChunk(0); sendChunk(0);
}; };