@liberation-data/desk 0.42.0 → 0.44.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.
Files changed (200) hide show
  1. package/README.md +312 -5
  2. package/dist/core/hosted.d.ts +222 -0
  3. package/dist/core/hosted.d.ts.map +1 -0
  4. package/dist/core/hosted.js +166 -0
  5. package/dist/core/hosted.js.map +1 -0
  6. package/dist/core/index.d.ts +10 -0
  7. package/dist/core/index.d.ts.map +1 -1
  8. package/dist/core/index.js +5 -0
  9. package/dist/core/index.js.map +1 -1
  10. package/dist/core/location.d.ts +8 -1
  11. package/dist/core/location.d.ts.map +1 -1
  12. package/dist/core/location.js +18 -2
  13. package/dist/core/location.js.map +1 -1
  14. package/dist/core/preferences.d.ts +27 -0
  15. package/dist/core/preferences.d.ts.map +1 -0
  16. package/dist/core/preferences.js +121 -0
  17. package/dist/core/preferences.js.map +1 -0
  18. package/dist/core/scheme.d.ts +18 -0
  19. package/dist/core/scheme.d.ts.map +1 -0
  20. package/dist/core/scheme.js +24 -0
  21. package/dist/core/scheme.js.map +1 -0
  22. package/dist/core/theme.d.ts +41 -0
  23. package/dist/core/theme.d.ts.map +1 -0
  24. package/dist/core/theme.js +56 -0
  25. package/dist/core/theme.js.map +1 -0
  26. package/dist/core/themeChange.d.ts +25 -0
  27. package/dist/core/themeChange.d.ts.map +1 -0
  28. package/dist/core/themeChange.js +49 -0
  29. package/dist/core/themeChange.js.map +1 -0
  30. package/dist/core/types.d.ts +6 -0
  31. package/dist/core/types.d.ts.map +1 -1
  32. package/dist/css/apps.css +183 -0
  33. package/dist/css/overlays.css +6 -0
  34. package/dist/css/shell.css +100 -0
  35. package/dist/css/tokens.css +10 -0
  36. package/dist/css/windows.css +21 -0
  37. package/dist/desk.css +220 -0
  38. package/dist/fx/fx.d.ts.map +1 -1
  39. package/dist/fx/fx.js +7 -31
  40. package/dist/fx/fx.js.map +1 -1
  41. package/dist/host/checkChrome.d.ts +20 -0
  42. package/dist/host/checkChrome.d.ts.map +1 -0
  43. package/dist/host/checkChrome.js +219 -0
  44. package/dist/host/checkChrome.js.map +1 -0
  45. package/dist/host/checkTheme.d.ts +35 -0
  46. package/dist/host/checkTheme.d.ts.map +1 -0
  47. package/dist/host/checkTheme.js +128 -0
  48. package/dist/host/checkTheme.js.map +1 -0
  49. package/dist/host/index.d.ts +7 -0
  50. package/dist/host/index.d.ts.map +1 -0
  51. package/dist/host/index.js +8 -0
  52. package/dist/host/index.js.map +1 -0
  53. package/dist/host/router.d.ts +99 -0
  54. package/dist/host/router.d.ts.map +1 -0
  55. package/dist/host/router.js +279 -0
  56. package/dist/host/router.js.map +1 -0
  57. package/dist/react/Desktop.d.ts.map +1 -1
  58. package/dist/react/Desktop.js +13 -2
  59. package/dist/react/Desktop.js.map +1 -1
  60. package/dist/react/Dock.d.ts +26 -2
  61. package/dist/react/Dock.d.ts.map +1 -1
  62. package/dist/react/Dock.js +23 -27
  63. package/dist/react/Dock.js.map +1 -1
  64. package/dist/react/MenuBar.d.ts +1 -1
  65. package/dist/react/MenuBar.d.ts.map +1 -1
  66. package/dist/react/MenuBar.js +36 -6
  67. package/dist/react/MenuBar.js.map +1 -1
  68. package/dist/react/app.d.ts +215 -0
  69. package/dist/react/app.d.ts.map +1 -0
  70. package/dist/react/app.js +192 -0
  71. package/dist/react/app.js.map +1 -0
  72. package/dist/react/appFrame.d.ts +11 -7
  73. package/dist/react/appFrame.d.ts.map +1 -1
  74. package/dist/react/appFrame.js +24 -6
  75. package/dist/react/appFrame.js.map +1 -1
  76. package/dist/react/appPanels.d.ts +25 -0
  77. package/dist/react/appPanels.d.ts.map +1 -0
  78. package/dist/react/appPanels.js +123 -0
  79. package/dist/react/appPanels.js.map +1 -0
  80. package/dist/react/appearance.d.ts +61 -0
  81. package/dist/react/appearance.d.ts.map +1 -0
  82. package/dist/react/appearance.js +119 -0
  83. package/dist/react/appearance.js.map +1 -0
  84. package/dist/react/codeBlock.d.ts.map +1 -1
  85. package/dist/react/codeBlock.js +6 -60
  86. package/dist/react/codeBlock.js.map +1 -1
  87. package/dist/react/context.d.ts +23 -1
  88. package/dist/react/context.d.ts.map +1 -1
  89. package/dist/react/context.js +108 -5
  90. package/dist/react/context.js.map +1 -1
  91. package/dist/react/copy.d.ts +22 -0
  92. package/dist/react/copy.d.ts.map +1 -0
  93. package/dist/react/copy.js +68 -0
  94. package/dist/react/copy.js.map +1 -0
  95. package/dist/react/dockStatus.d.ts +19 -0
  96. package/dist/react/dockStatus.d.ts.map +1 -0
  97. package/dist/react/dockStatus.js +25 -0
  98. package/dist/react/dockStatus.js.map +1 -0
  99. package/dist/react/documentTitle.d.ts.map +1 -1
  100. package/dist/react/documentTitle.js +4 -1
  101. package/dist/react/documentTitle.js.map +1 -1
  102. package/dist/react/frameMenus.d.ts +28 -0
  103. package/dist/react/frameMenus.d.ts.map +1 -0
  104. package/dist/react/frameMenus.js +85 -0
  105. package/dist/react/frameMenus.js.map +1 -0
  106. package/dist/react/hostApp.d.ts +15 -0
  107. package/dist/react/hostApp.d.ts.map +1 -0
  108. package/dist/react/hostApp.js +77 -0
  109. package/dist/react/hostApp.js.map +1 -0
  110. package/dist/react/hostDock.d.ts +25 -0
  111. package/dist/react/hostDock.d.ts.map +1 -0
  112. package/dist/react/hostDock.js +98 -0
  113. package/dist/react/hostDock.js.map +1 -0
  114. package/dist/react/hostMenu.d.ts +2 -36
  115. package/dist/react/hostMenu.d.ts.map +1 -1
  116. package/dist/react/hostMenu.js.map +1 -1
  117. package/dist/react/hosted.d.ts +28 -0
  118. package/dist/react/hosted.d.ts.map +1 -0
  119. package/dist/react/hosted.js +53 -0
  120. package/dist/react/hosted.js.map +1 -0
  121. package/dist/react/index.d.ts +12 -0
  122. package/dist/react/index.d.ts.map +1 -1
  123. package/dist/react/index.js +8 -0
  124. package/dist/react/index.js.map +1 -1
  125. package/dist/react/layers.d.ts.map +1 -1
  126. package/dist/react/layers.js +7 -2
  127. package/dist/react/layers.js.map +1 -1
  128. package/dist/react/overlays.d.ts.map +1 -1
  129. package/dist/react/overlays.js +29 -12
  130. package/dist/react/overlays.js.map +1 -1
  131. package/dist/react/shell.d.ts +16 -1
  132. package/dist/react/shell.d.ts.map +1 -1
  133. package/dist/react/shell.js +19 -3
  134. package/dist/react/shell.js.map +1 -1
  135. package/dist/react/windowMenus.d.ts +19 -0
  136. package/dist/react/windowMenus.d.ts.map +1 -0
  137. package/dist/react/windowMenus.js +88 -0
  138. package/dist/react/windowMenus.js.map +1 -0
  139. package/dist/shell/HostShell.d.ts +53 -0
  140. package/dist/shell/HostShell.d.ts.map +1 -0
  141. package/dist/shell/HostShell.js +116 -0
  142. package/dist/shell/HostShell.js.map +1 -0
  143. package/dist/shell/index.d.ts +7 -0
  144. package/dist/shell/index.d.ts.map +1 -0
  145. package/dist/shell/index.js +9 -0
  146. package/dist/shell/index.js.map +1 -0
  147. package/dist/shell/keys.d.ts +47 -0
  148. package/dist/shell/keys.d.ts.map +1 -0
  149. package/dist/shell/keys.js +68 -0
  150. package/dist/shell/keys.js.map +1 -0
  151. package/dist/shell/motion.d.ts +72 -0
  152. package/dist/shell/motion.d.ts.map +1 -0
  153. package/dist/shell/motion.js +118 -0
  154. package/dist/shell/motion.js.map +1 -0
  155. package/llms.txt +84 -9
  156. package/package.json +10 -1
  157. package/src/core/hosted.ts +351 -0
  158. package/src/core/index.ts +29 -0
  159. package/src/core/location.ts +23 -3
  160. package/src/core/preferences.ts +132 -0
  161. package/src/core/scheme.ts +32 -0
  162. package/src/core/theme.ts +91 -0
  163. package/src/core/themeChange.ts +59 -0
  164. package/src/core/types.ts +6 -0
  165. package/src/css/apps.css +183 -0
  166. package/src/css/overlays.css +6 -0
  167. package/src/css/shell.css +99 -0
  168. package/src/css/tokens.css +10 -0
  169. package/src/css/windows.css +21 -0
  170. package/src/fx/fx.ts +7 -25
  171. package/src/host/checkChrome.ts +257 -0
  172. package/src/host/checkTheme.ts +134 -0
  173. package/src/host/index.ts +10 -0
  174. package/src/host/router.ts +374 -0
  175. package/src/react/Desktop.tsx +21 -3
  176. package/src/react/Dock.tsx +40 -24
  177. package/src/react/MenuBar.tsx +39 -6
  178. package/src/react/app.tsx +354 -0
  179. package/src/react/appFrame.tsx +39 -14
  180. package/src/react/appPanels.tsx +210 -0
  181. package/src/react/appearance.tsx +195 -0
  182. package/src/react/codeBlock.tsx +7 -65
  183. package/src/react/context.tsx +131 -4
  184. package/src/react/copy.ts +81 -0
  185. package/src/react/dockStatus.ts +42 -0
  186. package/src/react/documentTitle.ts +4 -1
  187. package/src/react/frameMenus.ts +100 -0
  188. package/src/react/hostApp.tsx +89 -0
  189. package/src/react/hostDock.tsx +118 -0
  190. package/src/react/hostMenu.ts +3 -38
  191. package/src/react/hosted.tsx +65 -0
  192. package/src/react/index.ts +34 -0
  193. package/src/react/layers.ts +7 -2
  194. package/src/react/overlays.tsx +30 -12
  195. package/src/react/shell.tsx +33 -3
  196. package/src/react/windowMenus.ts +107 -0
  197. package/src/shell/HostShell.tsx +220 -0
  198. package/src/shell/index.ts +11 -0
  199. package/src/shell/keys.ts +101 -0
  200. package/src/shell/motion.ts +163 -0
@@ -0,0 +1,257 @@
1
+ import type { DeskChrome, HostAbout, HostAboutFact, HostDockEntry, HostDockItem, HostMenu, HostMenuItem, HostSettingsSection } from '../core/hosted.js'
2
+ import { fromOriginOrData, IMAGE_DATA, onOrigin } from './checkTheme.js'
3
+
4
+ /*
5
+ * CHECKING CHROME BEFORE THE HOST DRAWS IT.
6
+ *
7
+ * An app's menus, dock, Settings sections and About are drawn in the host's own frame, so they are
8
+ * input the host must not trust. A link there could run script in that frame (`javascript:`), an image
9
+ * could make it fetch from anywhere, and a label could be a megabyte or a list a million long. So the
10
+ * host checks the chrome first and draws only a copy built from what passed, as it does a theme.
11
+ *
12
+ * One bad part costs only itself: a menu item, dock item, section, link or fact that fails is dropped,
13
+ * and an image or a fact's link that fails is dropped from its item, which is kept. The rest is drawn,
14
+ * and what was dropped is reported for the host to log. Only chrome of the wrong shape is refused
15
+ * whole: not an object, top-level fields that are not lists, too large, or nested deeper than the
16
+ * type goes (a stack in a stack, a menu in a menu).
17
+ */
18
+
19
+ /** Chrome fit to draw, as a fresh copy, with why each dropped part was; or why it is refused whole. */
20
+ export type ChromeCheck =
21
+ | { readonly ok: true; readonly chrome: DeskChrome; readonly dropped: readonly string[] }
22
+ | { readonly ok: false; readonly reason: string }
23
+
24
+ /** The most the whole chrome may take as JSON: room for a dock of a few dozen `data:` icons. */
25
+ const MAX_CHROME = 2 * 1024 * 1024
26
+ const MAX_TEXT = 1000
27
+ const MAX_URL = 2048
28
+ /** One `data:` icon. */
29
+ const MAX_IMAGE = 256 * 1024
30
+ const MAX_MENUS = 50
31
+ const MAX_ROWS = 500
32
+ const MAX_SECTIONS = 200
33
+ const MAX_LINKS = 50
34
+
35
+ /** Thrown for a part that fails: the part is dropped, for this reason. */
36
+ class Refused extends Error {}
37
+ /** Thrown for chrome of the wrong shape: the whole message is refused. */
38
+ class Malformed extends Error {}
39
+ const refuse = (reason: string): never => {
40
+ throw new Refused(reason)
41
+ }
42
+
43
+ type Fields = Record<string, unknown>
44
+ /** One check's origin, and the reasons for what it dropped. */
45
+ interface Check {
46
+ readonly origin: string
47
+ readonly dropped: string[]
48
+ }
49
+
50
+ const record = (value: unknown, what: string): Fields =>
51
+ typeof value === 'object' && value !== null && !Array.isArray(value) ? (value as Fields) : refuse(`${what} is not an object`)
52
+
53
+ /** `make()`, or `otherwise` with the reason noted when the part fails. */
54
+ const orDrop = <T,>(c: Check, make: () => T, otherwise: T): T => {
55
+ try {
56
+ return make()
57
+ } catch (error) {
58
+ if (!(error instanceof Refused)) throw error
59
+ c.dropped.push(error.message)
60
+ return otherwise
61
+ }
62
+ }
63
+
64
+ /** The items of a list that pass `check`; the rest, and any past the first `max`, are dropped. */
65
+ const list = <T,>(c: Check, value: unknown, max: number, what: string, check: (value: unknown) => T): T[] => {
66
+ if (!Array.isArray(value)) return refuse(`${what} is not a list`)
67
+ if (value.length > max) c.dropped.push(`${what}: ${value.length - max} past the first ${max}`)
68
+ return value.slice(0, max).flatMap(item => orDrop(c, () => [check(item)], []))
69
+ }
70
+
71
+ /** Text a host draws: a string, not too long, with no control or direction-overriding characters. */
72
+ const text = (value: unknown, what: string): string =>
73
+ // eslint-disable-next-line no-control-regex
74
+ typeof value === 'string' && value.length <= MAX_TEXT && !/[\u0000-\u0008\u000b-\u001f\u007f‪-‮⁦-⁩]/.test(value)
75
+ ? value
76
+ : refuse(`${what} is not plain text`)
77
+
78
+ /** The field when present, checked by `check`; nothing when absent. */
79
+ const optional = <T,>(fields: Fields, key: string, check: (value: unknown, what: string) => T): { [k: string]: T } =>
80
+ fields[key] === undefined ? {} : { [key]: check(fields[key], key) }
81
+
82
+ const flag = (value: unknown, what: string): boolean => (typeof value === 'boolean' ? value : refuse(`${what} is not true or false`))
83
+
84
+ /**
85
+ * A link a host may open, in the system browser or a mail app: http(s) or `mailto:` anywhere, or the
86
+ * app's own origin. Resolved, so a relative one points at the app and not at the host.
87
+ */
88
+ const link = (value: unknown, what: string, origin: string): string => {
89
+ if (typeof value !== 'string' || value.length > MAX_URL) return refuse(`${what} is not a link`)
90
+ try {
91
+ const url = new URL(value, origin)
92
+ if (url.protocol === 'http:' || url.protocol === 'https:' || url.protocol === 'mailto:' || onOrigin(url, origin)) return url.href
93
+ } catch {
94
+ // Refused below.
95
+ }
96
+ return refuse(`${what} is not an http(s) or mailto: link`)
97
+ }
98
+
99
+ /** An image the host may load, dropped from its item when it fails: from the app's origin, or a `data:` raster image. Never SVG, which can carry script. */
100
+ const image = (c: Check, fields: Fields, what: string): { image?: string } =>
101
+ orDrop(
102
+ c,
103
+ () =>
104
+ optional(fields, 'image', value =>
105
+ (typeof value === 'string' && value.length <= MAX_IMAGE && fromOriginOrData(value, c.origin, IMAGE_DATA)) || refuse(`${what}'s image is not from the app`),
106
+ ),
107
+ {},
108
+ )
109
+
110
+ const menuItem = (value: unknown): HostMenuItem => {
111
+ const item = record(value, 'a menu item')
112
+ if (item.items !== undefined) throw new Malformed('a menu in a menu')
113
+ if (item.type !== 'item' && item.type !== 'separator' && item.type !== 'header') return refuse('a menu item has no type')
114
+ return {
115
+ type: item.type,
116
+ ...optional(item, 'id', text),
117
+ ...optional(item, 'label', text),
118
+ ...optional(item, 'detail', text),
119
+ ...optional(item, 'shortcut', text),
120
+ ...optional(item, 'showOnly', flag),
121
+ ...optional(item, 'enabled', flag),
122
+ ...optional(item, 'checked', flag),
123
+ }
124
+ }
125
+
126
+ const menu = (c: Check, value: unknown): HostMenu => {
127
+ const m = record(value, 'a menu')
128
+ return {
129
+ id: text(m.id, 'a menu id'),
130
+ label: text(m.label, 'a menu label'),
131
+ ...optional(m, 'primary', flag),
132
+ ...optional(m, 'status', flag),
133
+ items: list(c, m.items, MAX_ROWS, 'a menu', menuItem),
134
+ }
135
+ }
136
+
137
+ const dockItem = (c: Check, value: unknown): HostDockItem => {
138
+ const item = record(value, 'a dock item')
139
+ if (item.type === 'stack') throw new Malformed('a stack in a stack')
140
+ if (item.type !== 'item') return refuse('a dock item has no type')
141
+ return {
142
+ type: 'item',
143
+ id: text(item.id, 'a dock id'),
144
+ label: text(item.label, 'a dock label'),
145
+ ...optional(item, 'description', text),
146
+ ...image(c, item, 'a dock item'),
147
+ ...optional(item, 'badge', text),
148
+ ...optional(item, 'disabled', flag),
149
+ running: flag(item.running, 'running'),
150
+ focused: flag(item.focused, 'focused'),
151
+ minimized: flag(item.minimized, 'minimized'),
152
+ }
153
+ }
154
+
155
+ const dockEntry = (c: Check, value: unknown): HostDockEntry => {
156
+ const entry = record(value, 'a dock entry')
157
+ if (entry.type === 'separator') return { type: 'separator', id: text(entry.id, 'a dock id') }
158
+ if (entry.type !== 'stack') return dockItem(c, entry)
159
+ return {
160
+ type: 'stack',
161
+ id: text(entry.id, 'a dock id'),
162
+ label: text(entry.label, 'a dock label'),
163
+ ...image(c, entry, 'a stack'),
164
+ items: list(c, entry.items, MAX_ROWS, 'a stack', item => dockItem(c, item)),
165
+ }
166
+ }
167
+
168
+ const settingsSection = (value: unknown): HostSettingsSection => {
169
+ const section = record(value, 'a settings section')
170
+ return {
171
+ id: text(section.id, 'a section id'),
172
+ title: text(section.title, 'a section title'),
173
+ ...optional(section, 'group', text),
174
+ ...optional(section, 'badge', text),
175
+ }
176
+ }
177
+
178
+ const labelledLink = (c: Check, value: unknown) => {
179
+ const l = record(value, 'a link')
180
+ return { label: text(l.label, 'a link label'), url: link(l.url, 'a link', c.origin) }
181
+ }
182
+
183
+ const fact = (c: Check, value: unknown): HostAboutFact => {
184
+ const f = record(value, 'a fact')
185
+ if (f.copy !== undefined && f.copy !== false) return refuse('a fact has a bad copy')
186
+ return {
187
+ name: text(f.name, 'a fact name'),
188
+ value: text(f.value, 'a fact value'),
189
+ ...(f.link === undefined ? {} : orDrop<{ link: NonNullable<HostAboutFact['link']> } | Record<string, never>>(c, () => ({ link: labelledLink(c, f.link) }), {})),
190
+ ...(f.copy === false ? { copy: false as const } : {}),
191
+ }
192
+ }
193
+
194
+ const about = (c: Check, value: unknown): HostAbout | null => {
195
+ if (value === null) return null
196
+ if (typeof value !== 'object' || Array.isArray(value)) throw new Malformed('about is not an object')
197
+ const a = value as Fields
198
+ return orDrop(
199
+ c,
200
+ () => ({
201
+ name: text(a.name, 'the name'),
202
+ ...optional(a, 'version', text),
203
+ links: list(c, a.links, MAX_LINKS, 'links', l => labelledLink(c, l)),
204
+ ...optional(a, 'note', text),
205
+ facts:
206
+ a.facts === null
207
+ ? null
208
+ : list(c, a.facts, MAX_SECTIONS, 'facts', s => {
209
+ const section = record(s, 'a section of facts')
210
+ return { ...optional(section, 'title', text), facts: list(c, section.facts, MAX_ROWS, 'a section of facts', f => fact(c, f)) }
211
+ }),
212
+ }),
213
+ null,
214
+ )
215
+ }
216
+
217
+ /** A top-level list: refused whole when it is not one. */
218
+ const topList = <T,>(c: Check, value: unknown, max: number, what: string, check: (value: unknown) => T): T[] => {
219
+ if (!Array.isArray(value)) throw new Malformed(`${what} is not a list`)
220
+ return list(c, value, max, what, check)
221
+ }
222
+
223
+ /**
224
+ * Checks the chrome an app at `origin` sent before the host draws it, and returns a copy with only the
225
+ * fields a `DeskChrome` has. Text must be strings of bounded length, lists bounded too, links http(s),
226
+ * `mailto:` or the app's own, and images from `origin` or `data:` PNG, JPEG, WebP, AVIF or GIF.
227
+ * Relative links and images come back resolved against `origin`, since the host's frame is elsewhere.
228
+ * A part that fails is dropped and the rest kept, with a short reason in `dropped` for each; chrome
229
+ * of the wrong shape (see above) is refused whole.
230
+ */
231
+ export function checkChrome(value: unknown, origin: string): ChromeCheck {
232
+ let size: number
233
+ try {
234
+ size = JSON.stringify(value ?? null).length
235
+ } catch {
236
+ return { ok: false, reason: 'not data' }
237
+ }
238
+ if (size > MAX_CHROME) return { ok: false, reason: 'too large' }
239
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return { ok: false, reason: 'chrome is not an object' }
240
+ const chrome = value as Fields
241
+ const c: Check = { origin, dropped: [] }
242
+ try {
243
+ return {
244
+ ok: true,
245
+ chrome: {
246
+ menus: topList(c, chrome.menus, MAX_MENUS, 'menus', m => menu(c, m)),
247
+ dock: topList(c, chrome.dock, MAX_ROWS, 'the dock', entry => dockEntry(c, entry)),
248
+ settings: topList(c, chrome.settings, MAX_SECTIONS, 'settings', settingsSection),
249
+ about: about(c, chrome.about),
250
+ },
251
+ dropped: c.dropped,
252
+ }
253
+ } catch (error) {
254
+ if (error instanceof Malformed) return { ok: false, reason: error.message }
255
+ throw error
256
+ }
257
+ }
@@ -0,0 +1,134 @@
1
+ import { isImageRef } from '../core/theme.js'
2
+ import type { DeskTheme, ThemeFont } from '../core/theme.js'
3
+ import type { Look } from '../core/look.js'
4
+
5
+ /*
6
+ * CHECKING A THEME BEFORE IT RESTYLES SOMEBODY ELSE'S PAGE.
7
+ *
8
+ * A host passes the primary's theme to every other app, and each sets its tokens on its own root. A
9
+ * value that could load something — `url(…)`, `image-set(…)`, a font from elsewhere — would let one
10
+ * app make the others fetch from wherever it liked, or lay an image over part of them. So the host
11
+ * checks the theme first, and passes on only a copy built from what passed.
12
+ */
13
+
14
+ /** A theme fit to pass on, as a fresh copy, or why it is not. */
15
+ export type ThemeCheck = { readonly ok: true; readonly theme: DeskTheme } | { readonly ok: false; readonly reason: string }
16
+
17
+ const LOOKS: readonly string[] = ['mac', 'gnome', 'windows']
18
+
19
+ /** The CSS functions a token may use: colours, maths, gradients and other custom properties. Nothing that loads. */
20
+ const FUNCTIONS = new Set([
21
+ 'rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'lab', 'lch', 'oklab', 'oklch', 'color', 'color-mix', 'light-dark',
22
+ 'var', 'calc', 'min', 'max', 'clamp',
23
+ 'linear-gradient', 'radial-gradient', 'conic-gradient', 'repeating-linear-gradient', 'repeating-radial-gradient',
24
+ ])
25
+
26
+ const MAX_VALUE = 300
27
+ const MAX_TOKENS = 400
28
+ const MAX_FONTS = 16
29
+
30
+ /**
31
+ * Whether a custom property's value is plain: a colour, a length, a number, a font list or a gradient.
32
+ * Only letters, digits, spaces and `#.,%/'"+-()*` may appear, every function is one of `FUNCTIONS`, and
33
+ * nothing may escape, end the declaration or open a block. So `url(…)`, `image-set(…)`, `\75rl(` and
34
+ * `red; background: …` are all refused.
35
+ */
36
+ export function isPlainValue(value: string): boolean {
37
+ if (value.length > MAX_VALUE) return false
38
+ if (!/^[\w\s#.,%/'"+*()-]*$/.test(value)) return false
39
+ for (const match of value.matchAll(/([\w-]*)\s*\(/g)) {
40
+ if (!FUNCTIONS.has((match[1] ?? '').toLowerCase())) return false
41
+ }
42
+ return true
43
+ }
44
+
45
+ /**
46
+ * Whether `url` is on `origin`: the same scheme and host. Compared by hand rather than by `URL.origin`,
47
+ * which is `'null'` for every scheme a browser does not know (an Electron app's `app://` among them,
48
+ * and `javascript:` too), so it would take any of them for any other. A URL with no host is on none.
49
+ */
50
+ export function onOrigin(url: URL, origin: string): boolean {
51
+ const own = new URL(origin)
52
+ return url.host !== '' && url.protocol === own.protocol && url.host === own.host
53
+ }
54
+
55
+ /**
56
+ * `src` as a URL safe to put in CSS, when it is a `data:` URI of one of `kinds` or a URL on `origin`;
57
+ * null otherwise. A URL comes back resolved and percent-encoded, so it holds no quote.
58
+ */
59
+ export function fromOriginOrData(src: string, origin: string, kinds: RegExp): string | null {
60
+ if (src.startsWith('data:')) return kinds.test(src.slice(5)) && !/["\\\s]/.test(src) ? src : null
61
+ try {
62
+ const url = new URL(src, origin)
63
+ return onOrigin(url, origin) ? url.href : null
64
+ } catch {
65
+ return null
66
+ }
67
+ }
68
+
69
+ const FONT_DATA = /^(font\/|application\/(font|x-font|octet-stream))/i
70
+ export const IMAGE_DATA = /^image\/(png|jpeg|webp|avif|gif)[;,]/i
71
+ /** A font family as CSS names one: words, spaces and hyphens, nothing to quote or escape. */
72
+ const FAMILY = /^[\w -]{1,80}$/
73
+
74
+ /**
75
+ * Checks a theme published by `origin` before it is passed to other pages, and returns a copy with
76
+ * only the fields a `DeskTheme` has. Tokens must be custom properties with plain values
77
+ * (`isPlainValue`); fonts and an image wallpaper must come from `origin` or be `data:` URIs.
78
+ */
79
+ export function checkTheme(value: unknown, origin: string): ThemeCheck {
80
+ const fail = (reason: string): ThemeCheck => ({ ok: false, reason })
81
+ if (typeof value !== 'object' || value === null) return fail('not an object')
82
+ const theme = value as Record<string, unknown>
83
+ if (theme.mode !== 'light' && theme.mode !== 'dark') return fail('mode must be light or dark')
84
+ if (typeof theme.look !== 'string' || !LOOKS.includes(theme.look)) return fail('look must be mac, gnome or windows')
85
+ if (typeof theme.tokens !== 'object' || theme.tokens === null) return fail('tokens must be an object')
86
+ const entries = Object.entries(theme.tokens as Record<string, unknown>)
87
+ if (entries.length > MAX_TOKENS) return fail('too many tokens')
88
+ const tokens: Record<string, string> = {}
89
+ for (const [name, token] of entries) {
90
+ if (!/^--[\w-]+$/.test(name)) return fail(`${name} is not a custom property`)
91
+ if (typeof token !== 'string' || !isPlainValue(token)) return fail(`${name} is not a plain value`)
92
+ tokens[name] = token
93
+ }
94
+
95
+ let fonts: ThemeFont[] | undefined
96
+ if (theme.fonts !== undefined) {
97
+ if (!Array.isArray(theme.fonts) || theme.fonts.length > MAX_FONTS) return fail('fonts must be a short list')
98
+ fonts = []
99
+ for (const font of theme.fonts as unknown[]) {
100
+ const f = font as Record<string, unknown> | null
101
+ if (typeof f?.family !== 'string' || !FAMILY.test(f.family)) return fail('a font family is not plain')
102
+ const src = typeof f.src === 'string' ? fromOriginOrData(f.src, origin, FONT_DATA) : null
103
+ if (src === null) return fail(`font ${f.family} is not from the publisher`)
104
+ if (f.weight !== undefined && (typeof f.weight !== 'string' || !/^\d{3}( \d{3})?$|^(normal|bold)$/.test(f.weight))) return fail(`font ${f.family} has a bad weight`)
105
+ if (f.style !== undefined && f.style !== 'normal' && f.style !== 'italic') return fail(`font ${f.family} has a bad style`)
106
+ fonts.push({
107
+ family: f.family,
108
+ src,
109
+ ...(f.weight === undefined ? {} : { weight: f.weight as string }),
110
+ ...(f.style === undefined ? {} : { style: f.style as 'normal' | 'italic' }),
111
+ })
112
+ }
113
+ }
114
+
115
+ let wallpaper: string | undefined
116
+ if (theme.wallpaper !== undefined) {
117
+ if (typeof theme.wallpaper !== 'string') return fail('wallpaper must be a string')
118
+ const image = isImageRef(theme.wallpaper) ? fromOriginOrData(theme.wallpaper, origin, IMAGE_DATA) : isPlainValue(theme.wallpaper) ? theme.wallpaper : null
119
+ if (image === null) return fail('wallpaper is not from the publisher')
120
+ wallpaper = image
121
+ }
122
+
123
+ return {
124
+ ok: true,
125
+ theme: {
126
+ mode: theme.mode,
127
+ look: theme.look as Look,
128
+ tokens,
129
+ ...(fonts === undefined ? {} : { fonts }),
130
+ ...(wallpaper === undefined ? {} : { wallpaper }),
131
+ },
132
+ }
133
+ }
134
+
@@ -0,0 +1,10 @@
1
+ /*
2
+ * The host's side of hosted mode: what a native or web shell uses to run several Desk apps under one
3
+ * menu bar and dock. Apps never load it; they use `connectHost`, through `DeskProvider`, from core.
4
+ */
5
+ export { checkChrome } from './checkChrome.js'
6
+ export type { ChromeCheck } from './checkChrome.js'
7
+ export { checkTheme, isPlainValue } from './checkTheme.js'
8
+ export type { ThemeCheck } from './checkTheme.js'
9
+ export { createBroadcastRouter, createMemoryHost } from './router.js'
10
+ export type { BroadcastRouter, BroadcastRouterOptions, HostedApp, MemoryHost, Rate, RateLimits, Refusal } from './router.js'