Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

preview.ts

188 lines5.6 KB

1/**
2 * Preview - Schemas for the in-app browser preview surface.
3 *
4 * The preview is desktop-only (Chromium <webview>); the server tracks per-thread
5 * tab metadata so it survives client reconnects and multi-window. The desktop
6 * renderer mediates: it owns the actual <webview> and reports navigation back to
7 * the server via these RPCs, the server fans events to all subscribers.
8 *
9 * @module Preview
10 */
11import { Schema } from "effect";
12import { ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
13​
14const Url = TrimmedNonEmptyString.check(Schema.isMaxLength(2048));
15const Title = Schema.String.check(Schema.isMaxLength(512));
16​
17export const PreviewTabId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
18export type PreviewTabId = typeof PreviewTabId.Type;
19​
20export const PreviewNavStatus = Schema.Union([
21 Schema.TaggedStruct("Idle", {}),
22 Schema.TaggedStruct("Loading", {
23 url: Url,
24 title: Title,
25 }),
26 Schema.TaggedStruct("Success", {
27 url: Url,
28 title: Title,
29 }),
30 Schema.TaggedStruct("LoadFailed", {
31 url: Url,
32 title: Title,
33 code: Schema.Int,
34 description: Schema.String,
35 }),
36]);
37export type PreviewNavStatus = typeof PreviewNavStatus.Type;
38​
39export const PreviewSessionSnapshot = Schema.Struct({
40 threadId: TrimmedNonEmptyString,
41 tabId: PreviewTabId,
42 navStatus: PreviewNavStatus,
43 canGoBack: Schema.Boolean,
44 canGoForward: Schema.Boolean,
45 updatedAt: Schema.String,
46});
47export type PreviewSessionSnapshot = typeof PreviewSessionSnapshot.Type;
48​
49export const PreviewOpenInput = Schema.Struct({
50 threadId: ThreadId,
51 /** Omit to create an empty (Idle) tab the user can type into. */
52 url: Schema.optional(Url),
53});
54export type PreviewOpenInput = typeof PreviewOpenInput.Type;
55​
56export const PreviewNavigateInput = Schema.Struct({
57 threadId: ThreadId,
58 tabId: PreviewTabId,
59 url: Url,
60 resolvedTitle: Schema.optional(Title),
61});
62export type PreviewNavigateInput = typeof PreviewNavigateInput.Type;
63​
64export const PreviewReportStatusInput = Schema.Struct({
65 threadId: ThreadId,
66 tabId: PreviewTabId,
67 navStatus: PreviewNavStatus,
68 canGoBack: Schema.Boolean,
69 canGoForward: Schema.Boolean,
70});
71export type PreviewReportStatusInput = typeof PreviewReportStatusInput.Type;
72​
73export const PreviewRefreshInput = Schema.Struct({
74 threadId: ThreadId,
75 tabId: PreviewTabId,
76});
77export type PreviewRefreshInput = typeof PreviewRefreshInput.Type;
78​
79export const PreviewCloseInput = Schema.Struct({
80 threadId: ThreadId,
81 tabId: Schema.optional(PreviewTabId),
82});
83export type PreviewCloseInput = typeof PreviewCloseInput.Type;
84​
85export const PreviewListInput = Schema.Struct({
86 threadId: ThreadId,
87});
88export type PreviewListInput = typeof PreviewListInput.Type;
89​
90export const PreviewListResult = Schema.Struct({
91 sessions: Schema.Array(PreviewSessionSnapshot),
92});
93export type PreviewListResult = typeof PreviewListResult.Type;
94​
95const PreviewEventBaseSchema = Schema.Struct({
96 threadId: TrimmedNonEmptyString,
97 tabId: PreviewTabId,
98 createdAt: Schema.String,
99});
100​
101const PreviewOpenedEvent = Schema.Struct({
102 ...PreviewEventBaseSchema.fields,
103 type: Schema.Literal("opened"),
104 snapshot: PreviewSessionSnapshot,
105});
106​
107const PreviewNavigatedEvent = Schema.Struct({
108 ...PreviewEventBaseSchema.fields,
109 type: Schema.Literal("navigated"),
110 snapshot: PreviewSessionSnapshot,
111});
112​
113const PreviewFailedEvent = Schema.Struct({
114 ...PreviewEventBaseSchema.fields,
115 type: Schema.Literal("failed"),
116 url: Url,
117 title: Title,
118 code: Schema.Int,
119 description: Schema.String,
120});
121​
122const PreviewClosedEvent = Schema.Struct({
123 ...PreviewEventBaseSchema.fields,
124 type: Schema.Literal("closed"),
125});
126​
127export const PreviewEvent = Schema.Union([
128 PreviewOpenedEvent,
129 PreviewNavigatedEvent,
130 PreviewFailedEvent,
131 PreviewClosedEvent,
132]);
133export type PreviewEvent = typeof PreviewEvent.Type;
134​
135/**
136 * A localhost server detected by the port scanner. Used to populate the
137 * "Local" recommendations in the empty-state of the preview panel.
138 */
139export const DiscoveredLocalServer = Schema.Struct({
140 host: TrimmedNonEmptyString,
141 port: Schema.Int.check(Schema.isGreaterThan(0)).check(Schema.isLessThan(65536)),
142 url: Url,
143 processName: Schema.NullOr(TrimmedNonEmptyString),
144 pid: Schema.NullOr(Schema.Int.check(Schema.isGreaterThan(0))),
145 terminal: Schema.NullOr(
146 Schema.Struct({
147 threadId: ThreadId,
148 terminalId: TrimmedNonEmptyString,
149 }),
150 ),
151});
152export type DiscoveredLocalServer = typeof DiscoveredLocalServer.Type;
153​
154export const DiscoveredLocalServerList = Schema.Struct({
155 servers: Schema.Array(DiscoveredLocalServer),
156 scannedAt: Schema.String,
157});
158export type DiscoveredLocalServerList = typeof DiscoveredLocalServerList.Type;
159​
160export class PreviewSessionLookupError extends Schema.TaggedErrorClass<PreviewSessionLookupError>()(
161 "PreviewSessionLookupError",
162 {
163 threadId: Schema.String,
164 tabId: Schema.String,
165 },
166) {
167 override get message() {
168 return `Unknown preview session: thread=${this.threadId}, tab=${this.tabId}`;
169 }
171​
172export class PreviewInvalidUrlError extends Schema.TaggedErrorClass<PreviewInvalidUrlError>()(
173 "PreviewInvalidUrlError",
174 {
175 inputLength: Schema.Number,
176 reason: Schema.Literals(["empty", "parse", "unsupported-protocol", "unexpected"]),
177 protocol: Schema.optional(Schema.String),
178 cause: Schema.Defect(),
179 },
180) {
181 override get message() {
182 const protocol = this.protocol === undefined ? "" : `: ${this.protocol}`;
183 return `Invalid preview URL (${this.reason}${protocol}; input length ${this.inputLength}).`;
184 }
186​
187export const PreviewError = Schema.Union([PreviewSessionLookupError, PreviewInvalidUrlError]);
188export type PreviewError = typeof PreviewError.Type;