@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.
- package/README.md +49 -1
- package/dist/cli/macos-signing-cli.d.ts +2 -0
- package/dist/cli/macos-signing-cli.js +14 -0
- package/dist/desktop/channel-proof.js +7 -1
- package/dist/framework-source.json +1 -1
- package/dist/release/framework-package-publish.js +4 -2
- package/dist/release/local-release.d.ts +2 -0
- package/dist/release/local-release.js +25 -2
- package/dist/release/macos-signing.d.ts +27 -0
- package/dist/release/macos-signing.js +101 -0
- package/dist/shell/app-shell.js +21 -33
- package/dist/shell/navigation-drawer.d.ts +12 -0
- package/dist/shell/navigation-drawer.js +42 -0
- package/dist/updates/release-feed.d.ts +2 -0
- package/dist/updates/release-feed.js +11 -2
- package/package.json +16 -4
- package/src/shell/app-shell.tsx +29 -20
- package/src/shell/navigation-drawer.tsx +36 -0
- package/src/app/create-application.ts +0 -245
- package/src/app/service-registry.ts +0 -124
- package/src/ci/toolchain-identity.ts +0 -244
- package/src/cli/cef-helper-cli.ts +0 -21
- package/src/cli/cli-runner.ts +0 -29
- package/src/cli/create-application-cli.ts +0 -62
- package/src/cli/electrobun-shortcut-patch-cli.ts +0 -24
- package/src/cli/framework-update-cli.ts +0 -41
- package/src/cli/release-assets-plan-cli.ts +0 -14
- package/src/cli/release-cut-cli.ts +0 -41
- package/src/cli/release-descriptor-staging-cli.ts +0 -57
- package/src/cli/release-finalize-cli.ts +0 -34
- package/src/cli/release-promote-cli.ts +0 -31
- package/src/cli/release-publish-cli.ts +0 -46
- package/src/cli/release-sign-cli.ts +0 -24
- package/src/cli/release-step-cli.ts +0 -71
- package/src/cli/toolchain-identity-cli.ts +0 -12
- package/src/cli/update-helper-cli.ts +0 -12
- package/src/cli/windows-runtime-patch-cli.ts +0 -17
- package/src/desktop/application-command-client.ts +0 -43
- package/src/desktop/channel-handshake.ts +0 -136
- package/src/desktop/channel-proof.ts +0 -122
- package/src/desktop/desktop-client.ts +0 -303
- package/src/desktop/desktop-gateway.ts +0 -189
- package/src/desktop/desktop-host.ts +0 -335
- package/src/desktop/desktop-stream.ts +0 -324
- package/src/desktop/loopback-policy.ts +0 -41
- package/src/desktop/open-url-channel.ts +0 -79
- package/src/desktop/string-policy.ts +0 -9
- package/src/installer/archive-entry.ts +0 -32
- package/src/installer/command-artifact.ts +0 -307
- package/src/installer/command-qualification-command.ts +0 -75
- package/src/installer/hosted-runner.ts +0 -11
- package/src/installer/installed-payload.ts +0 -408
- package/src/installer/installer-ci-bootstrap.ts +0 -199
- package/src/installer/installer-ci-preflight.ts +0 -61
- package/src/installer/installer-ci-worker.ts +0 -105
- package/src/installer/installer-extraction.ts +0 -367
- package/src/installer/installer-fetch-guard.ts +0 -75
- package/src/installer/installer-identity.ts +0 -331
- package/src/installer/installer-layout.ts +0 -167
- package/src/installer/installer-observation.ts +0 -671
- package/src/installer/installer-process-cleanup.ts +0 -167
- package/src/installer/installer-qualification-command.ts +0 -81
- package/src/installer/installer-renderer-evidence.ts +0 -268
- package/src/installer/installer-upgrade-command.ts +0 -226
- package/src/installer/installer-worker-guard.ts +0 -112
- package/src/installer/native-executable.ts +0 -37
- package/src/installer/upgrade-asset-download.ts +0 -72
- package/src/installer/write-all.ts +0 -19
- package/src/native/app-bundle.ts +0 -86
- package/src/native/cef-helper.ts +0 -471
- package/src/native/desktop-build-hook.ts +0 -21
- package/src/native/electrobun-source-patch.ts +0 -107
- package/src/native/linux-cef-links.ts +0 -14
- package/src/native/native-secret-store.ts +0 -99
- package/src/native/native-utilities.ts +0 -166
- package/src/native/partitioned-window.ts +0 -62
- package/src/native/secure-window.ts +0 -115
- package/src/native/sidecar-services.ts +0 -71
- package/src/native/sidecar.ts +0 -387
- package/src/native/window-bootstrap.ts +0 -77
- package/src/native/windows-build-hook-internal.ts +0 -49
- package/src/native/windows-build-hook.ts +0 -12
- package/src/native/windows-runtime-patch-internal.ts +0 -48
- package/src/native/windows-runtime-patch.ts +0 -15
- package/src/release/feed-release-cut.ts +0 -110
- package/src/release/feed-state.ts +0 -135
- package/src/release/framework-package-publish.ts +0 -163
- package/src/release/framework-update.ts +0 -196
- package/src/release/github-cli.ts +0 -27
- package/src/release/github-release-assets.ts +0 -36
- package/src/release/github-release-cut.ts +0 -237
- package/src/release/github-release-lookup.ts +0 -39
- package/src/release/github-release-promotion.ts +0 -112
- package/src/release/github-release-provenance.ts +0 -65
- package/src/release/github-release-publisher.ts +0 -90
- package/src/release/github-release-qualification.ts +0 -144
- package/src/release/local-provenance.ts +0 -92
- package/src/release/local-release.ts +0 -525
- package/src/release/macos-dmg.ts +0 -113
- package/src/release/release-artifact.ts +0 -53
- package/src/release/release-assets-plan.ts +0 -118
- package/src/release/release-config.ts +0 -420
- package/src/release/release-cut-command.ts +0 -80
- package/src/release/release-cut.ts +0 -133
- package/src/release/release-descriptor-staging.ts +0 -242
- package/src/release/release-finalize.ts +0 -191
- package/src/release/release-promotion-command.ts +0 -36
- package/src/release/release-promotion.ts +0 -132
- package/src/release/release-publication.ts +0 -213
- package/src/release/release-publish-command.ts +0 -163
- package/src/release/release-qualification.ts +0 -397
- package/src/release/release-staging.ts +0 -87
- package/src/release/release-steps.ts +0 -173
- package/src/release/retained-release-build.ts +0 -217
- package/src/shell/application-menu-commands.ts +0 -6
- package/src/shell/application-menu.ts +0 -250
- package/src/updates/channel-update-manager.ts +0 -127
- package/src/updates/desktop-updater.ts +0 -440
- package/src/updates/release-feed.ts +0 -187
- package/src/updates/release-location.ts +0 -101
- package/src/updates/signed-release.ts +0 -311
- package/src/updates/stable-file.ts +0 -32
- package/src/updates/update-arm.ts +0 -107
- package/src/updates/update-channel-store.ts +0 -86
- package/src/updates/update-discovery.ts +0 -113
- package/src/updates/update-download.ts +0 -82
- package/src/updates/update-exclusive-rename.ts +0 -58
- package/src/updates/update-extraction.ts +0 -261
- package/src/updates/update-handoff-plan.ts +0 -75
- package/src/updates/update-helper.ts +0 -87
- package/src/updates/update-launch.ts +0 -33
- package/src/updates/update-manager.ts +0 -130
- package/src/updates/update-process-fence.ts +0 -125
- package/src/updates/update-resume.ts +0 -34
- package/src/updates/update-shutdown.ts +0 -37
- package/src/updates/update-swap-stage.ts +0 -75
- package/src/updates/update-swap.ts +0 -30
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import { dlopen, FFIType, ptr } from 'bun:ffi'
|
|
2
|
-
import { execFile } from 'node:child_process'
|
|
3
|
-
import { realpath } from 'node:fs/promises'
|
|
4
|
-
import { promisify } from 'node:util'
|
|
5
|
-
import { assertInstallerContext, type InstallerPlatform } from './installer-layout'
|
|
6
|
-
const execute = promisify(execFile)
|
|
7
|
-
/** Terminate only the verified app runtime process on a disposable runner. */
|
|
8
|
-
export async function terminateGuardedWorker(options: {
|
|
9
|
-
pid: number
|
|
10
|
-
runtime: string
|
|
11
|
-
platform: InstallerPlatform
|
|
12
|
-
env: Record<string, string | undefined>
|
|
13
|
-
}): Promise<void> {
|
|
14
|
-
const { pid, runtime, platform } = options
|
|
15
|
-
assertInstallerContext(options.env, platform, process.platform, process.arch)
|
|
16
|
-
if (!Number.isSafeInteger(pid) || pid < 2 || pid === process.pid)
|
|
17
|
-
throw new Error('Invalid guarded process identity')
|
|
18
|
-
const env: Record<string, string> = {}
|
|
19
|
-
for (const name of [
|
|
20
|
-
'PATH',
|
|
21
|
-
'SystemRoot',
|
|
22
|
-
'WINDIR',
|
|
23
|
-
'COMSPEC',
|
|
24
|
-
'PATHEXT',
|
|
25
|
-
'TMP',
|
|
26
|
-
'TEMP',
|
|
27
|
-
'TMPDIR',
|
|
28
|
-
'LANG',
|
|
29
|
-
'LC_ALL',
|
|
30
|
-
])
|
|
31
|
-
if (options.env[name]) env[name] = options.env[name]!
|
|
32
|
-
const expected = await realpath(runtime)
|
|
33
|
-
const actual = await runningExecutable(pid, platform, env)
|
|
34
|
-
// A worker that exited on its own is already cleaned up.
|
|
35
|
-
if (actual === undefined) return
|
|
36
|
-
// A macOS worker already exiting on its own has released its executable; it needs no
|
|
37
|
-
// signal, only time to finish.
|
|
38
|
-
if (actual !== EXITING) {
|
|
39
|
-
if (
|
|
40
|
-
platform === 'windows-x64'
|
|
41
|
-
? actual.toLowerCase() !== expected.toLowerCase()
|
|
42
|
-
: actual !== expected
|
|
43
|
-
)
|
|
44
|
-
throw new Error('Guarded process executable mismatch')
|
|
45
|
-
process.kill(pid, 'SIGKILL')
|
|
46
|
-
}
|
|
47
|
-
const deadline = Date.now() + 5000
|
|
48
|
-
while (Date.now() < deadline) {
|
|
49
|
-
// The executable was verified before the signal. On macOS its command may
|
|
50
|
-
// become unavailable while exit is in progress, before the process is a
|
|
51
|
-
// zombie; only the state is needed to observe completion here.
|
|
52
|
-
if (
|
|
53
|
-
platform === 'macos-arm64'
|
|
54
|
-
? await macosWorkerExited(() => readMacosState(pid, env))
|
|
55
|
-
: (await runningExecutable(pid, platform, env)) === undefined
|
|
56
|
-
)
|
|
57
|
-
return
|
|
58
|
-
await Bun.sleep(100)
|
|
59
|
-
}
|
|
60
|
-
throw new Error('Guarded process did not exit')
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const EXITING = Symbol('exiting')
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Read one `ps -o stat= -o comm=` line. macOS reports the command of a process that is
|
|
67
|
-
* exiting (`E`) or an unreaped zombie (`Z`) as `(bun)` or `<defunct>`, not an executable
|
|
68
|
-
* path. A zombie has already exited and cannot be killed; an exiting process is pending.
|
|
69
|
-
*/
|
|
70
|
-
export function macosProcess(line: string): string | typeof EXITING | undefined {
|
|
71
|
-
const match = /^(\S+)\s+(.+)$/.exec(line.trim())
|
|
72
|
-
if (!match) throw new Error('Unable to query guarded process state')
|
|
73
|
-
const [, state, command] = match as unknown as [string, string, string]
|
|
74
|
-
if (state.startsWith('Z')) return undefined
|
|
75
|
-
if (state.includes('E') || !command.startsWith('/')) return EXITING
|
|
76
|
-
return command
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** An absent or zombie process has exited; an exiting process is still pending. */
|
|
80
|
-
export async function macosWorkerExited(readState: () => Promise<string | undefined>) {
|
|
81
|
-
const state = (await readState())?.trim()
|
|
82
|
-
if (state === undefined) return true
|
|
83
|
-
if (!/^\S+$/.test(state)) throw new Error('Unable to query guarded process state')
|
|
84
|
-
return state.startsWith('Z')
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function readMacosState(pid: number, env: Record<string, string>) {
|
|
88
|
-
const listed = await execute('/bin/ps', ['-p', String(pid), '-o', 'stat='], {
|
|
89
|
-
env,
|
|
90
|
-
timeout: 5000,
|
|
91
|
-
maxBuffer: 16384,
|
|
92
|
-
}).catch((error: { code?: unknown }) => {
|
|
93
|
-
if (error.code === 1) return undefined
|
|
94
|
-
throw error
|
|
95
|
-
})
|
|
96
|
-
return listed?.stdout
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* The executable of a running process, or undefined once it has exited. On Windows
|
|
101
|
-
* an exited process stays openable while another process holds a handle to it (the
|
|
102
|
-
* Electrobun extractor that launched it), so a signal-0 probe would still report it
|
|
103
|
-
* alive; its exit code tells the truth.
|
|
104
|
-
*/
|
|
105
|
-
async function runningExecutable(
|
|
106
|
-
pid: number,
|
|
107
|
-
platform: InstallerPlatform,
|
|
108
|
-
env: Record<string, string>
|
|
109
|
-
): Promise<string | typeof EXITING | undefined> {
|
|
110
|
-
if (platform === 'linux-x64')
|
|
111
|
-
return realpath(`/proc/${pid}/exe`).catch((error: NodeJS.ErrnoException) => {
|
|
112
|
-
if (error.code === 'ENOENT') return undefined
|
|
113
|
-
throw error
|
|
114
|
-
})
|
|
115
|
-
if (platform === 'macos-arm64') {
|
|
116
|
-
const listed = await execute('/bin/ps', ['-p', String(pid), '-o', 'stat=', '-o', 'comm='], {
|
|
117
|
-
env,
|
|
118
|
-
timeout: 5000,
|
|
119
|
-
maxBuffer: 16384,
|
|
120
|
-
}).catch((error: { code?: unknown }) => {
|
|
121
|
-
// ps exits 1 when no process matches.
|
|
122
|
-
if (error.code === 1) return undefined
|
|
123
|
-
throw error
|
|
124
|
-
})
|
|
125
|
-
if (!listed) return undefined
|
|
126
|
-
const state = macosProcess(listed.stdout)
|
|
127
|
-
return state === EXITING || state === undefined ? state : realpath(state)
|
|
128
|
-
}
|
|
129
|
-
return windowsRunningExecutable(pid)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const PROCESS_QUERY_LIMITED_INFORMATION = 0x1000,
|
|
133
|
-
STILL_ACTIVE = 259
|
|
134
|
-
|
|
135
|
-
/** Ask the kernel directly: an exited process keeps no STILL_ACTIVE exit code even while
|
|
136
|
-
* another process holds a handle to it. */
|
|
137
|
-
async function windowsRunningExecutable(pid: number): Promise<string | undefined> {
|
|
138
|
-
const kernel = dlopen('kernel32.dll', {
|
|
139
|
-
OpenProcess: { args: [FFIType.u32, FFIType.i32, FFIType.u32], returns: FFIType.ptr },
|
|
140
|
-
GetExitCodeProcess: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
|
|
141
|
-
QueryFullProcessImageNameW: {
|
|
142
|
-
args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr],
|
|
143
|
-
returns: FFIType.i32,
|
|
144
|
-
},
|
|
145
|
-
CloseHandle: { args: [FFIType.ptr], returns: FFIType.i32 },
|
|
146
|
-
})
|
|
147
|
-
try {
|
|
148
|
-
const handle = kernel.symbols.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid)
|
|
149
|
-
// No such process, or a process id that was already released.
|
|
150
|
-
if (!handle) return undefined
|
|
151
|
-
try {
|
|
152
|
-
const code = new Uint32Array(1)
|
|
153
|
-
if (!kernel.symbols.GetExitCodeProcess(handle, ptr(code)))
|
|
154
|
-
throw new Error('Unable to query guarded process state')
|
|
155
|
-
if (code[0] !== STILL_ACTIVE) return undefined
|
|
156
|
-
const name = new Uint16Array(32768),
|
|
157
|
-
length = new Uint32Array([name.length])
|
|
158
|
-
if (!kernel.symbols.QueryFullProcessImageNameW(handle, 0, ptr(name), ptr(length)))
|
|
159
|
-
throw new Error('Unable to query guarded process executable')
|
|
160
|
-
return realpath(Buffer.from(name.buffer, 0, length[0]! * 2).toString('utf16le'))
|
|
161
|
-
} finally {
|
|
162
|
-
kernel.symbols.CloseHandle(handle)
|
|
163
|
-
}
|
|
164
|
-
} finally {
|
|
165
|
-
kernel.close()
|
|
166
|
-
}
|
|
167
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { lstat, writeFile } from 'node:fs/promises'
|
|
2
|
-
import { join, resolve } from 'node:path'
|
|
3
|
-
import { observeInstalledApp } from './installer-observation'
|
|
4
|
-
import { planReleaseAssets } from '../release/release-assets-plan'
|
|
5
|
-
import { parseReleaseConfig } from '../release/release-config'
|
|
6
|
-
import { validateInstallerReceipt, type InstallerReceipt } from '../release/release-qualification'
|
|
7
|
-
import type { InstallerPlatform, InstallerRenderer } from './installer-layout'
|
|
8
|
-
|
|
9
|
-
/** Qualify one platform in the trusted release-assets workflow and stage its receipt. */
|
|
10
|
-
export async function qualifyInstallerCommand(options: {
|
|
11
|
-
env: Record<string, string | undefined>
|
|
12
|
-
config: unknown
|
|
13
|
-
tag: string
|
|
14
|
-
platform: InstallerPlatform
|
|
15
|
-
renderer: InstallerRenderer
|
|
16
|
-
directory: string
|
|
17
|
-
}): Promise<InstallerReceipt> {
|
|
18
|
-
const { env, config, tag, platform, renderer } = options
|
|
19
|
-
if (
|
|
20
|
-
!['macos-arm64', 'windows-x64', 'linux-x64'].includes(platform) ||
|
|
21
|
-
!['native', 'cef'].includes(renderer) ||
|
|
22
|
-
typeof config !== 'object' ||
|
|
23
|
-
config === null ||
|
|
24
|
-
Array.isArray(config) ||
|
|
25
|
-
typeof (config as { appName?: unknown }).appName !== 'string' ||
|
|
26
|
-
typeof (config as { appId?: unknown }).appId !== 'string'
|
|
27
|
-
)
|
|
28
|
-
throw new Error('Invalid installer qualification context')
|
|
29
|
-
if (env.RELEASE_TAG !== tag) throw new Error('Installer release tag mismatch')
|
|
30
|
-
const plan = await planReleaseAssets({ env, config, tag })
|
|
31
|
-
if (env.SOURCE_SHA !== plan.sourceSha) throw new Error('Installer source identity mismatch')
|
|
32
|
-
const parsed = parseReleaseConfig(config),
|
|
33
|
-
requirements = parsed.requirements(tag),
|
|
34
|
-
policy = requirements.offlinePolicy,
|
|
35
|
-
names = requirements.platforms[platform]
|
|
36
|
-
if (!policy || !names || names.length !== 2)
|
|
37
|
-
throw new Error('Installer qualification requires an explicit offline policy and payloads')
|
|
38
|
-
const directory = resolve(options.directory),
|
|
39
|
-
destination = join(directory, `installer-test-${platform}.json`)
|
|
40
|
-
try {
|
|
41
|
-
await lstat(destination)
|
|
42
|
-
throw new Error('Installer receipt already exists')
|
|
43
|
-
} catch (error) {
|
|
44
|
-
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
|
|
45
|
-
}
|
|
46
|
-
const app = config as { appName: string; appId: string }
|
|
47
|
-
const result = await observeInstalledApp({
|
|
48
|
-
env,
|
|
49
|
-
platform,
|
|
50
|
-
renderer,
|
|
51
|
-
appName: app.appName,
|
|
52
|
-
appId: app.appId,
|
|
53
|
-
version: plan.version,
|
|
54
|
-
installer: join(directory, names[0]!),
|
|
55
|
-
archive: join(directory, names[1]!),
|
|
56
|
-
expectedReads: parsed.expectedReads,
|
|
57
|
-
offlinePolicy: policy,
|
|
58
|
-
sidecars: parsed.sidecars,
|
|
59
|
-
})
|
|
60
|
-
if (!result.evidence) throw new Error('Missing measured offline evidence')
|
|
61
|
-
const identity = {
|
|
62
|
-
tag,
|
|
63
|
-
sourceSha: plan.sourceSha,
|
|
64
|
-
workflowSha: plan.workflowSha,
|
|
65
|
-
runId: env.GITHUB_RUN_ID!,
|
|
66
|
-
runAttempt: Number(env.GITHUB_RUN_ATTEMPT),
|
|
67
|
-
}
|
|
68
|
-
const receipt: InstallerReceipt = {
|
|
69
|
-
schema: 2,
|
|
70
|
-
...identity,
|
|
71
|
-
platform,
|
|
72
|
-
installed: true,
|
|
73
|
-
launched: true,
|
|
74
|
-
rendererReady: true,
|
|
75
|
-
rendererEvidence: result.evidence,
|
|
76
|
-
assets: result.artifacts,
|
|
77
|
-
}
|
|
78
|
-
validateInstallerReceipt(identity, receipt, platform, names, result.artifacts, policy)
|
|
79
|
-
await writeFile(destination, JSON.stringify(receipt, null, 2) + '\n', { flag: 'wx', mode: 0o600 })
|
|
80
|
-
return receipt
|
|
81
|
-
}
|
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
import { parseOfflinePolicy, type OfflinePolicy } from '../release/release-qualification'
|
|
2
|
-
import { assertHostedRunner } from './hosted-runner'
|
|
3
|
-
|
|
4
|
-
function safeScriptString(value: string): string {
|
|
5
|
-
return JSON.stringify(value)
|
|
6
|
-
.replaceAll('<', '\\u003c')
|
|
7
|
-
.replaceAll('\u2028', '\\u2028')
|
|
8
|
-
.replaceAll('\u2029', '\\u2029')
|
|
9
|
-
}
|
|
10
|
-
/** Renderer observations only. Installer identity, startup guard and offline behavior
|
|
11
|
-
* must be independently verified before an installer receipt can be issued. */
|
|
12
|
-
export type InstallerRendererObservation = {
|
|
13
|
-
samples: number
|
|
14
|
-
elapsedMs: number
|
|
15
|
-
visible: boolean
|
|
16
|
-
readyState: string
|
|
17
|
-
width: number
|
|
18
|
-
height: number
|
|
19
|
-
interactiveElements: number
|
|
20
|
-
textLength: number
|
|
21
|
-
markerObserved?: boolean
|
|
22
|
-
foreignBlocked?: boolean
|
|
23
|
-
}
|
|
24
|
-
export function assertInstallerRendererObservation(
|
|
25
|
-
value: unknown,
|
|
26
|
-
offline = false
|
|
27
|
-
): asserts value is InstallerRendererObservation {
|
|
28
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
29
|
-
throw new Error('Invalid renderer observation')
|
|
30
|
-
const item = value as Record<string, unknown>
|
|
31
|
-
const keys = [
|
|
32
|
-
'samples',
|
|
33
|
-
'elapsedMs',
|
|
34
|
-
'visible',
|
|
35
|
-
'readyState',
|
|
36
|
-
'width',
|
|
37
|
-
'height',
|
|
38
|
-
'interactiveElements',
|
|
39
|
-
'textLength',
|
|
40
|
-
...(offline ? ['markerObserved', 'foreignBlocked'] : []),
|
|
41
|
-
]
|
|
42
|
-
if (
|
|
43
|
-
(offline && (item.markerObserved !== true || item.foreignBlocked !== true)) ||
|
|
44
|
-
Object.keys(item).length !== keys.length ||
|
|
45
|
-
keys.some((key) => !Object.hasOwn(item, key)) ||
|
|
46
|
-
item.samples !== 2 ||
|
|
47
|
-
typeof item.elapsedMs !== 'number' ||
|
|
48
|
-
!Number.isFinite(item.elapsedMs) ||
|
|
49
|
-
item.elapsedMs < 3000 ||
|
|
50
|
-
item.elapsedMs > 120000 ||
|
|
51
|
-
item.visible !== true ||
|
|
52
|
-
!['interactive', 'complete'].includes(String(item.readyState)) ||
|
|
53
|
-
typeof item.width !== 'number' ||
|
|
54
|
-
!Number.isFinite(item.width) ||
|
|
55
|
-
item.width < 100 ||
|
|
56
|
-
typeof item.height !== 'number' ||
|
|
57
|
-
!Number.isFinite(item.height) ||
|
|
58
|
-
item.height < 100 ||
|
|
59
|
-
!Number.isSafeInteger(item.interactiveElements) ||
|
|
60
|
-
Number(item.interactiveElements) < 1 ||
|
|
61
|
-
!Number.isSafeInteger(item.textLength) ||
|
|
62
|
-
Number(item.textLength) < 1
|
|
63
|
-
)
|
|
64
|
-
throw new Error('Incomplete renderer observation')
|
|
65
|
-
}
|
|
66
|
-
async function boundedBody(message: Request | Response, maximum: number): Promise<string> {
|
|
67
|
-
const declared = message.headers.get('content-length')
|
|
68
|
-
if (declared !== null && (!/^\d+$/.test(declared) || Number(declared) > maximum))
|
|
69
|
-
throw new Error('Renderer body exceeds bound')
|
|
70
|
-
if (!message.body) throw new Error('Missing renderer body')
|
|
71
|
-
const reader = message.body.getReader(),
|
|
72
|
-
chunks: Uint8Array[] = []
|
|
73
|
-
let size = 0
|
|
74
|
-
try {
|
|
75
|
-
while (true) {
|
|
76
|
-
const next = await reader.read()
|
|
77
|
-
if (next.done) break
|
|
78
|
-
size += next.value.length
|
|
79
|
-
if (size > maximum) {
|
|
80
|
-
await reader.cancel()
|
|
81
|
-
throw new Error('Renderer body exceeds bound')
|
|
82
|
-
}
|
|
83
|
-
chunks.push(next.value)
|
|
84
|
-
}
|
|
85
|
-
} finally {
|
|
86
|
-
reader.releaseLock()
|
|
87
|
-
}
|
|
88
|
-
const bytes = new Uint8Array(size)
|
|
89
|
-
let offset = 0
|
|
90
|
-
for (const chunk of chunks) {
|
|
91
|
-
bytes.set(chunk, offset)
|
|
92
|
-
offset += chunk.length
|
|
93
|
-
}
|
|
94
|
-
return new TextDecoder('utf-8', { fatal: true }).decode(bytes)
|
|
95
|
-
}
|
|
96
|
-
function probeScript(nonce: string, policy?: OfflinePolicy, foreignOrigin?: string): string {
|
|
97
|
-
const offline = policy
|
|
98
|
-
? `data.markerObserved=(d.body?.innerText||'').includes(${safeScriptString(policy.marker)});try{await fetch(${safeScriptString(foreignOrigin!)}+'/',{mode:'no-cors',cache:'no-store'});data.foreignBlocked=false}catch{data.foreignBlocked=true}`
|
|
99
|
-
: ''
|
|
100
|
-
return `<script>(()=>{const started=performance.now();requestAnimationFrame(()=>{setTimeout(()=>requestAnimationFrame(async()=>{const d=document;const data={samples:2,elapsedMs:performance.now()-started,visible:d.visibilityState==='visible',readyState:d.readyState,width:innerWidth,height:innerHeight,interactiveElements:d.querySelectorAll('button,input,textarea,select,a[href],[role="button"]').length,textLength:(d.body?.innerText||'').trim().length};${offline};fetch('/__framework_installer_probe/${nonce}',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify(data),redirect:'error'}).catch(()=>{});}),3100);});})();</script>`
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Wrap the installed host's handler on the nonce-bound loopback origin. */
|
|
104
|
-
export function createInstallerRendererHandler(options: {
|
|
105
|
-
env: Record<string, string | undefined>
|
|
106
|
-
origin: string
|
|
107
|
-
nonce: string
|
|
108
|
-
offlinePolicy?: OfflinePolicy
|
|
109
|
-
foreignOrigin?: string
|
|
110
|
-
handle(request: Request): Response | Promise<Response>
|
|
111
|
-
save(
|
|
112
|
-
value: InstallerRendererObservation,
|
|
113
|
-
state: { bootstrapDenied: boolean }
|
|
114
|
-
): void | Promise<void>
|
|
115
|
-
}): (request: Request) => Promise<Response> {
|
|
116
|
-
assertHostedRunner(options.env, 'Renderer probe')
|
|
117
|
-
const origin = new URL(options.origin)
|
|
118
|
-
if (
|
|
119
|
-
origin.protocol !== 'http:' ||
|
|
120
|
-
origin.hostname !== '127.0.0.1' ||
|
|
121
|
-
!origin.port ||
|
|
122
|
-
origin.origin !== options.origin ||
|
|
123
|
-
!/^[a-f0-9]{48}$/.test(options.nonce)
|
|
124
|
-
)
|
|
125
|
-
throw new Error('Invalid renderer probe context')
|
|
126
|
-
const policy =
|
|
127
|
-
options.offlinePolicy === undefined ? undefined : parseOfflinePolicy(options.offlinePolicy)
|
|
128
|
-
if (policy) {
|
|
129
|
-
if (!options.foreignOrigin) throw new Error('Missing foreign network sentinel')
|
|
130
|
-
const foreign = new URL(options.foreignOrigin)
|
|
131
|
-
if (
|
|
132
|
-
foreign.protocol !== 'http:' ||
|
|
133
|
-
foreign.hostname !== '127.0.0.1' ||
|
|
134
|
-
!foreign.port ||
|
|
135
|
-
foreign.origin !== options.foreignOrigin ||
|
|
136
|
-
foreign.origin === origin.origin
|
|
137
|
-
)
|
|
138
|
-
throw new Error('Invalid foreign network sentinel')
|
|
139
|
-
} else if (options.foreignOrigin !== undefined)
|
|
140
|
-
throw new Error('Unexpected foreign network sentinel')
|
|
141
|
-
const path = `/__framework_installer_probe/${options.nonce}`
|
|
142
|
-
let documentServed = false,
|
|
143
|
-
received = false,
|
|
144
|
-
bootstrapDenied = false
|
|
145
|
-
return async (request) => {
|
|
146
|
-
const url = new URL(request.url)
|
|
147
|
-
if (url.origin !== options.origin || request.headers.get('host') !== origin.host)
|
|
148
|
-
return new Response(null, { status: 403 })
|
|
149
|
-
if (policy?.kind === 'local-bootstrap' && url.pathname === policy.path) {
|
|
150
|
-
if (
|
|
151
|
-
request.headers.get('origin') === options.origin &&
|
|
152
|
-
[null, 'same-origin'].includes(request.headers.get('sec-fetch-site'))
|
|
153
|
-
)
|
|
154
|
-
bootstrapDenied = true
|
|
155
|
-
return new Response(null, { status: 503 })
|
|
156
|
-
}
|
|
157
|
-
if (url.pathname.startsWith('/__framework_installer_probe/')) {
|
|
158
|
-
if (
|
|
159
|
-
url.pathname !== path ||
|
|
160
|
-
url.search ||
|
|
161
|
-
request.method !== 'POST' ||
|
|
162
|
-
request.headers.get('origin') !== options.origin ||
|
|
163
|
-
![null, 'same-origin'].includes(request.headers.get('sec-fetch-site')) ||
|
|
164
|
-
request.headers.get('content-type') !== 'application/json' ||
|
|
165
|
-
!documentServed ||
|
|
166
|
-
received
|
|
167
|
-
)
|
|
168
|
-
return new Response(null, { status: 403 })
|
|
169
|
-
received = true
|
|
170
|
-
try {
|
|
171
|
-
const value: unknown = JSON.parse(await boundedBody(request, 4096))
|
|
172
|
-
assertInstallerRendererObservation(value, Boolean(policy))
|
|
173
|
-
await options.save(value, { bootstrapDenied })
|
|
174
|
-
return new Response(null, { status: 204 })
|
|
175
|
-
} catch {
|
|
176
|
-
return new Response(null, { status: 400 })
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
const response = await options.handle(request)
|
|
180
|
-
if (
|
|
181
|
-
request.method !== 'GET' ||
|
|
182
|
-
url.pathname !== '/' ||
|
|
183
|
-
url.search ||
|
|
184
|
-
response.status !== 200 ||
|
|
185
|
-
!/^text\/html\b/i.test(response.headers.get('content-type') ?? '')
|
|
186
|
-
)
|
|
187
|
-
return response
|
|
188
|
-
if (response.headers.has('content-encoding'))
|
|
189
|
-
throw new Error('Encoded renderer documents are unsupported')
|
|
190
|
-
const html = await boundedBody(response, 2 * 1024 * 1024)
|
|
191
|
-
if (!html.includes('</body>')) throw new Error('Invalid renderer document')
|
|
192
|
-
documentServed = true
|
|
193
|
-
const headers = new Headers(response.headers)
|
|
194
|
-
headers.delete('content-length')
|
|
195
|
-
headers.delete('content-encoding')
|
|
196
|
-
headers.delete('etag')
|
|
197
|
-
// Add a separate restrictive policy; never replace the app's own CSP.
|
|
198
|
-
headers.append(
|
|
199
|
-
'content-security-policy',
|
|
200
|
-
"default-src 'self' data:; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data: blob:; media-src 'self' data: blob:; font-src 'self' data:; frame-src 'none'; object-src 'none'; base-uri 'none'"
|
|
201
|
-
)
|
|
202
|
-
return new Response(
|
|
203
|
-
html.replace(
|
|
204
|
-
'</body>',
|
|
205
|
-
probeScript(options.nonce, policy, options.foreignOrigin) + '</body>'
|
|
206
|
-
),
|
|
207
|
-
{ status: response.status, statusText: response.statusText, headers }
|
|
208
|
-
)
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/** Intercept the one expected Bun.serve listener before importing the app worker. */
|
|
213
|
-
export function installInstallerRendererProbe(options: {
|
|
214
|
-
env: Record<string, string | undefined>
|
|
215
|
-
origin: string
|
|
216
|
-
nonce: string
|
|
217
|
-
allowEphemeral?: boolean
|
|
218
|
-
offlinePolicy?: OfflinePolicy
|
|
219
|
-
foreignOrigin?: string
|
|
220
|
-
runtime: { serve: (...args: any[]) => any }
|
|
221
|
-
save(
|
|
222
|
-
value: InstallerRendererObservation,
|
|
223
|
-
state: { bootstrapDenied: boolean }
|
|
224
|
-
): void | Promise<void>
|
|
225
|
-
}): void {
|
|
226
|
-
const { runtime } = options
|
|
227
|
-
const original = runtime.serve
|
|
228
|
-
if (typeof original !== 'function') throw new Error('Missing Bun server implementation')
|
|
229
|
-
let used = false
|
|
230
|
-
const port = Number(new URL(options.origin).port)
|
|
231
|
-
const replace = function (config: {
|
|
232
|
-
hostname?: unknown
|
|
233
|
-
port?: unknown
|
|
234
|
-
fetch?: unknown
|
|
235
|
-
[key: string]: unknown
|
|
236
|
-
}) {
|
|
237
|
-
if (
|
|
238
|
-
!config ||
|
|
239
|
-
config.hostname !== '127.0.0.1' ||
|
|
240
|
-
(Number(config.port) !== port && !(options.allowEphemeral === true && config.port === 0))
|
|
241
|
-
)
|
|
242
|
-
return original.call(runtime, config)
|
|
243
|
-
if (used || typeof config.fetch !== 'function')
|
|
244
|
-
throw new Error('Unexpected renderer probe listener')
|
|
245
|
-
used = true
|
|
246
|
-
const fetch = config.fetch as (
|
|
247
|
-
request: Request,
|
|
248
|
-
server: unknown
|
|
249
|
-
) => Response | Promise<Response>
|
|
250
|
-
let activeServer: unknown
|
|
251
|
-
const handler = createInstallerRendererHandler({
|
|
252
|
-
...options,
|
|
253
|
-
handle: (request) => fetch(request, activeServer),
|
|
254
|
-
})
|
|
255
|
-
return original.call(runtime, {
|
|
256
|
-
...config,
|
|
257
|
-
port,
|
|
258
|
-
fetch(request: Request, server: unknown) {
|
|
259
|
-
activeServer = server
|
|
260
|
-
return handler(request)
|
|
261
|
-
},
|
|
262
|
-
})
|
|
263
|
-
}
|
|
264
|
-
// Validate the full context before changing Bun.serve, even if no listener is opened.
|
|
265
|
-
createInstallerRendererHandler({ ...options, handle: () => new Response() })
|
|
266
|
-
if (!Reflect.set(runtime, 'serve', replace) || runtime.serve !== replace)
|
|
267
|
-
throw new Error('Cannot install renderer probe')
|
|
268
|
-
}
|