Some checks failed
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Failing after 8m39s
Build and deploy / Secret scan and release gate (push) Successful in 4s
Build and deploy / Publish images (push) Has been skipped
The worker scans one file at a time for every customer. On 2026-12-02 two files were checked together, and the one waiting behind the other gave up after two and a half minutes with "Verificação de segurança pendente"; its budget counted only its own scan, not the queue. The page now waits the file's own time plus the 15 minutes the worker gives one file before it calls the scan stuck (SCAN_STUCK_SECONDS), polling each second for the first two and a half minutes and then every five, and stops at once when the customer removes the file. A scan that never ends still says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
224 lines
7.3 KiB
JavaScript
224 lines
7.3 KiB
JavaScript
// Closing the upload: a short R2 failure is retried after 3 s and 6 s
|
|
// (contract output/revisao/contrato-r2-retry.md, Browser B1-B6).
|
|
// Dependency-free: Node loads web/upload.js with stub browser globals.
|
|
import assert from 'node:assert/strict';
|
|
import {readFileSync} from 'node:fs';
|
|
import vm from 'node:vm';
|
|
|
|
globalThis.window = globalThis;
|
|
globalThis.localStorage = {
|
|
store: new Map(),
|
|
getItem(key) { return this.store.has(key) ? this.store.get(key) : null; },
|
|
setItem(key, value) { this.store.set(key, String(value)); },
|
|
removeItem(key) { this.store.delete(key); },
|
|
};
|
|
|
|
let calls;
|
|
let delays;
|
|
let fetches;
|
|
let onTimeout;
|
|
let scanStates = [];
|
|
|
|
globalThis.setTimeout = (fn, delay) => {
|
|
delays.push(delay);
|
|
onTimeout?.(fn, delay);
|
|
fn();
|
|
return 0;
|
|
};
|
|
|
|
globalThis.XMLHttpRequest = class {
|
|
constructor() { this.status = 200; this.upload = {}; }
|
|
open() {}
|
|
send() { this.onload?.(); }
|
|
abort() { this.onabort?.(); }
|
|
};
|
|
|
|
globalThis.fetch = async (url, options) => {
|
|
fetches.push({url, options});
|
|
return {ok: true};
|
|
};
|
|
|
|
vm.runInThisContext(readFileSync('web/upload.js', 'utf8'));
|
|
|
|
const completeCalls = () => calls.filter(call => call.path === '/uploads/u1/complete');
|
|
const withStatus = (message, status) => {
|
|
const error = new Error(message);
|
|
error.status = status;
|
|
return error;
|
|
};
|
|
|
|
const makeApi = completeSteps => {
|
|
let completed = 0;
|
|
return async (path, body, options) => {
|
|
calls.push({path, body, options});
|
|
if (path === '/uploads') return {id: 'u1', part_bytes: 1000, parts: [], complete: false};
|
|
if (path === '/uploads/u1/parts/1') return {url: 'https://r2.test/part1'};
|
|
if (path === '/uploads/u1/complete') {
|
|
const step = completeSteps[Math.min(completed, completeSteps.length - 1)];
|
|
completed++;
|
|
if (step instanceof Error) throw step;
|
|
return step ?? {};
|
|
}
|
|
if (path === '/uploads/u1') return {scan_state: scanStates.length ? scanStates.shift() : 'clean'};
|
|
throw new Error('caminho inesperado: ' + path);
|
|
};
|
|
};
|
|
|
|
// One independent run: fresh records, a fresh File and a fresh api.
|
|
const run = async (completeSteps, {abortAfter} = {}) => {
|
|
calls = [];
|
|
delays = [];
|
|
fetches = [];
|
|
const controller = new AbortController();
|
|
onTimeout = abortAfter === undefined
|
|
? undefined
|
|
: (fn, delay) => { if (delay === abortAfter) controller.abort(); };
|
|
const api = makeApi(completeSteps);
|
|
const file = new File([new Uint8Array(1000)], 'a.png', {lastModified: 1});
|
|
let result;
|
|
let error;
|
|
try {
|
|
result = await window.dtfUpload(file, {
|
|
api,
|
|
prefix: '/uploads',
|
|
progress() {},
|
|
onBytes() {},
|
|
scope: 'guest',
|
|
resume: false,
|
|
signal: controller.signal,
|
|
});
|
|
} catch (caught) {
|
|
error = caught;
|
|
}
|
|
onTimeout = undefined;
|
|
return {result, error, calls, delays, fetches};
|
|
};
|
|
|
|
// B1. complete answers: no retry, upload continues to the scan wait.
|
|
{
|
|
const {result, error, delays} = await run([{}]);
|
|
assert.equal(error, undefined);
|
|
assert.equal(result, 'u1');
|
|
assert.equal(completeCalls().length, 1);
|
|
assert.deepEqual(delays, []);
|
|
}
|
|
|
|
// B2. No status (non-JSON reply or network) then success.
|
|
{
|
|
const noStatus = new Error('O serviço está indisponível. Tente novamente em instantes.');
|
|
const {result, error, delays} = await run([noStatus, {}]);
|
|
assert.equal(error, undefined);
|
|
assert.equal(result, 'u1');
|
|
assert.equal(completeCalls().length, 2);
|
|
assert.deepEqual(delays, [3000]);
|
|
}
|
|
|
|
// B2. A 503 then success, same wait.
|
|
{
|
|
const unavailable = withStatus('O serviço está indisponível. Tente novamente em instantes.', 503);
|
|
const {result, error, delays} = await run([unavailable, {}]);
|
|
assert.equal(error, undefined);
|
|
assert.equal(result, 'u1');
|
|
assert.equal(completeCalls().length, 2);
|
|
assert.deepEqual(delays, [3000]);
|
|
}
|
|
|
|
// B3. Three failures: the third error is thrown, after 3 s and 6 s.
|
|
{
|
|
const failures = [
|
|
new Error('falha 1'),
|
|
new Error('falha 2'),
|
|
new Error('falha 3'),
|
|
];
|
|
const {result, error, delays} = await run(failures);
|
|
assert.equal(result, undefined);
|
|
assert.equal(error, failures[2]);
|
|
assert.equal(completeCalls().length, 3);
|
|
assert.deepEqual(delays, [3000, 6000]);
|
|
}
|
|
|
|
// B4. Only the last attempt may be reported: attempts 1 and 2 expect the
|
|
// server errors, attempt 3 passes no options.
|
|
{
|
|
const failures = [
|
|
new Error('falha 1'),
|
|
new Error('falha 2'),
|
|
new Error('falha 3'),
|
|
];
|
|
await run(failures);
|
|
const options = completeCalls().map(call => call.options);
|
|
assert.deepEqual(options, [
|
|
{expected: [500, 502, 503, 504]},
|
|
{expected: [500, 502, 503, 504]},
|
|
{},
|
|
]);
|
|
}
|
|
|
|
// B5. A 409 is thrown at once, one call and no wait.
|
|
{
|
|
const conflict = withStatus('O envio do arquivo não chegou inteiro.', 409);
|
|
const {result, error, delays} = await run([conflict]);
|
|
assert.equal(result, undefined);
|
|
assert.equal(error, conflict);
|
|
assert.equal(completeCalls().length, 1);
|
|
assert.deepEqual(delays, []);
|
|
}
|
|
|
|
// B6. Aborted during the wait: the upload is given back (DELETE) and an
|
|
// AbortError is thrown; complete is not asked again.
|
|
{
|
|
const noStatus = new Error('O serviço está indisponível. Tente novamente em instantes.');
|
|
const {result, error, delays, fetches: requested} = await run([noStatus], {abortAfter: 3000});
|
|
assert.equal(result, undefined);
|
|
assert.equal(error?.name, 'AbortError');
|
|
assert.equal(completeCalls().length, 1);
|
|
assert.deepEqual(delays, [3000]);
|
|
assert.equal(requested.length, 1);
|
|
assert.equal(requested[0].url, '/api/uploads/u1');
|
|
assert.equal(requested[0].options.method, 'DELETE');
|
|
}
|
|
|
|
// A file the phone can no longer read (Android NotReadableError, DTF-WEB-2A):
|
|
// the customer is told in Portuguese to pick it again, and nothing is sent.
|
|
{
|
|
const RealBlob = globalThis.Blob;
|
|
globalThis.Blob = class extends RealBlob {
|
|
arrayBuffer() { return Promise.reject(new DOMException('The requested file could not be read', 'NotReadableError')); }
|
|
};
|
|
try {
|
|
const {result, error, calls} = await run([{}]);
|
|
assert.equal(result, undefined);
|
|
assert.equal(error?.unreadable, true);
|
|
assert.match(error.message, /salve no aparelho e escolha de novo/);
|
|
// Sentry learns the kind of file, never its name (DTF-WEB-2D).
|
|
assert.deepEqual(error.detalhe, {erro: 'NotReadableError', tipo: '', extensao: 'png', mb: 0});
|
|
assert.ok(!JSON.stringify(error.detalhe).includes('a.png'));
|
|
assert.equal(calls.length, 0, 'nothing reaches the server');
|
|
} finally {
|
|
globalThis.Blob = RealBlob;
|
|
}
|
|
}
|
|
|
|
// The antivirus takes one file at a time for every customer: a file waiting
|
|
// behind others is still waited for, past the old two and a half minutes
|
|
// (Sentry DTF-WEB-2E), polling every 5 s after the first 150.
|
|
{
|
|
scanStates = Array(250).fill('pending');
|
|
const {result, error} = await run([{}]);
|
|
assert.equal(error, undefined);
|
|
assert.equal(result, 'u1');
|
|
const waits = delays.filter(d => d === 1000 || d === 5000);
|
|
assert.ok(waits.filter(d => d === 1000).length === 150 && waits.includes(5000));
|
|
}
|
|
// A scan the server never finishes ends with the message, after its 15 minutes.
|
|
{
|
|
scanStates = Array(5000).fill('pending');
|
|
const {error} = await run([{}]);
|
|
assert.match(error?.message || '', /Verificação de segurança pendente/);
|
|
const waited = delays.filter(d => d === 1000 || d === 5000).reduce((t, d) => t + d, 0);
|
|
assert.ok(waited >= 900000 && waited < 910000, 'waited ' + waited);
|
|
scanStates = [];
|
|
}
|
|
|
|
console.log('upload retry checks passed');
|