@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.
- package/CHANGELOG.md +118 -0
- package/README.md +93 -6
- package/build/hud.d.ts +4 -1
- package/build/hud.iife.js +46 -9
- package/build/one.iife.js +1 -0
- package/build/protocol/index.d.ts +55 -4
- package/build/protocol/index.js +4 -2
- package/build/recorder/RecorderProvider.js +48 -9
- 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/capture/console.d.ts +2 -0
- package/build/recorder/capture/console.js +22 -2
- package/build/recorder/capture/nav.d.ts +13 -2
- package/build/recorder/capture/nav.js +49 -11
- package/build/recorder/capture/net.d.ts +2 -0
- package/build/recorder/capture/net.js +30 -4
- package/build/recorder/control.d.ts +17 -0
- package/build/recorder/control.js +39 -2
- package/build/recorder/hud/Hud.d.ts +1 -1
- package/build/recorder/hud/Hud.js +113 -30
- package/build/recorder/hud/RecorderPill.js +6 -2
- 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/chords.d.ts +10 -0
- package/build/recorder/hud/chords.js +34 -0
- package/build/recorder/hud/controller.d.ts +55 -3
- package/build/recorder/hud/controller.js +1 -1
- package/build/recorder/hud/flow.d.ts +22 -1
- package/build/recorder/hud/flow.js +16 -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/index.d.ts +1 -0
- package/build/recorder/index.js +1 -0
- package/build/recorder/lead.d.ts +90 -0
- package/build/recorder/lead.js +404 -0
- 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/one-iife.d.ts +13 -0
- package/build/recorder/one-iife.js +13 -0
- package/build/recorder/one.d.ts +170 -0
- package/build/recorder/one.js +292 -0
- package/build/recorder/page-controller.js +26 -4
- package/build/recorder/queue.d.ts +33 -3
- package/build/recorder/queue.js +409 -26
- package/build/recorder/report.d.ts +9 -0
- package/build/recorder/report.js +55 -6
- package/build/recorder/session.d.ts +33 -7
- package/build/recorder/session.js +111 -31
- package/build/recorder/state.d.ts +9 -0
- package/build/recorder/state.js +7 -0
- 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/build/recorder/version.d.ts +3 -0
- package/build/recorder/version.js +3 -0
- package/package.json +5 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entry of `build/one.iife.js`: the ONE recorder protocol (`one.ts`) as one
|
|
3
|
+
* self-contained script — no React, no imports — for the browser extension's
|
|
4
|
+
* content scripts, which have no bundler. It defines `globalThis.VitrinkaOne`:
|
|
5
|
+
*
|
|
6
|
+
* const env = VitrinkaOne.documentEnv();
|
|
7
|
+
* const claim = VitrinkaOne.readClaim(env);
|
|
8
|
+
*
|
|
9
|
+
* The extension vendors it beside `build/hud.iife.js` under the same pin
|
|
10
|
+
* (`tools/vendor-hud`), so both worlds speak one codec.
|
|
11
|
+
*/
|
|
12
|
+
import * as one from './one';
|
|
13
|
+
export type VitrinkaOneGlobal = typeof one;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entry of `build/one.iife.js`: the ONE recorder protocol (`one.ts`) as one
|
|
3
|
+
* self-contained script — no React, no imports — for the browser extension's
|
|
4
|
+
* content scripts, which have no bundler. It defines `globalThis.VitrinkaOne`:
|
|
5
|
+
*
|
|
6
|
+
* const env = VitrinkaOne.documentEnv();
|
|
7
|
+
* const claim = VitrinkaOne.readClaim(env);
|
|
8
|
+
*
|
|
9
|
+
* The extension vendors it beside `build/hud.iife.js` under the same pin
|
|
10
|
+
* (`tools/vendor-hud`), so both worlds speak one codec.
|
|
11
|
+
*/
|
|
12
|
+
import * as one from './one';
|
|
13
|
+
globalThis.VitrinkaOne = Object.freeze({ ...one });
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/** Protocol version; a reader takes the fields it knows from a higher one. */
|
|
2
|
+
export declare const ONE_VERSION = 1;
|
|
3
|
+
/** `<html>` attribute: a page recorder is mounted here (its recorder id, e.g. `web/0.5.0`). */
|
|
4
|
+
export declare const PAGE_ATTR = "data-vitrinka-page";
|
|
5
|
+
/** `<html>` attribute: the claim (JSON). */
|
|
6
|
+
export declare const LEAD_ATTR = "data-vitrinka-lead";
|
|
7
|
+
export declare const HELLO_EVENT = "vitrinka:hello";
|
|
8
|
+
export declare const COMMAND_EVENT = "vitrinka:command";
|
|
9
|
+
export declare const ACK_EVENT = "vitrinka:ack";
|
|
10
|
+
export type OneParty = 'page' | 'extension';
|
|
11
|
+
/** `saved` = the leader's pill still shows Saving / Saved / failed after a stop (D7); every other state is live. */
|
|
12
|
+
export type ClaimState = 'starting' | 'recording' | 'paused' | 'stopping' | 'saved';
|
|
13
|
+
export type CommandOp = 'pause' | 'stop' | 'note';
|
|
14
|
+
export type AckCode = 'not_recording' | 'stopping' | 'not_here' | 'bad_command';
|
|
15
|
+
/** Who records this document. No title, workspace, URL or credential: any script on the page reads it. */
|
|
16
|
+
export interface Claim {
|
|
17
|
+
v: number;
|
|
18
|
+
by: OneParty;
|
|
19
|
+
/** The holder: a page recorder instance's nonce, or the extension recording's generation. */
|
|
20
|
+
id: string;
|
|
21
|
+
state: ClaimState;
|
|
22
|
+
/** `''` while `starting`. */
|
|
23
|
+
sessionId: string;
|
|
24
|
+
/** Epoch ms the claim was taken. */
|
|
25
|
+
since: number;
|
|
26
|
+
}
|
|
27
|
+
/** What a party says about the recording it knows of. */
|
|
28
|
+
export interface Live {
|
|
29
|
+
by: OneParty;
|
|
30
|
+
state: ClaimState;
|
|
31
|
+
/** The sender holds this document's claim. */
|
|
32
|
+
here: boolean;
|
|
33
|
+
/** The claim's `id` when `here`. */
|
|
34
|
+
id?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface Hello {
|
|
37
|
+
v: number;
|
|
38
|
+
from: OneParty;
|
|
39
|
+
/** The sender instance; every party ignores events carrying its own. */
|
|
40
|
+
inst: string;
|
|
41
|
+
/** Asks every live party to answer with its own hello, inside this dispatch. */
|
|
42
|
+
ask?: true;
|
|
43
|
+
live?: Live | null;
|
|
44
|
+
/** The sender is leaving. */
|
|
45
|
+
gone?: true;
|
|
46
|
+
}
|
|
47
|
+
export interface Command {
|
|
48
|
+
v: number;
|
|
49
|
+
from: OneParty;
|
|
50
|
+
inst: string;
|
|
51
|
+
/** This command's id; its ack carries it back. */
|
|
52
|
+
id: string;
|
|
53
|
+
op: CommandOp;
|
|
54
|
+
/** A note's text. */
|
|
55
|
+
text?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface Ack {
|
|
58
|
+
v: number;
|
|
59
|
+
from: OneParty;
|
|
60
|
+
inst: string;
|
|
61
|
+
id: string;
|
|
62
|
+
ok: boolean;
|
|
63
|
+
code?: AckCode;
|
|
64
|
+
error?: string;
|
|
65
|
+
}
|
|
66
|
+
/** What a party answers a command with; `null` = not the holder (no answer at all). */
|
|
67
|
+
export type CommandReply = {
|
|
68
|
+
ok: true;
|
|
69
|
+
} | {
|
|
70
|
+
ok: false;
|
|
71
|
+
code: AckCode;
|
|
72
|
+
error?: string;
|
|
73
|
+
};
|
|
74
|
+
/** The attribute surface of `<html>` the claim lives on. */
|
|
75
|
+
export interface ClaimRoot {
|
|
76
|
+
getAttribute(name: string): string | null;
|
|
77
|
+
setAttribute(name: string, value: string): void;
|
|
78
|
+
removeAttribute(name: string): void;
|
|
79
|
+
}
|
|
80
|
+
export interface OneEnv {
|
|
81
|
+
/** `document.documentElement`. */
|
|
82
|
+
root: ClaimRoot;
|
|
83
|
+
/** `window`: where actions travel. */
|
|
84
|
+
bus: EventTarget;
|
|
85
|
+
/** Calls back after the claim attribute changed; returns the stop. Default: a MutationObserver on `root`. */
|
|
86
|
+
watch?: (fn: () => void) => () => void;
|
|
87
|
+
}
|
|
88
|
+
/** A party on the channel: which side it is and its instance nonce. */
|
|
89
|
+
export interface OneSelf {
|
|
90
|
+
from: OneParty;
|
|
91
|
+
inst: string;
|
|
92
|
+
}
|
|
93
|
+
/** The document's env; null where there is no DOM (SSR, a worker). */
|
|
94
|
+
export declare function documentEnv(): OneEnv | null;
|
|
95
|
+
/** A fresh nonce: `p.k3x9q2…` for a page instance. */
|
|
96
|
+
export declare function nonce(prefix: string): string;
|
|
97
|
+
/** Parse a claim attribute; null for anything that is not one. */
|
|
98
|
+
export declare function parseClaim(raw: string | null | undefined): Claim | null;
|
|
99
|
+
export declare function serializeClaim(c: Claim): string;
|
|
100
|
+
/** Live = a recording is (or is about to be) under way; `saved` is not. */
|
|
101
|
+
export declare function isLive(state: ClaimState | null | undefined): boolean;
|
|
102
|
+
export declare function parseHello(raw: unknown): Hello | null;
|
|
103
|
+
/** A command; null when it does not parse or names an unknown op (treated as absent). */
|
|
104
|
+
export declare function parseCommand(raw: unknown): Command | null;
|
|
105
|
+
export declare function parseAck(raw: unknown): Ack | null;
|
|
106
|
+
export declare function readClaim(env: OneEnv): Claim | null;
|
|
107
|
+
export type TakeResult = {
|
|
108
|
+
ok: true;
|
|
109
|
+
claim: Claim;
|
|
110
|
+
} | {
|
|
111
|
+
ok: false;
|
|
112
|
+
holder: Claim;
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* The synchronous check-and-set (D4): take this document's claim unless a
|
|
116
|
+
* LIVE claim of another holder stands and that holder still answers (D5).
|
|
117
|
+
* A `saved` claim, a stale one and the caller's own are taken over. Call it
|
|
118
|
+
* before any network call, inside one task.
|
|
119
|
+
*/
|
|
120
|
+
export declare function takeClaim(env: OneEnv, self: OneSelf, next: {
|
|
121
|
+
id: string;
|
|
122
|
+
state: ClaimState;
|
|
123
|
+
sessionId?: string;
|
|
124
|
+
}): TakeResult;
|
|
125
|
+
/** Move the holder's claim to `state` (and `sessionId`); false when the claim is not `id`'s. */
|
|
126
|
+
export declare function updateClaim(env: OneEnv, id: string, patch: {
|
|
127
|
+
state: ClaimState;
|
|
128
|
+
sessionId?: string;
|
|
129
|
+
}): boolean;
|
|
130
|
+
/** Remove the claim only if it is `id`'s — the holder releasing, or anyone clearing a stale one. */
|
|
131
|
+
export declare function releaseClaim(env: OneEnv, id: string): boolean;
|
|
132
|
+
/** Call back on every change of the claim attribute; returns the stop. */
|
|
133
|
+
export declare function watchClaim(env: OneEnv, fn: () => void): () => void;
|
|
134
|
+
export declare function markPage(env: OneEnv, recorderId: string): void;
|
|
135
|
+
export declare function unmarkPage(env: OneEnv): void;
|
|
136
|
+
export declare function pageMarked(env: OneEnv): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Ask who is here: every live party answers inside this dispatch. Returns
|
|
139
|
+
* the answers (never the caller's own).
|
|
140
|
+
*/
|
|
141
|
+
export declare function ask(env: OneEnv, self: OneSelf): Hello[];
|
|
142
|
+
/** Say what this party knows (no `ask`): an answer, a change, or `gone` on leaving. */
|
|
143
|
+
export declare function announce(env: OneEnv, self: OneSelf, live: Live | null, opts?: {
|
|
144
|
+
gone?: boolean;
|
|
145
|
+
}): void;
|
|
146
|
+
/**
|
|
147
|
+
* Serve the hello channel: an ask is answered at once with `live()`; any
|
|
148
|
+
* other hello (an answer to someone else, a change, a `gone`) reaches
|
|
149
|
+
* `onHello`. The party's own dispatches are ignored. `answerIf` keeps a
|
|
150
|
+
* party silent where it must not speak — the extension's `one.js` answers
|
|
151
|
+
* only in documents that carry `data-vitrinka-page` (D6).
|
|
152
|
+
*/
|
|
153
|
+
export declare function serveHello(env: OneEnv, self: OneSelf, live: () => Live | null, onHello?: (h: Hello) => void, opts?: {
|
|
154
|
+
answerIf?: () => boolean;
|
|
155
|
+
}): () => void;
|
|
156
|
+
/** Did the claim's holder answer? Matched on its `id` when the answer names one, else on its side. */
|
|
157
|
+
export declare function answeredBy(answers: readonly Hello[], holder: Claim): boolean;
|
|
158
|
+
/** Ping the claim's holder (D5): true when it answers inside the dispatch. */
|
|
159
|
+
export declare function holderAnswers(env: OneEnv, self: OneSelf, holder: Claim): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Send a command to the claim's holder; its ack arrives inside this
|
|
162
|
+
* dispatch. Null = nobody answered (the holder is gone, D5).
|
|
163
|
+
*/
|
|
164
|
+
export declare function sendCommand(env: OneEnv, self: OneSelf, op: CommandOp, text?: string): Ack | null;
|
|
165
|
+
/**
|
|
166
|
+
* Serve commands: `handle` answers a parsed command (another party's, a
|
|
167
|
+
* known op) with its reply — synchronously, inside the command's dispatch —
|
|
168
|
+
* or `null` when this party is not the holder, which sends no ack at all.
|
|
169
|
+
*/
|
|
170
|
+
export declare function serveCommands(env: OneEnv, self: OneSelf, handle: (c: Command) => CommandReply | null): () => void;
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ONE recorder (extension + page recorder decisions, 2026-10-12): the
|
|
3
|
+
* page-embedded recorder (main world) and the browser extension's content
|
|
4
|
+
* scripts (isolated world) share nothing but the DOM, so this is the whole
|
|
5
|
+
* protocol they speak over it — written once here, imported by the page
|
|
6
|
+
* recorder (`lead.ts`) and shipped to the extension as `build/one.iife.js`
|
|
7
|
+
* (`globalThis.VitrinkaOne`). DOM-light and React-free: every function takes
|
|
8
|
+
* a `OneEnv` (the `<html>` element and `window` by default), so it runs
|
|
9
|
+
* under bun without a DOM.
|
|
10
|
+
*
|
|
11
|
+
* - State lives on `<html>` attributes: `data-vitrinka-page` (a page
|
|
12
|
+
* recorder is mounted here) and `data-vitrinka-lead` (the claim: who
|
|
13
|
+
* records this document). A claim is written only by its holder, taken by
|
|
14
|
+
* a synchronous check-and-set (`takeClaim`), and cleared by anyone else
|
|
15
|
+
* only under its own `id` once its holder no longer answers (D4, D5).
|
|
16
|
+
* - Actions travel as `window` CustomEvents whose `detail` is a JSON STRING
|
|
17
|
+
* (an object `detail` from the isolated world reads `null` in the main
|
|
18
|
+
* world). `dispatchEvent` runs every world's listeners synchronously, so a
|
|
19
|
+
* `hello` ask and a `command` are answered inside their own dispatch: no
|
|
20
|
+
* answer means no live holder, and nobody ever waits on a timer.
|
|
21
|
+
* - Never `data-vitrinka-recorder` on `<html>`: that is rrweb's block
|
|
22
|
+
* selector and the click lanes' "ours" marker.
|
|
23
|
+
*/
|
|
24
|
+
import { listen } from './hud/listen';
|
|
25
|
+
/** Protocol version; a reader takes the fields it knows from a higher one. */
|
|
26
|
+
export const ONE_VERSION = 1;
|
|
27
|
+
/** `<html>` attribute: a page recorder is mounted here (its recorder id, e.g. `web/0.5.0`). */
|
|
28
|
+
export const PAGE_ATTR = 'data-vitrinka-page';
|
|
29
|
+
/** `<html>` attribute: the claim (JSON). */
|
|
30
|
+
export const LEAD_ATTR = 'data-vitrinka-lead';
|
|
31
|
+
export const HELLO_EVENT = 'vitrinka:hello';
|
|
32
|
+
export const COMMAND_EVENT = 'vitrinka:command';
|
|
33
|
+
export const ACK_EVENT = 'vitrinka:ack';
|
|
34
|
+
/** The document's env; null where there is no DOM (SSR, a worker). */
|
|
35
|
+
export function documentEnv() {
|
|
36
|
+
const doc = globalThis.document;
|
|
37
|
+
const root = doc?.documentElement;
|
|
38
|
+
if (!root || typeof globalThis.dispatchEvent !== 'function')
|
|
39
|
+
return null;
|
|
40
|
+
return { root, bus: globalThis };
|
|
41
|
+
}
|
|
42
|
+
/** A fresh nonce: `p.k3x9q2…` for a page instance. */
|
|
43
|
+
export function nonce(prefix) {
|
|
44
|
+
return `${prefix}.${Math.random().toString(36).slice(2, 10)}`;
|
|
45
|
+
}
|
|
46
|
+
// -- the codec ---------------------------------------------------------------
|
|
47
|
+
const PARTIES = ['page', 'extension'];
|
|
48
|
+
const STATES = ['starting', 'recording', 'paused', 'stopping', 'saved'];
|
|
49
|
+
const OPS = ['pause', 'stop', 'note'];
|
|
50
|
+
const CODES = ['not_recording', 'stopping', 'not_here', 'bad_command'];
|
|
51
|
+
function bagOf(raw) {
|
|
52
|
+
if (typeof raw !== 'string')
|
|
53
|
+
return null;
|
|
54
|
+
try {
|
|
55
|
+
const v = JSON.parse(raw);
|
|
56
|
+
return v !== null && typeof v === 'object' && !Array.isArray(v) ? v : null;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const str = (v) => typeof v === 'string';
|
|
63
|
+
const party = (v) => str(v) && PARTIES.includes(v);
|
|
64
|
+
const claimState = (v) => str(v) && STATES.includes(v);
|
|
65
|
+
/** `v` is a positive integer; a higher one still parses (D18). */
|
|
66
|
+
const version = (v) => typeof v === 'number' && Number.isInteger(v) && v >= 1;
|
|
67
|
+
/** Parse a claim attribute; null for anything that is not one. */
|
|
68
|
+
export function parseClaim(raw) {
|
|
69
|
+
const b = bagOf(raw);
|
|
70
|
+
if (!b || !version(b.v) || !party(b.by) || !str(b.id) || !b.id || !claimState(b.state))
|
|
71
|
+
return null;
|
|
72
|
+
return {
|
|
73
|
+
v: b.v,
|
|
74
|
+
by: b.by,
|
|
75
|
+
id: b.id,
|
|
76
|
+
state: b.state,
|
|
77
|
+
sessionId: str(b.sessionId) ? b.sessionId : '',
|
|
78
|
+
since: typeof b.since === 'number' ? b.since : 0,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function serializeClaim(c) {
|
|
82
|
+
return JSON.stringify({ v: c.v, by: c.by, id: c.id, state: c.state, sessionId: c.sessionId, since: c.since });
|
|
83
|
+
}
|
|
84
|
+
/** Live = a recording is (or is about to be) under way; `saved` is not. */
|
|
85
|
+
export function isLive(state) {
|
|
86
|
+
return state !== undefined && state !== null && state !== 'saved';
|
|
87
|
+
}
|
|
88
|
+
function parseLive(raw) {
|
|
89
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
|
|
90
|
+
return null;
|
|
91
|
+
const b = raw;
|
|
92
|
+
if (!party(b.by) || !claimState(b.state))
|
|
93
|
+
return null;
|
|
94
|
+
return { by: b.by, state: b.state, here: b.here === true, ...(str(b.id) && b.id ? { id: b.id } : {}) };
|
|
95
|
+
}
|
|
96
|
+
export function parseHello(raw) {
|
|
97
|
+
const b = bagOf(raw);
|
|
98
|
+
if (!b || !version(b.v) || !party(b.from) || !str(b.inst) || !b.inst)
|
|
99
|
+
return null;
|
|
100
|
+
const live = parseLive(b.live);
|
|
101
|
+
return {
|
|
102
|
+
v: b.v,
|
|
103
|
+
from: b.from,
|
|
104
|
+
inst: b.inst,
|
|
105
|
+
...(b.ask === true ? { ask: true } : {}),
|
|
106
|
+
...(b.live !== undefined ? { live } : {}),
|
|
107
|
+
...(b.gone === true ? { gone: true } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** A command; null when it does not parse or names an unknown op (treated as absent). */
|
|
111
|
+
export function parseCommand(raw) {
|
|
112
|
+
const b = bagOf(raw);
|
|
113
|
+
if (!b || !version(b.v) || !party(b.from) || !str(b.inst) || !b.inst || !str(b.id) || !b.id)
|
|
114
|
+
return null;
|
|
115
|
+
if (!str(b.op) || !OPS.includes(b.op))
|
|
116
|
+
return null;
|
|
117
|
+
return { v: b.v, from: b.from, inst: b.inst, id: b.id, op: b.op, ...(str(b.text) ? { text: b.text } : {}) };
|
|
118
|
+
}
|
|
119
|
+
export function parseAck(raw) {
|
|
120
|
+
const b = bagOf(raw);
|
|
121
|
+
if (!b || !version(b.v) || !party(b.from) || !str(b.inst) || !str(b.id) || typeof b.ok !== 'boolean')
|
|
122
|
+
return null;
|
|
123
|
+
return {
|
|
124
|
+
v: b.v,
|
|
125
|
+
from: b.from,
|
|
126
|
+
inst: b.inst,
|
|
127
|
+
id: b.id,
|
|
128
|
+
ok: b.ok,
|
|
129
|
+
...(str(b.code) && CODES.includes(b.code) ? { code: b.code } : {}),
|
|
130
|
+
...(str(b.error) ? { error: b.error } : {}),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// -- state: the attributes ---------------------------------------------------
|
|
134
|
+
export function readClaim(env) {
|
|
135
|
+
return parseClaim(env.root.getAttribute(LEAD_ATTR));
|
|
136
|
+
}
|
|
137
|
+
function writeClaim(env, c) {
|
|
138
|
+
const next = serializeClaim(c);
|
|
139
|
+
// Transitions only: the leader's rrweb and the page's own observers see every write.
|
|
140
|
+
if (env.root.getAttribute(LEAD_ATTR) !== next)
|
|
141
|
+
env.root.setAttribute(LEAD_ATTR, next);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The synchronous check-and-set (D4): take this document's claim unless a
|
|
145
|
+
* LIVE claim of another holder stands and that holder still answers (D5).
|
|
146
|
+
* A `saved` claim, a stale one and the caller's own are taken over. Call it
|
|
147
|
+
* before any network call, inside one task.
|
|
148
|
+
*/
|
|
149
|
+
export function takeClaim(env, self, next) {
|
|
150
|
+
const cur = readClaim(env);
|
|
151
|
+
if (cur && cur.id !== next.id && isLive(cur.state) && holderAnswers(env, self, cur))
|
|
152
|
+
return { ok: false, holder: cur };
|
|
153
|
+
const claim = {
|
|
154
|
+
v: ONE_VERSION,
|
|
155
|
+
by: self.from,
|
|
156
|
+
id: next.id,
|
|
157
|
+
state: next.state,
|
|
158
|
+
sessionId: next.sessionId ?? '',
|
|
159
|
+
// Re-taking its own live claim keeps its age; a new claim (or one over a `saved` face) starts now.
|
|
160
|
+
since: cur?.id === next.id && isLive(cur.state) ? cur.since : Date.now(),
|
|
161
|
+
};
|
|
162
|
+
writeClaim(env, claim);
|
|
163
|
+
return { ok: true, claim };
|
|
164
|
+
}
|
|
165
|
+
/** Move the holder's claim to `state` (and `sessionId`); false when the claim is not `id`'s. */
|
|
166
|
+
export function updateClaim(env, id, patch) {
|
|
167
|
+
const cur = readClaim(env);
|
|
168
|
+
if (cur?.id !== id)
|
|
169
|
+
return false;
|
|
170
|
+
writeClaim(env, { ...cur, v: ONE_VERSION, state: patch.state, sessionId: patch.sessionId ?? cur.sessionId });
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
/** Remove the claim only if it is `id`'s — the holder releasing, or anyone clearing a stale one. */
|
|
174
|
+
export function releaseClaim(env, id) {
|
|
175
|
+
if (readClaim(env)?.id !== id)
|
|
176
|
+
return false;
|
|
177
|
+
env.root.removeAttribute(LEAD_ATTR);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
/** Call back on every change of the claim attribute; returns the stop. */
|
|
181
|
+
export function watchClaim(env, fn) {
|
|
182
|
+
if (env.watch)
|
|
183
|
+
return env.watch(fn);
|
|
184
|
+
if (typeof MutationObserver !== 'function' || typeof Node === 'undefined' || !(env.root instanceof Node))
|
|
185
|
+
return () => undefined;
|
|
186
|
+
const mo = new MutationObserver(fn);
|
|
187
|
+
mo.observe(env.root, { attributes: true, attributeFilter: [LEAD_ATTR] });
|
|
188
|
+
return () => mo.disconnect();
|
|
189
|
+
}
|
|
190
|
+
export function markPage(env, recorderId) {
|
|
191
|
+
if (env.root.getAttribute(PAGE_ATTR) !== recorderId)
|
|
192
|
+
env.root.setAttribute(PAGE_ATTR, recorderId);
|
|
193
|
+
}
|
|
194
|
+
export function unmarkPage(env) {
|
|
195
|
+
env.root.removeAttribute(PAGE_ATTR);
|
|
196
|
+
}
|
|
197
|
+
export function pageMarked(env) {
|
|
198
|
+
return env.root.getAttribute(PAGE_ATTR) !== null;
|
|
199
|
+
}
|
|
200
|
+
// -- actions: the events -----------------------------------------------------
|
|
201
|
+
function emit(env, type, msg) {
|
|
202
|
+
env.bus.dispatchEvent(new CustomEvent(type, { detail: JSON.stringify(msg) }));
|
|
203
|
+
}
|
|
204
|
+
/** Listen natively (`hud/listen.ts`: a zone.js host routes it like any other). */
|
|
205
|
+
function on(env, type, fn) {
|
|
206
|
+
return listen(env.bus, type, (e) => fn(e.detail));
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Ask who is here: every live party answers inside this dispatch. Returns
|
|
210
|
+
* the answers (never the caller's own).
|
|
211
|
+
*/
|
|
212
|
+
export function ask(env, self) {
|
|
213
|
+
const answers = [];
|
|
214
|
+
const off = on(env, HELLO_EVENT, (d) => {
|
|
215
|
+
const h = parseHello(d);
|
|
216
|
+
if (h && !h.ask && h.inst !== self.inst)
|
|
217
|
+
answers.push(h);
|
|
218
|
+
});
|
|
219
|
+
try {
|
|
220
|
+
emit(env, HELLO_EVENT, { v: ONE_VERSION, from: self.from, inst: self.inst, ask: true });
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
off();
|
|
224
|
+
}
|
|
225
|
+
return answers;
|
|
226
|
+
}
|
|
227
|
+
/** Say what this party knows (no `ask`): an answer, a change, or `gone` on leaving. */
|
|
228
|
+
export function announce(env, self, live, opts = {}) {
|
|
229
|
+
emit(env, HELLO_EVENT, { v: ONE_VERSION, from: self.from, inst: self.inst, live, ...(opts.gone ? { gone: true } : {}) });
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Serve the hello channel: an ask is answered at once with `live()`; any
|
|
233
|
+
* other hello (an answer to someone else, a change, a `gone`) reaches
|
|
234
|
+
* `onHello`. The party's own dispatches are ignored. `answerIf` keeps a
|
|
235
|
+
* party silent where it must not speak — the extension's `one.js` answers
|
|
236
|
+
* only in documents that carry `data-vitrinka-page` (D6).
|
|
237
|
+
*/
|
|
238
|
+
export function serveHello(env, self, live, onHello, opts = {}) {
|
|
239
|
+
return on(env, HELLO_EVENT, (d) => {
|
|
240
|
+
const h = parseHello(d);
|
|
241
|
+
if (!h || h.inst === self.inst)
|
|
242
|
+
return;
|
|
243
|
+
if (!h.ask)
|
|
244
|
+
onHello?.(h);
|
|
245
|
+
else if (opts.answerIf?.() ?? true)
|
|
246
|
+
announce(env, self, live());
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/** Did the claim's holder answer? Matched on its `id` when the answer names one, else on its side. */
|
|
250
|
+
export function answeredBy(answers, holder) {
|
|
251
|
+
return answers.some((h) => h.live?.here === true && (h.live.id ? h.live.id === holder.id : h.from === holder.by));
|
|
252
|
+
}
|
|
253
|
+
/** Ping the claim's holder (D5): true when it answers inside the dispatch. */
|
|
254
|
+
export function holderAnswers(env, self, holder) {
|
|
255
|
+
return answeredBy(ask(env, self), holder);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Send a command to the claim's holder; its ack arrives inside this
|
|
259
|
+
* dispatch. Null = nobody answered (the holder is gone, D5).
|
|
260
|
+
*/
|
|
261
|
+
export function sendCommand(env, self, op, text) {
|
|
262
|
+
const id = nonce('c');
|
|
263
|
+
let ack = null;
|
|
264
|
+
const off = on(env, ACK_EVENT, (d) => {
|
|
265
|
+
const a = parseAck(d);
|
|
266
|
+
if (a && a.id === id && a.inst !== self.inst && !ack)
|
|
267
|
+
ack = a;
|
|
268
|
+
});
|
|
269
|
+
try {
|
|
270
|
+
emit(env, COMMAND_EVENT, { v: ONE_VERSION, from: self.from, inst: self.inst, id, op, ...(text !== undefined ? { text } : {}) });
|
|
271
|
+
}
|
|
272
|
+
finally {
|
|
273
|
+
off();
|
|
274
|
+
}
|
|
275
|
+
return ack;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Serve commands: `handle` answers a parsed command (another party's, a
|
|
279
|
+
* known op) with its reply — synchronously, inside the command's dispatch —
|
|
280
|
+
* or `null` when this party is not the holder, which sends no ack at all.
|
|
281
|
+
*/
|
|
282
|
+
export function serveCommands(env, self, handle) {
|
|
283
|
+
return on(env, COMMAND_EVENT, (d) => {
|
|
284
|
+
const c = parseCommand(d);
|
|
285
|
+
if (!c || c.inst === self.inst)
|
|
286
|
+
return;
|
|
287
|
+
const reply = handle(c);
|
|
288
|
+
if (!reply)
|
|
289
|
+
return;
|
|
290
|
+
emit(env, ACK_EVENT, { v: ONE_VERSION, from: self.from, inst: self.inst, id: c.id, ...reply });
|
|
291
|
+
});
|
|
292
|
+
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { readLink, recorderConfig, vitrinkaLinked } from './config';
|
|
2
|
+
import { holdFace, leadStatus, sendToLeader, setRemoteStop } from './lead';
|
|
2
3
|
import { forgetLink, linkDevice } from './link';
|
|
3
4
|
import { cachedAccount, cachedPrefs, fetchMe, savePrefs } from './me';
|
|
4
5
|
import { getState, health } from './queue';
|
|
5
6
|
import { boardUrlOf, readRecents, refreshRecents } from './recents';
|
|
6
|
-
import { canReport, holdReport, sendReport } from './report';
|
|
7
|
+
import { canAttach, canReport, holdReport, sendReport } from './report';
|
|
7
8
|
import { addAnnotation, addNote, RECORDER_ID, startSession, stopSession, togglePause } from './session';
|
|
8
|
-
import { annotateState, setAnnotating, subscribe } from './state';
|
|
9
|
+
import { annotateState, leadState, setAnnotating, subscribe } from './state';
|
|
9
10
|
function build() {
|
|
10
11
|
const rec = getState();
|
|
11
12
|
const cfg = recorderConfig();
|
|
@@ -47,6 +48,8 @@ function build() {
|
|
|
47
48
|
workspaceUrl: cfg.url,
|
|
48
49
|
version: RECORDER_ID,
|
|
49
50
|
canReport: canReport(),
|
|
51
|
+
canAttach: canAttach(),
|
|
52
|
+
...(leadState.following ? { following: leadState.following } : {}),
|
|
50
53
|
};
|
|
51
54
|
}
|
|
52
55
|
export function createPageController() {
|
|
@@ -66,10 +69,20 @@ export function createPageController() {
|
|
|
66
69
|
async start({ title }) {
|
|
67
70
|
await startSession({ title });
|
|
68
71
|
},
|
|
72
|
+
// While another recorder leads, pause, stop and a note act on it (ONE recorder D11).
|
|
69
73
|
async togglePause() {
|
|
74
|
+
if (leadState.following)
|
|
75
|
+
return sendToLeader('pause');
|
|
70
76
|
await togglePause();
|
|
71
77
|
},
|
|
72
78
|
async stop() {
|
|
79
|
+
if (leadState.following) {
|
|
80
|
+
sendToLeader('stop');
|
|
81
|
+
const sessionId = leadStatus().claim?.sessionId;
|
|
82
|
+
return sessionId ? { sessionId } : {};
|
|
83
|
+
}
|
|
84
|
+
// The stop's face holds the claim as `saved` from its first moment (D7).
|
|
85
|
+
holdFace('saving');
|
|
73
86
|
const boardUrl = getState()?.boardUrl;
|
|
74
87
|
const sessionId = getState()?.sessionId;
|
|
75
88
|
const done = await stopSession();
|
|
@@ -79,8 +92,15 @@ export function createPageController() {
|
|
|
79
92
|
const url = boardUrlOf(done) ?? boardUrl;
|
|
80
93
|
return { ...(url ? { boardUrl: url } : {}), ...(sessionId ? { sessionId } : {}) };
|
|
81
94
|
},
|
|
82
|
-
note
|
|
83
|
-
|
|
95
|
+
note(text, attachments) {
|
|
96
|
+
if (!leadState.following)
|
|
97
|
+
return addNote(text, attachments);
|
|
98
|
+
// Images never cross to the other recorder; its note carries the text.
|
|
99
|
+
if (attachments?.length)
|
|
100
|
+
console.warn('vitrinka: the note goes to the recorder that leads — without its images');
|
|
101
|
+
sendToLeader('note', text);
|
|
102
|
+
},
|
|
103
|
+
annotate: ({ text, rect, selector, task, attachments }) => addAnnotation(text, rect, selector, { task, ...(attachments ? { attachments } : {}) }),
|
|
84
104
|
setAnnotating,
|
|
85
105
|
async link() {
|
|
86
106
|
const flow = await linkDevice();
|
|
@@ -100,5 +120,7 @@ export function createPageController() {
|
|
|
100
120
|
refreshRecents: () => refreshRecents(getState()?.sessionId ?? null),
|
|
101
121
|
holdReport,
|
|
102
122
|
report: (r) => sendReport(r),
|
|
123
|
+
onRemoteStop: setRemoteStop,
|
|
124
|
+
faceChanged: holdFace,
|
|
103
125
|
};
|
|
104
126
|
}
|
|
@@ -14,6 +14,17 @@
|
|
|
14
14
|
* originally allocated seq (gap-fill), retried oldest-first AHEAD of the
|
|
15
15
|
* event flush. Chunks are kept in memory (they can be megabytes; the KV
|
|
16
16
|
* store is small) and persisted best-effort under a byte budget.
|
|
17
|
+
* - A note's attached images (recorder attachments) ride the same way: the
|
|
18
|
+
* note and its images take ONE allocation — the images' seqs first, the
|
|
19
|
+
* note's last, its payload listing theirs — and each image uploads to
|
|
20
|
+
* `/shot?seq=N` before its `attachment` row (blobKey) joins the stream.
|
|
21
|
+
* Their bytes wait in the blob journal (./storage/blobs — IndexedDB), so a
|
|
22
|
+
* queued image survives a reload and is uploaded before its row is
|
|
23
|
+
* delivered; they leave it once the server acknowledged the row. The
|
|
24
|
+
* capture journal of such a note is written only once those bytes are in
|
|
25
|
+
* (another tab must never recover an image it cannot read yet) and lives
|
|
26
|
+
* until every one of its seqs is acknowledged, so a reload or another tab
|
|
27
|
+
* can finish the lot.
|
|
17
28
|
* - A permanent server verdict (4xx minus 408/429) drops the item loudly;
|
|
18
29
|
* transient failures stop the pass and the next flush retries.
|
|
19
30
|
* - A reload keeps the undelivered event tail (KV store is durable).
|
|
@@ -28,6 +39,7 @@
|
|
|
28
39
|
*/
|
|
29
40
|
import type { RedactionPolicy } from '@vitrinka/redact';
|
|
30
41
|
import type { RecorderEvent } from '../protocol';
|
|
42
|
+
import type { StagedImage } from './attachments';
|
|
31
43
|
export declare const MAX_BATCH_BYTES: number;
|
|
32
44
|
/** The server's per-POST event cap. */
|
|
33
45
|
export declare const EVENTS_PER_POST = 500;
|
|
@@ -61,6 +73,11 @@ export interface SessionState {
|
|
|
61
73
|
* reload re-applies the same rules instead of silently reverting.
|
|
62
74
|
*/
|
|
63
75
|
policy?: RedactionPolicy | null;
|
|
76
|
+
/**
|
|
77
|
+
* The same read's `attachments` (recorder attachments D6): the sheets take
|
|
78
|
+
* images only on true. Durable with the session, like `policy`.
|
|
79
|
+
*/
|
|
80
|
+
attachments?: boolean;
|
|
64
81
|
/**
|
|
65
82
|
* When (ISO) another tab last renewed its claim to be stopping this
|
|
66
83
|
* session: the copy it stores reads as paused with its clock frozen at the
|
|
@@ -112,6 +129,11 @@ export declare function runBeforeStopHooks(): void;
|
|
|
112
129
|
export declare function adoptStoredState(raw: string | null): StoredChange;
|
|
113
130
|
/** Write the in-memory buffer (and the pending chunks) to storage NOW. */
|
|
114
131
|
export declare function persistNow(): void;
|
|
132
|
+
/**
|
|
133
|
+
* Delete journaled images nothing will upload: `ended`'s (a session just
|
|
134
|
+
* stopped), or, without it, every session's but the live one.
|
|
135
|
+
*/
|
|
136
|
+
export declare function pruneBlobJournal(ended?: string): Promise<void>;
|
|
115
137
|
export declare function queuedCount(): number;
|
|
116
138
|
/** Fresh-session baseline; called by startSession before capture begins. */
|
|
117
139
|
export declare function resetHealth(baseSeq?: number): void;
|
|
@@ -159,12 +181,13 @@ export declare function isSessionLive(id: string): boolean;
|
|
|
159
181
|
* kind+payload plus the route they observed. With no session the event goes
|
|
160
182
|
* to the flight recorder when the idle pill keeps one (memory only — never
|
|
161
183
|
* storage, never the network), else it is dropped. Returns the stamped ts
|
|
162
|
-
* (null when dropped).
|
|
184
|
+
* (null when dropped). `images` (a note's attachments, live sessions only)
|
|
185
|
+
* take the seqs just before the event's, which lists them as `attachments`.
|
|
163
186
|
*/
|
|
164
187
|
export declare function pushEvent(kind: string, payload: Record<string, unknown> | undefined, route: {
|
|
165
188
|
tabId: string;
|
|
166
189
|
tabHost: string;
|
|
167
|
-
}): string | null;
|
|
190
|
+
}, images?: readonly StagedImage[]): string | null;
|
|
168
191
|
/** Push a fully-formed event (chunk rows carry a pre-allocated seq). */
|
|
169
192
|
export declare function pushRawEvent(ev: RecorderEvent): void;
|
|
170
193
|
export declare function idleMs(): number;
|
|
@@ -195,7 +218,7 @@ export declare function scheduleFlush(): void;
|
|
|
195
218
|
export declare function flush(opts?: {
|
|
196
219
|
keepalive?: boolean;
|
|
197
220
|
}): Promise<boolean>;
|
|
198
|
-
/** Drain until buffer
|
|
221
|
+
/** Drain until buffer, chunks and images are empty or the deadline passes. */
|
|
199
222
|
export declare function drainBuffer(deadlineMs?: number): Promise<boolean>;
|
|
200
223
|
/** Reset buffers for a fresh session. */
|
|
201
224
|
export declare function resetQueues(): void;
|
|
@@ -209,5 +232,12 @@ export declare function __chunksForTests(): {
|
|
|
209
232
|
count: number;
|
|
210
233
|
sessionId: string;
|
|
211
234
|
}[];
|
|
235
|
+
/** Test-only: the images waiting for upload (in order), with their blob journal keys. */
|
|
236
|
+
export declare function __imagesForTests(): {
|
|
237
|
+
seq: number;
|
|
238
|
+
sessionId: string;
|
|
239
|
+
blob: string;
|
|
240
|
+
name: string;
|
|
241
|
+
}[];
|
|
212
242
|
/** Test-only: drop the in-memory caches while LEAVING storage intact (a reload). */
|
|
213
243
|
export declare function __dropCachesForTests(): void;
|