rig-review 2.0.0 → 2.2.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.
@@ -0,0 +1,124 @@
1
+ import {WORKSPACE_TARGET_LIMIT, WORKSPACE_READY_SCHEMA, WORKSPACE_SCHEMA, WORKSPACE_TARGET_DOMAIN, WORKSPACE_VISIBILITY_SCHEMA,
2
+ type WorkspaceList, type WorkspaceTargetView} from '../src/open-workspace-contract.ts';
3
+ import {getLanguage, onLanguageChange, setLanguagePreference, setLinkLanguage, tr} from './i18n.ts';
4
+ import {connectLiveWindow} from './live-connection.ts';
5
+
6
+ const root = document.getElementById('owner-workspace')!;
7
+ const header = document.createElement('header'); header.className = 'owner-header';
8
+ const heading = document.createElement('h1');
9
+ const status = document.createElement('p'); status.className = 'owner-status'; status.setAttribute('role', 'status');
10
+ const language = document.createElement('select');
11
+ for (const value of ['en', 'ko'] as const) {
12
+ const option = document.createElement('option'); option.value = value; language.append(option);
13
+ }
14
+ header.append(heading, status, language);
15
+ const list = document.createElement('nav'); list.className = 'owner-list';
16
+ const stage = document.createElement('main'); stage.className = 'owner-stage';
17
+ root.append(header, list, stage);
18
+ let current: WorkspaceList | null = null, selected: string | null = null, disposed = false;
19
+ let timer: ReturnType<typeof setTimeout> | undefined;
20
+ const cancellation = new AbortController();
21
+ const frames = new Map<string, {frame: HTMLIFrameElement; target: WorkspaceTargetView}>();
22
+ const locationURL = new URL(location.href);
23
+ let capability = locationURL.hash.startsWith('#cap=') ? locationURL.hash.slice(5) : '';
24
+ const storageKey = 'rig-review-owner-workspace/1:' + location.origin;
25
+ if (!/^[a-f0-9]{64}$/.test(capability)) capability = '';
26
+ try {
27
+ if (capability) sessionStorage.setItem(storageKey, capability);
28
+ else capability = sessionStorage.getItem(storageKey) ?? '';
29
+ } catch {}
30
+ locationURL.hash = '';
31
+ history.replaceState(history.state, '', locationURL);
32
+
33
+ function text() {
34
+ document.documentElement.lang = getLanguage();
35
+ heading.textContent = tr('Review workspace'); document.title = tr('Review workspace');
36
+ list.setAttribute('aria-label', tr('Review targets'));
37
+ language.setAttribute('aria-label', tr('Language'));
38
+ language.options[0]!.textContent = tr('English'); language.options[1]!.textContent = tr('Korean');
39
+ language.value = getLanguage();
40
+ }
41
+
42
+ function visibility(entry: {frame: HTMLIFrameElement; target: WorkspaceTargetView}) {
43
+ entry.frame.contentWindow?.postMessage({schema: WORKSPACE_VISIBILITY_SCHEMA,
44
+ incarnation: current?.incarnation, key: entry.target.key, sourceHash: entry.target.contentHash,
45
+ visible: selected === entry.target.key, language: getLanguage()}, location.origin);
46
+ }
47
+
48
+ async function grant(key: string) {
49
+ const encoder = new TextEncoder();
50
+ const secret = await crypto.subtle.importKey('raw', encoder.encode(capability), {name: 'HMAC', hash: 'SHA-256'}, false, ['sign']);
51
+ const bytes = await crypto.subtle.sign('HMAC', secret, encoder.encode(WORKSPACE_TARGET_DOMAIN + '\0' + current!.incarnation + '\0' + key));
52
+ return [...new Uint8Array(bytes)].map(value => value.toString(16).padStart(2, '0')).join('');
53
+ }
54
+
55
+ let selecting = 0;
56
+ async function select(target: WorkspaceTargetView) {
57
+ const intent = ++selecting;
58
+ if (target.unavailable) {status.textContent = tr(target.unavailable); return;}
59
+ let entry = frames.get(target.key);
60
+ if (!entry) {
61
+ if (frames.size >= WORKSPACE_TARGET_LIMIT) {status.textContent = tr('WORKSPACE_TARGET_LIMIT'); return;}
62
+ const token = await grant(target.key);
63
+ if (disposed || intent !== selecting) return;
64
+ const frame = document.createElement('iframe');
65
+ frame.title = target.title; frame.hidden = true;
66
+ frame.src = target.entry + '&lang=' + getLanguage() + '#cap=' + token;
67
+ entry = {frame, target}; frames.set(target.key, entry); stage.append(frame);
68
+ }
69
+ selected = target.key;
70
+ for (const mounted of frames.values()) {
71
+ visibility(mounted);
72
+ mounted.frame.hidden = mounted.target.key !== selected;
73
+ mounted.frame.inert = mounted.frame.hidden;
74
+ mounted.frame.setAttribute('aria-hidden', String(mounted.frame.hidden));
75
+ }
76
+ render();
77
+ }
78
+
79
+ function render() {
80
+ text(); list.replaceChildren();
81
+ for (const target of current?.targets ?? []) {
82
+ const button = document.createElement('button'); button.type = 'button';
83
+ button.dataset.targetKey = target.key; button.setAttribute('aria-pressed', String(target.key === selected));
84
+ const title = document.createElement('span'); title.className = 'owner-target-title'; title.textContent = target.title;
85
+ const state = document.createElement('span'); state.className = 'owner-target-status';
86
+ state.textContent = tr(target.unavailable ?? (target.viewOnly ? 'Inspection only' : 'Review target'));
87
+ const animation = document.createElement('span'); animation.className = 'owner-target-animation'; animation.textContent = target.animation;
88
+ button.append(title, state, animation); button.onclick = () => {void select(target).catch(() => {status.textContent = tr('WORKSPACE_REQUEST_REFUSED');});};
89
+ list.append(button);
90
+ }
91
+ }
92
+
93
+ async function refresh() {
94
+ try {
95
+ if (!capability) {status.textContent = tr('WORKSPACE_CAPABILITY_REQUIRED'); return;}
96
+ const response = await fetch('/workspace/api/targets', {
97
+ headers: {'X-Review-Capability': capability}, signal: AbortSignal.any([cancellation.signal, AbortSignal.timeout(5000)]),
98
+ });
99
+ const value = await response.json() as WorkspaceList & {error?: string};
100
+ if (!response.ok) {status.textContent = tr(value.error ?? 'WORKSPACE_REQUEST_REFUSED'); return;}
101
+ if (value.schema !== WORKSPACE_SCHEMA || !Array.isArray(value.targets) || value.targets.length > WORKSPACE_TARGET_LIMIT ||
102
+ current && current.incarnation !== value.incarnation) {status.textContent = tr('WORKSPACE_HOST_MISMATCH'); return;}
103
+ const changed = JSON.stringify(value) !== JSON.stringify(current);
104
+ if (!current) status.textContent = tr('Select a target. Drafts stay with their original source.');
105
+ current = value;
106
+ if (changed) render();
107
+ if (!selected && value.targets[0]) await select(value.targets[0]);
108
+ } catch {if (!disposed) status.textContent = tr('WORKSPACE_REQUEST_REFUSED');}
109
+ finally {if (!disposed) timer = setTimeout(() => {void refresh();}, 1000);}
110
+ }
111
+
112
+ window.addEventListener('message', event => {
113
+ if (event.origin !== location.origin || !current || !event.data || typeof event.data !== 'object') return;
114
+ const value = event.data;
115
+ if (value.schema !== WORKSPACE_READY_SCHEMA || value.incarnation !== current.incarnation) return;
116
+ const entry = frames.get(value.key);
117
+ if (entry && entry.frame.contentWindow === event.source && entry.target.contentHash === value.sourceHash) visibility(entry);
118
+ });
119
+ language.onchange = () => {setLinkLanguage(null); setLanguagePreference(language.value === 'ko' ? 'ko' : 'en');};
120
+ onLanguageChange(() => {render(); for (const entry of frames.values()) visibility(entry);});
121
+ const disconnect = connectLiveWindow({route: () => '/workspace/live'});
122
+ window.addEventListener('pagehide', () => {disposed = true; clearTimeout(timer); cancellation.abort(); disconnect();}, {once: true});
123
+ text();
124
+ void refresh();
@@ -0,0 +1,21 @@
1
+ import {WORKSPACE_READY_SCHEMA, WORKSPACE_VISIBILITY_SCHEMA} from '../src/open-workspace-contract.ts';
2
+ import {setLanguagePreference, setLinkLanguage} from './i18n.ts';
3
+
4
+ /** Same-origin shipped frames still validate the exact parent and displayed source. */
5
+ export function bindWorkspaceVisibility(binding: {key: string; sourceHash: string; pause(): void}) {
6
+ const incarnation = new URL(location.href).searchParams.get('workspace');
7
+ if (parent === window || !incarnation) return;
8
+ const receive = (event: MessageEvent) => {
9
+ if (event.origin !== location.origin || event.source !== parent || !event.data || typeof event.data !== 'object') return;
10
+ const value = event.data;
11
+ if (value.schema !== WORKSPACE_VISIBILITY_SCHEMA || value.incarnation !== incarnation ||
12
+ value.key !== binding.key || value.sourceHash !== binding.sourceHash || typeof value.visible !== 'boolean') return;
13
+ if (!value.visible) binding.pause();
14
+ if (value.language === 'en' || value.language === 'ko') {
15
+ setLinkLanguage(null); setLanguagePreference(value.language);
16
+ }
17
+ };
18
+ window.addEventListener('message', receive);
19
+ window.addEventListener('pagehide', () => window.removeEventListener('message', receive), {once: true});
20
+ parent.postMessage({schema: WORKSPACE_READY_SCHEMA, incarnation, key: binding.key, sourceHash: binding.sourceHash}, location.origin);
21
+ }