@adea-ai/app-framework 0.24.0 → 0.24.1

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 (128) hide show
  1. package/README.md +43 -1
  2. package/dist/framework-source.json +1 -1
  3. package/dist/release/framework-package-publish.js +4 -2
  4. package/dist/shell/app-shell.js +21 -33
  5. package/dist/shell/navigation-drawer.d.ts +12 -0
  6. package/dist/shell/navigation-drawer.js +42 -0
  7. package/package.json +13 -2
  8. package/src/shell/app-shell.tsx +29 -20
  9. package/src/shell/navigation-drawer.tsx +36 -0
  10. package/src/app/create-application.ts +0 -245
  11. package/src/app/service-registry.ts +0 -124
  12. package/src/ci/toolchain-identity.ts +0 -244
  13. package/src/cli/cef-helper-cli.ts +0 -21
  14. package/src/cli/cli-runner.ts +0 -29
  15. package/src/cli/create-application-cli.ts +0 -62
  16. package/src/cli/electrobun-shortcut-patch-cli.ts +0 -24
  17. package/src/cli/framework-update-cli.ts +0 -41
  18. package/src/cli/release-assets-plan-cli.ts +0 -14
  19. package/src/cli/release-cut-cli.ts +0 -41
  20. package/src/cli/release-descriptor-staging-cli.ts +0 -57
  21. package/src/cli/release-finalize-cli.ts +0 -34
  22. package/src/cli/release-promote-cli.ts +0 -31
  23. package/src/cli/release-publish-cli.ts +0 -46
  24. package/src/cli/release-sign-cli.ts +0 -24
  25. package/src/cli/release-step-cli.ts +0 -71
  26. package/src/cli/toolchain-identity-cli.ts +0 -12
  27. package/src/cli/update-helper-cli.ts +0 -12
  28. package/src/cli/windows-runtime-patch-cli.ts +0 -17
  29. package/src/desktop/application-command-client.ts +0 -43
  30. package/src/desktop/channel-handshake.ts +0 -136
  31. package/src/desktop/channel-proof.ts +0 -122
  32. package/src/desktop/desktop-client.ts +0 -303
  33. package/src/desktop/desktop-gateway.ts +0 -189
  34. package/src/desktop/desktop-host.ts +0 -335
  35. package/src/desktop/desktop-stream.ts +0 -324
  36. package/src/desktop/loopback-policy.ts +0 -41
  37. package/src/desktop/open-url-channel.ts +0 -79
  38. package/src/desktop/string-policy.ts +0 -9
  39. package/src/installer/archive-entry.ts +0 -32
  40. package/src/installer/command-artifact.ts +0 -307
  41. package/src/installer/command-qualification-command.ts +0 -75
  42. package/src/installer/hosted-runner.ts +0 -11
  43. package/src/installer/installed-payload.ts +0 -408
  44. package/src/installer/installer-ci-bootstrap.ts +0 -199
  45. package/src/installer/installer-ci-preflight.ts +0 -61
  46. package/src/installer/installer-ci-worker.ts +0 -105
  47. package/src/installer/installer-extraction.ts +0 -367
  48. package/src/installer/installer-fetch-guard.ts +0 -75
  49. package/src/installer/installer-identity.ts +0 -331
  50. package/src/installer/installer-layout.ts +0 -167
  51. package/src/installer/installer-observation.ts +0 -671
  52. package/src/installer/installer-process-cleanup.ts +0 -167
  53. package/src/installer/installer-qualification-command.ts +0 -81
  54. package/src/installer/installer-renderer-evidence.ts +0 -268
  55. package/src/installer/installer-upgrade-command.ts +0 -226
  56. package/src/installer/installer-worker-guard.ts +0 -112
  57. package/src/installer/native-executable.ts +0 -37
  58. package/src/installer/upgrade-asset-download.ts +0 -72
  59. package/src/installer/write-all.ts +0 -19
  60. package/src/native/app-bundle.ts +0 -86
  61. package/src/native/cef-helper.ts +0 -471
  62. package/src/native/desktop-build-hook.ts +0 -21
  63. package/src/native/electrobun-source-patch.ts +0 -107
  64. package/src/native/linux-cef-links.ts +0 -14
  65. package/src/native/native-secret-store.ts +0 -99
  66. package/src/native/native-utilities.ts +0 -166
  67. package/src/native/partitioned-window.ts +0 -62
  68. package/src/native/secure-window.ts +0 -115
  69. package/src/native/sidecar-services.ts +0 -71
  70. package/src/native/sidecar.ts +0 -387
  71. package/src/native/window-bootstrap.ts +0 -77
  72. package/src/native/windows-build-hook-internal.ts +0 -49
  73. package/src/native/windows-build-hook.ts +0 -12
  74. package/src/native/windows-runtime-patch-internal.ts +0 -48
  75. package/src/native/windows-runtime-patch.ts +0 -15
  76. package/src/release/feed-release-cut.ts +0 -110
  77. package/src/release/feed-state.ts +0 -135
  78. package/src/release/framework-package-publish.ts +0 -163
  79. package/src/release/framework-update.ts +0 -196
  80. package/src/release/github-cli.ts +0 -27
  81. package/src/release/github-release-assets.ts +0 -36
  82. package/src/release/github-release-cut.ts +0 -237
  83. package/src/release/github-release-lookup.ts +0 -39
  84. package/src/release/github-release-promotion.ts +0 -112
  85. package/src/release/github-release-provenance.ts +0 -65
  86. package/src/release/github-release-publisher.ts +0 -90
  87. package/src/release/github-release-qualification.ts +0 -144
  88. package/src/release/local-provenance.ts +0 -92
  89. package/src/release/local-release.ts +0 -525
  90. package/src/release/macos-dmg.ts +0 -113
  91. package/src/release/release-artifact.ts +0 -53
  92. package/src/release/release-assets-plan.ts +0 -118
  93. package/src/release/release-config.ts +0 -420
  94. package/src/release/release-cut-command.ts +0 -80
  95. package/src/release/release-cut.ts +0 -133
  96. package/src/release/release-descriptor-staging.ts +0 -242
  97. package/src/release/release-finalize.ts +0 -191
  98. package/src/release/release-promotion-command.ts +0 -36
  99. package/src/release/release-promotion.ts +0 -132
  100. package/src/release/release-publication.ts +0 -213
  101. package/src/release/release-publish-command.ts +0 -163
  102. package/src/release/release-qualification.ts +0 -397
  103. package/src/release/release-staging.ts +0 -87
  104. package/src/release/release-steps.ts +0 -173
  105. package/src/release/retained-release-build.ts +0 -217
  106. package/src/shell/application-menu-commands.ts +0 -6
  107. package/src/shell/application-menu.ts +0 -250
  108. package/src/updates/channel-update-manager.ts +0 -127
  109. package/src/updates/desktop-updater.ts +0 -440
  110. package/src/updates/release-feed.ts +0 -187
  111. package/src/updates/release-location.ts +0 -101
  112. package/src/updates/signed-release.ts +0 -311
  113. package/src/updates/stable-file.ts +0 -32
  114. package/src/updates/update-arm.ts +0 -107
  115. package/src/updates/update-channel-store.ts +0 -86
  116. package/src/updates/update-discovery.ts +0 -113
  117. package/src/updates/update-download.ts +0 -82
  118. package/src/updates/update-exclusive-rename.ts +0 -58
  119. package/src/updates/update-extraction.ts +0 -261
  120. package/src/updates/update-handoff-plan.ts +0 -75
  121. package/src/updates/update-helper.ts +0 -87
  122. package/src/updates/update-launch.ts +0 -33
  123. package/src/updates/update-manager.ts +0 -130
  124. package/src/updates/update-process-fence.ts +0 -125
  125. package/src/updates/update-resume.ts +0 -34
  126. package/src/updates/update-shutdown.ts +0 -37
  127. package/src/updates/update-swap-stage.ts +0 -75
  128. package/src/updates/update-swap.ts +0 -30
@@ -1,217 +0,0 @@
1
- import { githubCli } from './github-cli'
2
- import { createGitHubReleaseCutProvider } from './github-release-cut'
3
- import { parseReleaseConfig, platformPayloads, workflowFile } from './release-config'
4
- import { planReleaseAssets, releasePlanGitHubEnvironment } from './release-assets-plan'
5
- import { lookupRelease } from './github-release-lookup'
6
- import type { InstallerPlatform } from '../installer/installer-layout'
7
-
8
- type Selection =
9
- | { reuse: false }
10
- | { reuse: true; artifactId: string; runId: string; runAttempt: number; digest: string }
11
- const record = (value: unknown): value is Record<string, unknown> =>
12
- typeof value === 'object' && value !== null && !Array.isArray(value)
13
- const id = (value: unknown): string => {
14
- if (typeof value === 'number' && !Number.isSafeInteger(value))
15
- throw new Error('Invalid GitHub identity')
16
- const text = String(value)
17
- if (!/^[1-9]\d{0,19}$/.test(text)) throw new Error('Invalid GitHub identity')
18
- return text
19
- }
20
- /** Find the first trusted retained platform build, or refuse rebuilding published bytes. */
21
- export async function selectRetainedReleaseBuild(options: {
22
- env: Record<string, string | undefined>
23
- config: unknown
24
- tag: string
25
- platform: InstallerPlatform
26
- run?: (args: string[], input?: string) => Promise<string>
27
- }): Promise<Selection> {
28
- const { env, config, tag, platform } = options
29
- if (
30
- !['macos-arm64', 'windows-x64', 'linux-x64'].includes(platform) ||
31
- !record(config) ||
32
- typeof config.appName !== 'string' ||
33
- env.RELEASE_TAG !== tag
34
- )
35
- throw new Error('Invalid retained build context')
36
- const run = options.run ?? githubCli(releasePlanGitHubEnvironment(env))
37
- const plan = await planReleaseAssets({ env, config, tag, run })
38
- if (env.SOURCE_SHA !== plan.sourceSha) throw new Error('Retained build source mismatch')
39
- const parsed = parseReleaseConfig(config),
40
- assetsWorkflow = parsed.releaseWorkflows.assets,
41
- requirements = parsed.requirements(tag)
42
- if (requirements.platforms[platform]?.length !== 2)
43
- throw new Error('Invalid platform requirements')
44
- const expected = platformPayloads(requirements, platform)
45
- const repository = env.GITHUB_REPOSITORY!,
46
- base = `repos/${repository}`
47
- const read = async (path: string): Promise<unknown> => JSON.parse(await run(['api', path]))
48
- const release = await lookupRelease({
49
- tag,
50
- base,
51
- read,
52
- includeDraft: true,
53
- })
54
- // The build job's token cannot list drafts; planning already admitted this tag as the
55
- // hidden Release Please draft, which holds no published payload yet.
56
- const hiddenDraft = release === null
57
- if (!hiddenDraft && (!record(release) || release.tag_name !== tag))
58
- throw new Error('Invalid release assets')
59
- const releaseAssets: unknown[] = []
60
- if (!hiddenDraft) {
61
- const releaseId = id((release as { id?: unknown }).id)
62
- for (let page = 1; page <= 10; page++) {
63
- const assets = await read(`${base}/releases/${releaseId}/assets?per_page=100&page=${page}`)
64
- if (!Array.isArray(assets) || assets.length > 100) throw new Error('Invalid release assets')
65
- releaseAssets.push(...assets)
66
- if (assets.length < 100) break
67
- if (page === 10) throw new Error('Too many release assets')
68
- }
69
- }
70
- if (
71
- releaseAssets.some((asset) => !record(asset) || typeof asset.name !== 'string') ||
72
- new Set(releaseAssets.map((asset) => (asset as { name: string }).name)).size !==
73
- releaseAssets.length
74
- )
75
- throw new Error('Invalid release assets')
76
- const published = expected.some((name) =>
77
- releaseAssets.some((asset) => (asset as { name: string }).name === name)
78
- )
79
- const provider = createGitHubReleaseCutProvider({
80
- repository,
81
- appName: config.appName,
82
- requirements: parsed.requirements,
83
- assetsWorkflow,
84
- releasePleaseManifestPath: parsed.releasePleaseManifestPath,
85
- run,
86
- })
87
- const candidates: Selection[] = [],
88
- seenArtifacts = new Set<string>(),
89
- seenRuns = new Set<string>()
90
- for (let page = 1; page <= 10; page++) {
91
- const response = await read(
92
- `${base}/actions/workflows/${workflowFile(assetsWorkflow)}/runs?event=workflow_dispatch&per_page=100&page=${page}`
93
- )
94
- if (
95
- !record(response) ||
96
- !Array.isArray(response.workflow_runs) ||
97
- response.workflow_runs.length > 100
98
- )
99
- throw new Error('Invalid release workflow listing')
100
- for (const value of response.workflow_runs) {
101
- if (
102
- !record(value) ||
103
- value.display_title !== `framework-release-assets:${tag}` ||
104
- value.head_branch !== 'main' ||
105
- value.event !== 'workflow_dispatch' ||
106
- value.path !== assetsWorkflow
107
- )
108
- continue
109
- const runId = id(value.id)
110
- if (seenRuns.has(runId)) throw new Error('Duplicate workflow run')
111
- seenRuns.add(runId)
112
- if (value.status !== 'completed' && runId !== env.GITHUB_RUN_ID) continue
113
- if (
114
- typeof value.head_sha !== 'string' ||
115
- !/^[a-f0-9]{40}$/.test(value.head_sha) ||
116
- !(await provider.isMainAncestor(value.head_sha))
117
- )
118
- throw new Error('Untrusted retained workflow')
119
- for (let artifactPage = 1; artifactPage <= 10; artifactPage++) {
120
- const listing = await read(
121
- `${base}/actions/runs/${runId}/artifacts?per_page=100&page=${artifactPage}`
122
- )
123
- if (!record(listing) || !Array.isArray(listing.artifacts) || listing.artifacts.length > 100)
124
- throw new Error('Invalid retained artifact listing')
125
- for (const artifact of listing.artifacts) {
126
- if (!record(artifact) || typeof artifact.name !== 'string')
127
- throw new Error('Invalid retained artifact')
128
- const prefix = `framework-build-${tag}-${platform}-${plan.sourceSha}-r${runId}-a`
129
- if (!artifact.name.startsWith(prefix)) continue
130
- const attemptText = artifact.name.slice(prefix.length)
131
- if (artifact.expired === true) continue
132
- if (
133
- !/^[1-9]\d{0,8}$/.test(attemptText) ||
134
- artifact.expired !== false ||
135
- typeof artifact.digest !== 'string' ||
136
- !/^sha256:[a-f0-9]{64}$/.test(artifact.digest) ||
137
- !Number.isSafeInteger(artifact.size_in_bytes) ||
138
- Number(artifact.size_in_bytes) < 1 ||
139
- !record(artifact.workflow_run) ||
140
- id(artifact.workflow_run.id) !== runId ||
141
- artifact.workflow_run.head_sha !== value.head_sha ||
142
- artifact.workflow_run.head_branch !== 'main'
143
- )
144
- throw new Error('Invalid retained artifact provenance')
145
- const artifactId = id(artifact.id)
146
- if (seenArtifacts.has(artifactId)) throw new Error('Duplicate retained artifact')
147
- seenArtifacts.add(artifactId)
148
- const attempt = Number(attemptText)
149
- if (runId === env.GITHUB_RUN_ID && attempt >= Number(env.GITHUB_RUN_ATTEMPT)) continue
150
- const attemptRun = await read(`${base}/actions/runs/${runId}/attempts/${attempt}`)
151
- if (
152
- !record(attemptRun) ||
153
- id(attemptRun.id) !== runId ||
154
- attemptRun.run_attempt !== attempt ||
155
- attemptRun.head_sha !== value.head_sha ||
156
- attemptRun.head_branch !== 'main' ||
157
- attemptRun.event !== 'workflow_dispatch' ||
158
- attemptRun.path !== assetsWorkflow ||
159
- attemptRun.display_title !== `framework-release-assets:${tag}` ||
160
- !record(attemptRun.repository) ||
161
- attemptRun.repository.full_name !== repository ||
162
- attemptRun.status !== 'completed'
163
- )
164
- throw new Error('Retained attempt provenance mismatch')
165
- const jobs: Record<string, unknown>[] = []
166
- for (let jobPage = 1; jobPage <= 10; jobPage++) {
167
- const data = await read(
168
- `${base}/actions/runs/${runId}/attempts/${attempt}/jobs?per_page=100&page=${jobPage}`
169
- )
170
- if (
171
- !record(data) ||
172
- !Array.isArray(data.jobs) ||
173
- data.jobs.length > 100 ||
174
- data.jobs.some((job) => !record(job))
175
- )
176
- throw new Error('Invalid retained job listing')
177
- jobs.push(...(data.jobs as Record<string, unknown>[]))
178
- if (data.jobs.length < 100) break
179
- if (jobPage === 10) throw new Error('Too many retained jobs')
180
- }
181
- const build = jobs.filter((job) => job.name === `Build / ${platform}`)
182
- if (
183
- build.length !== 1 ||
184
- build[0]!.status !== 'completed' ||
185
- build[0]!.conclusion !== 'success'
186
- )
187
- throw new Error('Retained build job is not successful')
188
- candidates.push({
189
- reuse: true,
190
- artifactId,
191
- runId,
192
- runAttempt: attempt,
193
- digest: artifact.digest,
194
- })
195
- }
196
- if (listing.artifacts.length < 100) break
197
- if (artifactPage === 10) throw new Error('Too many retained artifacts')
198
- }
199
- }
200
- if (response.workflow_runs.length < 100) break
201
- if (page === 10) throw new Error('Too many release workflow runs')
202
- }
203
- const sorted = candidates
204
- .filter((value): value is Extract<Selection, { reuse: true }> => value.reuse)
205
- // The filter creates a new array; keep ES2022 runtime compatibility.
206
- // oxlint-disable-next-line unicorn/no-array-sort
207
- .sort((left, right) =>
208
- BigInt(left.runId) < BigInt(right.runId)
209
- ? -1
210
- : BigInt(left.runId) > BigInt(right.runId)
211
- ? 1
212
- : left.runAttempt - right.runAttempt
213
- )
214
- if (sorted.length) return sorted[0]!
215
- if (published) throw new Error('Published payload has no retained build; refusing to rebuild')
216
- return { reuse: false }
217
- }
@@ -1,6 +0,0 @@
1
- /** Shared surfaces a native menu can open in the window. */
2
- export const applicationMenuCommands = ['about', 'settings', 'help', 'updates'] as const
3
- export type ApplicationMenuCommand = (typeof applicationMenuCommands)[number]
4
-
5
- /** The stream the window opens to receive menu commands. */
6
- export const applicationCommandStream = 'shell.commands'
@@ -1,250 +0,0 @@
1
- import type { createDesktopStreams } from '../desktop/desktop-stream'
2
- import { containsAsciiControl } from '../desktop/string-policy'
3
-
4
- import {
5
- applicationCommandStream,
6
- applicationMenuCommands,
7
- type ApplicationMenuCommand,
8
- } from './application-menu-commands'
9
-
10
- export * from './application-menu-commands'
11
-
12
- /** The subset of Electrobun's application menu item configuration the framework emits. */
13
- export type ApplicationMenuItem =
14
- | { type: 'separator' }
15
- | {
16
- label?: string
17
- role?: string
18
- action?: string
19
- accelerator?: string
20
- enabled?: boolean
21
- submenu?: ApplicationMenuItem[]
22
- }
23
-
24
- export type ApplicationMenuLink = Readonly<{ label: string; url: string }>
25
-
26
- /** An app-owned top-level menu; its actions are delivered to `onAction`. */
27
- export type ApplicationMenuSection = Readonly<{
28
- label: string
29
- items: readonly ApplicationMenuItem[]
30
- }>
31
-
32
- export type ApplicationMenuOptions = Readonly<{
33
- name: string
34
- platform: string
35
- /** Adds Check for Updates to the application menu. */
36
- updates?: boolean
37
- /** Adds Settings… with the platform settings accelerator. */
38
- settings?: boolean
39
- /** Help menu links opened in the system browser. */
40
- helpLinks?: readonly ApplicationMenuLink[]
41
- /** App menus placed between View and Window. */
42
- sections?: readonly ApplicationMenuSection[]
43
- }>
44
-
45
- const prefix = 'app-framework:'
46
- const linkPrefix = `${prefix}link:`
47
- const appAction = /^[a-z][a-z0-9]*(?:[.:-][a-z0-9]+)*$/
48
- const separator = { type: 'separator' } as const
49
-
50
- function label(value: string) {
51
- return (
52
- typeof value === 'string' && value.trim() && value.length <= 100 && !containsAsciiControl(value)
53
- )
54
- }
55
-
56
- function validateItems(items: readonly ApplicationMenuItem[], depth = 0): ApplicationMenuItem[] {
57
- if (!Array.isArray(items) || items.length > 64 || depth > 2) throw new Error('Invalid menu items')
58
- return items.map((item) => {
59
- if ('type' in item) return separator
60
- if (
61
- (item.label !== undefined && !label(item.label)) ||
62
- (item.action !== undefined &&
63
- (!appAction.test(item.action) || item.action.startsWith(prefix))) ||
64
- (item.role !== undefined && !/^[a-zA-Z]+$/.test(item.role)) ||
65
- (item.action === undefined && item.role === undefined && item.submenu === undefined) ||
66
- (item.label === undefined && item.role === undefined)
67
- )
68
- throw new Error('Invalid menu item')
69
- return {
70
- ...item,
71
- ...(item.submenu ? { submenu: validateItems(item.submenu, depth + 1) } : {}),
72
- }
73
- })
74
- }
75
-
76
- function validLink(link: ApplicationMenuLink) {
77
- if (!label(link.label)) return false
78
- try {
79
- const url = new URL(link.url)
80
- return url.protocol === 'https:' && url.username === '' && url.password === ''
81
- } catch {
82
- return false
83
- }
84
- }
85
-
86
- /**
87
- * Build the standard native menu: application, Edit, View, app sections, Window and Help.
88
- * Electrobun implements application menus on macOS and Windows; Linux apps keep the same
89
- * surfaces in the window's account menu.
90
- */
91
- export function createApplicationMenu(options: ApplicationMenuOptions): ApplicationMenuItem[] {
92
- const links = options.helpLinks ?? []
93
- if (
94
- !label(options.name) ||
95
- !Array.isArray(links) ||
96
- links.length > 16 ||
97
- !links.every(validLink) ||
98
- (options.sections?.length ?? 0) > 8
99
- )
100
- throw new Error('Invalid application menu configuration')
101
- const sections = (options.sections ?? []).map((section) => {
102
- if (!label(section.label)) throw new Error('Invalid application menu configuration')
103
- return { label: section.label, submenu: validateItems(section.items) }
104
- })
105
- const mac = options.platform === 'darwin'
106
- const about = { label: `About ${options.name}`, action: `${prefix}about` }
107
- const settings = options.settings
108
- ? [{ label: mac ? 'Settings…' : 'Settings', action: `${prefix}settings`, accelerator: ',' }]
109
- : []
110
- const updates = options.updates
111
- ? [{ label: 'Check for Updates…', action: `${prefix}updates` }]
112
- : []
113
- const help: ApplicationMenuItem[] = [
114
- { label: `${options.name} Help`, action: `${prefix}help` },
115
- ...(links.length ? [separator] : []),
116
- ...links.map((link, index) => ({ label: link.label, action: `${linkPrefix}${index}` })),
117
- ]
118
- const edit: ApplicationMenuItem = {
119
- label: 'Edit',
120
- submenu: [
121
- { role: 'undo' },
122
- { role: 'redo' },
123
- separator,
124
- { role: 'cut' },
125
- { role: 'copy' },
126
- { role: 'paste' },
127
- ...(mac ? [{ role: 'pasteAndMatchStyle' }] : []),
128
- { role: 'delete' },
129
- { role: 'selectAll' },
130
- ],
131
- }
132
- const view: ApplicationMenuItem = { label: 'View', submenu: [{ role: 'toggleFullScreen' }] }
133
- const window: ApplicationMenuItem = {
134
- label: 'Window',
135
- submenu: mac
136
- ? [{ role: 'minimize' }, { role: 'zoom' }, separator, { role: 'bringAllToFront' }]
137
- : [{ role: 'minimize' }, { role: 'close' }],
138
- }
139
- if (mac)
140
- return [
141
- {
142
- label: options.name,
143
- submenu: [
144
- about,
145
- ...updates,
146
- separator,
147
- ...settings,
148
- ...(settings.length ? [separator] : []),
149
- { role: 'hide' },
150
- { role: 'hideOthers' },
151
- { role: 'showAll' },
152
- separator,
153
- { role: 'quit' },
154
- ],
155
- },
156
- edit,
157
- view,
158
- ...sections,
159
- window,
160
- { label: 'Help', submenu: help },
161
- ]
162
- return [
163
- {
164
- label: 'File',
165
- submenu: [...settings, ...(settings.length ? [separator] : []), { role: 'quit' }],
166
- },
167
- edit,
168
- view,
169
- ...sections,
170
- window,
171
- { label: 'Help', submenu: [...help, separator, ...updates, about] },
172
- ]
173
- }
174
-
175
- export type ApplicationMenuDispatch = Readonly<{
176
- /** Deliver a shared surface command to the window. */
177
- command(command: ApplicationMenuCommand): void
178
- openExternal(url: string): unknown
179
- /** App-owned actions from `sections`. */
180
- action?(action: string): void
181
- }>
182
-
183
- function actionOf(event: unknown): string | undefined {
184
- if (typeof event !== 'object' || event === null) return undefined
185
- const data = (event as { data?: unknown }).data
186
- if (typeof data !== 'object' || data === null) return undefined
187
- const action = (data as { action?: unknown }).action
188
- return typeof action === 'string' && action.length <= 200 ? action : undefined
189
- }
190
-
191
- /** Route an `application-menu-clicked` event to shared commands, help links or app actions. */
192
- export function createApplicationMenuHandler(
193
- options: Pick<ApplicationMenuOptions, 'helpLinks'> & ApplicationMenuDispatch
194
- ) {
195
- const links = [...(options.helpLinks ?? [])]
196
- return (event: unknown) => {
197
- const action = actionOf(event)
198
- if (action === undefined) return
199
- if (action.startsWith(linkPrefix)) {
200
- const index = action.slice(linkPrefix.length)
201
- const link = /^(?:0|[1-9]\d?)$/.test(index) ? links[Number(index)] : undefined
202
- if (link && validLink(link)) void options.openExternal(link.url)
203
- return
204
- }
205
- if (action.startsWith(prefix)) {
206
- const command = action.slice(prefix.length)
207
- if ((applicationMenuCommands as readonly string[]).includes(command))
208
- options.command(command as ApplicationMenuCommand)
209
- return
210
- }
211
- if (appAction.test(action)) options.action?.(action)
212
- }
213
- }
214
-
215
- /**
216
- * Broadcast menu commands to authenticated windows over the desktop stream channel.
217
- * Commands carry no data beyond their name; the window decides how to present them.
218
- */
219
- export function createApplicationCommandBroadcaster<Principal>(options: {
220
- authorize(principal: Principal): boolean | Promise<boolean>
221
- }) {
222
- const subscribers = new Set<(payload: string) => boolean>()
223
- return {
224
- register(streams: ReturnType<typeof createDesktopStreams<Principal>>) {
225
- streams.register(applicationCommandStream, {
226
- parse(value) {
227
- if (value !== null) throw new Error('Invalid input')
228
- return null
229
- },
230
- authorize: (principal) => options.authorize(principal),
231
- open(_principal, _input, context) {
232
- const send = (payload: string) => context.current() && context.send(payload)
233
- subscribers.add(send)
234
- return {
235
- receive: () => context.close(),
236
- close: () => subscribers.delete(send),
237
- }
238
- },
239
- })
240
- },
241
- /** Returns whether at least one window received the command. */
242
- send(command: ApplicationMenuCommand): boolean {
243
- if (!(applicationMenuCommands as readonly string[]).includes(command)) return false
244
- const payload = JSON.stringify({ command })
245
- let delivered = false
246
- for (const send of subscribers) delivered = send(payload) || delivered
247
- return delivered
248
- },
249
- }
250
- }
@@ -1,127 +0,0 @@
1
- import { createUpdateManager } from './update-manager'
2
- import { isUpdateChannel, type UpdateChannel } from './release-version'
3
- import { releaseChannelForUpdate } from './update-channel-store'
4
- import type { ReleaseTrust } from './signed-release'
5
- import type { FetchFunction } from './release-location'
6
-
7
- type ManagerOptions = Parameters<typeof createUpdateManager>[0]
8
- type State = ReturnType<ReturnType<typeof createUpdateManager>['status']>
9
- /** Re-read the persisted channel for every check and bind an approval to that choice. */
10
- export function createChannelUpdateManager(options: {
11
- store: { read(): Promise<UpdateChannel>; save(channel: UpdateChannel): Promise<UpdateChannel> }
12
- trust: Omit<ReleaseTrust, 'channel'>
13
- discoveryNames: Record<UpdateChannel, string>
14
- currentVersion: string
15
- directory: string
16
- maxBytes: number
17
- stage: ManagerOptions['stage']
18
- discover?: ManagerOptions['discover']
19
- download?: ManagerOptions['download']
20
- fetch?: FetchFunction
21
- }) {
22
- let busy = false,
23
- active: { channel: UpdateChannel; manager: ReturnType<typeof createUpdateManager> } | undefined
24
- let state: State = Object.freeze({
25
- phase: 'idle',
26
- currentVersion: options.currentVersion,
27
- availableVersion: null,
28
- error: null,
29
- })
30
- const acquire = () => {
31
- if (busy) throw new Error('Update manager is busy')
32
- busy = true
33
- }
34
- const idle = () =>
35
- (state = Object.freeze({
36
- phase: 'idle',
37
- currentVersion: options.currentVersion,
38
- availableVersion: null,
39
- error: null,
40
- }))
41
- const failed = () =>
42
- (state = Object.freeze({
43
- phase: 'failed',
44
- currentVersion: options.currentVersion,
45
- availableVersion: null,
46
- error: 'Update check failed. Please try again.',
47
- }))
48
- const status = () => active?.manager.status() ?? state
49
- return {
50
- status,
51
- readChannel: () => options.store.read(),
52
- async saveChannel(channel: UpdateChannel) {
53
- if (!isUpdateChannel(channel)) throw new Error('Invalid update channel')
54
- acquire()
55
- try {
56
- if (status().phase === 'staged') throw new Error('An update is already staged')
57
- const saved = await options.store.save(channel)
58
- if (saved !== channel) throw new Error('Update channel was not saved')
59
- active = undefined
60
- idle()
61
- return saved
62
- } finally {
63
- busy = false
64
- }
65
- },
66
- async check() {
67
- acquire()
68
- try {
69
- if (status().phase === 'staged') throw new Error('An update is already staged')
70
- active = undefined
71
- state = Object.freeze({
72
- phase: 'checking',
73
- currentVersion: options.currentVersion,
74
- availableVersion: null,
75
- error: null,
76
- })
77
- try {
78
- const channel = await options.store.read()
79
- if (!isUpdateChannel(channel)) throw new Error('Invalid update channel')
80
- const manager = createUpdateManager({
81
- currentVersion: options.currentVersion,
82
- trust: { ...options.trust, channel: releaseChannelForUpdate(channel) },
83
- discoveryName: options.discoveryNames[channel],
84
- directory: options.directory,
85
- maxBytes: options.maxBytes,
86
- discover: options.discover,
87
- download: options.download,
88
- fetch: options.fetch,
89
- stage: async (input) => {
90
- if ((await options.store.read()) !== channel)
91
- throw new Error('Update channel changed')
92
- await options.stage(input)
93
- if ((await options.store.read()) !== channel)
94
- throw new Error('Update channel changed')
95
- },
96
- })
97
- active = { channel, manager }
98
- state = await manager.check()
99
- if ((await options.store.read()) !== channel) {
100
- active = undefined
101
- return failed()
102
- }
103
- return state
104
- } catch {
105
- active = undefined
106
- return failed()
107
- }
108
- } finally {
109
- busy = false
110
- }
111
- },
112
- async install(input: { approved: boolean; expectedVersion: string }) {
113
- acquire()
114
- try {
115
- if (!active || (await options.store.read()) !== active.channel) {
116
- active = undefined
117
- idle()
118
- throw new Error('Update channel changed')
119
- }
120
- state = await active.manager.install(input)
121
- return state
122
- } finally {
123
- busy = false
124
- }
125
- },
126
- }
127
- }