@vitrinka/web 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +60 -0
- package/README.md +59 -5
- package/build/hud.d.ts +1 -1
- package/build/hud.iife.js +46 -9
- package/build/protocol/index.d.ts +55 -4
- package/build/protocol/index.js +4 -2
- package/build/recorder/api.d.ts +25 -5
- package/build/recorder/api.js +21 -9
- package/build/recorder/attachments.d.ts +19 -0
- package/build/recorder/attachments.js +31 -0
- package/build/recorder/hud/Hud.d.ts +1 -1
- package/build/recorder/hud/Hud.js +43 -10
- package/build/recorder/hud/Sheet.d.ts +40 -5
- package/build/recorder/hud/Sheet.js +134 -8
- package/build/recorder/hud/attach.d.ts +26 -0
- package/build/recorder/hud/attach.js +136 -0
- package/build/recorder/hud/controller.d.ts +31 -3
- package/build/recorder/hud/controller.js +1 -1
- package/build/recorder/hud/host.js +2 -0
- package/build/recorder/hud/icons.d.ts +3 -0
- package/build/recorder/hud/icons.js +7 -0
- package/build/recorder/hud/layer.d.ts +4 -1
- package/build/recorder/hud/layer.js +11 -3
- package/build/recorder/hud/resize.d.ts +83 -0
- package/build/recorder/hud/resize.js +236 -0
- package/build/recorder/hud/styles.d.ts +1 -1
- package/build/recorder/hud/styles.js +38 -1
- package/build/recorder/link.d.ts +4 -1
- package/build/recorder/link.js +6 -2
- package/build/recorder/me.d.ts +7 -0
- package/build/recorder/me.js +62 -12
- package/build/recorder/page-controller.js +4 -3
- package/build/recorder/queue.d.ts +33 -3
- package/build/recorder/queue.js +404 -24
- package/build/recorder/report.d.ts +9 -0
- package/build/recorder/report.js +46 -4
- package/build/recorder/session.d.ts +13 -5
- package/build/recorder/session.js +42 -12
- package/build/recorder/storage/blobs.d.ts +32 -0
- package/build/recorder/storage/blobs.js +83 -0
- package/build/recorder/storage/index.js +3 -2
- package/package.json +1 -1
package/build/recorder/queue.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { api, permanentStatus, uploadChunk, VitrinkaApiError } from './api';
|
|
1
|
+
import { api, permanentStatus, uploadChunk, uploadImage, VitrinkaApiError } from './api';
|
|
2
2
|
import { pushFlightEvent } from './flight';
|
|
3
3
|
import { notify } from './state';
|
|
4
4
|
import { getRecorderStorage } from './storage';
|
|
5
|
+
import { getRecorderBlobStore } from './storage/blobs';
|
|
5
6
|
const FLUSH_MS = 2000;
|
|
6
7
|
const MAX_BUFFER = 20000;
|
|
7
8
|
const MAX_PENDING = 200; // rrweb chunks in memory; oldest evicted loudly
|
|
@@ -187,6 +188,8 @@ export function adoptStoredState(raw) {
|
|
|
187
188
|
// A policy this tab already holds outlives a record written before it.
|
|
188
189
|
if (next.policy === undefined && cur.policy !== undefined)
|
|
189
190
|
next.policy = cur.policy;
|
|
191
|
+
if (next.attachments === undefined && cur.attachments !== undefined)
|
|
192
|
+
next.attachments = cur.attachments;
|
|
190
193
|
// A session this tab is stopping comes back as the frozen copy it wrote
|
|
191
194
|
// (another tab wrote its cache back): this tab's clock and capture stay live.
|
|
192
195
|
if (stoppingHere?.sessionId === next.sessionId) {
|
|
@@ -229,6 +232,7 @@ export function persistNow() {
|
|
|
229
232
|
}
|
|
230
233
|
persistBuffer();
|
|
231
234
|
persistChunks();
|
|
235
|
+
persistImages();
|
|
232
236
|
}
|
|
233
237
|
function persistBuffer() {
|
|
234
238
|
if (persistTimer) {
|
|
@@ -284,10 +288,139 @@ function setChunks(chunks) {
|
|
|
284
288
|
tailDropped = false;
|
|
285
289
|
persistChunks();
|
|
286
290
|
}
|
|
291
|
+
// -- attached images ---------------------------------------------------------
|
|
292
|
+
/** The pending images' metadata (small: their bytes are in the blob journal). */
|
|
293
|
+
let imageCache;
|
|
294
|
+
/** Bytes this document holds itself, by journal key — no journal read to upload them. */
|
|
295
|
+
const imageBlobs = new Map();
|
|
296
|
+
/** Journal keys whose bytes are not (yet) known to be in a durable journal. */
|
|
297
|
+
const unjournaled = new Set();
|
|
298
|
+
/** Uploaded images whose row awaits the server's ack: row seq → journal key, deleted on ack. */
|
|
299
|
+
const rowBlobs = new Map();
|
|
300
|
+
/** Journal reads refused in a row, by key: a journal that never answers must not hold Stop forever. */
|
|
301
|
+
const readFailures = new Map();
|
|
302
|
+
const READ_ATTEMPTS = 5;
|
|
303
|
+
/**
|
|
304
|
+
* The queued images' budget across notes (one note holds ≤ 10): past either
|
|
305
|
+
* bound the oldest goes loudly and its note lands without it — a tester
|
|
306
|
+
* attaching on while offline never grows memory and the journal unbounded.
|
|
307
|
+
*/
|
|
308
|
+
const MAX_IMAGES = 200;
|
|
309
|
+
const MAX_IMAGE_BYTES = 64 * 1024 * 1024;
|
|
310
|
+
let imageNo = 0;
|
|
311
|
+
let volatileWarned = false;
|
|
312
|
+
/** KV mark: the blob journal may hold something (spares IndexedDB for testers who never attach). */
|
|
313
|
+
const BLOBS_KEY = 'blobs';
|
|
314
|
+
function getImages() {
|
|
315
|
+
imageCache ??= readJson('images', []);
|
|
316
|
+
return imageCache;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Always persisted: metadata only — and only of images whose bytes are in
|
|
320
|
+
* the blob journal, the one place the next document can read them from.
|
|
321
|
+
*/
|
|
322
|
+
function persistImages() {
|
|
323
|
+
if (tailDropped)
|
|
324
|
+
return;
|
|
325
|
+
const images = getImages().filter((image) => !unjournaled.has(image.blob));
|
|
326
|
+
if (images.length === 0)
|
|
327
|
+
kv().remove('images');
|
|
328
|
+
else
|
|
329
|
+
safeSet('images', JSON.stringify(images));
|
|
330
|
+
}
|
|
331
|
+
function setImages(images) {
|
|
332
|
+
imageCache = images;
|
|
333
|
+
tailDropped = false;
|
|
334
|
+
persistImages();
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Hold a note's images for the queue: their bytes in memory for this
|
|
338
|
+
* document and in the blob journal for a reload or another tab. Returns
|
|
339
|
+
* the drafts the capture journal records (journal key + payload), and
|
|
340
|
+
* `journaled` — true once every image's bytes are durably in the journal:
|
|
341
|
+
* only then may anything outside this document be told about them.
|
|
342
|
+
*/
|
|
343
|
+
function stageImages(sessionId, images) {
|
|
344
|
+
const store = getRecorderBlobStore();
|
|
345
|
+
if (!store.durable && !volatileWarned) {
|
|
346
|
+
volatileWarned = true;
|
|
347
|
+
console.warn('vitrinka: no IndexedDB — an attached image will not survive a reload');
|
|
348
|
+
}
|
|
349
|
+
safeSet(BLOBS_KEY, '1');
|
|
350
|
+
const writes = [];
|
|
351
|
+
const drafts = images.map(({ blob, payload }) => {
|
|
352
|
+
const key = `${sessionId}/${RECORDER_DOCUMENT_KEY}.${++imageNo}`;
|
|
353
|
+
imageBlobs.set(key, blob);
|
|
354
|
+
unjournaled.add(key);
|
|
355
|
+
writes.push(store.put(key, blob).then(() => {
|
|
356
|
+
// Released (or acknowledged) while the write was out: nothing will read it back.
|
|
357
|
+
const needed = imageBlobs.has(key) || getImages().some((image) => image.blob === key) || [...rowBlobs.values()].includes(key);
|
|
358
|
+
if (!needed) {
|
|
359
|
+
unjournaled.delete(key);
|
|
360
|
+
forgetBlob(key);
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
if (!store.durable)
|
|
364
|
+
return false;
|
|
365
|
+
unjournaled.delete(key);
|
|
366
|
+
if (getImages().some((image) => image.blob === key))
|
|
367
|
+
persistImages();
|
|
368
|
+
return true;
|
|
369
|
+
}, (error) => {
|
|
370
|
+
console.warn('vitrinka: an attached image could not be journaled — it will not survive a reload', error);
|
|
371
|
+
return false;
|
|
372
|
+
}));
|
|
373
|
+
return { blob: key, payload };
|
|
374
|
+
});
|
|
375
|
+
return { drafts, journaled: Promise.all(writes).then((durable) => durable.every(Boolean)) };
|
|
376
|
+
}
|
|
377
|
+
/** Delete one image's bytes from the blob journal. */
|
|
378
|
+
function forgetBlob(key) {
|
|
379
|
+
void getRecorderBlobStore()
|
|
380
|
+
.delete(key)
|
|
381
|
+
.catch((error) => console.warn('vitrinka: blob journal delete failed', error));
|
|
382
|
+
}
|
|
383
|
+
/** Let go of images nothing here will upload; `journal` also deletes their bytes there. */
|
|
384
|
+
function releaseImages(keys, journal) {
|
|
385
|
+
for (const key of keys) {
|
|
386
|
+
imageBlobs.delete(key);
|
|
387
|
+
unjournaled.delete(key);
|
|
388
|
+
readFailures.delete(key);
|
|
389
|
+
if (journal)
|
|
390
|
+
forgetBlob(key);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
const draftImageKeys = (draft) => (draft.kind === 'event' ? (draft.images ?? []).map((i) => i.blob) : []);
|
|
394
|
+
/**
|
|
395
|
+
* Delete journaled images nothing will upload: `ended`'s (a session just
|
|
396
|
+
* stopped), or, without it, every session's but the live one.
|
|
397
|
+
*/
|
|
398
|
+
export async function pruneBlobJournal(ended) {
|
|
399
|
+
if (!kv().getString(BLOBS_KEY))
|
|
400
|
+
return;
|
|
401
|
+
const store = getRecorderBlobStore();
|
|
402
|
+
const live = getState()?.sessionId;
|
|
403
|
+
try {
|
|
404
|
+
const keys = await store.keys();
|
|
405
|
+
let kept = 0;
|
|
406
|
+
for (const key of keys) {
|
|
407
|
+
const drop = ended ? key.startsWith(`${ended}/`) : !live || !key.startsWith(`${live}/`);
|
|
408
|
+
if (drop)
|
|
409
|
+
await store.delete(key);
|
|
410
|
+
else
|
|
411
|
+
kept++;
|
|
412
|
+
}
|
|
413
|
+
if (kept === 0 && !getState())
|
|
414
|
+
kv().remove(BLOBS_KEY);
|
|
415
|
+
}
|
|
416
|
+
catch (error) {
|
|
417
|
+
console.warn('vitrinka: blob journal cleanup failed', error);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
287
420
|
export function queuedCount() {
|
|
288
421
|
recoverAllocations();
|
|
289
422
|
pruneAcknowledgedCaptures();
|
|
290
|
-
return getBuffer().length + getChunks().length + pendingAllocations.size;
|
|
423
|
+
return getBuffer().length + getChunks().length + getImages().length + pendingAllocations.size;
|
|
291
424
|
}
|
|
292
425
|
// -- health + server reconciliation (extension D4/D5/D9) ---------------------
|
|
293
426
|
let lastSyncAt = 0;
|
|
@@ -422,7 +555,19 @@ const pendingAllocations = new Set();
|
|
|
422
555
|
const MAX_ALLOCATION_BYTES = 16 * 1024 * 1024;
|
|
423
556
|
let allocationBytes = 0;
|
|
424
557
|
let allocationChunks = 0;
|
|
558
|
+
/** Per capture journal: the seqs it still waits on; the journal goes once none is left. */
|
|
425
559
|
const allocatedCaptures = new Map();
|
|
560
|
+
/** How many seqs a capture takes: one, or a note's images + the note. */
|
|
561
|
+
function seqCount(draft) {
|
|
562
|
+
return draft.kind === 'event' ? 1 + (draft.images?.length ?? 0) : 1;
|
|
563
|
+
}
|
|
564
|
+
function seqsFrom(first, draft) {
|
|
565
|
+
return new Set(Array.from({ length: seqCount(draft) }, (_, i) => first + i));
|
|
566
|
+
}
|
|
567
|
+
/** A capture journal as stored: with `left` once some of its seqs settled. */
|
|
568
|
+
function journalJson(draft, seq, left) {
|
|
569
|
+
return JSON.stringify(left && left.size < seqCount(draft) ? { draft, seq, left: [...left] } : { draft, seq });
|
|
570
|
+
}
|
|
426
571
|
let allocationsRecovered = false;
|
|
427
572
|
let allocationBusy = null;
|
|
428
573
|
function removeAllocation(item) {
|
|
@@ -441,6 +586,7 @@ function addAllocation(item) {
|
|
|
441
586
|
const oldest = pendingAllocations.values().next().value;
|
|
442
587
|
removeAllocation(oldest);
|
|
443
588
|
kv().remove(oldest.key);
|
|
589
|
+
releaseImages(draftImageKeys(oldest.draft), true);
|
|
444
590
|
const error = new Error('vitrinka: pending allocation budget full — oldest capture dropped');
|
|
445
591
|
noteFailure(error);
|
|
446
592
|
console.warn(error.message);
|
|
@@ -463,12 +609,47 @@ function recoverAllocations(refresh = false) {
|
|
|
463
609
|
addAllocation({ ...item, key, persisted: true, bytes: utf8Bytes(JSON.stringify(item)) });
|
|
464
610
|
}
|
|
465
611
|
}
|
|
466
|
-
|
|
612
|
+
/**
|
|
613
|
+
* `seq` is the capture's first: a note's images take it and the next ones,
|
|
614
|
+
* the note the last. `left` (its journal's) restores only the seqs still
|
|
615
|
+
* waited on — a settled image never re-queues to crowd out a live one.
|
|
616
|
+
*/
|
|
617
|
+
function appendCapture(draft, seq, left) {
|
|
467
618
|
const { ts, tabId, tabHost, sessionId } = draft;
|
|
619
|
+
const awaited = (at) => !left || left.has(at);
|
|
468
620
|
if (draft.kind === 'event') {
|
|
621
|
+
const images = draft.images ?? [];
|
|
622
|
+
const own = seq + images.length;
|
|
623
|
+
const event = images.length
|
|
624
|
+
? { ...draft.event, payload: { ...draft.event.payload, attachments: images.map((_, i) => seq + i) } }
|
|
625
|
+
: draft.event;
|
|
469
626
|
const buffer = getBuffer();
|
|
470
|
-
if (!buffer.some((
|
|
471
|
-
buffer.push({ seq, ts, tabId, tabHost, ...
|
|
627
|
+
if (awaited(own) && !buffer.some((e) => e.seq === own))
|
|
628
|
+
buffer.push({ seq: own, ts, tabId, tabHost, ...event });
|
|
629
|
+
if (images.length) {
|
|
630
|
+
const pending = getImages();
|
|
631
|
+
images.forEach((image, i) => {
|
|
632
|
+
const at = seq + i;
|
|
633
|
+
if (!awaited(at) || pending.some((p) => p.seq === at))
|
|
634
|
+
return;
|
|
635
|
+
// Already uploaded, its row waits in the buffer (a reload restored it):
|
|
636
|
+
// its bytes still leave the journal on that row's ack.
|
|
637
|
+
if (buffer.some((e) => e.seq === at)) {
|
|
638
|
+
rowBlobs.set(at, image.blob);
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
pending.push({ seq: at, ts, tabId, tabHost, sessionId, blob: image.blob, payload: image.payload });
|
|
642
|
+
});
|
|
643
|
+
let bytes = pending.reduce((sum, p) => sum + p.payload.bytes, 0);
|
|
644
|
+
while (pending.length > MAX_IMAGES || bytes > MAX_IMAGE_BYTES) {
|
|
645
|
+
const dropped = pending.shift();
|
|
646
|
+
bytes -= dropped.payload.bytes;
|
|
647
|
+
clearStoredCaptures(dropped.sessionId, new Set([dropped.seq]));
|
|
648
|
+
releaseImages([dropped.blob], true);
|
|
649
|
+
console.warn(`vitrinka: attached image queue full — dropped ${dropped.payload.name} (seq ${dropped.seq}); its note lands without it`);
|
|
650
|
+
}
|
|
651
|
+
setImages(pending);
|
|
652
|
+
}
|
|
472
653
|
if (buffer.length > MAX_BUFFER) {
|
|
473
654
|
const dropped = buffer.splice(0, buffer.length - MAX_BUFFER);
|
|
474
655
|
clearStoredCaptures(sessionId, new Set(dropped.map((event) => event.seq)));
|
|
@@ -491,9 +672,29 @@ function appendCapture(draft, seq) {
|
|
|
491
672
|
schedulePersist();
|
|
492
673
|
scheduleFlush();
|
|
493
674
|
}
|
|
675
|
+
/**
|
|
676
|
+
* `seqs` were acknowledged (or dropped): a journal none of whose seqs is
|
|
677
|
+
* left goes; one with some left records which (merged with another tab's
|
|
678
|
+
* record — `left` only shrinks).
|
|
679
|
+
*/
|
|
494
680
|
function clearStoredCaptures(sessionId, seqs) {
|
|
495
681
|
for (const [key, item] of allocatedCaptures) {
|
|
496
|
-
if (item.sessionId
|
|
682
|
+
if (item.sessionId !== sessionId)
|
|
683
|
+
continue;
|
|
684
|
+
// A journal holds one seq, or a note's few: walk its own, never the batch.
|
|
685
|
+
const before = item.seqs.size;
|
|
686
|
+
for (const seq of item.seqs)
|
|
687
|
+
if (seqs.has(seq))
|
|
688
|
+
item.seqs.delete(seq);
|
|
689
|
+
const saved = item.seqs.size && item.seqs.size < before && item.persisted ? readJson(key, null) : null;
|
|
690
|
+
if (saved) {
|
|
691
|
+
const left = new Set([...item.seqs].filter((seq) => !saved.left || saved.left.includes(seq)));
|
|
692
|
+
if (left.size)
|
|
693
|
+
safeSet(key, journalJson(saved.draft, saved.seq, left));
|
|
694
|
+
else
|
|
695
|
+
item.seqs.clear(); // another tab settled the rest
|
|
696
|
+
}
|
|
697
|
+
if (item.seqs.size === 0) {
|
|
497
698
|
kv().remove(key);
|
|
498
699
|
allocatedCaptures.delete(key);
|
|
499
700
|
const documentKey = key.slice(ALLOCATION_PREFIX.length, key.lastIndexOf('.'));
|
|
@@ -509,7 +710,8 @@ function pruneAcknowledgedCaptures() {
|
|
|
509
710
|
const delivered = new Set();
|
|
510
711
|
for (const [key, item] of allocatedCaptures) {
|
|
511
712
|
if (item.persisted && !kv().getString(key)) {
|
|
512
|
-
|
|
713
|
+
for (const seq of item.seqs)
|
|
714
|
+
delivered.add(seq);
|
|
513
715
|
allocatedCaptures.delete(key);
|
|
514
716
|
}
|
|
515
717
|
}
|
|
@@ -517,19 +719,29 @@ function pruneAcknowledgedCaptures() {
|
|
|
517
719
|
return;
|
|
518
720
|
setBuffer(getBuffer().filter((event) => !delivered.has(event.seq)));
|
|
519
721
|
setChunks(getChunks().filter((chunk) => !delivered.has(chunk.seq)));
|
|
722
|
+
const images = getImages();
|
|
723
|
+
if (images.some((image) => delivered.has(image.seq))) {
|
|
724
|
+
// The helper uploaded them and deletes their bytes on its own ack.
|
|
725
|
+
releaseImages(images.filter((image) => delivered.has(image.seq)).map((image) => image.blob), false);
|
|
726
|
+
setImages(images.filter((image) => !delivered.has(image.seq)));
|
|
727
|
+
}
|
|
520
728
|
}
|
|
521
729
|
/** A departing document can be acknowledged by a helper only with a full journal. */
|
|
522
730
|
export function tailIsDurable() {
|
|
523
731
|
if (!kv().keys)
|
|
524
732
|
return false;
|
|
525
|
-
const durableSeqs = new Set([...allocatedCaptures].filter(([key]) => kv().getString(key)).
|
|
733
|
+
const durableSeqs = new Set([...allocatedCaptures].filter(([key]) => kv().getString(key)).flatMap(([, item]) => [...item.seqs]));
|
|
526
734
|
for (const item of pendingAllocations) {
|
|
527
735
|
if (item.persisted && item.seq !== undefined && kv().getString(item.key))
|
|
528
|
-
|
|
736
|
+
for (const seq of seqsFrom(item.seq, item.draft))
|
|
737
|
+
durableSeqs.add(seq);
|
|
529
738
|
}
|
|
530
|
-
|
|
739
|
+
// An image's bytes must be in the blob journal too, or a helper has nothing to upload.
|
|
740
|
+
const bytesDurable = (key) => !unjournaled.has(key);
|
|
741
|
+
return [...pendingAllocations].every((item) => item.persisted && !!kv().getString(item.key) && draftImageKeys(item.draft).every(bytesDurable))
|
|
531
742
|
&& getBuffer().every((event) => durableSeqs.has(event.seq))
|
|
532
|
-
&& getChunks().every((chunk) => durableSeqs.has(chunk.seq))
|
|
743
|
+
&& getChunks().every((chunk) => durableSeqs.has(chunk.seq))
|
|
744
|
+
&& getImages().every((image) => durableSeqs.has(image.seq) && bytesDurable(image.blob));
|
|
533
745
|
}
|
|
534
746
|
/** One worker per document: evicted drafts cannot stay held by queued lock callbacks. */
|
|
535
747
|
function allocatePending() {
|
|
@@ -546,25 +758,29 @@ function allocatePending() {
|
|
|
546
758
|
// Another tab already delivered this journal; never allocate it twice.
|
|
547
759
|
if (!saved) {
|
|
548
760
|
removeAllocation(item);
|
|
761
|
+
releaseImages(draftImageKeys(item.draft), false);
|
|
549
762
|
return;
|
|
550
763
|
}
|
|
551
764
|
item.seq = saved.seq;
|
|
765
|
+
item.left = saved.left;
|
|
552
766
|
}
|
|
553
767
|
adoptStoredState(storage.getString(REC_KEY) ?? null);
|
|
554
768
|
if (getState()?.sessionId !== item.draft.sessionId) {
|
|
555
769
|
removeAllocation(item);
|
|
770
|
+
releaseImages(draftImageKeys(item.draft), true);
|
|
556
771
|
return;
|
|
557
772
|
}
|
|
558
|
-
const seq = item.seq ?? allocSeq(
|
|
773
|
+
const seq = item.seq ?? allocSeq(seqCount(item.draft))?.seq;
|
|
559
774
|
if (seq === undefined)
|
|
560
775
|
return;
|
|
561
776
|
item.seq = seq;
|
|
777
|
+
const seqs = item.left ? new Set(item.left) : seqsFrom(seq, item.draft);
|
|
562
778
|
// Keep the payload AND its identity until the server acknowledges it.
|
|
563
779
|
// A recovering document reuses this seq even when the old tab is alive.
|
|
564
780
|
if (item.persisted)
|
|
565
|
-
storage.set(item.key,
|
|
566
|
-
allocatedCaptures.set(item.key, { sessionId: item.draft.sessionId,
|
|
567
|
-
appendCapture(item.draft, seq);
|
|
781
|
+
storage.set(item.key, journalJson(item.draft, seq, seqs));
|
|
782
|
+
allocatedCaptures.set(item.key, { sessionId: item.draft.sessionId, seqs, persisted: item.persisted });
|
|
783
|
+
appendCapture(item.draft, seq, seqs);
|
|
568
784
|
removeAllocation(item);
|
|
569
785
|
});
|
|
570
786
|
}
|
|
@@ -577,8 +793,14 @@ function allocatePending() {
|
|
|
577
793
|
})).finally(() => { allocationBusy = null; });
|
|
578
794
|
return allocationBusy;
|
|
579
795
|
}
|
|
580
|
-
/**
|
|
581
|
-
|
|
796
|
+
/**
|
|
797
|
+
* Accept now, persist now, stamp under the shared lock later. A note with
|
|
798
|
+
* images (`journaled`) persists its journal only once their bytes are in
|
|
799
|
+
* the blob journal: another tab recovering it sooner would read no bytes
|
|
800
|
+
* and drop them as lost. Until then (for good, when the bytes never get
|
|
801
|
+
* there) only this document can deliver it.
|
|
802
|
+
*/
|
|
803
|
+
function queueCapture(draft, journaled) {
|
|
582
804
|
const accepted = getState();
|
|
583
805
|
if (!accepted || accepted.paused || accepted.dead)
|
|
584
806
|
return false;
|
|
@@ -586,17 +808,35 @@ function queueCapture(draft) {
|
|
|
586
808
|
if (kv().withLock) {
|
|
587
809
|
const key = `${ALLOCATION_PREFIX}${RECORDER_DOCUMENT_KEY}.${++captureNo}`;
|
|
588
810
|
const raw = JSON.stringify({ draft });
|
|
589
|
-
const item = { key, draft, persisted: safeSet(key, raw), bytes: utf8Bytes(raw) };
|
|
811
|
+
const item = { key, draft, persisted: !journaled && safeSet(key, raw), bytes: utf8Bytes(raw) };
|
|
590
812
|
addAllocation(item);
|
|
591
813
|
void allocatePending();
|
|
814
|
+
void journaled?.then((durable) => {
|
|
815
|
+
if (durable)
|
|
816
|
+
publishAllocation(item);
|
|
817
|
+
});
|
|
592
818
|
}
|
|
593
819
|
else {
|
|
594
|
-
const allocation = allocSeq(
|
|
820
|
+
const allocation = allocSeq(seqCount(draft));
|
|
595
821
|
if (allocation)
|
|
596
822
|
appendCapture(draft, allocation.seq);
|
|
823
|
+
else
|
|
824
|
+
releaseImages(draftImageKeys(draft), true);
|
|
597
825
|
}
|
|
598
826
|
return true;
|
|
599
827
|
}
|
|
828
|
+
/**
|
|
829
|
+
* Persist a capture's journal late (its images' bytes just landed), with
|
|
830
|
+
* its seq when it has one. A capture delivered, dropped or reset meanwhile
|
|
831
|
+
* is no longer held here, and nothing is written.
|
|
832
|
+
*/
|
|
833
|
+
function publishAllocation(item) {
|
|
834
|
+
const allocated = allocatedCaptures.get(item.key);
|
|
835
|
+
if (allocated)
|
|
836
|
+
allocated.persisted = safeSet(item.key, journalJson(item.draft, item.seq, allocated.seqs));
|
|
837
|
+
else if (pendingAllocations.has(item))
|
|
838
|
+
item.persisted = safeSet(item.key, JSON.stringify({ draft: item.draft }));
|
|
839
|
+
}
|
|
600
840
|
/**
|
|
601
841
|
* In-flight CAPTURES (async network body reads) that have not yet appended
|
|
602
842
|
* their event. Stop must settle these before draining.
|
|
@@ -649,9 +889,10 @@ export function isSessionLive(id) {
|
|
|
649
889
|
* kind+payload plus the route they observed. With no session the event goes
|
|
650
890
|
* to the flight recorder when the idle pill keeps one (memory only — never
|
|
651
891
|
* storage, never the network), else it is dropped. Returns the stamped ts
|
|
652
|
-
* (null when dropped).
|
|
892
|
+
* (null when dropped). `images` (a note's attachments, live sessions only)
|
|
893
|
+
* take the seqs just before the event's, which lists them as `attachments`.
|
|
653
894
|
*/
|
|
654
|
-
export function pushEvent(kind, payload, route) {
|
|
895
|
+
export function pushEvent(kind, payload, route, images = []) {
|
|
655
896
|
const rec = getState();
|
|
656
897
|
if (!rec)
|
|
657
898
|
return pushFlightEvent(kind, payload, route);
|
|
@@ -659,7 +900,16 @@ export function pushEvent(kind, payload, route) {
|
|
|
659
900
|
return null;
|
|
660
901
|
const ts = new Date().toISOString();
|
|
661
902
|
const { tabId, tabHost } = route;
|
|
662
|
-
|
|
903
|
+
const staged = images.length ? stageImages(rec.sessionId, images) : null;
|
|
904
|
+
queueCapture({
|
|
905
|
+
sessionId: rec.sessionId,
|
|
906
|
+
ts,
|
|
907
|
+
tabId,
|
|
908
|
+
tabHost,
|
|
909
|
+
kind: 'event',
|
|
910
|
+
event: { kind, payload },
|
|
911
|
+
...(staged ? { images: staged.drafts } : {}),
|
|
912
|
+
}, staged?.journaled);
|
|
663
913
|
noteActivity();
|
|
664
914
|
return ts;
|
|
665
915
|
}
|
|
@@ -805,6 +1055,103 @@ async function drainPending(limit = 5) {
|
|
|
805
1055
|
setChunks(getChunks().filter((c) => !gone.has(c.seq)));
|
|
806
1056
|
return getChunks().length === 0;
|
|
807
1057
|
}
|
|
1058
|
+
/**
|
|
1059
|
+
* Upload queued images oldest-first, the chunks' way: `/shot?seq=N` with the
|
|
1060
|
+
* bytes this document holds, else the blob journal's; only then does the
|
|
1061
|
+
* `attachment` row (payload, blobKey) join the stream under its original seq.
|
|
1062
|
+
* An image whose bytes are gone (never journaled before a reload) or that the
|
|
1063
|
+
* server refuses for good is dropped loudly — its note lands without it. A
|
|
1064
|
+
* refused journal READ is not gone bytes: it stops the pass like a transient
|
|
1065
|
+
* upload failure, and only READ_ATTEMPTS refusals in a row give the image up.
|
|
1066
|
+
* Returns true when no image is left.
|
|
1067
|
+
*/
|
|
1068
|
+
async function drainImages(limit = 4) {
|
|
1069
|
+
const images = getImages();
|
|
1070
|
+
if (images.length === 0)
|
|
1071
|
+
return true;
|
|
1072
|
+
const rec = getState();
|
|
1073
|
+
let done = 0;
|
|
1074
|
+
const gone = new Set();
|
|
1075
|
+
const lost = [];
|
|
1076
|
+
for (const item of images) {
|
|
1077
|
+
if (done >= limit)
|
|
1078
|
+
break;
|
|
1079
|
+
done++;
|
|
1080
|
+
if (!rec || item.sessionId !== rec.sessionId) {
|
|
1081
|
+
console.warn(`vitrinka: dropping attachment seq ${item.seq} from ended session ${item.sessionId}`);
|
|
1082
|
+
gone.add(item.seq);
|
|
1083
|
+
lost.push(item.blob);
|
|
1084
|
+
continue;
|
|
1085
|
+
}
|
|
1086
|
+
let blob = imageBlobs.get(item.blob) ?? null;
|
|
1087
|
+
if (!blob) {
|
|
1088
|
+
try {
|
|
1089
|
+
blob = await getRecorderBlobStore().get(item.blob);
|
|
1090
|
+
readFailures.delete(item.blob);
|
|
1091
|
+
}
|
|
1092
|
+
catch (e) {
|
|
1093
|
+
if (movedOn(rec))
|
|
1094
|
+
return getImages().length === 0;
|
|
1095
|
+
const tries = (readFailures.get(item.blob) ?? 0) + 1;
|
|
1096
|
+
console.warn(`vitrinka: blob journal read failed (${tries}/${READ_ATTEMPTS})`, e);
|
|
1097
|
+
if (tries < READ_ATTEMPTS) {
|
|
1098
|
+
readFailures.set(item.blob, tries);
|
|
1099
|
+
noteFailure(e);
|
|
1100
|
+
break; // transient — the bytes stay queued and journaled, the next flush retries
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
if (movedOn(rec))
|
|
1104
|
+
return getImages().length === 0;
|
|
1105
|
+
}
|
|
1106
|
+
if (!blob) {
|
|
1107
|
+
console.warn(`vitrinka: attachment seq ${item.seq} (${item.payload.name}) lost its bytes — its note lands without it`);
|
|
1108
|
+
clearStoredCaptures(item.sessionId, new Set([item.seq]));
|
|
1109
|
+
gone.add(item.seq);
|
|
1110
|
+
lost.push(item.blob);
|
|
1111
|
+
continue;
|
|
1112
|
+
}
|
|
1113
|
+
try {
|
|
1114
|
+
const up = await uploadImage(rec.sessionId, item.seq, blob);
|
|
1115
|
+
if (movedOn(rec))
|
|
1116
|
+
return getImages().length === 0;
|
|
1117
|
+
pushRawEvent({
|
|
1118
|
+
seq: item.seq,
|
|
1119
|
+
ts: item.ts,
|
|
1120
|
+
tabId: item.tabId,
|
|
1121
|
+
tabHost: item.tabHost,
|
|
1122
|
+
kind: 'attachment',
|
|
1123
|
+
payload: { ...item.payload },
|
|
1124
|
+
blobKey: up.blobKey,
|
|
1125
|
+
});
|
|
1126
|
+
// The bytes stay journaled until the row is acknowledged (a helper tab may still need them).
|
|
1127
|
+
imageBlobs.delete(item.blob);
|
|
1128
|
+
rowBlobs.set(item.seq, item.blob);
|
|
1129
|
+
noteSync();
|
|
1130
|
+
gone.add(item.seq);
|
|
1131
|
+
}
|
|
1132
|
+
catch (e) {
|
|
1133
|
+
if (movedOn(rec))
|
|
1134
|
+
return getImages().length === 0;
|
|
1135
|
+
if (e instanceof VitrinkaApiError && permanentStatus(e.status)) {
|
|
1136
|
+
console.warn(`vitrinka: attachment seq ${item.seq} rejected permanently (${e.status}) — dropped`);
|
|
1137
|
+
clearStoredCaptures(item.sessionId, new Set([item.seq]));
|
|
1138
|
+
gone.add(item.seq);
|
|
1139
|
+
lost.push(item.blob);
|
|
1140
|
+
continue;
|
|
1141
|
+
}
|
|
1142
|
+
noteFailure(e);
|
|
1143
|
+
console.warn('vitrinka: attachment upload failed', e);
|
|
1144
|
+
break; // transient — stop the pass, the next flush retries
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
if (gone.size) {
|
|
1148
|
+
// The rows land in storage before the pending entries leave it.
|
|
1149
|
+
persistBuffer();
|
|
1150
|
+
setImages(getImages().filter((image) => !gone.has(image.seq)));
|
|
1151
|
+
}
|
|
1152
|
+
releaseImages(lost, true);
|
|
1153
|
+
return getImages().length === 0;
|
|
1154
|
+
}
|
|
808
1155
|
let flushTimer = null;
|
|
809
1156
|
export function scheduleFlush() {
|
|
810
1157
|
if (flushTimer)
|
|
@@ -835,7 +1182,10 @@ async function flushInner(opts) {
|
|
|
835
1182
|
await allocatePending();
|
|
836
1183
|
if (pendingAllocations.size)
|
|
837
1184
|
return false;
|
|
838
|
-
const
|
|
1185
|
+
const chunksClear = await drainPending();
|
|
1186
|
+
// A leaving page cannot finish an image upload; the blob journal keeps it for the next document.
|
|
1187
|
+
const imagesClear = opts.keepalive ? getImages().length === 0 : await drainImages();
|
|
1188
|
+
const pendingClear = chunksClear && imagesClear;
|
|
839
1189
|
if (!pendingClear)
|
|
840
1190
|
scheduleFlush();
|
|
841
1191
|
const rec = getState();
|
|
@@ -909,11 +1259,19 @@ async function flushInner(opts) {
|
|
|
909
1259
|
clearStoredCaptures(rec.sessionId, sent);
|
|
910
1260
|
const rest = getBuffer().filter((e) => !sent.has(e.seq));
|
|
911
1261
|
setBuffer(rest);
|
|
1262
|
+
// An acknowledged attachment row: its bytes are the server's now.
|
|
1263
|
+
for (const seq of sent) {
|
|
1264
|
+
const key = rowBlobs.get(seq);
|
|
1265
|
+
if (key === undefined)
|
|
1266
|
+
continue;
|
|
1267
|
+
rowBlobs.delete(seq);
|
|
1268
|
+
forgetBlob(key);
|
|
1269
|
+
}
|
|
912
1270
|
if (rest.length)
|
|
913
1271
|
scheduleFlush();
|
|
914
1272
|
return true;
|
|
915
1273
|
}
|
|
916
|
-
/** Drain until buffer
|
|
1274
|
+
/** Drain until buffer, chunks and images are empty or the deadline passes. */
|
|
917
1275
|
export async function drainBuffer(deadlineMs = 60000) {
|
|
918
1276
|
const t0 = Date.now();
|
|
919
1277
|
while (Date.now() - t0 < deadlineMs) {
|
|
@@ -941,6 +1299,12 @@ export function resetQueues() {
|
|
|
941
1299
|
allocationBytes = allocationChunks = 0;
|
|
942
1300
|
setBuffer([]);
|
|
943
1301
|
setChunks([]);
|
|
1302
|
+
// The journaled bytes go with pruneBlobJournal (the lifecycle knows which session ended).
|
|
1303
|
+
imageBlobs.clear();
|
|
1304
|
+
unjournaled.clear();
|
|
1305
|
+
rowBlobs.clear();
|
|
1306
|
+
readFailures.clear();
|
|
1307
|
+
setImages([]);
|
|
944
1308
|
}
|
|
945
1309
|
/**
|
|
946
1310
|
* Forget this tab's buffers WITHOUT writing storage: another tab owns the
|
|
@@ -954,6 +1318,10 @@ function dropLocalTail() {
|
|
|
954
1318
|
bufferCache = [];
|
|
955
1319
|
chunkCache = [];
|
|
956
1320
|
chunkBytes = 0;
|
|
1321
|
+
imageCache = [];
|
|
1322
|
+
imageBlobs.clear();
|
|
1323
|
+
unjournaled.clear();
|
|
1324
|
+
rowBlobs.clear();
|
|
957
1325
|
pendingAllocations.clear();
|
|
958
1326
|
allocationBytes = allocationChunks = 0;
|
|
959
1327
|
allocatedCaptures.clear();
|
|
@@ -966,6 +1334,8 @@ export function __resetForTests() {
|
|
|
966
1334
|
kv().remove('rec');
|
|
967
1335
|
kv().remove('buffer');
|
|
968
1336
|
kv().remove('chunks');
|
|
1337
|
+
kv().remove('images');
|
|
1338
|
+
kv().remove(BLOBS_KEY);
|
|
969
1339
|
kv().remove('seq');
|
|
970
1340
|
for (const key of kv().keys?.() ?? [])
|
|
971
1341
|
if (key.startsWith(ALLOCATION_PREFIX))
|
|
@@ -980,6 +1350,10 @@ export function __bufferForTests() {
|
|
|
980
1350
|
export function __chunksForTests() {
|
|
981
1351
|
return getChunks().map((c) => ({ seq: c.seq, count: c.count, sessionId: c.sessionId }));
|
|
982
1352
|
}
|
|
1353
|
+
/** Test-only: the images waiting for upload (in order), with their blob journal keys. */
|
|
1354
|
+
export function __imagesForTests() {
|
|
1355
|
+
return getImages().map((i) => ({ seq: i.seq, sessionId: i.sessionId, blob: i.blob, name: i.payload.name }));
|
|
1356
|
+
}
|
|
983
1357
|
/** Test-only: drop the in-memory caches while LEAVING storage intact (a reload). */
|
|
984
1358
|
export function __dropCachesForTests() {
|
|
985
1359
|
pendingAllocations.clear();
|
|
@@ -1005,6 +1379,12 @@ export function __dropCachesForTests() {
|
|
|
1005
1379
|
bufferCache = undefined;
|
|
1006
1380
|
chunkCache = undefined;
|
|
1007
1381
|
chunkBytes = 0;
|
|
1382
|
+
// A reload keeps only what storage and the blob journal hold.
|
|
1383
|
+
imageCache = undefined;
|
|
1384
|
+
imageBlobs.clear();
|
|
1385
|
+
unjournaled.clear();
|
|
1386
|
+
rowBlobs.clear();
|
|
1387
|
+
readFailures.clear();
|
|
1008
1388
|
disarmReconcile();
|
|
1009
1389
|
lastSyncAt = 0;
|
|
1010
1390
|
lastError = '';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ViewRect } from './session';
|
|
2
|
+
import type { HudAttachment } from './hud/controller';
|
|
2
3
|
/** A mounted pill wants the flight recorder; returns the release. */
|
|
3
4
|
export declare function wantFlight(): () => void;
|
|
4
5
|
/**
|
|
@@ -8,6 +9,12 @@ export declare function wantFlight(): () => void;
|
|
|
8
9
|
export declare function syncFlight(): Promise<void>;
|
|
9
10
|
/** Can the pill offer "Report a bug" now — into the live session, or with the idle clip? */
|
|
10
11
|
export declare function canReport(): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Do the sheets take images now (recorder attachments D6)? Recording: the
|
|
14
|
+
* session's policy read said so. Idle: the flight recorder's did — only the
|
|
15
|
+
* bug report sheet opens then.
|
|
16
|
+
*/
|
|
17
|
+
export declare function canAttach(): boolean;
|
|
11
18
|
export interface ReportNote {
|
|
12
19
|
/** The description; its first line titles the report session. */
|
|
13
20
|
text: string;
|
|
@@ -15,6 +22,8 @@ export interface ReportNote {
|
|
|
15
22
|
rect: ViewRect | null;
|
|
16
23
|
/** The marked element's selector; '' for a region or no mark. */
|
|
17
24
|
selector: string;
|
|
25
|
+
/** Images the reporter attached (the HUD's, already normalized). */
|
|
26
|
+
attachments?: readonly HudAttachment[];
|
|
18
27
|
}
|
|
19
28
|
/**
|
|
20
29
|
* The report sheet opened (freeze the last minute — the buffer itself keeps
|