@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,34 +0,0 @@
1
- import { readFile } from 'node:fs/promises'
2
- import { gitCli, originRepository } from '../release/local-provenance'
3
- import { verifyLocalRelease } from '../release/local-release'
4
- import { finalizeReleasePleaseDraft, parseFinalizationEvent } from '../release/release-finalize'
5
-
6
- // A local release has no draft to finalize: `--local <config> <tag>` verifies what the feed
7
- // publishes for the tag instead, after an upload.
8
- if (process.argv[2] === '--local') {
9
- const [configPath, tag, ...extra] = process.argv.slice(3)
10
- if (!configPath || !tag || extra.length) throw new Error('Expected release configuration and tag')
11
- const config: unknown = JSON.parse(await readFile(configPath, 'utf8'))
12
- const result = await verifyLocalRelease({
13
- config,
14
- tag,
15
- repository: await originRepository(gitCli()),
16
- fetch,
17
- })
18
- console.log(JSON.stringify(result))
19
- } else {
20
- const configPath = process.argv[2]
21
- if (!configPath || process.argv.length !== 3 || !process.env.GITHUB_EVENT_PATH)
22
- throw new Error('Expected release configuration and workflow event')
23
- const config: unknown = JSON.parse(await readFile(configPath, 'utf8'))
24
- if (typeof config !== 'object' || config === null || Array.isArray(config))
25
- throw new Error('Invalid release configuration')
26
- const event: unknown = JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH, 'utf8'))
27
- const identity = parseFinalizationEvent(
28
- event,
29
- process.env.GITHUB_REPOSITORY ?? '',
30
- process.env.GITHUB_EVENT_NAME
31
- )
32
- await finalizeReleasePleaseDraft({ env: process.env, config, ...identity })
33
- console.log('Qualified draft published as a prerelease')
34
- }
@@ -1,31 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { gitCli, originRepository } from '../release/local-provenance'
3
- import { runLocalPromotion } from '../release/local-release'
4
- import { runPromotionCommand } from '../release/release-promotion-command'
5
- import { readJsonFile, runCli } from './cli-runner'
6
-
7
- // `release-promote-cli.js <config>` in the trusted workflow, or
8
- // `release-promote-cli.js --local <config> <out>`, which stages the feed update under <out>.
9
- const local = process.argv[2] === '--local'
10
- await runCli(
11
- 'Unable to promote the release; check the trusted workflow or feed and release configuration.',
12
- async () => {
13
- const [path, out, ...extra] = process.argv.slice(local ? 3 : 2)
14
- const held = process.env.STABLE_PROMOTION_HELD
15
- if (!path || extra.length || !out !== !local || !['true', 'false', undefined].includes(held))
16
- throw new Error('Invalid workflow invocation')
17
- const config = await readJsonFile(path, 65536)
18
- const result = local
19
- ? await runLocalPromotion({
20
- config,
21
- out: out!,
22
- now: new Date().toISOString(),
23
- held: held === 'true',
24
- repository: await originRepository(gitCli()),
25
- fetch,
26
- })
27
- : await runPromotionCommand({ env: process.env, config })
28
- process.stdout.write(JSON.stringify(local ? { ...result, plan: undefined } : result) + '\n')
29
- },
30
- { requireGitHubActions: !local }
31
- )
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { gitCli } from '../release/local-provenance'
3
- import {
4
- hostReleasePlatform,
5
- localTagProvenance,
6
- stageLocalRelease,
7
- } from '../release/local-release'
8
- import { runReleasePublishCommand } from '../release/release-publish-command'
9
- import { readJsonFile, runCli } from './cli-runner'
10
-
11
- // `release-publish-cli.js <directory> <config>` in the trusted assets workflow, or
12
- // `release-publish-cli.js --local <directory> <config> <out>` with RELEASE_TAG and SOURCE_SHA,
13
- // which stages the feed upload under <out> instead of publishing.
14
- const local = process.argv[2] === '--local'
15
- await runCli(
16
- 'Unable to publish release artifacts; check the trusted workflow or local checkout, receipts, signing configuration and staged bytes.',
17
- async () => {
18
- const [directory, configPath, out, ...extra] = process.argv.slice(local ? 3 : 2)
19
- if (!directory || !configPath || extra.length || !out !== !local)
20
- throw new Error('Invalid workflow invocation')
21
- const config = await readJsonFile(configPath, 65536)
22
- if (local) {
23
- const { RELEASE_TAG: tag, SOURCE_SHA: sourceSha } = process.env
24
- if (!tag || !sourceSha) throw new Error('Missing local release identity')
25
- const { repository } = await localTagProvenance({ config, tag, sourceSha, git: gitCli() })
26
- const plan = await stageLocalRelease({
27
- config,
28
- directory,
29
- out: out!,
30
- tag,
31
- sourceSha,
32
- repository,
33
- platform: hostReleasePlatform(),
34
- now: new Date().toISOString(),
35
- fetch,
36
- })
37
- process.stdout.write(JSON.stringify({ tag, objects: plan.objects.length }) + '\n')
38
- return
39
- }
40
- const result = await runReleasePublishCommand({ env: process.env, config, directory })
41
- process.stdout.write(
42
- JSON.stringify({ tag: result.tag, runId: result.runId, runAttempt: result.runAttempt }) + '\n'
43
- )
44
- },
45
- { requireGitHubActions: !local }
46
- )
@@ -1,24 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { createPrivateKey } from 'node:crypto'
3
- import { signReleaseArtifact, type ReleaseArtifactIdentity } from '../release/release-artifact'
4
- import { readJsonFile, runCli } from './cli-runner'
5
-
6
- // The signing key is passed in the environment, never in command arguments or output.
7
- await runCli(
8
- 'Unable to sign release artifact; check the identity, archive and signing key.',
9
- async () => {
10
- const [identityPath, artifactPath, feed, ...extra] = process.argv.slice(2)
11
- if (
12
- !identityPath ||
13
- !artifactPath ||
14
- !feed ||
15
- extra.length ||
16
- !process.env.ADEA_RELEASE_SIGNING_KEY
17
- )
18
- throw new Error('usage')
19
- const identity = (await readJsonFile(identityPath, 16384)) as ReleaseArtifactIdentity
20
- const key = createPrivateKey(process.env.ADEA_RELEASE_SIGNING_KEY)
21
- const result = await signReleaseArtifact(artifactPath, identity, key, feed)
22
- process.stdout.write(JSON.stringify(result) + '\n')
23
- }
24
- )
@@ -1,71 +0,0 @@
1
- #!/usr/bin/env bun
2
- import {
3
- inspectStagedStep,
4
- qualifyStep,
5
- qualifyUpgradeStep,
6
- releasePlatform,
7
- selectRetainedStep,
8
- stageBuildStep,
9
- type ReleaseStepContext,
10
- } from '../release/release-steps'
11
- import { readJsonFile, runCli } from './cli-runner'
12
-
13
- // `release-step-cli.js <step> [--artifacts <dir>] <platform> [directory]`, run
14
- // in the app directory with RELEASE_TAG. The reusable release-assets workflow
15
- // appends the platform and staging directory to each `release:<step>` script.
16
- await runCli(
17
- (error) => `Release step failed: ${error instanceof Error ? error.message : String(error)}`,
18
- async () => {
19
- const args = process.argv.slice(2)
20
- const flag = args.indexOf('--artifacts')
21
- const artifacts = flag === -1 ? undefined : args.splice(flag, 2)[1]
22
- if (flag !== -1 && !artifacts) throw new Error('Expected a directory after --artifacts')
23
- const [step, platform, directory, ...extra] = args
24
- const tag = process.env.RELEASE_TAG
25
- if (!tag || extra.length)
26
- throw new Error('Expected <step> <platform> [directory] and RELEASE_TAG')
27
- const config = await readJsonFile('release-config.json', 262144)
28
- if (
29
- typeof config !== 'object' ||
30
- config === null ||
31
- typeof (config as { appName?: unknown }).appName !== 'string'
32
- )
33
- throw new Error('release-config.json names no app')
34
- const context: ReleaseStepContext = {
35
- env: process.env,
36
- config: config as ReleaseStepContext['config'],
37
- tag,
38
- platform: releasePlatform(platform),
39
- }
40
- if (step === 'select-retained') {
41
- if (directory) throw new Error('select-retained takes only the platform')
42
- return selectRetainedStep(context)
43
- }
44
- if (!directory) throw new Error('Expected a staging directory')
45
- switch (step) {
46
- case 'stage-build':
47
- console.log(JSON.stringify(await stageBuildStep(context, directory, artifacts)))
48
- return
49
- case 'inspect':
50
- console.log(JSON.stringify((await inspectStagedStep(context, directory)).identity))
51
- return
52
- case 'qualify':
53
- await qualifyStep(context, directory)
54
- console.log('Installer qualification receipt staged')
55
- return
56
- case 'qualify-upgrade': {
57
- const receipt = await qualifyUpgradeStep(context, directory, (stage) =>
58
- console.log(`upgrade: ${stage}`)
59
- )
60
- console.log(
61
- receipt
62
- ? `Upgraded ${receipt.from.tag} to ${tag} and observed the new version`
63
- : 'No earlier published release to upgrade from'
64
- )
65
- return
66
- }
67
- default:
68
- throw new Error(`Unknown release step ${String(step)}`)
69
- }
70
- }
71
- )
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env node
2
- import { verifyToolchain } from '../ci/toolchain-identity'
3
-
4
- // Run with node: the id's libc comes from Node's process report, as in the
5
- // `vp` config process that wrote it.
6
- const at = process.argv.indexOf('--expect')
7
- const expected = at === -1 ? undefined : process.argv[at + 1]
8
- if (!expected) {
9
- console.error('toolchain-identity: usage: adea-toolchain-identity --expect <id>')
10
- process.exit(2)
11
- }
12
- process.exit(verifyToolchain(expected))
@@ -1,12 +0,0 @@
1
- import { runUpdateHelper } from '../updates/update-helper'
2
-
3
- // Bundled standalone (dist/update-helper.bundle.js) so an app can ship it next
4
- // to its own bundle and run it with a copy of its Bun runtime.
5
- const index = process.argv.indexOf('--staging')
6
- const staging = index > 0 ? process.argv[index + 1] : undefined
7
- if (!staging) {
8
- console.error('Usage: update-helper --staging <directory>')
9
- process.exit(2)
10
- }
11
- const outcome = await runUpdateHelper({ staging }).catch(() => 'failed' as const)
12
- process.exit(outcome === 'updated' ? 0 : 1)
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { readFile, stat, writeFile } from 'node:fs/promises'
3
- import {
4
- patchPinnedWindowsRuntime,
5
- SECURE_WINDOWS_WRAPPER_SHA256,
6
- } from '../native/windows-runtime-patch'
7
- import { runCli } from './cli-runner'
8
-
9
- await runCli('Unable to patch the pinned unsigned Windows runtime.', async () => {
10
- const [input, output, ...extra] = process.argv.slice(2)
11
- if (!input || !output || extra.length) throw new Error('Invalid invocation')
12
- const info = await stat(input)
13
- if (!info.isFile() || info.size > 64 * 1024 * 1024) throw new Error('Invalid runtime')
14
- const patched = patchPinnedWindowsRuntime(await readFile(input))
15
- await writeFile(output, patched, { flag: 'wx', mode: 0o600 })
16
- process.stdout.write(`Patched Windows runtime SHA-256: ${SECURE_WINDOWS_WRAPPER_SHA256}\n`)
17
- })
@@ -1,43 +0,0 @@
1
- import {
2
- applicationCommandStream,
3
- applicationMenuCommands,
4
- } from '../shell/application-menu-commands'
5
-
6
- /** The shared-surface commands `ApplicationUtilities` provides. */
7
- export type ApplicationCommandTarget = Readonly<{
8
- openAbout(): void
9
- openSettings(): void
10
- openHelp(): void
11
- openUpdates(): void
12
- }>
13
-
14
- type StreamClient = {
15
- openStream(
16
- name: string,
17
- input: unknown,
18
- onMessage: (payload: string | ArrayBuffer) => void,
19
- onClose?: () => void
20
- ): Promise<{ close(): void }>
21
- }
22
-
23
- /** Route native menu commands from the desktop host to the shared surfaces. */
24
- export async function connectApplicationCommands(
25
- client: StreamClient,
26
- commands: ApplicationCommandTarget
27
- ): Promise<() => void> {
28
- const stream = await client.openStream(applicationCommandStream, null, (payload) => {
29
- if (typeof payload !== 'string' || payload.length > 64) return
30
- let command: unknown
31
- try {
32
- command = (JSON.parse(payload) as { command?: unknown }).command
33
- } catch {
34
- return
35
- }
36
- if (!(applicationMenuCommands as readonly unknown[]).includes(command)) return
37
- if (command === 'about') commands.openAbout()
38
- else if (command === 'settings') commands.openSettings()
39
- else if (command === 'help') commands.openHelp()
40
- else commands.openUpdates()
41
- })
42
- return () => stream.close()
43
- }
@@ -1,136 +0,0 @@
1
- // Adapted from adea-ai/adea's dev-runtime/channel/authority.ts at
2
- // eb11acdf14cb21a56bd90fdd9e6bdef9170e71d9 (Apache-2.0).
3
- import { randomBytes, randomUUID, timingSafeEqual } from 'node:crypto'
4
- import type { ChannelIdentity, VerifiedChannel } from './channel-proof'
5
-
6
- type Channel<Principal> = VerifiedChannel<Principal> & { credentialId: string; secret: Buffer }
7
- type Launch<Principal> = { secret: Buffer; expiresAt: number; principal: Principal }
8
-
9
- /**
10
- * In-process channel authority. Deliver bootstrap only through a trusted native
11
- * window and require origin screening separately. Never put bootstrap in an HTTP
12
- * document or treat browser headers as proof of window identity.
13
- */
14
- export function createChannelAuthority<Principal>(
15
- options: {
16
- now?: () => number
17
- bootstrapLifetimeMs?: number
18
- channelLifetimeMs?: number
19
- maxChannels?: number
20
- } = {}
21
- ) {
22
- const clock = options.now ?? Date.now
23
- const bootstrapLifetime = options.bootstrapLifetimeMs ?? 60000
24
- const channelLifetime = options.channelLifetimeMs ?? 12 * 60 * 60 * 1000
25
- const maxChannels = options.maxChannels ?? 4
26
- if (
27
- ![bootstrapLifetime, channelLifetime, maxChannels].every(
28
- (value) => Number.isSafeInteger(value) && value > 0
29
- )
30
- )
31
- throw new Error('Invalid channel authority configuration')
32
- let launch: Launch<Principal> | undefined
33
- const channels = new Map<string, Channel<Principal>>()
34
- const listeners = new Set<(identity: ChannelIdentity) => void>()
35
- function remove(id: string, channel: Channel<Principal>) {
36
- if (channels.get(id) !== channel) return
37
- channels.delete(id)
38
- channel.secret.fill(0)
39
- // Snapshot because listeners may add or remove subscriptions while revocation is delivered.
40
- // oxlint-disable-next-line unicorn/no-useless-spread
41
- for (const listener of [...listeners]) {
42
- try {
43
- listener({ channelId: id, credentialId: channel.credentialId })
44
- } catch {
45
- /* Revocation must not be interrupted by a listener. */
46
- }
47
- }
48
- }
49
- function time() {
50
- const at = clock()
51
- if (
52
- !Number.isSafeInteger(at) ||
53
- at < 0 ||
54
- at > Number.MAX_SAFE_INTEGER - Math.max(bootstrapLifetime, channelLifetime)
55
- )
56
- throw new Error('Invalid channel clock')
57
- return at
58
- }
59
- function clearLaunch() {
60
- launch?.secret.fill(0)
61
- launch = undefined
62
- }
63
- function sweep(at: number) {
64
- if (launch && launch.expiresAt <= at) clearLaunch()
65
- for (const [id, channel] of channels) if (channel.expiresAt <= at) remove(id, channel)
66
- }
67
- return {
68
- /** Invalidates an earlier unused bootstrap; the caller must keep the result secret. */
69
- issue(principal: Principal) {
70
- const at = time()
71
- sweep(at)
72
- clearLaunch()
73
- const secret = randomBytes(32)
74
- launch = { secret, expiresAt: at + bootstrapLifetime, principal }
75
- return { bootstrap: secret.toString('base64url'), expiresAt: launch.expiresAt }
76
- },
77
- handshake(bootstrap: string):
78
- | { ok: false }
79
- | {
80
- ok: true
81
- channelId: string
82
- credentialId: string
83
- clientSecret: string
84
- expiresAt: number
85
- } {
86
- const at = time()
87
- sweep(at)
88
- if (!launch || typeof bootstrap !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(bootstrap))
89
- return { ok: false }
90
- const supplied = Buffer.from(bootstrap, 'base64url')
91
- if (
92
- supplied.length !== launch.secret.length ||
93
- !timingSafeEqual(supplied, launch.secret) ||
94
- channels.size >= maxChannels
95
- )
96
- return { ok: false }
97
- const principal = launch.principal
98
- clearLaunch()
99
- const channelId = randomUUID()
100
- const credentialId = randomUUID()
101
- const secret = randomBytes(32)
102
- const expiresAt = at + channelLifetime
103
- channels.set(channelId, { credentialId, secret, expiresAt, principal })
104
- return {
105
- ok: true,
106
- channelId,
107
- credentialId,
108
- clientSecret: secret.toString('base64url'),
109
- expiresAt,
110
- }
111
- },
112
- resolve(identity: ChannelIdentity): VerifiedChannel<Principal> | undefined {
113
- const at = time()
114
- sweep(at)
115
- const channel = channels.get(identity.channelId)
116
- if (!channel || channel.credentialId !== identity.credentialId) return undefined
117
- return channel
118
- },
119
- revoke(identity: ChannelIdentity) {
120
- const channel = channels.get(identity.channelId)
121
- if (channel && channel.credentialId === identity.credentialId)
122
- remove(identity.channelId, channel)
123
- },
124
- revokeAll() {
125
- clearLaunch()
126
- for (const [id, channel] of channels) remove(id, channel)
127
- },
128
- /** Notify long-lived transports after a channel is removed. Expiry is swept on authority access. */
129
- onRevoke(listener: (identity: ChannelIdentity) => void) {
130
- listeners.add(listener)
131
- return () => {
132
- listeners.delete(listener)
133
- }
134
- },
135
- }
136
- }
@@ -1,122 +0,0 @@
1
- // Compatible with the legacy invoke proof in adea-ai/adea's channel/authority.ts
2
- // at eb11acdf14cb21a56bd90fdd9e6bdef9170e71d9 (Apache-2.0).
3
- import { createHash, createHmac, timingSafeEqual } from 'node:crypto'
4
- import { containsAsciiControl } from './string-policy'
5
-
6
- export type ChannelIdentity = Readonly<{ channelId: string; credentialId: string }>
7
- export type ProofFields = ChannelIdentity &
8
- Readonly<{ nonce: string; timestamp: string; bodySha256: string }>
9
- export type SignedRequest = ChannelIdentity &
10
- Readonly<{ nonce: string; timestamp: string; proof: string; body: string }>
11
- export type VerifiedChannel<Principal> = Readonly<{
12
- secret: Uint8Array
13
- expiresAt: number
14
- principal: Principal
15
- }>
16
- export type ProofResult<Principal> =
17
- | { ok: true; principal: Principal }
18
- | { ok: false; code: 'unauthenticated' | 'expired' | 'replay' | 'capacity' | 'invalid' }
19
- const separator = '\u001f'
20
- const field = (value: unknown): value is string =>
21
- typeof value === 'string' &&
22
- value.length > 0 &&
23
- value.length <= 128 &&
24
- !containsAsciiControl(value)
25
-
26
- /** Exact wire input used by Adea's current signed invoke bridge. */
27
- export function legacyProofMessage(input: ProofFields, context = 'adea-invoke-v1'): string {
28
- if (
29
- !field(context) ||
30
- !field(input.channelId) ||
31
- !field(input.credentialId) ||
32
- !field(input.nonce) ||
33
- !/^\d{1,16}$/.test(input.timestamp) ||
34
- !/^[a-f0-9]{64}$/.test(input.bodySha256)
35
- )
36
- throw new Error('Invalid proof fields')
37
- return [
38
- context,
39
- input.channelId,
40
- input.credentialId,
41
- input.nonce,
42
- input.timestamp,
43
- input.bodySha256,
44
- ].join(separator)
45
- }
46
-
47
- /**
48
- * Authenticate exact invoke bodies against an already established channel.
49
- * The host owns secure channel creation, credential delivery and revocation;
50
- * origin checks alone cannot supply a channel. This does not authorize services.
51
- */
52
- export function createChannelProofVerifier<Principal>(options: {
53
- resolve(identity: ChannelIdentity): VerifiedChannel<Principal> | undefined
54
- now?: () => number
55
- context?: string
56
- maxNonces?: number
57
- maxBodyBytes?: number
58
- }) {
59
- const now = options.now ?? Date.now
60
- const maxNonces = options.maxNonces ?? 4096
61
- const maxBodyBytes = options.maxBodyBytes ?? 256 * 1024
62
- const context = options.context ?? 'adea-invoke-v1'
63
- if (
64
- !field(context) ||
65
- !Number.isSafeInteger(maxNonces) ||
66
- maxNonces < 1 ||
67
- !Number.isSafeInteger(maxBodyBytes) ||
68
- maxBodyBytes < 1
69
- )
70
- throw new Error('Invalid channel proof configuration')
71
- const nonces = new Map<string, number>()
72
- return {
73
- verify(input: SignedRequest): ProofResult<Principal> {
74
- if (
75
- !field(input?.channelId) ||
76
- !field(input.credentialId) ||
77
- !field(input.nonce) ||
78
- typeof input.body !== 'string' ||
79
- Buffer.byteLength(input.body) > maxBodyBytes ||
80
- typeof input.timestamp !== 'string' ||
81
- !/^\d{1,16}$/.test(input.timestamp) ||
82
- typeof input.proof !== 'string' ||
83
- !/^[A-Za-z0-9_-]{43}$/.test(input.proof)
84
- )
85
- return { ok: false, code: 'invalid' }
86
- const at = now()
87
- const timestamp = Number(input.timestamp)
88
- if (
89
- !Number.isSafeInteger(at) ||
90
- !Number.isSafeInteger(timestamp) ||
91
- Math.abs(at - timestamp) > 30000
92
- )
93
- return { ok: false, code: 'expired' }
94
- let channel: VerifiedChannel<Principal> | undefined
95
- try {
96
- channel = options.resolve({ channelId: input.channelId, credentialId: input.credentialId })
97
- } catch {
98
- return { ok: false, code: 'unauthenticated' }
99
- }
100
- if (
101
- !channel ||
102
- !(channel.secret instanceof Uint8Array) ||
103
- channel.secret.byteLength !== 32 ||
104
- !Number.isSafeInteger(channel.expiresAt) ||
105
- channel.expiresAt <= at
106
- )
107
- return { ok: false, code: 'unauthenticated' }
108
- const bodySha256 = createHash('sha256').update(input.body, 'utf8').digest('hex')
109
- const message = legacyProofMessage({ ...input, bodySha256 }, context)
110
- const expected = createHmac('sha256', channel.secret).update(message, 'utf8').digest()
111
- const received = Buffer.from(input.proof, 'base64url')
112
- if (received.length !== expected.length || !timingSafeEqual(received, expected))
113
- return { ok: false, code: 'unauthenticated' }
114
- for (const [key, expiry] of nonces) if (expiry < at) nonces.delete(key)
115
- const key = `${input.channelId}\0${input.credentialId}\0${input.nonce}`
116
- if (nonces.has(key)) return { ok: false, code: 'replay' }
117
- if (nonces.size >= maxNonces) return { ok: false, code: 'capacity' }
118
- nonces.set(key, timestamp + 60000)
119
- return { ok: true, principal: channel.principal }
120
- },
121
- }
122
- }