@hyperframes/studio 0.9.31 → 0.9.33

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 (31) hide show
  1. package/dist/assets/{codec-DXM4vA6G.js → codec-qz1-OXLj.js} +1 -1
  2. package/dist/assets/{hyperframes-player-D_016Ubp.js → hyperframes-player-2eG1zFX5.js} +1 -1
  3. package/dist/assets/{index-dsfjBidg.css → index-BJTCbv8z.css} +1 -1
  4. package/dist/assets/{index-Ob53qo6K.js → index-DDmhnfrS.js} +189 -189
  5. package/dist/assets/{index-Bw4stQMm.js → index-DUveJ_YP.js} +1 -1
  6. package/dist/assets/{pausedVideoDecoder-vbdgaMqL.js → pausedVideoDecoder-CoHTKUjM.js} +1 -1
  7. package/dist/{chunk-J3VJE463.js → chunk-52AJB4PN.js} +4 -1
  8. package/dist/{chunk-LRTUWOR4.js → chunk-YKBN653R.js} +1 -1
  9. package/dist/index.html +2 -2
  10. package/dist/index.js +2054 -1392
  11. package/dist/player.js +2 -2
  12. package/dist/styles.css +1 -1
  13. package/dist/ui.js +1 -1
  14. package/package.json +8 -8
  15. package/src/App.tsx +1 -0
  16. package/src/components/StudioHeader.tsx +4 -0
  17. package/src/components/share/QuickShare.test.tsx +622 -0
  18. package/src/components/share/QuickShare.tsx +104 -0
  19. package/src/components/share/QuickShareControls.tsx +265 -0
  20. package/src/components/share/ShareAudience.tsx +120 -0
  21. package/src/components/share/localShareClient.ts +171 -0
  22. package/src/components/share/localShareTelemetry.test.ts +22 -0
  23. package/src/components/share/localShareTelemetry.ts +13 -0
  24. package/src/components/share/useQuickShare.ts +215 -0
  25. package/src/components/sidebar/CompositionsTab.hoverPreview.test.tsx +100 -0
  26. package/src/components/sidebar/CompositionsTab.test.ts +2 -24
  27. package/src/components/sidebar/CompositionsTab.tsx +35 -100
  28. package/src/components/ui/Input.tsx +4 -1
  29. package/src/components/ui/valueControls.test.tsx +85 -0
  30. package/src/webmcp/polyfill.transport.test.ts +59 -0
  31. package/src/webmcp/polyfill.ts +15 -0
@@ -0,0 +1,104 @@
1
+ import { useState } from "react";
2
+ import { useMountEffect } from "../../hooks/useMountEffect";
3
+ import { Button, Popover } from "../ui";
4
+ import { shareBootstrap, safeShareUrl } from "./localShareClient";
5
+ import { useQuickShare } from "./useQuickShare";
6
+ import {
7
+ ShareSettings,
8
+ ShareNotices,
9
+ ShareActions,
10
+ ShareSourceStatus,
11
+ ShareLogin,
12
+ SharePlan,
13
+ } from "./QuickShareControls";
14
+ export function QuickShare({ beforeShare }: { beforeShare?: () => Promise<void> }) {
15
+ const [token, setToken] = useState<string | null>(null);
16
+ const [open, setOpen] = useState(false);
17
+ useMountEffect(() => {
18
+ if (!document.querySelector('meta[name="hyperframes-local-share"][content="1"]')) return;
19
+ const controller = new AbortController();
20
+ void shareBootstrap(controller.signal)
21
+ .then((value) => {
22
+ if (!controller.signal.aborted) setToken(value);
23
+ })
24
+ .catch(() => {});
25
+ return () => controller.abort();
26
+ });
27
+ if (!token) return null;
28
+ return (
29
+ <Popover
30
+ open={open}
31
+ onOpenChange={setOpen}
32
+ align="end"
33
+ aria-label="Share project"
34
+ trigger={<Button variant="secondary">Share</Button>}
35
+ className="w-96 max-w-[calc(100vw-2rem)] rounded-2xl p-4"
36
+ >
37
+ {open ? <QuickShareContent token={token} beforeShare={beforeShare} /> : null}
38
+ </Popover>
39
+ );
40
+ }
41
+
42
+ export function QuickShareContent({
43
+ token,
44
+ beforeShare,
45
+ }: {
46
+ token: string;
47
+ beforeShare?: () => Promise<void>;
48
+ }) {
49
+ const [includeSource, setIncludeSource] = useState(false);
50
+ const model = { ...useQuickShare(token, beforeShare), includeSource, setIncludeSource };
51
+ const { snapshot, progress, error } = model;
52
+ const url = safeShareUrl(snapshot?.share.url ?? "");
53
+ const manageUrl = safeShareUrl(snapshot?.share.manageUrl ?? "");
54
+ return (
55
+ <div className="flex flex-col gap-3 text-step-13">
56
+ <div>
57
+ <h2 className="text-step-15 font-semibold">Share project</h2>
58
+ <p className="mt-1 text-step-11 text-text-3">Share the latest saved project.</p>
59
+ </div>
60
+ {snapshot ? (
61
+ <>
62
+ <ShareSettings model={model} snapshot={snapshot} />
63
+ <ShareNotices model={model} snapshot={snapshot} />
64
+ <ShareSourceStatus model={model} snapshot={snapshot} />
65
+ <ShareActions model={model} snapshot={snapshot} url={url} />
66
+ <SharePlan model={model} snapshot={snapshot} />
67
+ </>
68
+ ) : null}
69
+ {!snapshot && !error ? <p role="status">Loading sharing…</p> : null}
70
+ {progress ? (
71
+ <p role="status">{progress} You can close this panel; keep Studio running.</p>
72
+ ) : null}
73
+ {model.notice ? <p role="status">{model.notice}</p> : null}
74
+ <ShareLogin model={model} />
75
+ {error ? (
76
+ <p role="alert" className="text-step-11 text-text-2">
77
+ {error}
78
+ </p>
79
+ ) : null}
80
+ <div className="flex flex-wrap items-center gap-2 border-t border-border pt-3">
81
+ {manageUrl ? (
82
+ <a
83
+ href={manageUrl}
84
+ target="_blank"
85
+ rel="noreferrer"
86
+ className="flex h-8 items-center rounded-full px-2 text-step-12 text-text-2 hover:bg-hover"
87
+ >
88
+ Manage sharing
89
+ </a>
90
+ ) : null}
91
+ {url ? (
92
+ <a
93
+ href={url}
94
+ target="_blank"
95
+ rel="noreferrer"
96
+ className="flex h-8 items-center rounded-full px-2 text-step-12 text-text-2 hover:bg-hover"
97
+ >
98
+ Open shared page
99
+ </a>
100
+ ) : null}
101
+ </div>
102
+ </div>
103
+ );
104
+ }
@@ -0,0 +1,265 @@
1
+ import type { LocalShareSnapshot } from "@hyperframes/studio-server";
2
+ import { Button } from "../ui";
3
+ import { ShareAudience } from "./ShareAudience";
4
+ import { Link, ArrowUpRight } from "@phosphor-icons/react";
5
+ import type { QuickShareModel } from "./useQuickShare";
6
+
7
+ type Props = { model: QuickShareModel; snapshot: LocalShareSnapshot };
8
+ export function ShareSettings({ model, snapshot }: Props) {
9
+ return <ShareAudience model={model} snapshot={snapshot} />;
10
+ }
11
+ export function ShareNotices({ model, snapshot }: Props) {
12
+ const { share } = snapshot;
13
+ const messages = [
14
+ model.audience === "invite" ? "Add people from Manage sharing after creating the link." : null,
15
+ share.changed
16
+ ? "Local changes aren’t shared yet. Update the shared version when you’re ready."
17
+ : share.changed === null
18
+ ? "Changes after your last upload aren’t shared automatically. Update to share the current project."
19
+ : null,
20
+ share.state === "link_off" ? "The link is off. Updating the video keeps it off." : null,
21
+ share.state === "processing" && !model.busy && snapshot.job?.state !== "failed"
22
+ ? "The shared video is still being prepared. Reopen sharing to check its status."
23
+ : null,
24
+ share.state === "unavailable" ? "Sharing is paused. Open Manage sharing for details." : null,
25
+ !snapshot.versionSharing && share.state === "not_shared"
26
+ ? "Sharing isn’t available for this account yet."
27
+ : null,
28
+ ];
29
+ return (
30
+ <>
31
+ {share.state === "failed" && share.failure ? (
32
+ <p role="alert" className="text-step-11 text-text-3">
33
+ {share.failure}
34
+ </p>
35
+ ) : null}
36
+ {messages.filter(Boolean).map((message) => (
37
+ <p key={message} className="text-step-11 text-text-3">
38
+ {message}
39
+ </p>
40
+ ))}
41
+ </>
42
+ );
43
+ }
44
+ export function ShareActions({ model, snapshot, url }: Props & { url: string | null }) {
45
+ if (snapshot.share.state === "not_shared")
46
+ return <CreateShare model={model} snapshot={snapshot} />;
47
+ return (
48
+ <>
49
+ {url ? <CopyShare model={model} snapshot={snapshot} url={url} /> : null}
50
+ {snapshot.versionSharing && snapshot.share.state !== "unavailable" ? (
51
+ <Button
52
+ className="h-9 rounded-lg"
53
+ variant={snapshot.share.changed !== false ? "primary" : "secondary"}
54
+ disabled={model.busy}
55
+ onClick={() => void model.run({ action: "update", includeSource: model.includeSource })}
56
+ >
57
+ {(snapshot.job?.state === "failed" || snapshot.share.state === "failed") &&
58
+ snapshot.share.hasReadyVersion === false
59
+ ? "Create share link"
60
+ : "Update shared version"}
61
+ </Button>
62
+ ) : null}
63
+ <ManageShare model={model} snapshot={snapshot} url={url} />
64
+ </>
65
+ );
66
+ }
67
+ function CreateShare({ model, snapshot }: Props) {
68
+ const space = snapshot.spaces.find((item) => item.id === model.spaceId);
69
+ const canCreate = snapshot.versionSharing && space?.canCreate && model.audience !== "";
70
+ return (
71
+ <>
72
+ {!space || !space.canCreate ? (
73
+ <p className="text-step-11 text-text-3">
74
+ Sharing isn’t available in the workspace selected at sign-in. Sign in with an accessible
75
+ workspace to continue.
76
+ </p>
77
+ ) : null}
78
+ <Button
79
+ variant="primary"
80
+ className="h-9 self-end rounded-lg px-3 text-step-13"
81
+ disabled={model.busy || !canCreate}
82
+ onClick={() => {
83
+ if (model.audience)
84
+ void model.run({
85
+ action: "share",
86
+ audience: model.audience,
87
+ space: model.spaceId,
88
+ includeSource: model.includeSource,
89
+ });
90
+ }}
91
+ >
92
+ Create share link
93
+ </Button>
94
+ </>
95
+ );
96
+ }
97
+ function CopyShare({ model, snapshot, url }: Props & { url: string }) {
98
+ const label = model.copied
99
+ ? "Copied"
100
+ : snapshot.share.changed !== false
101
+ ? "Copy current shared link"
102
+ : "Copy link";
103
+ return (
104
+ <div className="flex min-w-0 items-center gap-2 rounded-xl border border-border p-2.5">
105
+ <span
106
+ className="min-w-0 flex-1 truncate rounded-lg bg-press px-2.5 py-2 font-mono text-step-11 text-text-3"
107
+ title={url}
108
+ >
109
+ {url.replace(/^https:\/\//, "")}
110
+ </span>
111
+ <Button
112
+ className="h-9 shrink-0 rounded-lg px-3 text-step-12"
113
+ variant={snapshot.share.changed !== false ? "secondary" : "primary"}
114
+ disabled={model.busy}
115
+ onClick={() => void model.copy(url)}
116
+ >
117
+ <Link size={15} aria-hidden="true" />
118
+ {label}
119
+ </Button>
120
+ </div>
121
+ );
122
+ }
123
+ function ManageShare({ model, snapshot, url }: Props & { url: string | null }) {
124
+ return (
125
+ <div className="flex flex-wrap items-center justify-end gap-1">
126
+ {model.audience && model.audience !== snapshot.share.audience ? (
127
+ <Button
128
+ variant="ghost"
129
+ className="h-8 rounded-full text-step-13"
130
+ disabled={model.busy}
131
+ onClick={() => {
132
+ if (model.audience) void model.run({ action: "access", audience: model.audience });
133
+ }}
134
+ >
135
+ Save access
136
+ </Button>
137
+ ) : null}
138
+ {snapshot.share.state === "link_off" ? (
139
+ <Button disabled={model.busy} onClick={() => void model.run({ action: "on" })}>
140
+ Turn link on
141
+ </Button>
142
+ ) : null}
143
+ {url ? (
144
+ <Button
145
+ variant="ghost"
146
+ className="h-8 rounded-full text-step-13"
147
+ disabled={model.busy}
148
+ onClick={() => {
149
+ if (
150
+ window.confirm(
151
+ "Turn off this link? Everyone using the link loses access. Comments are kept.",
152
+ )
153
+ )
154
+ void model.run({ action: "off" });
155
+ }}
156
+ >
157
+ Turn link off
158
+ </Button>
159
+ ) : null}
160
+ </div>
161
+ );
162
+ }
163
+
164
+ export function ShareSourceStatus({ model, snapshot }: Props) {
165
+ const last = snapshot.job?.result;
166
+ const recover = snapshot.job?.error?.code === "source_unavailable";
167
+ let message = "Video only unless you choose to include project files.";
168
+ if (last?.source === "video_only")
169
+ message = "Last Studio upload: video only. Project files weren’t included.";
170
+ if (last?.source === "ready")
171
+ message =
172
+ last.state === "ready"
173
+ ? "Last Studio upload: project files included."
174
+ : "Project files are ready; sharing is still processing.";
175
+ return (
176
+ <>
177
+ <label className="flex items-start gap-2 text-step-12 text-text-3">
178
+ <input
179
+ type="checkbox"
180
+ checked={model.includeSource}
181
+ disabled={model.busy}
182
+ onChange={(event) => model.setIncludeSource(event.target.checked)}
183
+ />
184
+ <span>
185
+ Include editable project files
186
+ <span className="block text-step-11">
187
+ Uploads portable project files, including unused and Git-ignored files. Review your
188
+ folder and .hyperframesignore before sharing.
189
+ </span>
190
+ </span>
191
+ </label>
192
+ <p className="text-step-11 text-text-3">{message}</p>
193
+ {recover ? (
194
+ <Button
195
+ variant="ghost"
196
+ className="h-8 rounded-full text-step-13"
197
+ disabled={model.busy}
198
+ onClick={() =>
199
+ void model.run({ action: "update", videoOnly: true, includeSource: false })
200
+ }
201
+ >
202
+ Retry with video only
203
+ </Button>
204
+ ) : null}
205
+ </>
206
+ );
207
+ }
208
+
209
+ export function ShareLogin({ model }: { model: QuickShareModel }) {
210
+ if (
211
+ !model.errorCode ||
212
+ ![
213
+ "not_configured",
214
+ "unauthenticated",
215
+ "login_expired",
216
+ "login_changed",
217
+ "refresh_failed",
218
+ "device_auth_failed",
219
+ ].includes(model.errorCode)
220
+ )
221
+ return null;
222
+ return (
223
+ <>
224
+ <Button
225
+ variant="primary"
226
+ disabled={model.busy}
227
+ onClick={() => void model.run({ action: "login" })}
228
+ >
229
+ Sign in
230
+ </Button>
231
+ <p className="text-step-11 text-text-3">
232
+ Or run <code>hyperframes auth login</code> in your terminal, then return to Studio. Sharing
233
+ reconnects automatically.
234
+ </p>
235
+ </>
236
+ );
237
+ }
238
+
239
+ export function SharePlan({ model, snapshot }: Props) {
240
+ const space = snapshot.spaces.find((item) => item.id === model.spaceId);
241
+ const upgrade = space?.upgradeUrl;
242
+ const allowed =
243
+ upgrade &&
244
+ ["https://hyperframes.dev/pricing", "https://dev.hyperframes.dev/pricing"].includes(upgrade);
245
+ return (
246
+ <div className="flex flex-wrap items-center justify-between gap-2 text-step-12">
247
+ {space && space.limit !== null && space.used !== null ? (
248
+ <span className="text-text-3 tabular-nums">
249
+ {space.used} of {space.limit} free share links used
250
+ </span>
251
+ ) : null}
252
+ {space?.plan === "free" && allowed ? (
253
+ <a
254
+ href={upgrade}
255
+ target="_blank"
256
+ rel="noreferrer"
257
+ className="inline-flex h-8 items-center gap-1 rounded-full px-2 font-semibold text-accent hover:bg-hover"
258
+ >
259
+ Upgrade to Pro
260
+ <ArrowUpRight size={13} aria-hidden="true" />
261
+ </a>
262
+ ) : null}
263
+ </div>
264
+ );
265
+ }
@@ -0,0 +1,120 @@
1
+ import { CaretDown, Globe, Lock, Users, UserPlus } from "@phosphor-icons/react";
2
+ import type { LocalShareAudience, LocalShareSnapshot } from "@hyperframes/studio-server";
3
+ import { Menu, MenuRadioGroup, MenuRadioItem } from "../ui";
4
+ import { isAudience } from "./localShareClient";
5
+ import type { QuickShareModel } from "./useQuickShare";
6
+
7
+ const AUDIENCES = {
8
+ public: { label: "Anyone with the link", detail: "Anyone you send the link to", icon: Globe },
9
+ team: {
10
+ label: "Anyone in this workspace",
11
+ detail: "People in your signed-in workspace",
12
+ icon: Users,
13
+ },
14
+ invite: {
15
+ label: "Only invited people",
16
+ detail: "People you add in Manage sharing",
17
+ icon: UserPlus,
18
+ },
19
+ private: { label: "Only you", detail: "Only you can access this share", icon: Lock },
20
+ };
21
+
22
+ export function ShareAudience({
23
+ model,
24
+ snapshot,
25
+ }: {
26
+ model: QuickShareModel;
27
+ snapshot: LocalShareSnapshot;
28
+ }) {
29
+ const selected = model.audience ? AUDIENCES[model.audience] : null;
30
+ const Icon = selected?.icon ?? Lock;
31
+ const space = snapshot.spaces.find((item) => item.id === model.spaceId);
32
+ const choices: LocalShareAudience[] = ["public", "team", "invite"];
33
+ if (snapshot.share.state !== "not_shared") choices.push("private");
34
+ return (
35
+ <Menu
36
+ aria-label="Who can access"
37
+ className="w-80 max-w-[calc(100vw-2rem)] rounded-xl p-1.5"
38
+ trigger={
39
+ <button
40
+ type="button"
41
+ aria-label="Who can access"
42
+ disabled={model.busy || snapshot.share.state === "unavailable"}
43
+ className="flex min-h-11 w-full items-center gap-2.5 rounded-xl px-1.5 py-1.5 text-left text-step-13 hover:bg-hover disabled:opacity-40"
44
+ >
45
+ <span className="grid size-8 shrink-0 place-items-center rounded-full bg-press text-text-2">
46
+ <Icon size={16} aria-hidden="true" />
47
+ </span>
48
+ <span className="min-w-0 flex-1 font-semibold">
49
+ {selected?.label ?? "Choose who can access"}
50
+ </span>
51
+ <CaretDown size={14} aria-hidden="true" />
52
+ </button>
53
+ }
54
+ >
55
+ <MenuRadioGroup
56
+ value={model.audience}
57
+ onValueChange={(value) => {
58
+ if (isAudience(value)) model.setAudience(value);
59
+ }}
60
+ >
61
+ {choices.map((value) => {
62
+ const option = AUDIENCES[value];
63
+ const OptionIcon = option.icon;
64
+ const needsPro = requiresPro(space, value, snapshot.share.audience);
65
+ return (
66
+ <MenuRadioItem
67
+ key={value}
68
+ value={value}
69
+ closeOnClick
70
+ disabled={
71
+ model.busy ||
72
+ (!space?.audiences.includes(value) && value !== snapshot.share.audience)
73
+ }
74
+ className="h-auto min-h-12 rounded-lg px-2 py-2 text-step-13"
75
+ >
76
+ <span className="flex items-center gap-2.5">
77
+ <span className="grid size-7 shrink-0 place-items-center rounded-full bg-press text-text-2">
78
+ <OptionIcon size={15} aria-hidden="true" />
79
+ </span>
80
+ <span className="min-w-0 leading-snug">
81
+ <span className="flex items-center gap-2 font-semibold">
82
+ {option.label}
83
+ {needsPro ? (
84
+ <span className="rounded bg-press px-1.5 py-0.5 text-step-10 font-semibold">
85
+ Pro
86
+ </span>
87
+ ) : null}
88
+ </span>
89
+ <span className="block whitespace-normal text-step-11 text-text-3">
90
+ {needsPro ? "Upgrade to Pro to use this option" : option.detail}
91
+ </span>
92
+ </span>
93
+ </span>
94
+ </MenuRadioItem>
95
+ );
96
+ })}
97
+ </MenuRadioGroup>
98
+ {space?.plan === "free" &&
99
+ (!space.audiences.includes("team") || !space.audiences.includes("invite")) ? (
100
+ <p className="mx-2 mt-1 border-t border-border px-1 py-2 text-step-11 text-text-2">
101
+ Free includes anyone-with-the-link sharing. Upgrade to Pro for workspace and invite-only
102
+ sharing.
103
+ </p>
104
+ ) : null}
105
+ </Menu>
106
+ );
107
+ }
108
+
109
+ function requiresPro(
110
+ space: LocalShareSnapshot["spaces"][number] | undefined,
111
+ value: LocalShareAudience,
112
+ current: LocalShareAudience | null,
113
+ ) {
114
+ return (
115
+ space?.plan === "free" &&
116
+ !space.audiences.includes(value) &&
117
+ value !== current &&
118
+ (value === "team" || value === "invite")
119
+ );
120
+ }
@@ -0,0 +1,171 @@
1
+ import type {
2
+ LocalShareAction,
3
+ LocalShareAudience,
4
+ LocalShareJob,
5
+ LocalShareSnapshot,
6
+ LocalShareState,
7
+ LocalShareSpace,
8
+ } from "@hyperframes/studio-server";
9
+ import { studioApiFetch } from "../../utils/studioApiFetch";
10
+
11
+ const audiences: readonly LocalShareAudience[] = ["public", "team", "invite", "private"];
12
+ const record = (value: unknown): value is Record<string, unknown> =>
13
+ typeof value === "object" && value !== null && !Array.isArray(value);
14
+ const text = (value: unknown): value is string => typeof value === "string";
15
+ const nullableText = (value: unknown): value is string | null => value === null || text(value);
16
+ const nullableBoolean = (value: unknown): value is boolean | null =>
17
+ value === null || typeof value === "boolean";
18
+ const nullableNumber = (value: unknown): value is number | null =>
19
+ value === null || (typeof value === "number" && Number.isFinite(value));
20
+ export const isAudience = (value: unknown): value is LocalShareAudience =>
21
+ audiences.some((mode) => mode === value);
22
+ function isState(value: unknown): value is LocalShareState {
23
+ return (
24
+ record(value) &&
25
+ ["not_shared", "link_off", "ready", "processing", "failed", "unavailable"].includes(
26
+ String(value.state),
27
+ ) &&
28
+ optionalFailure(value.failure) &&
29
+ nullableText(value.url) &&
30
+ text(value.manageUrl) &&
31
+ nullableBoolean(value.changed) &&
32
+ nullableAudience(value.audience) &&
33
+ nullableText(value.spaceId) &&
34
+ ["not_checked", "ready", "video_only"].includes(String(value.source))
35
+ );
36
+ }
37
+ function isSpace(value: unknown): value is LocalShareSpace {
38
+ return (
39
+ record(value) &&
40
+ text(value.id) &&
41
+ text(value.name) &&
42
+ typeof value.bound === "boolean" &&
43
+ typeof value.canCreate === "boolean" &&
44
+ Array.isArray(value.audiences) &&
45
+ value.audiences.every(isAudience) &&
46
+ nullableNumber(value.used) &&
47
+ nullableNumber(value.limit)
48
+ );
49
+ }
50
+ function isError(value: unknown): boolean {
51
+ return value === null || (record(value) && text(value.code) && text(value.message));
52
+ }
53
+
54
+ function isJob(value: unknown): value is LocalShareJob {
55
+ return (
56
+ record(value) &&
57
+ text(value.id) &&
58
+ ["running", "done", "failed"].includes(String(value.state)) &&
59
+ text(value.step) &&
60
+ (value.result === null || isState(value.result)) &&
61
+ isError(value.error)
62
+ );
63
+ }
64
+ function isSnapshot(value: unknown): value is LocalShareSnapshot {
65
+ return (
66
+ record(value) &&
67
+ typeof value.versionSharing === "boolean" &&
68
+ isState(value.share) &&
69
+ Array.isArray(value.spaces) &&
70
+ value.spaces.every(isSpace) &&
71
+ (value.job === null || isJob(value.job))
72
+ );
73
+ }
74
+ export class LocalShareError extends Error {
75
+ constructor(
76
+ public readonly code: string,
77
+ message: string,
78
+ public readonly job: LocalShareJob | null = null,
79
+ ) {
80
+ super(message);
81
+ }
82
+ }
83
+ async function json(
84
+ path: string,
85
+ token: string | null,
86
+ signal: AbortSignal,
87
+ action?: LocalShareAction,
88
+ ): Promise<unknown> {
89
+ const response = await studioApiFetch(`/api/share/${path}`, {
90
+ method: action ? "POST" : "GET",
91
+ signal: AbortSignal.any([signal, AbortSignal.timeout(30_000)]),
92
+ headers: {
93
+ ...(token ? { "X-Hyperframes-Share-CSRF": token } : {}),
94
+ ...(action ? { "Content-Type": "application/json" } : {}),
95
+ },
96
+ ...(action ? { body: JSON.stringify(action) } : {}),
97
+ });
98
+ const body: unknown = await response.json().catch(() => null);
99
+ if (!response.ok) throw responseError(body);
100
+ return body;
101
+ }
102
+ function responseError(body: unknown): LocalShareError {
103
+ const code = record(body) && text(body.code) ? body.code : "unavailable";
104
+ const message =
105
+ record(body) && text(body.message)
106
+ ? body.message
107
+ : "Sharing is unavailable. Try again from a local Studio preview.";
108
+ const job = record(body) && isJob(body.job) ? body.job : null;
109
+ return new LocalShareError(code, message, job);
110
+ }
111
+
112
+ export async function shareBootstrap(signal: AbortSignal): Promise<string> {
113
+ const value = await json("bootstrap", null, signal);
114
+ if (!record(value) || !text(value.csrf) || !/^[A-Za-z0-9_-]{43}$/.test(value.csrf))
115
+ throw new LocalShareError("unavailable", "Sharing is not available in this Studio host.");
116
+ return value.csrf;
117
+ }
118
+ export async function shareSnapshot(
119
+ token: string,
120
+ signal: AbortSignal,
121
+ ): Promise<LocalShareSnapshot> {
122
+ const value = await json("snapshot", token, signal);
123
+ if (!isSnapshot(value))
124
+ throw new LocalShareError(
125
+ "unavailable",
126
+ "Sharing returned an unreadable response. Refresh Studio.",
127
+ );
128
+ return value;
129
+ }
130
+ export async function shareAction(
131
+ token: string,
132
+ action: LocalShareAction,
133
+ signal: AbortSignal,
134
+ ): Promise<LocalShareJob> {
135
+ const value = await json("actions", token, signal, action);
136
+ if (!isJob(value))
137
+ throw new LocalShareError(
138
+ "unavailable",
139
+ "Could not start sharing. Refresh to check its status.",
140
+ );
141
+ return value;
142
+ }
143
+ export async function shareJob(
144
+ token: string,
145
+ id: string,
146
+ signal: AbortSignal,
147
+ ): Promise<LocalShareJob> {
148
+ const value = await json(`jobs/${encodeURIComponent(id)}`, token, signal);
149
+ if (!isJob(value))
150
+ throw new LocalShareError(
151
+ "unavailable",
152
+ "Could not read sharing progress. Refresh to check its status.",
153
+ );
154
+ return value;
155
+ }
156
+ export function safeShareUrl(value: string): string | null {
157
+ try {
158
+ const url = new URL(value);
159
+ return url.protocol === "https:" && !url.username && !url.password ? url.href : null;
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+
165
+ function optionalFailure(value: unknown): boolean {
166
+ return value === undefined || nullableText(value);
167
+ }
168
+
169
+ function nullableAudience(value: unknown): boolean {
170
+ return value === null || isAudience(value);
171
+ }
@@ -0,0 +1,22 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+ const policy = vi.hoisted(() => vi.fn());
3
+ vi.mock("../../telemetry/policy", () => ({ browserTelemetryAllowed: policy }));
4
+ import { shareInteraction } from "./localShareTelemetry";
5
+ afterEach(() => vi.unstubAllGlobals());
6
+ it("obeys browser opt-out before calling the guarded local bridge", async () => {
7
+ const fetch = vi.fn(async () => new Response(null, { status: 204 }));
8
+ vi.stubGlobal("fetch", fetch);
9
+ policy.mockReturnValue(false);
10
+ shareInteraction("token", { event: "share_opened", link: "none" });
11
+ expect(fetch).not.toHaveBeenCalled();
12
+ policy.mockReturnValue(true);
13
+ shareInteraction("token", { event: "share_link_copied", via: "copy" });
14
+ expect(fetch).toHaveBeenCalledWith(
15
+ "/api/share/events",
16
+ expect.objectContaining({
17
+ method: "POST",
18
+ headers: { "Content-Type": "application/json", "X-Hyperframes-Share-CSRF": "token" },
19
+ body: JSON.stringify({ event: "share_link_copied", via: "copy" }),
20
+ }),
21
+ );
22
+ });