@adea-ai/app-framework 0.24.0 → 0.25.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 (137) hide show
  1. package/README.md +49 -1
  2. package/dist/cli/macos-signing-cli.d.ts +2 -0
  3. package/dist/cli/macos-signing-cli.js +14 -0
  4. package/dist/desktop/channel-proof.js +7 -1
  5. package/dist/framework-source.json +1 -1
  6. package/dist/release/framework-package-publish.js +4 -2
  7. package/dist/release/local-release.d.ts +2 -0
  8. package/dist/release/local-release.js +25 -2
  9. package/dist/release/macos-signing.d.ts +27 -0
  10. package/dist/release/macos-signing.js +101 -0
  11. package/dist/shell/app-shell.js +21 -33
  12. package/dist/shell/navigation-drawer.d.ts +12 -0
  13. package/dist/shell/navigation-drawer.js +42 -0
  14. package/dist/updates/release-feed.d.ts +2 -0
  15. package/dist/updates/release-feed.js +11 -2
  16. package/package.json +16 -4
  17. package/src/shell/app-shell.tsx +29 -20
  18. package/src/shell/navigation-drawer.tsx +36 -0
  19. package/src/app/create-application.ts +0 -245
  20. package/src/app/service-registry.ts +0 -124
  21. package/src/ci/toolchain-identity.ts +0 -244
  22. package/src/cli/cef-helper-cli.ts +0 -21
  23. package/src/cli/cli-runner.ts +0 -29
  24. package/src/cli/create-application-cli.ts +0 -62
  25. package/src/cli/electrobun-shortcut-patch-cli.ts +0 -24
  26. package/src/cli/framework-update-cli.ts +0 -41
  27. package/src/cli/release-assets-plan-cli.ts +0 -14
  28. package/src/cli/release-cut-cli.ts +0 -41
  29. package/src/cli/release-descriptor-staging-cli.ts +0 -57
  30. package/src/cli/release-finalize-cli.ts +0 -34
  31. package/src/cli/release-promote-cli.ts +0 -31
  32. package/src/cli/release-publish-cli.ts +0 -46
  33. package/src/cli/release-sign-cli.ts +0 -24
  34. package/src/cli/release-step-cli.ts +0 -71
  35. package/src/cli/toolchain-identity-cli.ts +0 -12
  36. package/src/cli/update-helper-cli.ts +0 -12
  37. package/src/cli/windows-runtime-patch-cli.ts +0 -17
  38. package/src/desktop/application-command-client.ts +0 -43
  39. package/src/desktop/channel-handshake.ts +0 -136
  40. package/src/desktop/channel-proof.ts +0 -122
  41. package/src/desktop/desktop-client.ts +0 -303
  42. package/src/desktop/desktop-gateway.ts +0 -189
  43. package/src/desktop/desktop-host.ts +0 -335
  44. package/src/desktop/desktop-stream.ts +0 -324
  45. package/src/desktop/loopback-policy.ts +0 -41
  46. package/src/desktop/open-url-channel.ts +0 -79
  47. package/src/desktop/string-policy.ts +0 -9
  48. package/src/installer/archive-entry.ts +0 -32
  49. package/src/installer/command-artifact.ts +0 -307
  50. package/src/installer/command-qualification-command.ts +0 -75
  51. package/src/installer/hosted-runner.ts +0 -11
  52. package/src/installer/installed-payload.ts +0 -408
  53. package/src/installer/installer-ci-bootstrap.ts +0 -199
  54. package/src/installer/installer-ci-preflight.ts +0 -61
  55. package/src/installer/installer-ci-worker.ts +0 -105
  56. package/src/installer/installer-extraction.ts +0 -367
  57. package/src/installer/installer-fetch-guard.ts +0 -75
  58. package/src/installer/installer-identity.ts +0 -331
  59. package/src/installer/installer-layout.ts +0 -167
  60. package/src/installer/installer-observation.ts +0 -671
  61. package/src/installer/installer-process-cleanup.ts +0 -167
  62. package/src/installer/installer-qualification-command.ts +0 -81
  63. package/src/installer/installer-renderer-evidence.ts +0 -268
  64. package/src/installer/installer-upgrade-command.ts +0 -226
  65. package/src/installer/installer-worker-guard.ts +0 -112
  66. package/src/installer/native-executable.ts +0 -37
  67. package/src/installer/upgrade-asset-download.ts +0 -72
  68. package/src/installer/write-all.ts +0 -19
  69. package/src/native/app-bundle.ts +0 -86
  70. package/src/native/cef-helper.ts +0 -471
  71. package/src/native/desktop-build-hook.ts +0 -21
  72. package/src/native/electrobun-source-patch.ts +0 -107
  73. package/src/native/linux-cef-links.ts +0 -14
  74. package/src/native/native-secret-store.ts +0 -99
  75. package/src/native/native-utilities.ts +0 -166
  76. package/src/native/partitioned-window.ts +0 -62
  77. package/src/native/secure-window.ts +0 -115
  78. package/src/native/sidecar-services.ts +0 -71
  79. package/src/native/sidecar.ts +0 -387
  80. package/src/native/window-bootstrap.ts +0 -77
  81. package/src/native/windows-build-hook-internal.ts +0 -49
  82. package/src/native/windows-build-hook.ts +0 -12
  83. package/src/native/windows-runtime-patch-internal.ts +0 -48
  84. package/src/native/windows-runtime-patch.ts +0 -15
  85. package/src/release/feed-release-cut.ts +0 -110
  86. package/src/release/feed-state.ts +0 -135
  87. package/src/release/framework-package-publish.ts +0 -163
  88. package/src/release/framework-update.ts +0 -196
  89. package/src/release/github-cli.ts +0 -27
  90. package/src/release/github-release-assets.ts +0 -36
  91. package/src/release/github-release-cut.ts +0 -237
  92. package/src/release/github-release-lookup.ts +0 -39
  93. package/src/release/github-release-promotion.ts +0 -112
  94. package/src/release/github-release-provenance.ts +0 -65
  95. package/src/release/github-release-publisher.ts +0 -90
  96. package/src/release/github-release-qualification.ts +0 -144
  97. package/src/release/local-provenance.ts +0 -92
  98. package/src/release/local-release.ts +0 -525
  99. package/src/release/macos-dmg.ts +0 -113
  100. package/src/release/release-artifact.ts +0 -53
  101. package/src/release/release-assets-plan.ts +0 -118
  102. package/src/release/release-config.ts +0 -420
  103. package/src/release/release-cut-command.ts +0 -80
  104. package/src/release/release-cut.ts +0 -133
  105. package/src/release/release-descriptor-staging.ts +0 -242
  106. package/src/release/release-finalize.ts +0 -191
  107. package/src/release/release-promotion-command.ts +0 -36
  108. package/src/release/release-promotion.ts +0 -132
  109. package/src/release/release-publication.ts +0 -213
  110. package/src/release/release-publish-command.ts +0 -163
  111. package/src/release/release-qualification.ts +0 -397
  112. package/src/release/release-staging.ts +0 -87
  113. package/src/release/release-steps.ts +0 -173
  114. package/src/release/retained-release-build.ts +0 -217
  115. package/src/shell/application-menu-commands.ts +0 -6
  116. package/src/shell/application-menu.ts +0 -250
  117. package/src/updates/channel-update-manager.ts +0 -127
  118. package/src/updates/desktop-updater.ts +0 -440
  119. package/src/updates/release-feed.ts +0 -187
  120. package/src/updates/release-location.ts +0 -101
  121. package/src/updates/signed-release.ts +0 -311
  122. package/src/updates/stable-file.ts +0 -32
  123. package/src/updates/update-arm.ts +0 -107
  124. package/src/updates/update-channel-store.ts +0 -86
  125. package/src/updates/update-discovery.ts +0 -113
  126. package/src/updates/update-download.ts +0 -82
  127. package/src/updates/update-exclusive-rename.ts +0 -58
  128. package/src/updates/update-extraction.ts +0 -261
  129. package/src/updates/update-handoff-plan.ts +0 -75
  130. package/src/updates/update-helper.ts +0 -87
  131. package/src/updates/update-launch.ts +0 -33
  132. package/src/updates/update-manager.ts +0 -130
  133. package/src/updates/update-process-fence.ts +0 -125
  134. package/src/updates/update-resume.ts +0 -34
  135. package/src/updates/update-shutdown.ts +0 -37
  136. package/src/updates/update-swap-stage.ts +0 -75
  137. package/src/updates/update-swap.ts +0 -30
@@ -1,99 +0,0 @@
1
- type SecretBackend = {
2
- get(input: { service: string; name: string }): Promise<string | null>
3
- set(input: { service: string; name: string; value: string; persist: 'local' }): Promise<void>
4
- delete(input: { service: string; name: string }): Promise<boolean>
5
- }
6
- const identifier = (value: unknown): value is string =>
7
- typeof value === 'string' &&
8
- value.length > 0 &&
9
- value.length <= 128 &&
10
- /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(value)
11
-
12
- /** Native credentials only. Apps choose the service namespace and own authorization and migration. */
13
- export function createNativeSecretStore(options: {
14
- service: string
15
- backend?: SecretBackend
16
- maxBytes?: number
17
- }) {
18
- const maxBytes = options.maxBytes ?? 2048
19
- if (
20
- !identifier(options.service) ||
21
- !Number.isSafeInteger(maxBytes) ||
22
- maxBytes < 1 ||
23
- maxBytes > 2048
24
- )
25
- throw new Error('Invalid credential store configuration')
26
- const backend = options.backend ?? Bun.secrets
27
- if (
28
- !backend ||
29
- typeof backend.get !== 'function' ||
30
- typeof backend.set !== 'function' ||
31
- typeof backend.delete !== 'function'
32
- )
33
- throw new Error('Credential store unavailable')
34
- let queue = Promise.resolve()
35
- const name = (value: unknown) => {
36
- if (!identifier(value)) throw new Error('Invalid credential name')
37
- return value
38
- }
39
- function ordered<T>(task: () => Promise<T>): Promise<T> {
40
- const result = queue.then(task)
41
- queue = result.then(
42
- () => {},
43
- () => {}
44
- )
45
- return result
46
- }
47
- async function read(account: string): Promise<string | null> {
48
- try {
49
- const value = await backend.get({ service: options.service, name: account })
50
- if (
51
- value !== null &&
52
- (typeof value !== 'string' ||
53
- value.includes('\0') ||
54
- Buffer.byteLength(value, 'utf8') > maxBytes)
55
- )
56
- throw new Error('Invalid native result')
57
- return value
58
- } catch {
59
- throw new Error('Credential store unavailable')
60
- }
61
- }
62
- return {
63
- get(account: string) {
64
- const key = name(account)
65
- return ordered(() => read(key))
66
- },
67
- set(account: string, value: string) {
68
- const key = name(account)
69
- if (
70
- typeof value !== 'string' ||
71
- !value ||
72
- value.includes('\0') ||
73
- Buffer.byteLength(value, 'utf8') > maxBytes
74
- )
75
- throw new Error('Invalid credential value')
76
- return ordered(async () => {
77
- try {
78
- await backend.set({ service: options.service, name: key, value, persist: 'local' })
79
- } catch {
80
- throw new Error('Credential store unavailable')
81
- }
82
- if ((await read(key)) !== value) throw new Error('Credential store verification failed')
83
- })
84
- },
85
- delete(account: string) {
86
- const key = name(account)
87
- return ordered(async () => {
88
- try {
89
- const removed = await backend.delete({ service: options.service, name: key })
90
- if (typeof removed !== 'boolean' || (await read(key)) !== null)
91
- throw new Error('Invalid native result')
92
- return removed
93
- } catch {
94
- throw new Error('Credential store unavailable')
95
- }
96
- })
97
- },
98
- }
99
- }
@@ -1,166 +0,0 @@
1
- import { isAbsolute } from 'node:path'
2
- import type { createServiceRegistry } from '../app/service-registry'
3
- import { containsAsciiControl } from '../desktop/string-policy'
4
-
5
- type Utility = 'info' | 'open-external' | 'pick-folder' | 'pick-files'
6
- /** What the web side may ask an open-file dialog for. Electrobun 2.0.5 has no save dialog. */
7
- export type PickFilesRequest = Readonly<{
8
- startingFolder?: string
9
- /** File extensions without the dot, e.g. `['png', 'jpg']`; omitted allows any file. */
10
- extensions?: readonly string[]
11
- multiple: boolean
12
- }>
13
- const MAX_PICKED_FILES = 1000
14
- const object = (value: unknown): value is Record<string, unknown> =>
15
- typeof value === 'object' && value !== null && !Array.isArray(value)
16
- const path = (value: unknown): value is string =>
17
- typeof value === 'string' &&
18
- value.length > 0 &&
19
- value.length <= 4096 &&
20
- !containsAsciiControl(value) &&
21
- isAbsolute(value) &&
22
- (process.platform !== 'win32' ||
23
- /^[A-Za-z]:[\\/]/.test(value) ||
24
- /^\\\\[A-Za-z0-9][A-Za-z0-9._-]*\\[^\\/?]+(?:\\|$)/.test(value))
25
- /** Register shared native utilities behind the host's authenticated principal and policy. */
26
- export function registerNativeUtilities<Principal>(options: {
27
- services: ReturnType<typeof createServiceRegistry<Principal>>
28
- info: Readonly<{ name: string; version: string; platform: string }>
29
- authorize(principal: Principal, utility: Utility): boolean | Promise<boolean>
30
- openExternal(url: string): boolean | Promise<boolean>
31
- pickFolder(startingFolder?: string): Promise<readonly string[]>
32
- /** Optional open-file dialog, registered as `shell.pick-files` when supplied. */
33
- pickFiles?(request: PickFilesRequest): Promise<readonly string[]>
34
- }): () => void {
35
- const { services, authorize, openExternal, pickFolder, pickFiles } = options
36
- const info = {
37
- name: options.info?.name,
38
- version: options.info?.version,
39
- platform: options.info?.platform,
40
- }
41
- if (
42
- !services ||
43
- typeof authorize !== 'function' ||
44
- typeof openExternal !== 'function' ||
45
- typeof pickFolder !== 'function' ||
46
- (pickFiles !== undefined && typeof pickFiles !== 'function') ||
47
- typeof info.name !== 'string' ||
48
- !info.name.trim() ||
49
- info.name.length > 100 ||
50
- containsAsciiControl(info.name) ||
51
- typeof info.version !== 'string' ||
52
- info.version.length > 64 ||
53
- !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(info.version) ||
54
- !['darwin', 'win32', 'linux'].includes(info.platform)
55
- )
56
- throw new Error('Invalid native utility configuration')
57
- const unregister: (() => void)[] = []
58
- const remove = () => {
59
- // splice returns a fresh array; reverse it while keeping the runtime target at ES2022.
60
- // oxlint-disable-next-line unicorn/no-array-reverse
61
- for (const fn of unregister.splice(0).reverse()) fn()
62
- }
63
- try {
64
- unregister.push(
65
- services.register('shell.info', {
66
- parse(value) {
67
- if (value !== null) throw new Error('Invalid input')
68
- return null
69
- },
70
- authorize: (principal) => authorize(principal, 'info'),
71
- run: () => ({ ...info }),
72
- })
73
- )
74
- unregister.push(
75
- services.register('shell.open-external', {
76
- parse(value) {
77
- if (
78
- !object(value) ||
79
- Object.keys(value).length !== 1 ||
80
- typeof value.url !== 'string' ||
81
- value.url.length > 2048 ||
82
- containsAsciiControl(value.url, true)
83
- )
84
- throw new Error('Invalid URL')
85
- const url = new URL(value.url)
86
- if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
87
- throw new Error('Invalid URL')
88
- return url.href
89
- },
90
- authorize: (principal) => authorize(principal, 'open-external'),
91
- async run(_principal, url) {
92
- const opened = await openExternal(url)
93
- if (typeof opened !== 'boolean') throw new Error('Invalid native result')
94
- return { opened }
95
- },
96
- })
97
- )
98
- unregister.push(
99
- services.register('shell.pick-folder', {
100
- parse(value) {
101
- if (
102
- !object(value) ||
103
- Object.keys(value).some((key) => key !== 'startingFolder') ||
104
- (value.startingFolder !== undefined && !path(value.startingFolder))
105
- )
106
- throw new Error('Invalid folder request')
107
- return value.startingFolder as string | undefined
108
- },
109
- authorize: (principal) => authorize(principal, 'pick-folder'),
110
- async run(_principal, startingFolder) {
111
- const paths = await pickFolder(startingFolder)
112
- if (!Array.isArray(paths) || paths.length > 1 || paths.some((value) => !path(value)))
113
- throw new Error('Invalid native result')
114
- return { paths: [...paths] }
115
- },
116
- })
117
- )
118
- if (pickFiles)
119
- unregister.push(
120
- services.register('shell.pick-files', {
121
- parse(value): PickFilesRequest {
122
- if (
123
- !object(value) ||
124
- Object.keys(value).some(
125
- (key) => !['startingFolder', 'extensions', 'multiple'].includes(key)
126
- ) ||
127
- (value.startingFolder !== undefined && !path(value.startingFolder)) ||
128
- (value.multiple !== undefined && typeof value.multiple !== 'boolean') ||
129
- (value.extensions !== undefined &&
130
- (!Array.isArray(value.extensions) ||
131
- value.extensions.length === 0 ||
132
- value.extensions.length > 32 ||
133
- value.extensions.some(
134
- (item) => typeof item !== 'string' || !/^[A-Za-z0-9]{1,16}$/.test(item)
135
- )))
136
- )
137
- throw new Error('Invalid file request')
138
- return {
139
- ...(value.startingFolder === undefined
140
- ? {}
141
- : { startingFolder: value.startingFolder as string }),
142
- ...(value.extensions === undefined
143
- ? {}
144
- : { extensions: [...(value.extensions as string[])] }),
145
- multiple: value.multiple === true,
146
- }
147
- },
148
- authorize: (principal) => authorize(principal, 'pick-files'),
149
- async run(_principal, request) {
150
- const paths = await pickFiles(request)
151
- if (
152
- !Array.isArray(paths) ||
153
- paths.length > (request.multiple ? MAX_PICKED_FILES : 1) ||
154
- paths.some((value) => !path(value))
155
- )
156
- throw new Error('Invalid native result')
157
- return { paths: [...paths] }
158
- },
159
- })
160
- )
161
- } catch (error) {
162
- remove()
163
- throw error
164
- }
165
- return remove
166
- }
@@ -1,62 +0,0 @@
1
- /**
2
- * The part of Electrobun's `BrowserView` class this helper relies on: the
3
- * constructor stores `options.partition` and then calls `init()`, which passes
4
- * `this.partition` to the native `createWebview` request (2.0.1 and 2.0.5).
5
- */
6
- export type PartitionableViewClass = {
7
- prototype: { init(this: { partition: string | null }): unknown }
8
- }
9
- export type PartitionedWindow = {
10
- webview?: { partition?: string | null } | null
11
- close(): unknown
12
- }
13
-
14
- const PARTITION = /^(?:persist:)?[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/
15
- let constructing = false
16
-
17
- /**
18
- * Construct a `BrowserWindow` whose own webview uses `partition`.
19
- *
20
- * Electrobun accepts `partition` on `BrowserView` but its `BrowserWindow`
21
- * constructor does not forward it to the view it creates. While `construct`
22
- * runs synchronously, this assigns the partition to the first view initialized
23
- * and restores `BrowserView.prototype.init` afterwards. It fails closed (and
24
- * closes the window) unless exactly one view was created and the window's view
25
- * reports the requested partition. Without a partition it only calls
26
- * `construct`, so ordinary windows keep Electrobun's default profile.
27
- */
28
- export function constructPartitionedWindow<Window extends PartitionedWindow>(options: {
29
- BrowserView: PartitionableViewClass
30
- partition: string | null | undefined
31
- construct(): Window
32
- }): Window {
33
- const { partition } = options
34
- if (partition === undefined || partition === null) return options.construct()
35
- if (!PARTITION.test(partition)) throw new Error('Invalid browser partition')
36
- const prototype = options.BrowserView.prototype
37
- const init = prototype.init
38
- if (typeof init !== 'function') throw new Error('Unsupported Electrobun BrowserView')
39
- if (constructing) throw new Error('Partitioned windows must be constructed one at a time')
40
- let views = 0
41
- let window: Window | undefined
42
- constructing = true
43
- prototype.init = function (this: { partition: string | null }) {
44
- if (++views === 1) this.partition = partition
45
- return init.call(this)
46
- }
47
- try {
48
- window = options.construct()
49
- } finally {
50
- prototype.init = init
51
- constructing = false
52
- }
53
- if (views !== 1 || window.webview?.partition !== partition) {
54
- try {
55
- window.close()
56
- } catch {
57
- /* Preserve the partition error. */
58
- }
59
- throw new Error('Electrobun did not create the window view in the requested partition')
60
- }
61
- return window
62
- }
@@ -1,115 +0,0 @@
1
- import { createLoopbackPolicy } from '../desktop/loopback-policy'
2
- import { createRendererProfile, type DesktopRenderer } from './renderer-profile'
3
- import { createWindowOptions, type WindowFrame } from './window-options'
4
-
5
- export type SecureWindowOptions = Omit<ReturnType<typeof createWindowOptions>, 'url'> & {
6
- url: null | 'about:blank'
7
- renderer: DesktopRenderer
8
- preload: string
9
- sandbox: true
10
- hidden?: boolean
11
- }
12
- export type NativeWindow = {
13
- webview: {
14
- setNavigationRules(rules: string[]): unknown
15
- loadURL(url: string): unknown
16
- on?(
17
- event: 'did-commit-navigation' | 'will-navigate',
18
- handler: (event: unknown) => void
19
- ): unknown
20
- }
21
- on(event: 'close', handler: () => void): unknown
22
- close(): unknown
23
- }
24
-
25
- /**
26
- * Create a blank window, install navigation rules, then load its trusted origin.
27
- * Electrobun 2.0.1 does not reliably apply the constructor's navigationRules;
28
- * setting them after a URL starts loading is too late to protect the preload.
29
- */
30
- export function createSecureWindow<Window extends NativeWindow>(options: {
31
- origin: string
32
- grant: { origin: string; preload: string; revoke(): void }
33
- renderer: DesktopRenderer
34
- platform: string
35
- title: string
36
- frame?: WindowFrame
37
- transparent?: boolean
38
- hidden?: boolean
39
- construct(options: SecureWindowOptions): Window
40
- }): Window {
41
- const origin = createLoopbackPolicy(options.origin).origin
42
- if (origin !== options.grant.origin) throw new Error('Window grant origin mismatch')
43
- const renderer = createRendererProfile(options.renderer).renderer
44
- let window: Window | undefined
45
- let deadline: ReturnType<typeof setTimeout> | undefined
46
- const clearDeadline = () => {
47
- if (deadline) clearTimeout(deadline)
48
- deadline = undefined
49
- }
50
- try {
51
- window = options.construct({
52
- ...createWindowOptions({
53
- title: options.title,
54
- url: origin + '/',
55
- platform: options.platform,
56
- renderer,
57
- frame: options.frame,
58
- transparent: options.transparent,
59
- }),
60
- url: options.platform === 'linux' && renderer === 'cef' ? 'about:blank' : null,
61
- renderer,
62
- preload: options.grant.preload,
63
- sandbox: true,
64
- hidden: options.hidden,
65
- })
66
- window.on('close', () => {
67
- clearDeadline()
68
- options.grant.revoke()
69
- })
70
- window.webview.setNavigationRules(['^*', origin + '/*'])
71
- // Linux CEF creates the browser asynchronously and starts with about:blank.
72
- // Wait for that initial navigation to commit before loading the trusted page:
73
- // otherwise the initial blank navigation can supersede the requested URL.
74
- if (options.platform === 'linux' && renderer === 'cef') {
75
- const view = window.webview
76
- const on = view.on
77
- if (!on) throw new Error('Window navigation events are unavailable')
78
- let blankCommitted = false
79
- on.call(view, 'did-commit-navigation', (event) => {
80
- const detail = (event as { data?: { detail?: unknown } })?.data?.detail
81
- if (detail === 'about:blank' && !blankCommitted) {
82
- blankCommitted = true
83
- try {
84
- view.loadURL(origin + '/')
85
- } catch {
86
- clearDeadline()
87
- options.grant.revoke()
88
- window?.close()
89
- }
90
- } else if (typeof detail === 'string') {
91
- try {
92
- if (new URL(detail).origin === origin) clearDeadline()
93
- } catch {
94
- /* Ignore unrelated navigation. */
95
- }
96
- }
97
- })
98
- deadline = setTimeout(() => {
99
- clearDeadline()
100
- options.grant.revoke()
101
- window?.close()
102
- }, 10000)
103
- } else window.webview.loadURL(origin + '/')
104
- return window
105
- } catch (error) {
106
- clearDeadline()
107
- options.grant.revoke()
108
- try {
109
- window?.close()
110
- } catch {
111
- /* Preserve the setup error. */
112
- }
113
- throw error
114
- }
115
- }
@@ -1,71 +0,0 @@
1
- import { ServiceError, type createServiceRegistry } from '../app/service-registry'
2
- import type { Sidecar } from './sidecar'
3
-
4
- const MAX_ARGUMENT_KEYS = 64
5
-
6
- /** Accept a web caller's arguments: absent, null, or a plain object. */
7
- export function parseSidecarArgs(value: unknown): Record<string, unknown> {
8
- if (value === undefined || value === null) return {}
9
- if (typeof value !== 'object' || Array.isArray(value)) throw new Error('Invalid input')
10
- const prototype = Object.getPrototypeOf(value)
11
- if (prototype !== Object.prototype && prototype !== null) throw new Error('Invalid input')
12
- if (Object.keys(value).length > MAX_ARGUMENT_KEYS) throw new Error('Invalid input')
13
- return value as Record<string, unknown>
14
- }
15
-
16
- /**
17
- * Register sidecar commands as framework services so the web side calls them
18
- * through the desktop client. A service named `${prefix}${command}` forwards its
19
- * arguments to the sidecar command and returns the reply value. A sidecar
20
- * failure becomes a `ServiceError` whose code is the failure reason
21
- * (`command_failed`, `timeout`, `exited`, ...) and whose message is readable.
22
- */
23
- export function registerSidecarServices<Principal>(options: {
24
- services: ReturnType<typeof createServiceRegistry<Principal>>
25
- sidecar: Pick<Sidecar, 'call'>
26
- commands: readonly string[]
27
- authorize(principal: Principal, command: string): boolean | Promise<boolean>
28
- /** Prepended to each command to form its service name. Default none. */
29
- prefix?: string
30
- /** Replaces the default plain-object argument check. */
31
- parse?(command: string, value: unknown): Record<string, unknown>
32
- /** Invoke body limit for each service; see `ServiceDefinition.maxBodyBytes`. */
33
- maxBodyBytes?: number
34
- }): () => void {
35
- const { services, sidecar, commands, authorize } = options
36
- const prefix = options.prefix ?? ''
37
- const parse = options.parse ?? ((_command: string, value: unknown) => parseSidecarArgs(value))
38
- if (
39
- !services ||
40
- typeof sidecar?.call !== 'function' ||
41
- typeof authorize !== 'function' ||
42
- typeof parse !== 'function' ||
43
- typeof prefix !== 'string' ||
44
- !Array.isArray(commands) ||
45
- new Set(commands).size !== commands.length
46
- )
47
- throw new Error('Invalid sidecar service configuration')
48
- const unregister: (() => void)[] = []
49
- const remove = () => {
50
- for (const fn of unregister.splice(0)) fn()
51
- }
52
- try {
53
- for (const command of commands)
54
- unregister.push(
55
- services.register(prefix + command, {
56
- parse: (value) => parse(command, value),
57
- authorize: (principal) => authorize(principal, command),
58
- async run(_principal, args) {
59
- const reply = await sidecar.call(command, args)
60
- if (reply.ok) return reply.value
61
- throw new ServiceError(reply.reason, reply.error)
62
- },
63
- ...(options.maxBodyBytes === undefined ? {} : { maxBodyBytes: options.maxBodyBytes }),
64
- })
65
- )
66
- } catch (error) {
67
- remove()
68
- throw error
69
- }
70
- return remove
71
- }