@pioneercompany/app-sdk 0.1.3 → 0.3.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/README.md +33 -11
- package/dist/client/index.d.ts +3 -14
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +6 -19
- package/dist/client/index.js.map +1 -1
- package/dist/client/theme.d.ts +59 -0
- package/dist/client/theme.d.ts.map +1 -0
- package/dist/client/theme.js +392 -0
- package/dist/client/theme.js.map +1 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/render.d.ts +18 -0
- package/dist/server/render.d.ts.map +1 -0
- package/dist/server/render.js +83 -0
- package/dist/server/render.js.map +1 -0
- package/dist-cjs/client/index.d.ts +3 -14
- package/dist-cjs/client/index.d.ts.map +1 -1
- package/dist-cjs/client/index.js +9 -20
- package/dist-cjs/client/index.js.map +1 -1
- package/dist-cjs/client/theme.d.ts +59 -0
- package/dist-cjs/client/theme.d.ts.map +1 -0
- package/dist-cjs/client/theme.js +396 -0
- package/dist-cjs/client/theme.js.map +1 -0
- package/dist-cjs/server/index.d.ts +3 -0
- package/dist-cjs/server/index.d.ts.map +1 -1
- package/dist-cjs/server/index.js +4 -0
- package/dist-cjs/server/index.js.map +1 -1
- package/dist-cjs/server/render.d.ts +18 -0
- package/dist-cjs/server/render.d.ts.map +1 -0
- package/dist-cjs/server/render.js +86 -0
- package/dist-cjs/server/render.js.map +1 -0
- package/docs/contract.md +27 -19
- package/docs/theme.md +56 -0
- package/fixtures/protocol.json +603 -18
- package/package.json +1 -1
- package/src/client/index.ts +11 -31
- package/src/client/theme.ts +418 -0
- package/src/server/index.ts +6 -0
- package/src/server/render.ts +87 -0
package/src/client/index.ts
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
1
1
|
import { failure } from "../error.js";
|
|
2
2
|
export { PioneerError } from "../error.js";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
readonly "accent-foreground": string;
|
|
12
|
-
/** Semantic status colors; absent on older Pioneer hosts. */
|
|
13
|
-
readonly success?: string;
|
|
14
|
-
readonly warning?: string;
|
|
15
|
-
readonly danger?: string;
|
|
16
|
-
readonly info?: string;
|
|
17
|
-
}
|
|
4
|
+
import { decodeTheme, type Theme } from "./theme.js";
|
|
5
|
+
export { THEME_COLOR_KEYS, THEME_CONFIG_COLOR_KEYS } from "./theme.js";
|
|
6
|
+
export type {
|
|
7
|
+
Theme, ThemeMode, ThemeValue, ThemeObject, ThemeColorName, ThemeColors,
|
|
8
|
+
ThemeConfig, ThemeConfigColors, ThemeConfigColorName, ThemeToken,
|
|
9
|
+
} from "./theme.js";
|
|
10
|
+
|
|
18
11
|
export interface ConnectAppOptions {
|
|
19
12
|
/** Exact grant root, absolute or relative to document URL; must end in /. */
|
|
20
13
|
baseUrl?: string;
|
|
@@ -84,23 +77,10 @@ export async function connectApp(options: ConnectAppOptions = {}): Promise<AppCl
|
|
|
84
77
|
}
|
|
85
78
|
return;
|
|
86
79
|
}
|
|
87
|
-
if (!data || data.type !== "pioneer.theme"
|
|
88
|
-
const
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
for (const key of colors) {
|
|
92
|
-
if (typeof value[key] !== "string" || value[key].length > 128 || !CSS.supports("color", value[key])) return;
|
|
93
|
-
}
|
|
94
|
-
const statusColors = ["success", "warning", "danger", "info"] as const;
|
|
95
|
-
for (const key of statusColors) {
|
|
96
|
-
if (value[key] !== undefined &&
|
|
97
|
-
(typeof value[key] !== "string" || value[key].length > 128 || !CSS.supports("color", value[key]))) return;
|
|
98
|
-
}
|
|
99
|
-
current = Object.freeze({
|
|
100
|
-
appearance: value.appearance,
|
|
101
|
-
...Object.fromEntries(colors.map(key => [key, value[key]])),
|
|
102
|
-
...Object.fromEntries(statusColors.filter(key => value[key] !== undefined).map(key => [key, value[key]])),
|
|
103
|
-
}) as Readonly<Theme>;
|
|
80
|
+
if (!data || data.type !== "pioneer.theme") return;
|
|
81
|
+
const theme = decodeTheme(data.theme);
|
|
82
|
+
if (!theme) return;
|
|
83
|
+
current = theme;
|
|
104
84
|
for (const listener of [...listeners]) {
|
|
105
85
|
if (disposed) break;
|
|
106
86
|
if (!listeners.has(listener)) continue;
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/** JSON theme snapshot: native field names are retained without aliases. */
|
|
2
|
+
export type ThemeMode = "light" | "dark";
|
|
3
|
+
export type ThemeValue = null | boolean | number | string | readonly ThemeValue[] | ThemeObject;
|
|
4
|
+
export interface ThemeObject { readonly [key: string]: ThemeValue | undefined }
|
|
5
|
+
|
|
6
|
+
/** Resolved native colors, including roles absent from a user's theme file. */
|
|
7
|
+
export const THEME_COLOR_KEYS = [
|
|
8
|
+
"accent",
|
|
9
|
+
"accent_foreground",
|
|
10
|
+
"accordion",
|
|
11
|
+
"background",
|
|
12
|
+
"border",
|
|
13
|
+
"button",
|
|
14
|
+
"button_active",
|
|
15
|
+
"button_foreground",
|
|
16
|
+
"button_hover",
|
|
17
|
+
"button_danger",
|
|
18
|
+
"button_danger_active",
|
|
19
|
+
"button_danger_foreground",
|
|
20
|
+
"button_danger_hover",
|
|
21
|
+
"button_info",
|
|
22
|
+
"button_info_active",
|
|
23
|
+
"button_info_foreground",
|
|
24
|
+
"button_info_hover",
|
|
25
|
+
"button_primary",
|
|
26
|
+
"button_primary_active",
|
|
27
|
+
"button_primary_foreground",
|
|
28
|
+
"button_primary_hover",
|
|
29
|
+
"button_secondary",
|
|
30
|
+
"button_secondary_active",
|
|
31
|
+
"button_secondary_foreground",
|
|
32
|
+
"button_secondary_hover",
|
|
33
|
+
"button_success",
|
|
34
|
+
"button_success_active",
|
|
35
|
+
"button_success_foreground",
|
|
36
|
+
"button_success_hover",
|
|
37
|
+
"button_warning",
|
|
38
|
+
"button_warning_active",
|
|
39
|
+
"button_warning_foreground",
|
|
40
|
+
"button_warning_hover",
|
|
41
|
+
"group_box",
|
|
42
|
+
"group_box_foreground",
|
|
43
|
+
"caret",
|
|
44
|
+
"chart_1",
|
|
45
|
+
"chart_2",
|
|
46
|
+
"chart_3",
|
|
47
|
+
"chart_4",
|
|
48
|
+
"chart_5",
|
|
49
|
+
"chart_bullish",
|
|
50
|
+
"chart_bearish",
|
|
51
|
+
"chart_grid",
|
|
52
|
+
"danger",
|
|
53
|
+
"danger_active",
|
|
54
|
+
"danger_foreground",
|
|
55
|
+
"danger_hover",
|
|
56
|
+
"description_list_label",
|
|
57
|
+
"description_list_label_foreground",
|
|
58
|
+
"drag_border",
|
|
59
|
+
"drop_target",
|
|
60
|
+
"foreground",
|
|
61
|
+
"info",
|
|
62
|
+
"info_active",
|
|
63
|
+
"info_foreground",
|
|
64
|
+
"info_hover",
|
|
65
|
+
"input",
|
|
66
|
+
"link",
|
|
67
|
+
"link_active",
|
|
68
|
+
"link_hover",
|
|
69
|
+
"list",
|
|
70
|
+
"list_active",
|
|
71
|
+
"list_active_border",
|
|
72
|
+
"list_even",
|
|
73
|
+
"list_head",
|
|
74
|
+
"list_hover",
|
|
75
|
+
"muted",
|
|
76
|
+
"muted_foreground",
|
|
77
|
+
"popover",
|
|
78
|
+
"popover_foreground",
|
|
79
|
+
"primary",
|
|
80
|
+
"primary_active",
|
|
81
|
+
"primary_foreground",
|
|
82
|
+
"primary_hover",
|
|
83
|
+
"progress_bar",
|
|
84
|
+
"ring",
|
|
85
|
+
"scrollbar",
|
|
86
|
+
"scrollbar_thumb",
|
|
87
|
+
"scrollbar_thumb_hover",
|
|
88
|
+
"secondary",
|
|
89
|
+
"secondary_active",
|
|
90
|
+
"secondary_foreground",
|
|
91
|
+
"secondary_hover",
|
|
92
|
+
"selection",
|
|
93
|
+
"sidebar",
|
|
94
|
+
"sidebar_accent",
|
|
95
|
+
"sidebar_accent_foreground",
|
|
96
|
+
"sidebar_border",
|
|
97
|
+
"sidebar_foreground",
|
|
98
|
+
"sidebar_primary",
|
|
99
|
+
"sidebar_primary_foreground",
|
|
100
|
+
"skeleton",
|
|
101
|
+
"slider_bar",
|
|
102
|
+
"slider_thumb",
|
|
103
|
+
"success",
|
|
104
|
+
"success_foreground",
|
|
105
|
+
"success_hover",
|
|
106
|
+
"success_active",
|
|
107
|
+
"switch",
|
|
108
|
+
"switch_thumb",
|
|
109
|
+
"tab",
|
|
110
|
+
"tab_active",
|
|
111
|
+
"tab_active_foreground",
|
|
112
|
+
"tab_bar",
|
|
113
|
+
"tab_bar_segmented",
|
|
114
|
+
"tab_foreground",
|
|
115
|
+
"table",
|
|
116
|
+
"table_active",
|
|
117
|
+
"table_active_border",
|
|
118
|
+
"table_even",
|
|
119
|
+
"table_head",
|
|
120
|
+
"table_head_foreground",
|
|
121
|
+
"table_foot",
|
|
122
|
+
"table_foot_foreground",
|
|
123
|
+
"table_hover",
|
|
124
|
+
"table_row_border",
|
|
125
|
+
"title_bar",
|
|
126
|
+
"title_bar_border",
|
|
127
|
+
"status_bar",
|
|
128
|
+
"status_bar_border",
|
|
129
|
+
"warning",
|
|
130
|
+
"warning_active",
|
|
131
|
+
"warning_hover",
|
|
132
|
+
"warning_foreground",
|
|
133
|
+
"overlay",
|
|
134
|
+
"window_border",
|
|
135
|
+
"red",
|
|
136
|
+
"red_light",
|
|
137
|
+
"green",
|
|
138
|
+
"green_light",
|
|
139
|
+
"blue",
|
|
140
|
+
"blue_light",
|
|
141
|
+
"yellow",
|
|
142
|
+
"yellow_light",
|
|
143
|
+
"magenta",
|
|
144
|
+
"magenta_light",
|
|
145
|
+
"cyan",
|
|
146
|
+
"cyan_light",
|
|
147
|
+
] as const;
|
|
148
|
+
export type ThemeColorName = typeof THEME_COLOR_KEYS[number];
|
|
149
|
+
export type ThemeColors = Readonly<Record<ThemeColorName, string> & Record<string, string>>;
|
|
150
|
+
|
|
151
|
+
/** Theme-file keys from the complete GPUI ThemeConfigColors schema. */
|
|
152
|
+
export const THEME_CONFIG_COLOR_KEYS = [
|
|
153
|
+
"accent.background",
|
|
154
|
+
"accent.foreground",
|
|
155
|
+
"accordion.background",
|
|
156
|
+
"background",
|
|
157
|
+
"border",
|
|
158
|
+
"button.background",
|
|
159
|
+
"button.active.background",
|
|
160
|
+
"button.foreground",
|
|
161
|
+
"button.hover.background",
|
|
162
|
+
"button.danger.background",
|
|
163
|
+
"button.danger.active.background",
|
|
164
|
+
"button.danger.foreground",
|
|
165
|
+
"button.danger.hover.background",
|
|
166
|
+
"button.info.background",
|
|
167
|
+
"button.info.active.background",
|
|
168
|
+
"button.info.foreground",
|
|
169
|
+
"button.info.hover.background",
|
|
170
|
+
"button.primary.background",
|
|
171
|
+
"button.primary.active.background",
|
|
172
|
+
"button.primary.foreground",
|
|
173
|
+
"button.primary.hover.background",
|
|
174
|
+
"button.secondary.background",
|
|
175
|
+
"button.secondary.active.background",
|
|
176
|
+
"button.secondary.foreground",
|
|
177
|
+
"button.secondary.hover.background",
|
|
178
|
+
"button.success.background",
|
|
179
|
+
"button.success.active.background",
|
|
180
|
+
"button.success.foreground",
|
|
181
|
+
"button.success.hover.background",
|
|
182
|
+
"button.warning.background",
|
|
183
|
+
"button.warning.active.background",
|
|
184
|
+
"button.warning.foreground",
|
|
185
|
+
"button.warning.hover.background",
|
|
186
|
+
"group_box.background",
|
|
187
|
+
"group_box.foreground",
|
|
188
|
+
"group_box.title.foreground",
|
|
189
|
+
"caret",
|
|
190
|
+
"chart.1",
|
|
191
|
+
"chart.2",
|
|
192
|
+
"chart.3",
|
|
193
|
+
"chart.4",
|
|
194
|
+
"chart.5",
|
|
195
|
+
"chart.bullish",
|
|
196
|
+
"chart.bearish",
|
|
197
|
+
"chart.grid",
|
|
198
|
+
"danger.background",
|
|
199
|
+
"danger.active.background",
|
|
200
|
+
"danger.foreground",
|
|
201
|
+
"danger.hover.background",
|
|
202
|
+
"description_list.label.background",
|
|
203
|
+
"description_list.label.foreground",
|
|
204
|
+
"drag.border",
|
|
205
|
+
"drop_target.background",
|
|
206
|
+
"foreground",
|
|
207
|
+
"info.background",
|
|
208
|
+
"info.active.background",
|
|
209
|
+
"info.foreground",
|
|
210
|
+
"info.hover.background",
|
|
211
|
+
"input.border",
|
|
212
|
+
"link",
|
|
213
|
+
"link.active",
|
|
214
|
+
"link.hover",
|
|
215
|
+
"list.background",
|
|
216
|
+
"list.active.background",
|
|
217
|
+
"list.active.border",
|
|
218
|
+
"list.even.background",
|
|
219
|
+
"list.head.background",
|
|
220
|
+
"list.hover.background",
|
|
221
|
+
"muted.background",
|
|
222
|
+
"muted.foreground",
|
|
223
|
+
"popover.background",
|
|
224
|
+
"popover.foreground",
|
|
225
|
+
"primary.background",
|
|
226
|
+
"primary.active.background",
|
|
227
|
+
"primary.foreground",
|
|
228
|
+
"primary.hover.background",
|
|
229
|
+
"progress.bar.background",
|
|
230
|
+
"ring",
|
|
231
|
+
"scrollbar.background",
|
|
232
|
+
"scrollbar.thumb.background",
|
|
233
|
+
"scrollbar.thumb.hover.background",
|
|
234
|
+
"secondary.background",
|
|
235
|
+
"secondary.active.background",
|
|
236
|
+
"secondary.foreground",
|
|
237
|
+
"secondary.hover.background",
|
|
238
|
+
"selection.background",
|
|
239
|
+
"sidebar.background",
|
|
240
|
+
"sidebar.accent.background",
|
|
241
|
+
"sidebar.accent.foreground",
|
|
242
|
+
"sidebar.border",
|
|
243
|
+
"sidebar.foreground",
|
|
244
|
+
"sidebar.primary.background",
|
|
245
|
+
"sidebar.primary.foreground",
|
|
246
|
+
"skeleton.background",
|
|
247
|
+
"slider.background",
|
|
248
|
+
"slider.thumb.background",
|
|
249
|
+
"success.background",
|
|
250
|
+
"success.foreground",
|
|
251
|
+
"success.hover.background",
|
|
252
|
+
"success.active.background",
|
|
253
|
+
"switch.background",
|
|
254
|
+
"switch.thumb.background",
|
|
255
|
+
"tab.background",
|
|
256
|
+
"tab.active.background",
|
|
257
|
+
"tab.active.foreground",
|
|
258
|
+
"tab_bar.background",
|
|
259
|
+
"tab_bar.segmented.background",
|
|
260
|
+
"tab.foreground",
|
|
261
|
+
"table.background",
|
|
262
|
+
"table.active.background",
|
|
263
|
+
"table.active.border",
|
|
264
|
+
"table.even.background",
|
|
265
|
+
"table.head.background",
|
|
266
|
+
"table.head.foreground",
|
|
267
|
+
"table.foot.background",
|
|
268
|
+
"table.foot.foreground",
|
|
269
|
+
"table.hover.background",
|
|
270
|
+
"table.row.border",
|
|
271
|
+
"title_bar.background",
|
|
272
|
+
"title_bar.border",
|
|
273
|
+
"status_bar.background",
|
|
274
|
+
"status_bar.border",
|
|
275
|
+
"warning.background",
|
|
276
|
+
"warning.active.background",
|
|
277
|
+
"warning.hover.background",
|
|
278
|
+
"warning.foreground",
|
|
279
|
+
"overlay",
|
|
280
|
+
"window.border",
|
|
281
|
+
"base.blue",
|
|
282
|
+
"base.blue.light",
|
|
283
|
+
"base.cyan",
|
|
284
|
+
"base.cyan.light",
|
|
285
|
+
"base.green",
|
|
286
|
+
"base.green.light",
|
|
287
|
+
"base.magenta",
|
|
288
|
+
"base.magenta.light",
|
|
289
|
+
"base.red",
|
|
290
|
+
"base.red.light",
|
|
291
|
+
"base.yellow",
|
|
292
|
+
"base.yellow.light",
|
|
293
|
+
] as const;
|
|
294
|
+
export type ThemeConfigColorName = typeof THEME_CONFIG_COLOR_KEYS[number];
|
|
295
|
+
export type ThemeConfigColors = Readonly<Partial<Record<ThemeConfigColorName, string | null>> & ThemeObject>;
|
|
296
|
+
|
|
297
|
+
export interface ThemeConfig extends ThemeObject {
|
|
298
|
+
readonly name: string;
|
|
299
|
+
readonly mode: ThemeMode;
|
|
300
|
+
readonly is_default?: boolean;
|
|
301
|
+
readonly "font.size"?: number | null;
|
|
302
|
+
readonly "font.family"?: string | null;
|
|
303
|
+
readonly "mono_font.size"?: number | null;
|
|
304
|
+
readonly "mono_font.family"?: string | null;
|
|
305
|
+
readonly radius?: number | null;
|
|
306
|
+
readonly "radius.lg"?: number | null;
|
|
307
|
+
readonly "window.radius"?: number | null;
|
|
308
|
+
readonly shadow?: boolean | null;
|
|
309
|
+
readonly colors: ThemeConfigColors;
|
|
310
|
+
readonly highlight?: ThemeObject | null;
|
|
311
|
+
}
|
|
312
|
+
export interface ThemeToken extends ThemeObject {
|
|
313
|
+
readonly color: string;
|
|
314
|
+
readonly background: ThemeValue;
|
|
315
|
+
}
|
|
316
|
+
/** All serializable native Theme fields, plus its full semantic projection. */
|
|
317
|
+
export interface Theme extends ThemeObject {
|
|
318
|
+
readonly mode: ThemeMode;
|
|
319
|
+
readonly colors: ThemeColors;
|
|
320
|
+
readonly light_theme: ThemeConfig;
|
|
321
|
+
readonly dark_theme: ThemeConfig;
|
|
322
|
+
readonly tokens: Readonly<Record<string, ThemeToken>>;
|
|
323
|
+
readonly semantic_tokens: ThemeObject;
|
|
324
|
+
readonly highlight_theme: ThemeObject;
|
|
325
|
+
readonly font_family: string;
|
|
326
|
+
readonly font_size: number;
|
|
327
|
+
readonly mono_font_family: string;
|
|
328
|
+
readonly mono_font_size: number;
|
|
329
|
+
readonly radius: number;
|
|
330
|
+
readonly radius_lg: number;
|
|
331
|
+
readonly window_radius: number;
|
|
332
|
+
readonly shadow: boolean;
|
|
333
|
+
readonly focus_ring: boolean;
|
|
334
|
+
readonly transparent: string;
|
|
335
|
+
readonly scrollbar_mode: string;
|
|
336
|
+
readonly list: ThemeObject;
|
|
337
|
+
readonly sheet: ThemeObject;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function object(value: ThemeValue | undefined): value is ThemeObject {
|
|
341
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
342
|
+
}
|
|
343
|
+
const mode = (value: unknown): value is ThemeMode => value === "light" || value === "dark";
|
|
344
|
+
const color = (value: unknown): value is string =>
|
|
345
|
+
typeof value === "string" && value.length <= 128 && CSS.supports("color", value);
|
|
346
|
+
|
|
347
|
+
/** Clone/freeze the complete JSON tree; never project it onto a field whitelist. */
|
|
348
|
+
export function decodeTheme(value: unknown): Readonly<Theme> | null {
|
|
349
|
+
let nodes = 0;
|
|
350
|
+
let characters = 0;
|
|
351
|
+
const ancestors = new Set<object>();
|
|
352
|
+
const clone = (value: unknown, depth: number): ThemeValue => {
|
|
353
|
+
if (++nodes > 50000 || depth > 32) throw new Error("Theme exceeds limits");
|
|
354
|
+
if (value === null || typeof value === "boolean") return value;
|
|
355
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
356
|
+
if (typeof value === "string") {
|
|
357
|
+
characters += value.length;
|
|
358
|
+
if (value.length > 65536 || characters > 1024 * 1024) throw new Error("Theme exceeds limits");
|
|
359
|
+
return value;
|
|
360
|
+
}
|
|
361
|
+
if (typeof value !== "object" || ancestors.has(value)) throw new Error("Invalid theme JSON");
|
|
362
|
+
const prototype = Object.getPrototypeOf(value);
|
|
363
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null)
|
|
364
|
+
throw new Error("Invalid theme object");
|
|
365
|
+
ancestors.add(value);
|
|
366
|
+
try {
|
|
367
|
+
if (Array.isArray(value)) {
|
|
368
|
+
if (value.length > 50000 - nodes) throw new Error("Theme exceeds limits");
|
|
369
|
+
return Object.freeze(Array.from(value, item => clone(item, depth + 1)));
|
|
370
|
+
}
|
|
371
|
+
const source = Object.entries(value);
|
|
372
|
+
if (source.length > 50000 - nodes) throw new Error("Theme exceeds limits");
|
|
373
|
+
const entries = source.map(([key, item]) => {
|
|
374
|
+
characters += key.length;
|
|
375
|
+
if (key.length > 256 || characters > 1024 * 1024) throw new Error("Theme exceeds limits");
|
|
376
|
+
return [key, clone(item, depth + 1)] as const;
|
|
377
|
+
});
|
|
378
|
+
return Object.freeze(Object.fromEntries(entries));
|
|
379
|
+
} finally { ancestors.delete(value); }
|
|
380
|
+
};
|
|
381
|
+
try {
|
|
382
|
+
const theme = clone(value, 0);
|
|
383
|
+
if (!object(theme) || !mode(theme.mode)) return null;
|
|
384
|
+
const colors = theme.colors;
|
|
385
|
+
if (!object(colors) || !THEME_COLOR_KEYS.every(key => color(colors[key]))) return null;
|
|
386
|
+
if (!Object.values(colors).every(color)) return null;
|
|
387
|
+
for (const [key, expected] of [["light_theme", "light"], ["dark_theme", "dark"]] as const) {
|
|
388
|
+
const config = theme[key];
|
|
389
|
+
if (!object(config) || config.mode !== expected || typeof config.name !== "string" || !object(config.colors)) return null;
|
|
390
|
+
if (!Object.values(config.colors).every(value => value === null || typeof value === "string")) return null;
|
|
391
|
+
for (const key of ["font.size", "mono_font.size", "radius", "radius.lg", "window.radius"]) {
|
|
392
|
+
const value = config[key];
|
|
393
|
+
if (value !== undefined && value !== null && (typeof value !== "number" || value < 0)) return null;
|
|
394
|
+
}
|
|
395
|
+
for (const key of ["font.family", "mono_font.family"]) {
|
|
396
|
+
if (config[key] !== undefined && config[key] !== null && typeof config[key] !== "string") return null;
|
|
397
|
+
}
|
|
398
|
+
if (config.is_default !== undefined && typeof config.is_default !== "boolean") return null;
|
|
399
|
+
if (config.shadow !== undefined && config.shadow !== null && typeof config.shadow !== "boolean") return null;
|
|
400
|
+
if (config.highlight !== undefined && config.highlight !== null && !object(config.highlight)) return null;
|
|
401
|
+
}
|
|
402
|
+
for (const key of ["font_family", "mono_font_family", "scrollbar_mode"]) {
|
|
403
|
+
if (typeof theme[key] !== "string") return null;
|
|
404
|
+
}
|
|
405
|
+
for (const key of ["font_size", "mono_font_size", "radius", "radius_lg", "window_radius"]) {
|
|
406
|
+
const value = theme[key];
|
|
407
|
+
if (typeof value !== "number" || value < 0) return null;
|
|
408
|
+
}
|
|
409
|
+
if (typeof theme.shadow !== "boolean" || typeof theme.focus_ring !== "boolean" || !color(theme.transparent)) return null;
|
|
410
|
+
for (const key of ["tokens", "semantic_tokens", "highlight_theme", "list", "sheet"]) {
|
|
411
|
+
if (!object(theme[key])) return null;
|
|
412
|
+
}
|
|
413
|
+
for (const token of Object.values(theme.tokens as ThemeObject)) {
|
|
414
|
+
if (!object(token) || !color(token.color) || !("background" in token)) return null;
|
|
415
|
+
}
|
|
416
|
+
return theme as Theme;
|
|
417
|
+
} catch { return null; }
|
|
418
|
+
}
|
package/src/server/index.ts
CHANGED
|
@@ -2,12 +2,14 @@ import { timingSafeEqual } from "node:crypto";
|
|
|
2
2
|
import type { IncomingMessage } from "node:http";
|
|
3
3
|
import { configuration } from "./config.js";
|
|
4
4
|
import type { ServerOptions } from "./config.js";
|
|
5
|
+
import { createRender } from "./render.js";
|
|
5
6
|
import { Transport } from "./transport.js";
|
|
6
7
|
import { Pty } from "./pty.js";
|
|
7
8
|
import type { CreatePtyOptions } from "./pty.js";
|
|
8
9
|
import { failure } from "../error.js";
|
|
9
10
|
export { PioneerError } from "../error.js";
|
|
10
11
|
export type { Bootstrap, ServerOptions } from "./config.js";
|
|
12
|
+
export type { ScreenshotOptions, PdfOptions } from "./render.js";
|
|
11
13
|
export type { OperationOptions } from "./transport.js";
|
|
12
14
|
export type { Dimensions, CreatePtyOptions, ReadPtyOptions, OutputFrame, OutputSubscription, SubscribeOptions } from "./pty.js";
|
|
13
15
|
export type { PtyHead, PtySnapshot } from "./output.js";
|
|
@@ -21,6 +23,7 @@ export interface AppServerContext {
|
|
|
21
23
|
readonly appData: string;
|
|
22
24
|
readonly host: {
|
|
23
25
|
readonly capabilities: readonly string[];
|
|
26
|
+
readonly render: ReturnType<typeof createRender>;
|
|
24
27
|
readonly pty: { create(options: CreatePtyOptions): Promise<Pty> };
|
|
25
28
|
};
|
|
26
29
|
authenticateServiceRequest(headers: RequestHeaders): void;
|
|
@@ -44,6 +47,7 @@ export function nodeRequestHeaders(request: Pick<IncomingMessage, "rawHeaders">)
|
|
|
44
47
|
export async function createAppServerContext(options: ServerOptions = {}): Promise<AppServerContext> {
|
|
45
48
|
const { bootstrap, appData } = await configuration(options);
|
|
46
49
|
const expected = Buffer.from(bootstrap.ui_proxy.credential, "ascii");
|
|
50
|
+
const lifetime = new AbortController();
|
|
47
51
|
const transport = new Transport(bootstrap.host_api);
|
|
48
52
|
const capabilities = Object.freeze([...(bootstrap.host_api?.capabilities ?? [])]);
|
|
49
53
|
const owned = new Set<Pty>();
|
|
@@ -62,6 +66,7 @@ export async function createAppServerContext(options: ServerOptions = {}): Promi
|
|
|
62
66
|
appData,
|
|
63
67
|
host: Object.freeze({
|
|
64
68
|
capabilities,
|
|
69
|
+
render: createRender(bootstrap.host_api, assertOpen, lifetime.signal),
|
|
65
70
|
pty: Object.freeze({
|
|
66
71
|
async create(options: CreatePtyOptions) {
|
|
67
72
|
assertOpen();
|
|
@@ -93,6 +98,7 @@ export async function createAppServerContext(options: ServerOptions = {}): Promi
|
|
|
93
98
|
close() {
|
|
94
99
|
if (closing) return closing;
|
|
95
100
|
stopped = true;
|
|
101
|
+
lifetime.abort();
|
|
96
102
|
closing = (async () => {
|
|
97
103
|
const settled = await Promise.allSettled([...creating]);
|
|
98
104
|
const terminals = new Set([...owned, ...settled.flatMap(r => r.status === "fulfilled" ? [r.value] : [])]);
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Bootstrap } from "./config.js";
|
|
2
|
+
import { failure, hostFailure } from "../error.js";
|
|
3
|
+
|
|
4
|
+
const maxInput = 64 * 1024 * 1024;
|
|
5
|
+
const maxOutput = 32 * 1024 * 1024;
|
|
6
|
+
|
|
7
|
+
export interface ScreenshotOptions {
|
|
8
|
+
path: string;
|
|
9
|
+
input: unknown;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface PdfOptions {
|
|
14
|
+
html: string;
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createRender(
|
|
19
|
+
host: Bootstrap["host_api"],
|
|
20
|
+
assertOpen: () => void,
|
|
21
|
+
lifetime: AbortSignal,
|
|
22
|
+
) {
|
|
23
|
+
async function request(body: object, mime: string, signal?: AbortSignal) {
|
|
24
|
+
assertOpen();
|
|
25
|
+
if (!host?.capabilities.includes("host.render")) throw failure("sdk.capability_unavailable");
|
|
26
|
+
const endpoint = new URL(host.endpoint);
|
|
27
|
+
endpoint.protocol = endpoint.protocol === "wss:" ? "https:" : "http:";
|
|
28
|
+
endpoint.pathname = endpoint.pathname.replace(/\/$/, "") + "/render";
|
|
29
|
+
const payload = JSON.stringify(body);
|
|
30
|
+
if (Buffer.byteLength(payload) > maxInput) throw failure("sdk.render_input_limit");
|
|
31
|
+
const requestSignal = AbortSignal.any([
|
|
32
|
+
lifetime, AbortSignal.timeout(240000), ...(signal ? [signal] : []),
|
|
33
|
+
]);
|
|
34
|
+
try {
|
|
35
|
+
const response = await fetch(endpoint, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
redirect: "error",
|
|
38
|
+
headers: { authorization: "Bearer " + host.credential, "content-type": "application/json" },
|
|
39
|
+
body: payload,
|
|
40
|
+
signal: requestSignal,
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok || !response.body || response.headers.get("content-type") !== mime) {
|
|
43
|
+
if (response.body) await response.body.cancel();
|
|
44
|
+
if (response.status === 429) throw hostFailure("host.render_busy");
|
|
45
|
+
if (response.status === 401 || response.status === 403) throw hostFailure("host.revoked");
|
|
46
|
+
throw hostFailure("host.render_failed");
|
|
47
|
+
}
|
|
48
|
+
const reader = response.body.getReader();
|
|
49
|
+
const chunks: Uint8Array[] = [];
|
|
50
|
+
let size = 0;
|
|
51
|
+
try {
|
|
52
|
+
while (true) {
|
|
53
|
+
const next = await reader.read();
|
|
54
|
+
if (next.done) break;
|
|
55
|
+
size += next.value.byteLength;
|
|
56
|
+
if (size > maxOutput) throw failure("sdk.render_output_limit");
|
|
57
|
+
chunks.push(next.value);
|
|
58
|
+
}
|
|
59
|
+
} finally {
|
|
60
|
+
await reader.cancel().catch(() => undefined);
|
|
61
|
+
reader.releaseLock();
|
|
62
|
+
}
|
|
63
|
+
if (!size) throw failure("sdk.render_empty_output");
|
|
64
|
+
return { data: Buffer.concat(chunks, size), headers: response.headers };
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (requestSignal.aborted) throw failure("sdk.render_cancelled");
|
|
67
|
+
if (error instanceof TypeError) throw failure("sdk.render_unavailable");
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return Object.freeze({
|
|
73
|
+
async screenshot({ path, input, signal }: ScreenshotOptions) {
|
|
74
|
+
const { data, headers } = await request({ operation: "screenshot", path, input }, "image/png", signal);
|
|
75
|
+
const encoded = headers.get("pioneer-render-metadata");
|
|
76
|
+
if (!encoded) throw failure("sdk.render_invalid_metadata");
|
|
77
|
+
try {
|
|
78
|
+
return { data, metadata: JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")) as unknown };
|
|
79
|
+
} catch {
|
|
80
|
+
throw failure("sdk.render_invalid_metadata");
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
async pdf({ html, signal }: PdfOptions) {
|
|
84
|
+
return (await request({ operation: "pdf", html }, "application/pdf", signal)).data;
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|