: asapien/signet

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
// Development-only contract for the real browser transport helper.
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import vm from 'node:vm';

const source = await fs.readFile(new URL('../www/app.js', import.meta.url), 'utf8');
const helper = source.slice(source.indexOf('async function uploadPdf('), source.indexOf('\nconst fmtTime'));
const chunk = 256 * 1024;
const file = (size) => ({ size, slice: (start, end) => new Blob([new Uint8Array(Math.min(end, size) - start)]) });
const load = (api) => vm.runInNewContext(helper + '\nuploadPdf', { api });
let calls = [], progress = [];
let upload = load(async (path, { fields }) => {
  calls.push({ path, offset: fields.offset, size: fields.file.size });
  return path === '/docs' ? { id: 'draft', upload_offset: fields.file.size, upload_total: 32 * 1024 * 1024 } :
    { upload_offset: fields.offset + fields.file.size, upload_total: 32 * 1024 * 1024 };
});
assert.equal((await upload(file(32 * 1024 * 1024), (n) => progress.push(n))).id, 'draft');
assert.equal(calls.length, 128);
assert(calls.every((c) => c.size <= chunk));
assert.equal(progress.at(-1), 32 * 1024 * 1024);
assert.equal(calls[1].offset, chunk);
assert.equal(calls.at(-1).offset, 127 * chunk);
let attempted = 0;
upload = load(async () => { attempted++; });
await assert.rejects(upload(file(32 * 1024 * 1024 + 1), () => {}), /32 MiB/);
await assert.rejects(upload(file(5), () => {}), /32 MiB/);
assert.equal(attempted, 0);
// Only an identified chunk is retried, with exactly the same body and offset.
let retryFields, retries = 0;
upload = load(async (path, { fields }) => {
  if (path === '/docs') return { id: 'retry', upload_offset: chunk, upload_total: chunk + 7 };
  if (retries++ === 0) { retryFields = fields; throw new Error('network interrupted after commit'); }
  assert.equal(fields, retryFields);
  assert.equal(fields.offset, chunk);
  assert.equal(fields.file.size, 7);
  return { upload_offset: chunk + 7, upload_total: chunk + 7 };
});
assert.equal((await upload(file(chunk + 7), () => {})).id, 'retry');
assert.equal(retries, 2);
let conflicts = 0;
upload = load(async (path) => {
  if (path === '/docs') return { id: 'conflict', upload_offset: chunk, upload_total: chunk + 1 };
  conflicts++;
  const err = new Error('conflict'); err.status = 409; throw err;
});
await assert.rejects(upload(file(chunk + 1), () => {}), (e) => e.status === 409 && e.docId === 'conflict');
assert.equal(conflicts, 1);
upload = load(async (path) => path === '/docs' ? { id: 'stale', upload_offset: chunk, upload_total: chunk + 1 } : { upload_offset: chunk, upload_total: chunk + 1 });
await assert.rejects(upload(file(chunk + 1), () => {}), (e) => e.docId === 'stale' && /progress/.test(e.message));
let creations = 0;
upload = load(async () => { creations++; throw new Error('unknown creation result'); });
await assert.rejects(upload(file(100), () => {}), /unknown creation/);
assert.equal(creations, 1);
console.log('upload client: exact 32 MiB, bounded slices, tail, exact retry, no conflict/creation retry and stale progress passed');