@vitrinka/web 0.3.4 → 0.5.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.
Files changed (69) hide show
  1. package/CHANGELOG.md +118 -0
  2. package/README.md +93 -6
  3. package/build/hud.d.ts +4 -1
  4. package/build/hud.iife.js +46 -9
  5. package/build/one.iife.js +1 -0
  6. package/build/protocol/index.d.ts +55 -4
  7. package/build/protocol/index.js +4 -2
  8. package/build/recorder/RecorderProvider.js +48 -9
  9. package/build/recorder/api.d.ts +25 -5
  10. package/build/recorder/api.js +21 -9
  11. package/build/recorder/attachments.d.ts +19 -0
  12. package/build/recorder/attachments.js +31 -0
  13. package/build/recorder/capture/console.d.ts +2 -0
  14. package/build/recorder/capture/console.js +22 -2
  15. package/build/recorder/capture/nav.d.ts +13 -2
  16. package/build/recorder/capture/nav.js +49 -11
  17. package/build/recorder/capture/net.d.ts +2 -0
  18. package/build/recorder/capture/net.js +30 -4
  19. package/build/recorder/control.d.ts +17 -0
  20. package/build/recorder/control.js +39 -2
  21. package/build/recorder/hud/Hud.d.ts +1 -1
  22. package/build/recorder/hud/Hud.js +113 -30
  23. package/build/recorder/hud/RecorderPill.js +6 -2
  24. package/build/recorder/hud/Sheet.d.ts +40 -5
  25. package/build/recorder/hud/Sheet.js +134 -8
  26. package/build/recorder/hud/attach.d.ts +26 -0
  27. package/build/recorder/hud/attach.js +136 -0
  28. package/build/recorder/hud/chords.d.ts +10 -0
  29. package/build/recorder/hud/chords.js +34 -0
  30. package/build/recorder/hud/controller.d.ts +55 -3
  31. package/build/recorder/hud/controller.js +1 -1
  32. package/build/recorder/hud/flow.d.ts +22 -1
  33. package/build/recorder/hud/flow.js +16 -1
  34. package/build/recorder/hud/host.js +2 -0
  35. package/build/recorder/hud/icons.d.ts +3 -0
  36. package/build/recorder/hud/icons.js +7 -0
  37. package/build/recorder/hud/layer.d.ts +4 -1
  38. package/build/recorder/hud/layer.js +11 -3
  39. package/build/recorder/hud/resize.d.ts +83 -0
  40. package/build/recorder/hud/resize.js +236 -0
  41. package/build/recorder/hud/styles.d.ts +1 -1
  42. package/build/recorder/hud/styles.js +38 -1
  43. package/build/recorder/index.d.ts +1 -0
  44. package/build/recorder/index.js +1 -0
  45. package/build/recorder/lead.d.ts +90 -0
  46. package/build/recorder/lead.js +404 -0
  47. package/build/recorder/link.d.ts +4 -1
  48. package/build/recorder/link.js +6 -2
  49. package/build/recorder/me.d.ts +7 -0
  50. package/build/recorder/me.js +62 -12
  51. package/build/recorder/one-iife.d.ts +13 -0
  52. package/build/recorder/one-iife.js +13 -0
  53. package/build/recorder/one.d.ts +170 -0
  54. package/build/recorder/one.js +292 -0
  55. package/build/recorder/page-controller.js +26 -4
  56. package/build/recorder/queue.d.ts +33 -3
  57. package/build/recorder/queue.js +409 -26
  58. package/build/recorder/report.d.ts +9 -0
  59. package/build/recorder/report.js +55 -6
  60. package/build/recorder/session.d.ts +33 -7
  61. package/build/recorder/session.js +111 -31
  62. package/build/recorder/state.d.ts +9 -0
  63. package/build/recorder/state.js +7 -0
  64. package/build/recorder/storage/blobs.d.ts +32 -0
  65. package/build/recorder/storage/blobs.js +83 -0
  66. package/build/recorder/storage/index.js +3 -2
  67. package/build/recorder/version.d.ts +3 -0
  68. package/build/recorder/version.js +3 -0
  69. package/package.json +5 -3
@@ -0,0 +1,90 @@
1
+ /**
2
+ * The page side of ONE recorder (extension + page recorder decisions,
3
+ * 2026-10-12): this document's recorder either LEADS — holds the claim on
4
+ * `<html>` — or FOLLOWS the recorder that does: the browser extension, or
5
+ * another recorder bundle on the page. The protocol itself is `one.ts`.
6
+ *
7
+ * Started by the recorder root (`startLead`, RecorderProvider). While it runs:
8
+ * - `data-vitrinka-page` marks the document, written before the first ask
9
+ * (the extension's `one.js` answers only where it stands).
10
+ * - It answers `hello` asks with what it leads; the extension's hellos tell
11
+ * it the extension records — here (its claim) or in another tab (D6).
12
+ * - Following: `leadState.following` is set, every lane and the flight
13
+ * recorder go quiet, the HUD hides; a session of its own (resumed after a
14
+ * reload, joined from another tab) waits, capturing nothing, and resumes
15
+ * once the leader lets go (D14). The follower pings the holder when the
16
+ * claim changes, every 10 s, on becoming visible and before any remote
17
+ * command; a holder that does not answer is cleared under its own id (D5).
18
+ * - Leading: it holds the claim through every transition — starting,
19
+ * recording, paused, stopping, and `saved` while the HUD's stop face shows
20
+ * (D7) — and answers the holder's commands synchronously (D11).
21
+ */
22
+ import type { HudFace } from './hud/controller';
23
+ import { type Claim, type CommandOp, type OneEnv, type OneParty } from './one';
24
+ export type Leader = OneParty;
25
+ /**
26
+ * A Start refused because another recorder leads (D12). `code` is
27
+ * `recorder_busy`; the message is shown verbatim where Start was pressed.
28
+ */
29
+ export declare class RecorderBusyError extends Error {
30
+ readonly code: "recorder_busy";
31
+ readonly leader: Leader;
32
+ constructor(message: string, leader: Leader);
33
+ }
34
+ /** The leader's own doors, which a remote command opens (the root supplies them). */
35
+ export interface LeadHandlers {
36
+ pause(): void;
37
+ /** The direct stop, for when no HUD registered its Stop flow. */
38
+ stop(): void;
39
+ note(text: string): void;
40
+ /**
41
+ * Re-read the durable session record before a waiting session resumes
42
+ * (`resyncStoredSession`); false when the cached one ended or was replaced.
43
+ */
44
+ resync?(): boolean;
45
+ }
46
+ /** How often a follower pings the holder. */
47
+ export declare const PING_MS = 10000;
48
+ /**
49
+ * Ping (D5): ask who is here, take the extension's word, clear a holder that
50
+ * no longer answers (under its own id), re-derive. A follower runs it every
51
+ * `PING_MS`, on becoming visible and before a remote command, so a page
52
+ * never waits on an extension that was reloaded or uninstalled.
53
+ */
54
+ export declare function pingLeader(): void;
55
+ /**
56
+ * Join the channel for this document; returns the leave. Idempotent across
57
+ * mounts (the last handlers win). `at` is the document's env unless a test
58
+ * passes its own.
59
+ */
60
+ export declare function startLead(h: LeadHandlers, at?: OneEnv | null): () => void;
61
+ /**
62
+ * The Start check-and-set (D4, D12): synchronous, before any network call.
63
+ * Throws a `RecorderBusyError` while another recorder leads — a live claim
64
+ * whose holder answers, or the extension recording in another tab — else
65
+ * claims the document as `starting`. A no-op without a mounted root.
66
+ */
67
+ export declare function claimForStart(): void;
68
+ /** The Start settled: created (the claim moves on with the session) or not (it goes). */
69
+ export declare function settleStart(created: boolean): void;
70
+ /** This document's Stop is draining `sessionId` (null: it settled). */
71
+ export declare function setLeadStopping(sessionId: string | null): void;
72
+ /** The HUD's flow face (D7). */
73
+ export declare function holdFace(f: HudFace): void;
74
+ /** The HUD's Stop flow, run by a remote stop (D11); returns the unregister. */
75
+ export declare function setRemoteStop(run: () => void): () => void;
76
+ /** Who leads now: this recorder (`page`), the one it follows, or nobody. */
77
+ export interface LeadStatus {
78
+ leader: Leader | null;
79
+ following: Leader | null;
80
+ /** This document's claim (any holder). */
81
+ claim: Claim | null;
82
+ }
83
+ export declare function leadStatus(): LeadStatus;
84
+ /**
85
+ * A follower's pause, stop or note, acted on by the leader (D11). Pings the
86
+ * holder first; throws with the reason when it is gone, refused, or the
87
+ * extension leads from another tab (whose endpoint here takes no command).
88
+ */
89
+ export declare function sendToLeader(op: CommandOp, text?: string): void;
90
+ export declare function __resetLeadForTests(): void;
@@ -0,0 +1,404 @@
1
+ import { announce, answeredBy, ask, documentEnv, isLive, markPage, nonce, readClaim, releaseClaim, sendCommand, serveCommands, serveHello, takeClaim, unmarkPage, updateClaim, watchClaim, } from './one';
2
+ import { getState, stoppingElsewhere } from './queue';
3
+ import { leadState, notify, subscribe } from './state';
4
+ import { RECORDER_ID } from './version';
5
+ /**
6
+ * A Start refused because another recorder leads (D12). `code` is
7
+ * `recorder_busy`; the message is shown verbatim where Start was pressed.
8
+ */
9
+ export class RecorderBusyError extends Error {
10
+ code = 'recorder_busy';
11
+ leader;
12
+ constructor(message, leader) {
13
+ super(message);
14
+ this.name = 'RecorderBusy';
15
+ this.leader = leader;
16
+ }
17
+ }
18
+ /** How often a follower pings the holder. */
19
+ export const PING_MS = 10_000;
20
+ const self = { from: 'page', inst: nonce('p') };
21
+ let env = null;
22
+ let users = 0;
23
+ let handlers = null;
24
+ let teardown = null;
25
+ /** Extension parties (by instance) and the recording each says is live. */
26
+ const extension = new Map();
27
+ let starting = false;
28
+ let stopping = null;
29
+ let face = 'none';
30
+ let remoteStop = null;
31
+ let pingTimer = null;
32
+ // -- what this document knows --------------------------------------------------
33
+ function heldClaim() {
34
+ if (!env)
35
+ return null;
36
+ const c = readClaim(env);
37
+ return c?.id === self.inst ? c : null;
38
+ }
39
+ function extensionLive() {
40
+ for (const l of extension.values())
41
+ if (isLive(l.state))
42
+ return l;
43
+ return null;
44
+ }
45
+ /** A claim of another holder (any state: a `saved` one keeps its face, D7), else the extension elsewhere. */
46
+ function computeFollowing() {
47
+ if (!env)
48
+ return null;
49
+ const c = readClaim(env);
50
+ if (c)
51
+ return c.id === self.inst ? null : c.by;
52
+ return extensionLive() ? 'extension' : null;
53
+ }
54
+ function liveOf() {
55
+ const c = heldClaim();
56
+ return c ? { by: 'page', state: c.state, here: true, id: c.id } : null;
57
+ }
58
+ /** The claim this recorder should hold now; null = none. */
59
+ function desired(held) {
60
+ if (starting)
61
+ return { state: 'starting', sessionId: '' };
62
+ const rec = getState();
63
+ if (rec) {
64
+ const state = stopping === rec.sessionId || stoppingElsewhere(rec) ? 'stopping' : rec.paused ? 'paused' : 'recording';
65
+ return { state, sessionId: rec.sessionId };
66
+ }
67
+ // The stop's face still shows on this pill: hold the claim as `saved` (D7).
68
+ if (held && (face === 'saving' || face === 'saved' || face === 'failed'))
69
+ return { state: 'saved', sessionId: held.sessionId };
70
+ return null;
71
+ }
72
+ /** Write the claim this recorder should hold — never over another holder (D14). */
73
+ function syncClaim() {
74
+ if (!env)
75
+ return;
76
+ const cur = readClaim(env);
77
+ const held = cur?.id === self.inst ? cur : null;
78
+ const want = desired(held);
79
+ if (!want) {
80
+ if (held && releaseClaim(env, self.inst))
81
+ announce(env, self, null);
82
+ return;
83
+ }
84
+ if (held) {
85
+ if (held.state !== want.state || held.sessionId !== want.sessionId) {
86
+ updateClaim(env, self.inst, want);
87
+ announce(env, self, liveOf());
88
+ }
89
+ return;
90
+ }
91
+ // A live session of this page's own (a resume, a cross-tab join) claims an
92
+ // unheld document only: another holder keeps it, and an extension
93
+ // recording elsewhere keeps the browser — the session waits (D14).
94
+ if (want.state === 'saved' || cur || extensionLive())
95
+ return;
96
+ // Another recorder bundle in THIS document may have ended (or replaced)
97
+ // the session meanwhile, and storage events never reach the document that
98
+ // wrote them: the durable record decides before the cache resumes.
99
+ if (!starting && handlers?.resync && !handlers.resync())
100
+ return;
101
+ if (takeClaim(env, self, { id: self.inst, ...want }).ok)
102
+ announce(env, self, liveOf());
103
+ }
104
+ function armPing() {
105
+ const want = leadState.following !== null;
106
+ if (want && !pingTimer)
107
+ pingTimer = setInterval(pingLeader, PING_MS);
108
+ else if (!want && pingTimer) {
109
+ clearInterval(pingTimer);
110
+ pingTimer = null;
111
+ }
112
+ }
113
+ /** Re-derive the claim and who leads; announce a change of leader to the lanes and the HUD. */
114
+ function refresh() {
115
+ if (!env)
116
+ return;
117
+ syncClaim();
118
+ const next = computeFollowing();
119
+ if (next === leadState.following)
120
+ return;
121
+ leadState.following = next;
122
+ armPing();
123
+ notify();
124
+ }
125
+ /** Take the extension parties' word from a complete set of answers. */
126
+ function absorbAnswers(answers) {
127
+ extension.clear();
128
+ for (const h of answers)
129
+ if (h.from === 'extension' && h.live)
130
+ extension.set(h.inst, h.live);
131
+ }
132
+ /**
133
+ * Ping (D5): ask who is here, take the extension's word, clear a holder that
134
+ * no longer answers (under its own id), re-derive. A follower runs it every
135
+ * `PING_MS`, on becoming visible and before a remote command, so a page
136
+ * never waits on an extension that was reloaded or uninstalled.
137
+ */
138
+ export function pingLeader() {
139
+ if (!env)
140
+ return;
141
+ const answers = ask(env, self);
142
+ absorbAnswers(answers);
143
+ const cur = readClaim(env);
144
+ if (cur && cur.id !== self.inst && !answeredBy(answers, cur))
145
+ releaseClaim(env, cur.id);
146
+ refresh();
147
+ }
148
+ function onHello(h) {
149
+ // Another page bundle speaks through the claim; the extension's word on
150
+ // a recording elsewhere arrives only here.
151
+ if (h.from !== 'extension')
152
+ return;
153
+ if (h.gone || !h.live)
154
+ extension.delete(h.inst);
155
+ else
156
+ extension.set(h.inst, h.live);
157
+ refresh();
158
+ }
159
+ function onClaimChange() {
160
+ if (!env)
161
+ return;
162
+ const cur = readClaim(env);
163
+ // A new holder must still answer (an orphaned one never will).
164
+ if (cur && cur.id !== self.inst)
165
+ pingLeader();
166
+ else
167
+ refresh();
168
+ }
169
+ /** The holder's side of a remote command (D11): synchronous, inside its dispatch. */
170
+ function onCommand(c) {
171
+ const held = heldClaim();
172
+ if (!held)
173
+ return null;
174
+ const rec = getState();
175
+ if (!rec || held.state === 'saved')
176
+ return { ok: false, code: 'not_recording', error: 'the recording has ended' };
177
+ if (held.state === 'starting')
178
+ return { ok: false, code: 'not_recording', error: 'the recording is still starting' };
179
+ if (held.state === 'stopping')
180
+ return { ok: false, code: 'stopping', error: `still saving #${rec.sessionId}` };
181
+ try {
182
+ switch (c.op) {
183
+ case 'pause':
184
+ if (rec.dead)
185
+ return { ok: false, code: 'not_recording', error: 'the server ended this recording' };
186
+ handlers?.pause();
187
+ return { ok: true };
188
+ case 'stop':
189
+ if (remoteStop)
190
+ remoteStop();
191
+ else
192
+ handlers?.stop();
193
+ return { ok: true };
194
+ case 'note': {
195
+ const text = c.text?.trim() ?? '';
196
+ if (!text)
197
+ return { ok: false, code: 'bad_command', error: 'a note needs text' };
198
+ if (rec.dead)
199
+ return { ok: false, code: 'not_recording', error: 'the server ended this recording' };
200
+ if (rec.paused)
201
+ return { ok: false, code: 'bad_command', error: 'the recording is paused — resume it to add a note' };
202
+ handlers?.note(text);
203
+ return { ok: true };
204
+ }
205
+ }
206
+ }
207
+ catch (e) {
208
+ console.warn(`vitrinka: remote ${c.op} failed`, e);
209
+ return { ok: false, code: 'bad_command', error: e instanceof Error ? e.message : String(e) };
210
+ }
211
+ }
212
+ // -- the root's door -----------------------------------------------------------
213
+ /**
214
+ * Join the channel for this document; returns the leave. Idempotent across
215
+ * mounts (the last handlers win). `at` is the document's env unless a test
216
+ * passes its own.
217
+ */
218
+ export function startLead(h, at = documentEnv()) {
219
+ handlers = h;
220
+ if (!at)
221
+ return () => undefined;
222
+ users++;
223
+ if (!env) {
224
+ env = at;
225
+ const e = at;
226
+ // First: the extension's one.js answers only where this mark stands.
227
+ markPage(e, RECORDER_ID);
228
+ const offHello = serveHello(e, self, liveOf, onHello);
229
+ const offCommands = serveCommands(e, self, onCommand);
230
+ const offWatch = watchClaim(e, onClaimChange);
231
+ const offState = subscribe(refresh);
232
+ const doc = globalThis.document;
233
+ const onVisible = () => {
234
+ if (doc?.visibilityState === 'visible' && leadState.following)
235
+ pingLeader();
236
+ };
237
+ doc?.addEventListener('visibilitychange', onVisible);
238
+ teardown = () => {
239
+ doc?.removeEventListener('visibilitychange', onVisible);
240
+ offState();
241
+ offWatch();
242
+ offCommands();
243
+ releaseClaim(e, self.inst);
244
+ // Another recorder bundle still mounted here keeps the mark.
245
+ const others = ask(e, self).some((x) => x.from === 'page');
246
+ announce(e, self, null, { gone: true });
247
+ offHello();
248
+ if (!others)
249
+ unmarkPage(e);
250
+ };
251
+ // The mount's ask: who records, here or elsewhere — then claim a resumed session, or follow.
252
+ pingLeader();
253
+ }
254
+ let left = false;
255
+ return () => {
256
+ if (left)
257
+ return;
258
+ left = true;
259
+ users--;
260
+ if (users > 0)
261
+ return;
262
+ teardown?.();
263
+ resetLead();
264
+ };
265
+ }
266
+ /** The root left: forget the channel. The HUD's registrations (its face, its Stop flow) are the controller's and stay. */
267
+ function resetLead() {
268
+ teardown = null;
269
+ env = null;
270
+ handlers = null;
271
+ extension.clear();
272
+ starting = false;
273
+ stopping = null;
274
+ if (pingTimer)
275
+ clearInterval(pingTimer);
276
+ pingTimer = null;
277
+ if (leadState.following !== null) {
278
+ leadState.following = null;
279
+ notify();
280
+ }
281
+ }
282
+ // -- the session's doors -------------------------------------------------------
283
+ function busy(leader, here, sessionId) {
284
+ const n = sessionId ? ` (#${sessionId})` : '';
285
+ if (leader === 'page')
286
+ return new RecorderBusyError(`another recorder on this page is already recording${n} — use its pill, or stop it first`, 'page');
287
+ return here
288
+ ? new RecorderBusyError(`the Vitrinka extension is already recording this page${n} — use its pill, or stop it from the extension`, 'extension')
289
+ : new RecorderBusyError('the Vitrinka extension is recording in another tab — stop it there first', 'extension');
290
+ }
291
+ /**
292
+ * The Start check-and-set (D4, D12): synchronous, before any network call.
293
+ * Throws a `RecorderBusyError` while another recorder leads — a live claim
294
+ * whose holder answers, or the extension recording in another tab — else
295
+ * claims the document as `starting`. A no-op without a mounted root.
296
+ */
297
+ export function claimForStart() {
298
+ if (!env)
299
+ return;
300
+ if (starting)
301
+ throw new Error('vitrinka: a session is already starting');
302
+ const answers = ask(env, self);
303
+ absorbAnswers(answers);
304
+ const cur = readClaim(env);
305
+ if (cur && cur.id !== self.inst && isLive(cur.state)) {
306
+ if (answeredBy(answers, cur)) {
307
+ refresh();
308
+ throw busy(cur.by, true, cur.sessionId);
309
+ }
310
+ releaseClaim(env, cur.id);
311
+ }
312
+ const ext = extensionLive();
313
+ if (ext) {
314
+ refresh();
315
+ throw busy('extension', ext.here, '');
316
+ }
317
+ // A session of this page's own — live, or waiting while another recorder's
318
+ // Saved face still shows (D14) — is never replaced by a new one.
319
+ if (getState())
320
+ throw new Error('vitrinka: a session is already recording');
321
+ const taken = takeClaim(env, self, { id: self.inst, state: 'starting' });
322
+ if (!taken.ok) {
323
+ refresh();
324
+ throw busy(taken.holder.by, true, taken.holder.sessionId);
325
+ }
326
+ starting = true;
327
+ announce(env, self, liveOf());
328
+ refresh();
329
+ }
330
+ /** The Start settled: created (the claim moves on with the session) or not (it goes). */
331
+ export function settleStart(created) {
332
+ if (!starting)
333
+ return;
334
+ starting = false;
335
+ if (!created && env && getState() === null)
336
+ releaseClaim(env, self.inst);
337
+ refresh();
338
+ }
339
+ /** This document's Stop is draining `sessionId` (null: it settled). */
340
+ export function setLeadStopping(sessionId) {
341
+ stopping = sessionId;
342
+ refresh();
343
+ }
344
+ // -- the controller's doors ----------------------------------------------------
345
+ /** The HUD's flow face (D7). */
346
+ export function holdFace(f) {
347
+ if (face === f)
348
+ return;
349
+ face = f;
350
+ refresh();
351
+ }
352
+ /** The HUD's Stop flow, run by a remote stop (D11); returns the unregister. */
353
+ export function setRemoteStop(run) {
354
+ remoteStop = run;
355
+ return () => {
356
+ if (remoteStop === run)
357
+ remoteStop = null;
358
+ };
359
+ }
360
+ export function leadStatus() {
361
+ const claim = env ? readClaim(env) : null;
362
+ const following = leadState.following;
363
+ const leader = following ?? (claim?.id === self.inst || getState() ? 'page' : null);
364
+ return { leader, following, claim };
365
+ }
366
+ function refusal(op, code, error, holder) {
367
+ if (code === 'not_recording')
368
+ return op === 'note' ? 'the recording ended before the note arrived' : (error ?? 'the recording has ended');
369
+ if (code === 'stopping')
370
+ return `still saving${holder.sessionId ? ` #${holder.sessionId}` : ''}`;
371
+ return error ?? 'the recorder refused the command';
372
+ }
373
+ /**
374
+ * A follower's pause, stop or note, acted on by the leader (D11). Pings the
375
+ * holder first; throws with the reason when it is gone, refused, or the
376
+ * extension leads from another tab (whose endpoint here takes no command).
377
+ */
378
+ export function sendToLeader(op, text) {
379
+ if (!env)
380
+ throw new Error('vitrinka: no session is recording');
381
+ pingLeader();
382
+ const holder = readClaim(env);
383
+ if (!holder || holder.id === self.inst || !isLive(holder.state)) {
384
+ if (extensionLive())
385
+ throw new Error('the Vitrinka extension is recording in another tab — use it there');
386
+ throw new Error(op === 'note' ? 'the recording ended before the note arrived' : 'vitrinka: no session is recording');
387
+ }
388
+ const ack = sendCommand(env, self, op, text);
389
+ if (!ack) {
390
+ // No synchronous answer: the holder is gone (D5).
391
+ releaseClaim(env, holder.id);
392
+ refresh();
393
+ throw new Error('the recorder that held this recording is gone');
394
+ }
395
+ if (!ack.ok)
396
+ throw new Error(refusal(op, ack.code, ack.error, holder));
397
+ }
398
+ export function __resetLeadForTests() {
399
+ teardown?.();
400
+ users = 0;
401
+ resetLead();
402
+ face = 'none';
403
+ remoteStop = null;
404
+ }
@@ -16,7 +16,10 @@ export interface DeviceLink {
16
16
  * page, and a token approved into any other one is discarded unstored.
17
17
  */
18
18
  export declare function linkDevice(): Promise<DeviceLink>;
19
- /** Forget the stored link and its account; a live session ends locally (its tail is dropped). */
19
+ /**
20
+ * Forget the stored link and its account; a live session ends locally (its
21
+ * tail is dropped, its attached images' bytes leave the blob journal).
22
+ */
20
23
  export declare function forgetLink(): void;
21
24
  /** Register the 401 → unlinked transition. Returns the uninstaller. */
22
25
  export declare function installUnauthorizedHandler(): () => void;
@@ -6,7 +6,7 @@ import { linkWorkspace, pollLink, startLink } from '@vitrinka/link';
6
6
  import { onUnauthorized } from './api';
7
7
  import { clearLink, defaultLinkLabel, recorderConfig, storeLink } from './config';
8
8
  import { clearAccount } from './me';
9
- import { getState, resetQueues, setState } from './queue';
9
+ import { getState, pruneBlobJournal, resetQueues, setState } from './queue';
10
10
  import { updateRecent } from './recents';
11
11
  import { elapsedOf } from './session';
12
12
  import { notify } from './state';
@@ -27,7 +27,10 @@ export async function linkDevice() {
27
27
  });
28
28
  return { start, linked, cancel: () => ac.abort() };
29
29
  }
30
- /** Forget the stored link and its account; a live session ends locally (its tail is dropped). */
30
+ /**
31
+ * Forget the stored link and its account; a live session ends locally (its
32
+ * tail is dropped, its attached images' bytes leave the blob journal).
33
+ */
31
34
  export function forgetLink() {
32
35
  clearLink();
33
36
  clearAccount();
@@ -36,6 +39,7 @@ export function forgetLink() {
36
39
  updateRecent(rec.sessionId, { status: 'unsaved', durationMs: elapsedOf(rec) });
37
40
  setState(null);
38
41
  resetQueues();
42
+ void pruneBlobJournal(rec.sessionId);
39
43
  }
40
44
  notify();
41
45
  }
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * GET /api/v1/recorder/me → {kind, workspace, user, project, label, prefs}
5
5
  * PATCH /api/v1/recorder/me {prefs: {size?, verbose?}} → the same envelope
6
+ * PATCH /api/v1/recorder/me {prefs: {sheetW?, sheetH?}} → the same envelope
6
7
  *
7
8
  * Prefs live in the recorder storage first (`prefs`): the first paint, an
8
9
  * offline page and a key build (`prefs: null`, PATCH answers 409) all read the
@@ -10,6 +11,12 @@
10
11
  * or a network error means a server too old for the route or an offline one:
11
12
  * the local copy stands, said once. A 401 keeps the session doors' semantics
12
13
  * (api.ts forgets the link).
14
+ *
15
+ * The sheet size (`sheetW`/`sheetH`, CSS px 0..1600, 0 = the HUD's default)
16
+ * rides its OWN PATCH. A server that predates it answers that one 422: the
17
+ * size then stays on this device for the document's lifetime (never asked
18
+ * again, never an error to show), and an answer without the fields never
19
+ * resets the local size — size/verbose sync as before.
13
20
  */
14
21
  import { type HudAccount, type HudPrefs } from './hud/controller';
15
22
  /** The `/recorder/me` envelope. */
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * GET /api/v1/recorder/me → {kind, workspace, user, project, label, prefs}
5
5
  * PATCH /api/v1/recorder/me {prefs: {size?, verbose?}} → the same envelope
6
+ * PATCH /api/v1/recorder/me {prefs: {sheetW?, sheetH?}} → the same envelope
6
7
  *
7
8
  * Prefs live in the recorder storage first (`prefs`): the first paint, an
8
9
  * offline page and a key build (`prefs: null`, PATCH answers 409) all read the
@@ -10,6 +11,12 @@
10
11
  * or a network error means a server too old for the route or an offline one:
11
12
  * the local copy stands, said once. A 401 keeps the session doors' semantics
12
13
  * (api.ts forgets the link).
14
+ *
15
+ * The sheet size (`sheetW`/`sheetH`, CSS px 0..1600, 0 = the HUD's default)
16
+ * rides its OWN PATCH. A server that predates it answers that one 422: the
17
+ * size then stays on this device for the document's lifetime (never asked
18
+ * again, never an error to show), and an answer without the fields never
19
+ * resets the local size — size/verbose sync as before.
13
20
  */
14
21
  import { DEFAULT_PREFS } from './hud/controller';
15
22
  import { api, VitrinkaApiError } from './api';
@@ -19,6 +26,12 @@ import { getRecorderStorage } from './storage';
19
26
  const PREFS_KEY = 'prefs';
20
27
  const ME_KEY = 'me';
21
28
  const SIZES = ['sm', 'md', 'lg'];
29
+ /** The largest sheet size the server stores. */
30
+ const SHEET_PX_MAX = 1600;
31
+ /** A sheet size: a finite number ≥ 0 (within what the server stores), else 0 = the default. */
32
+ function sheetPx(v) {
33
+ return typeof v === 'number' && Number.isFinite(v) && v >= 0 ? Math.min(v, SHEET_PX_MAX) : 0;
34
+ }
22
35
  function parsePrefs(raw) {
23
36
  if (!raw || typeof raw !== 'object')
24
37
  return null;
@@ -26,6 +39,8 @@ function parsePrefs(raw) {
26
39
  return {
27
40
  size: SIZES.includes(o.size) ? o.size : DEFAULT_PREFS.size,
28
41
  verbose: typeof o.verbose === 'boolean' ? o.verbose : DEFAULT_PREFS.verbose,
42
+ sheetW: sheetPx(o.sheetW),
43
+ sheetH: sheetPx(o.sheetH),
29
44
  };
30
45
  }
31
46
  function readJson(key) {
@@ -87,6 +102,21 @@ function noteUnavailable(e) {
87
102
  warned = true;
88
103
  console.warn('vitrinka: /recorder/me unavailable — HUD prefs stay on this device', e);
89
104
  }
105
+ /** A 422 to the sheet-size PATCH said the server predates it: kept local, not sent again this document. */
106
+ let sheetLocal = false;
107
+ /**
108
+ * The server's prefs, keeping the local sheet size where the answer has none
109
+ * (a server before sheetW/sheetH) or this document stopped sending it.
110
+ */
111
+ function withLocalSheet(server, raw) {
112
+ const o = raw;
113
+ const local = cachedPrefs();
114
+ return {
115
+ ...server,
116
+ sheetW: !sheetLocal && typeof o.sheetW === 'number' ? server.sheetW : local.sheetW,
117
+ sheetH: !sheetLocal && typeof o.sheetH === 'number' ? server.sheetH : local.sheetH,
118
+ };
119
+ }
90
120
  function isMe(o) {
91
121
  if (!o || typeof o !== 'object')
92
122
  return false;
@@ -117,7 +147,7 @@ function adopt(me, gen) {
117
147
  write(ME_KEY, account);
118
148
  const server = parsePrefs(me.prefs);
119
149
  if (server && gen === edits)
120
- storePrefs(server);
150
+ storePrefs(withLocalSheet(server, me.prefs));
121
151
  notify();
122
152
  return account;
123
153
  }
@@ -143,23 +173,42 @@ export async function fetchMe() {
143
173
  * account) and an unavailable server keep it local. Never rejects.
144
174
  */
145
175
  export async function savePrefs(patch) {
146
- const next = { ...cachedPrefs(), ...patch };
176
+ const { sheetW, sheetH, ...hud } = patch;
177
+ const sheet = {
178
+ ...(sheetW === undefined ? {} : { sheetW: sheetPx(sheetW) }),
179
+ ...(sheetH === undefined ? {} : { sheetH: sheetPx(sheetH) }),
180
+ };
181
+ const next = { ...cachedPrefs(), ...hud, ...sheet };
147
182
  storePrefs(next);
148
183
  const gen = ++edits;
149
184
  notify();
150
185
  if (!vitrinkaLinked() || unavailable || cachedAccount()?.kind === 'key')
151
186
  return next;
152
- try {
153
- adopt(await api('PATCH', '/api/v1/recorder/me', { prefs: patch }), gen);
154
- }
155
- catch (e) {
156
- if (e instanceof VitrinkaApiError && e.status === 409)
157
- return next;
158
- if (soft(e))
159
- noteUnavailable(e);
160
- else
161
- console.warn('vitrinka: saving HUD prefs failed — kept on this device', e);
187
+ // One PATCH per part: a server before the sheet size answers 422 for a body
188
+ // naming it, which must never cost size/verbose their server copy.
189
+ const parts = [hud, sheetLocal ? {} : sheet].filter((p) => Object.values(p).some((v) => v !== undefined));
190
+ let answer;
191
+ for (const prefs of parts) {
192
+ try {
193
+ answer = await api('PATCH', '/api/v1/recorder/me', { prefs });
194
+ }
195
+ catch (e) {
196
+ if (e instanceof VitrinkaApiError && e.status === 409)
197
+ return next;
198
+ if (prefs === sheet && e instanceof VitrinkaApiError && e.status === 422) {
199
+ sheetLocal = true;
200
+ console.warn('vitrinka: this server keeps no sheet size — it stays on this device', e);
201
+ continue;
202
+ }
203
+ if (soft(e))
204
+ noteUnavailable(e);
205
+ else
206
+ console.warn('vitrinka: saving HUD prefs failed — kept on this device', e);
207
+ break;
208
+ }
162
209
  }
210
+ if (answer !== undefined)
211
+ adopt(answer, gen);
163
212
  return cachedPrefs();
164
213
  }
165
214
  /** Test-only: drop the caches and the unavailable latch. */
@@ -168,5 +217,6 @@ export function __resetMeForTests() {
168
217
  meCache = undefined;
169
218
  unavailable = false;
170
219
  warned = false;
220
+ sheetLocal = false;
171
221
  edits = 0;
172
222
  }