@adea-ai/app-framework 0.0.0-stage → 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.
- package/LICENSE +201 -0
- package/NOTICE +30 -0
- package/README.md +1009 -2
- package/dist/app/application.d.ts +47 -0
- package/dist/app/application.js +115 -0
- package/dist/app/create-application.d.ts +13 -0
- package/dist/app/create-application.js +178 -0
- package/dist/app/service-registry.d.ts +44 -0
- package/dist/app/service-registry.js +116 -0
- package/dist/appearance/appearance-host.d.ts +62 -0
- package/dist/appearance/appearance-host.js +115 -0
- package/dist/appearance/appearance-preferences.d.ts +106 -0
- package/dist/appearance/appearance-preferences.js +175 -0
- package/dist/appearance/composition.d.ts +11 -0
- package/dist/appearance/composition.js +16 -0
- package/dist/appearance/custom-theme-import.d.ts +14 -0
- package/dist/appearance/custom-theme-import.js +315 -0
- package/dist/appearance/custom-theme-library.d.ts +146 -0
- package/dist/appearance/custom-theme-library.js +378 -0
- package/dist/appearance/editor.d.ts +20 -0
- package/dist/appearance/editor.js +47 -0
- package/dist/appearance/theme-file-importer.d.ts +23 -0
- package/dist/appearance/theme-file-importer.js +66 -0
- package/dist/appearance/theme-record-adapter.d.ts +26 -0
- package/dist/appearance/theme-record-adapter.js +64 -0
- package/dist/appearance/theme-registry.d.ts +10 -0
- package/dist/appearance/theme-registry.js +16 -0
- package/dist/appearance/window-surface.d.ts +5 -0
- package/dist/appearance/window-surface.js +9 -0
- package/dist/ci/toolchain-identity.d.ts +39 -0
- package/dist/ci/toolchain-identity.js +196 -0
- package/dist/cli/cef-helper-cli.d.ts +2 -0
- package/dist/cli/cef-helper-cli.js +18 -0
- package/dist/cli/cli-runner.d.ts +8 -0
- package/dist/cli/cli-runner.js +23 -0
- package/dist/cli/create-application-cli.d.ts +2 -0
- package/dist/cli/create-application-cli.js +40 -0
- package/dist/cli/electrobun-shortcut-patch-cli.d.ts +2 -0
- package/dist/cli/electrobun-shortcut-patch-cli.js +17 -0
- package/dist/cli/framework-update-cli.d.ts +2 -0
- package/dist/cli/framework-update-cli.js +40 -0
- package/dist/cli/release-assets-plan-cli.d.ts +2 -0
- package/dist/cli/release-assets-plan-cli.js +14 -0
- package/dist/cli/release-cut-cli.d.ts +2 -0
- package/dist/cli/release-cut-cli.js +27 -0
- package/dist/cli/release-descriptor-staging-cli.d.ts +2 -0
- package/dist/cli/release-descriptor-staging-cli.js +45 -0
- package/dist/cli/release-finalize-cli.d.ts +1 -0
- package/dist/cli/release-finalize-cli.js +30 -0
- package/dist/cli/release-promote-cli.d.ts +2 -0
- package/dist/cli/release-promote-cli.js +32 -0
- package/dist/cli/release-publish-cli.d.ts +2 -0
- package/dist/cli/release-publish-cli.js +48 -0
- package/dist/cli/release-sign-cli.d.ts +2 -0
- package/dist/cli/release-sign-cli.js +13 -0
- package/dist/cli/release-step-cli.d.ts +2 -0
- package/dist/cli/release-step-cli.js +44 -0
- package/dist/cli/toolchain-identity-cli.d.ts +2 -0
- package/dist/cli/toolchain-identity-cli.js +10 -0
- package/dist/cli/update-helper-cli.d.ts +1 -0
- package/dist/cli/update-helper-cli.js +10 -0
- package/dist/cli/windows-runtime-patch-cli.d.ts +2 -0
- package/dist/cli/windows-runtime-patch-cli.js +17 -0
- package/dist/desktop/application-command-client.d.ts +15 -0
- package/dist/desktop/application-command-client.js +21 -0
- package/dist/desktop/channel-handshake.d.ts +32 -0
- package/dist/desktop/channel-handshake.js +111 -0
- package/dist/desktop/channel-proof.d.ts +43 -0
- package/dist/desktop/channel-proof.js +85 -0
- package/dist/desktop/client-assets.d.ts +7 -0
- package/dist/desktop/client-assets.js +59 -0
- package/dist/desktop/desktop-client.d.ts +23 -0
- package/dist/desktop/desktop-client.js +231 -0
- package/dist/desktop/desktop-gateway.d.ts +18 -0
- package/dist/desktop/desktop-gateway.js +146 -0
- package/dist/desktop/desktop-host.d.ts +48 -0
- package/dist/desktop/desktop-host.js +208 -0
- package/dist/desktop/desktop-stream.d.ts +36 -0
- package/dist/desktop/desktop-stream.js +231 -0
- package/dist/desktop/loopback-policy.d.ts +19 -0
- package/dist/desktop/loopback-policy.js +23 -0
- package/dist/desktop/open-url-channel.d.ts +15 -0
- package/dist/desktop/open-url-channel.js +55 -0
- package/dist/desktop/string-policy.d.ts +2 -0
- package/dist/desktop/string-policy.js +11 -0
- package/dist/framework-source.json +1 -0
- package/dist/installer/archive-entry.d.ts +6 -0
- package/dist/installer/archive-entry.js +14 -0
- package/dist/installer/command-artifact.d.ts +47 -0
- package/dist/installer/command-artifact.js +261 -0
- package/dist/installer/command-qualification-command.d.ts +12 -0
- package/dist/installer/command-qualification-command.js +71 -0
- package/dist/installer/hosted-runner.d.ts +4 -0
- package/dist/installer/hosted-runner.js +10 -0
- package/dist/installer/installed-payload.d.ts +29 -0
- package/dist/installer/installed-payload.js +312 -0
- package/dist/installer/installer-ci-bootstrap.d.ts +26 -0
- package/dist/installer/installer-ci-bootstrap.js +160 -0
- package/dist/installer/installer-ci-preflight.d.ts +1 -0
- package/dist/installer/installer-ci-preflight.js +49 -0
- package/dist/installer/installer-ci-worker.d.ts +1 -0
- package/dist/installer/installer-ci-worker.js +105 -0
- package/dist/installer/installer-extraction.d.ts +14 -0
- package/dist/installer/installer-extraction.js +289 -0
- package/dist/installer/installer-fetch-guard.d.ts +15 -0
- package/dist/installer/installer-fetch-guard.js +50 -0
- package/dist/installer/installer-identity.d.ts +23 -0
- package/dist/installer/installer-identity.js +286 -0
- package/dist/installer/installer-layout.d.ts +21 -0
- package/dist/installer/installer-layout.js +89 -0
- package/dist/installer/installer-observation.d.ts +64 -0
- package/dist/installer/installer-observation.js +473 -0
- package/dist/installer/installer-process-cleanup.d.ts +18 -0
- package/dist/installer/installer-process-cleanup.js +158 -0
- package/dist/installer/installer-qualification-command.d.ts +11 -0
- package/dist/installer/installer-qualification-command.js +71 -0
- package/dist/installer/installer-renderer-evidence.d.ts +43 -0
- package/dist/installer/installer-renderer-evidence.js +136 -0
- package/dist/installer/installer-upgrade-command.d.ts +37 -0
- package/dist/installer/installer-upgrade-command.js +172 -0
- package/dist/installer/installer-worker-guard.d.ts +21 -0
- package/dist/installer/installer-worker-guard.js +72 -0
- package/dist/installer/native-executable.d.ts +5 -0
- package/dist/installer/native-executable.js +21 -0
- package/dist/installer/upgrade-asset-download.d.ts +13 -0
- package/dist/installer/upgrade-asset-download.js +41 -0
- package/dist/installer/write-all.d.ts +6 -0
- package/dist/installer/write-all.js +11 -0
- package/dist/native/app-bundle.d.ts +28 -0
- package/dist/native/app-bundle.js +60 -0
- package/dist/native/cef-helper.d.ts +28 -0
- package/dist/native/cef-helper.js +402 -0
- package/dist/native/desktop-build-hook.d.ts +7 -0
- package/dist/native/desktop-build-hook.js +20 -0
- package/dist/native/electrobun-source-patch.d.ts +25 -0
- package/dist/native/electrobun-source-patch.js +93 -0
- package/dist/native/linux-cef-links.d.ts +2 -0
- package/dist/native/linux-cef-links.js +15 -0
- package/dist/native/native-secret-store.d.ts +27 -0
- package/dist/native/native-secret-store.js +70 -0
- package/dist/native/native-utilities.d.ts +24 -0
- package/dist/native/native-utilities.js +85 -0
- package/dist/native/partitioned-window.d.ts +34 -0
- package/dist/native/partitioned-window.js +44 -0
- package/dist/native/renderer-profile.d.ts +53 -0
- package/dist/native/renderer-profile.js +30 -0
- package/dist/native/secure-window.d.ts +38 -0
- package/dist/native/secure-window.js +77 -0
- package/dist/native/sidecar-services.d.ts +23 -0
- package/dist/native/sidecar-services.js +46 -0
- package/dist/native/sidecar.d.ts +81 -0
- package/dist/native/sidecar.js +250 -0
- package/dist/native/window-bootstrap.d.ts +24 -0
- package/dist/native/window-bootstrap.js +63 -0
- package/dist/native/window-options.d.ts +32 -0
- package/dist/native/window-options.js +23 -0
- package/dist/native/windows-build-hook-internal.d.ts +2 -0
- package/dist/native/windows-build-hook-internal.js +32 -0
- package/dist/native/windows-build-hook.d.ts +2 -0
- package/dist/native/windows-build-hook.js +12 -0
- package/dist/native/windows-runtime-patch-internal.d.ts +3 -0
- package/dist/native/windows-runtime-patch-internal.js +20 -0
- package/dist/native/windows-runtime-patch.d.ts +4 -0
- package/dist/native/windows-runtime-patch.js +12 -0
- package/dist/release/feed-release-cut.d.ts +23 -0
- package/dist/release/feed-release-cut.js +93 -0
- package/dist/release/feed-state.d.ts +25 -0
- package/dist/release/feed-state.js +81 -0
- package/dist/release/framework-package-publish.d.ts +21 -0
- package/dist/release/framework-package-publish.js +127 -0
- package/dist/release/framework-update.d.ts +30 -0
- package/dist/release/framework-update.js +139 -0
- package/dist/release/github-cli.d.ts +6 -0
- package/dist/release/github-cli.js +16 -0
- package/dist/release/github-release-assets.d.ts +3 -0
- package/dist/release/github-release-assets.js +24 -0
- package/dist/release/github-release-cut.d.ts +14 -0
- package/dist/release/github-release-cut.js +174 -0
- package/dist/release/github-release-lookup.d.ts +7 -0
- package/dist/release/github-release-lookup.js +29 -0
- package/dist/release/github-release-promotion.d.ts +10 -0
- package/dist/release/github-release-promotion.js +78 -0
- package/dist/release/github-release-provenance.d.ts +9 -0
- package/dist/release/github-release-provenance.js +27 -0
- package/dist/release/github-release-publisher.d.ts +9 -0
- package/dist/release/github-release-publisher.js +63 -0
- package/dist/release/github-release-qualification.d.ts +12 -0
- package/dist/release/github-release-qualification.js +80 -0
- package/dist/release/local-provenance.d.ts +24 -0
- package/dist/release/local-provenance.js +72 -0
- package/dist/release/local-release.d.ts +133 -0
- package/dist/release/local-release.js +387 -0
- package/dist/release/macos-dmg.d.ts +22 -0
- package/dist/release/macos-dmg.js +91 -0
- package/dist/release/release-artifact.d.ts +6 -0
- package/dist/release/release-artifact.js +38 -0
- package/dist/release/release-assets-plan.d.ts +14 -0
- package/dist/release/release-assets-plan.js +65 -0
- package/dist/release/release-config.d.ts +76 -0
- package/dist/release/release-config.js +180 -0
- package/dist/release/release-cut-command.d.ts +13 -0
- package/dist/release/release-cut-command.js +39 -0
- package/dist/release/release-cut.d.ts +54 -0
- package/dist/release/release-cut.js +76 -0
- package/dist/release/release-descriptor-staging.d.ts +34 -0
- package/dist/release/release-descriptor-staging.js +147 -0
- package/dist/release/release-finalize.d.ts +16 -0
- package/dist/release/release-finalize.js +104 -0
- package/dist/release/release-promotion-command.d.ts +12 -0
- package/dist/release/release-promotion-command.js +28 -0
- package/dist/release/release-promotion.d.ts +29 -0
- package/dist/release/release-promotion.js +57 -0
- package/dist/release/release-publication.d.ts +33 -0
- package/dist/release/release-publication.js +138 -0
- package/dist/release/release-publish-command.d.ts +7 -0
- package/dist/release/release-publish-command.js +91 -0
- package/dist/release/release-qualification.d.ts +106 -0
- package/dist/release/release-qualification.js +135 -0
- package/dist/release/release-staging.d.ts +9 -0
- package/dist/release/release-staging.js +61 -0
- package/dist/release/release-steps.d.ts +38 -0
- package/dist/release/release-steps.js +147 -0
- package/dist/release/retained-release-build.d.ts +19 -0
- package/dist/release/retained-release-build.js +122 -0
- package/dist/shell/app-shell.d.ts +61 -0
- package/dist/shell/app-shell.js +306 -0
- package/dist/shell/app-utilities.d.ts +122 -0
- package/dist/shell/app-utilities.js +334 -0
- package/dist/shell/application-menu-commands.d.ts +5 -0
- package/dist/shell/application-menu-commands.js +11 -0
- package/dist/shell/application-menu.d.ts +61 -0
- package/dist/shell/application-menu.js +208 -0
- package/dist/shell/help-shortcuts.d.ts +12 -0
- package/dist/shell/help-shortcuts.js +22 -0
- package/dist/shell/navigation-drawer.d.ts +12 -0
- package/dist/shell/navigation-drawer.js +42 -0
- package/dist/shell/settings-sections.d.ts +31 -0
- package/dist/shell/settings-sections.js +50 -0
- package/dist/shell/shortcut-platform.d.ts +17 -0
- package/dist/shell/shortcut-platform.js +25 -0
- package/dist/update-helper.bundle.js +260 -0
- package/dist/updates/channel-update-manager.d.ts +46 -0
- package/dist/updates/channel-update-manager.js +114 -0
- package/dist/updates/desktop-updater.d.ts +104 -0
- package/dist/updates/desktop-updater.js +345 -0
- package/dist/updates/release-feed.d.ts +61 -0
- package/dist/updates/release-feed.js +111 -0
- package/dist/updates/release-location.d.ts +9 -0
- package/dist/updates/release-location.js +80 -0
- package/dist/updates/release-version.d.ts +12 -0
- package/dist/updates/release-version.js +43 -0
- package/dist/updates/signed-release.d.ts +63 -0
- package/dist/updates/signed-release.js +162 -0
- package/dist/updates/stable-file.d.ts +10 -0
- package/dist/updates/stable-file.js +22 -0
- package/dist/updates/update-adapter.d.ts +57 -0
- package/dist/updates/update-adapter.js +72 -0
- package/dist/updates/update-arm.d.ts +24 -0
- package/dist/updates/update-arm.js +95 -0
- package/dist/updates/update-channel-store.d.ts +8 -0
- package/dist/updates/update-channel-store.js +72 -0
- package/dist/updates/update-discovery.d.ts +13 -0
- package/dist/updates/update-discovery.js +56 -0
- package/dist/updates/update-download.d.ts +14 -0
- package/dist/updates/update-download.js +67 -0
- package/dist/updates/update-exclusive-rename.d.ts +2 -0
- package/dist/updates/update-exclusive-rename.js +52 -0
- package/dist/updates/update-extraction.d.ts +18 -0
- package/dist/updates/update-extraction.js +183 -0
- package/dist/updates/update-handoff-plan.d.ts +22 -0
- package/dist/updates/update-handoff-plan.js +28 -0
- package/dist/updates/update-helper.d.ts +24 -0
- package/dist/updates/update-helper.js +56 -0
- package/dist/updates/update-launch.d.ts +5 -0
- package/dist/updates/update-launch.js +27 -0
- package/dist/updates/update-manager.d.ts +40 -0
- package/dist/updates/update-manager.js +83 -0
- package/dist/updates/update-process-fence.d.ts +10 -0
- package/dist/updates/update-process-fence.js +114 -0
- package/dist/updates/update-resume.d.ts +13 -0
- package/dist/updates/update-resume.js +34 -0
- package/dist/updates/update-shutdown.d.ts +15 -0
- package/dist/updates/update-shutdown.js +29 -0
- package/dist/updates/update-state.d.ts +30 -0
- package/dist/updates/update-state.js +18 -0
- package/dist/updates/update-swap-stage.d.ts +27 -0
- package/dist/updates/update-swap-stage.js +58 -0
- package/dist/updates/update-swap.d.ts +10 -0
- package/dist/updates/update-swap.js +30 -0
- package/dist/views/app-about.d.ts +22 -0
- package/dist/views/app-about.js +43 -0
- package/dist/views/app-appearance.d.ts +11 -0
- package/dist/views/app-appearance.js +42 -0
- package/dist/views/app-help.d.ts +17 -0
- package/dist/views/app-help.js +42 -0
- package/dist/views/app-settings.d.ts +26 -0
- package/dist/views/app-settings.js +97 -0
- package/dist/views/appearance-surface.d.ts +9 -0
- package/dist/views/appearance-surface.js +334 -0
- package/dist/views/update-surface.d.ts +34 -0
- package/dist/views/update-surface.js +64 -0
- package/package.json +374 -4
- package/src/app/application.tsx +138 -0
- package/src/appearance/appearance-host.tsx +205 -0
- package/src/appearance/appearance-preferences.ts +287 -0
- package/src/appearance/composition.ts +29 -0
- package/src/appearance/custom-theme-import.ts +424 -0
- package/src/appearance/custom-theme-library.ts +600 -0
- package/src/appearance/editor.ts +94 -0
- package/src/appearance/theme-file-importer.ts +103 -0
- package/src/appearance/theme-record-adapter.ts +106 -0
- package/src/appearance/theme-registry.ts +20 -0
- package/src/appearance/window-surface.ts +18 -0
- package/src/desktop/client-assets.ts +75 -0
- package/src/native/electrobun-cef-main-frame-bridges.patch +63 -0
- package/src/native/electrobun-windows-shortcut-com.patch +923 -0
- package/src/native/renderer-profile.ts +27 -0
- package/src/native/window-options.ts +24 -0
- package/src/shell/app-shell.tsx +294 -0
- package/src/shell/app-utilities.tsx +437 -0
- package/src/shell/help-shortcuts.ts +24 -0
- package/src/shell/navigation-drawer.tsx +36 -0
- package/src/shell/settings-sections.ts +74 -0
- package/src/shell/shortcut-platform.ts +35 -0
- package/src/updates/release-version.ts +57 -0
- package/src/updates/update-adapter.ts +129 -0
- package/src/updates/update-state.ts +34 -0
- package/src/views/app-about.tsx +44 -0
- package/src/views/app-appearance.tsx +32 -0
- package/src/views/app-help.tsx +43 -0
- package/src/views/app-settings.tsx +88 -0
- package/src/views/appearance-surface.tsx +427 -0
- package/src/views/update-surface.tsx +85 -0
- package/templates/application/.gitattributes +1 -0
- package/templates/application/.github/workflows/release-assets.yml +29 -0
- package/templates/application/.github/workflows/release-finalize.yml +29 -0
- package/templates/application/.github/workflows/release-please.yml +19 -0
- package/templates/application/.github/workflows/release.yml +29 -0
- package/templates/application/.github/workflows/validation.yml +28 -0
- package/templates/application/.oxfmtrc.json +6 -0
- package/templates/application/.oxlintrc.json +48 -0
- package/templates/application/.release-please-manifest.json +3 -0
- package/templates/application/README.md +47 -0
- package/templates/application/electrobun.config.ts +22 -0
- package/templates/application/release-please-config.json +12 -0
- package/templates/application/scripts/app-version.ts +9 -0
- package/templates/application/scripts/build-desktop.ts +34 -0
- package/templates/application/scripts/secure-build.ts +1 -0
- package/templates/application/scripts/stage-release.ts +21 -0
- package/templates/application/src/bun/index.ts +80 -0
- package/templates/application/src/electrobun.d.ts +27 -0
- package/templates/application/src/web/index.html +12 -0
- package/templates/application/src/web/main.tsx +97 -0
- package/templates/application/src/web/public/icon.svg +1 -0
- package/templates/application/src/web/style.css +3 -0
- package/templates/application/tests/app-config.test.ts +9 -0
- package/templates/application/tsconfig.json +15 -0
- package/templates/application/vite.config.ts +10 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { createLoopbackPolicy } from "./loopback-policy.js";
|
|
2
|
+
// src/desktop/desktop-client.ts
|
|
3
|
+
function bytesToBase64Url(bytes) {
|
|
4
|
+
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
5
|
+
}
|
|
6
|
+
/** Binds a request body to the channel credential: an HMAC over the purpose, channel,
|
|
7
|
+
* credential, a fresh nonce, the time and the body's SHA-256. */
|
|
8
|
+
async function signRequest(active, purpose, body, time) {
|
|
9
|
+
const nonce = bytesToBase64Url(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(24)));
|
|
10
|
+
const timestamp = String(time);
|
|
11
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(body)));
|
|
12
|
+
const bodyHash = Array.from(digest, (value) => value.toString(16).padStart(2, "0")).join("");
|
|
13
|
+
const message = [
|
|
14
|
+
purpose,
|
|
15
|
+
active.channelId,
|
|
16
|
+
active.credentialId,
|
|
17
|
+
nonce,
|
|
18
|
+
timestamp,
|
|
19
|
+
bodyHash
|
|
20
|
+
].join("");
|
|
21
|
+
return {
|
|
22
|
+
nonce,
|
|
23
|
+
timestamp,
|
|
24
|
+
proof: bytesToBase64Url(new Uint8Array(await crypto.subtle.sign("HMAC", active.key, new TextEncoder().encode(message))))
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function base64UrlToBytes(value) {
|
|
28
|
+
const text = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
29
|
+
return Uint8Array.from(atob(text + "=".repeat((4 - text.length % 4) % 4)), (c) => c.charCodeAt(0));
|
|
30
|
+
}
|
|
31
|
+
/** Browser-side client for the authenticated desktop gateway. Share one client per window: acquiring a new bootstrap revokes the window's previous channel. */
|
|
32
|
+
function createDesktopClient(options = {}) {
|
|
33
|
+
const origin = createLoopbackPolicy(options.origin ?? globalThis.location.origin).origin;
|
|
34
|
+
const request = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
35
|
+
const crypto = options.crypto ?? globalThis.crypto;
|
|
36
|
+
const now = options.now ?? Date.now;
|
|
37
|
+
const bootstrap = options.bootstrap ?? (() => {
|
|
38
|
+
const acquire = globalThis.__frameworkAcquireBootstrap;
|
|
39
|
+
if (!acquire) throw new Error("Desktop window bootstrap is unavailable");
|
|
40
|
+
return acquire();
|
|
41
|
+
});
|
|
42
|
+
let session;
|
|
43
|
+
let pending;
|
|
44
|
+
let generation = 0;
|
|
45
|
+
let disposed = false;
|
|
46
|
+
const sockets = /* @__PURE__ */ new Set();
|
|
47
|
+
function closeSockets() {
|
|
48
|
+
for (const socket of sockets) socket.close();
|
|
49
|
+
sockets.clear();
|
|
50
|
+
}
|
|
51
|
+
async function connect() {
|
|
52
|
+
if (disposed) throw new Error("Desktop client is closed");
|
|
53
|
+
if (session && session.expiresAt > now()) return session;
|
|
54
|
+
if (pending) return pending;
|
|
55
|
+
const current = generation;
|
|
56
|
+
const task = (async () => {
|
|
57
|
+
const launch = await bootstrap();
|
|
58
|
+
if (disposed || generation !== current) throw new Error("Desktop channel attempt was superseded");
|
|
59
|
+
const result = await request(origin + "/__framework/handshake", {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: { "content-type": "application/json" },
|
|
62
|
+
body: JSON.stringify({ bootstrap: launch.bootstrap })
|
|
63
|
+
});
|
|
64
|
+
if (!result.ok) throw new Error("Desktop channel handshake failed");
|
|
65
|
+
const reply = await result.json();
|
|
66
|
+
if (typeof reply !== "object" || reply === null) throw new Error("Invalid desktop channel reply");
|
|
67
|
+
const record = reply;
|
|
68
|
+
if (record.ok !== true || typeof record.channelId !== "string" || typeof record.credentialId !== "string" || typeof record.clientSecret !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(record.clientSecret) || typeof record.expiresAt !== "number" || !Number.isSafeInteger(record.expiresAt) || record.expiresAt <= now()) throw new Error("Invalid desktop channel reply");
|
|
69
|
+
const secret = base64UrlToBytes(record.clientSecret);
|
|
70
|
+
if (secret.length !== 32) throw new Error("Invalid desktop channel reply");
|
|
71
|
+
let key;
|
|
72
|
+
try {
|
|
73
|
+
key = await crypto.subtle.importKey("raw", secret, {
|
|
74
|
+
name: "HMAC",
|
|
75
|
+
hash: "SHA-256"
|
|
76
|
+
}, false, ["sign"]);
|
|
77
|
+
} finally {
|
|
78
|
+
secret.fill(0);
|
|
79
|
+
}
|
|
80
|
+
if (disposed || generation !== current) throw new Error("Desktop channel attempt was superseded");
|
|
81
|
+
return session = {
|
|
82
|
+
channelId: record.channelId,
|
|
83
|
+
credentialId: record.credentialId,
|
|
84
|
+
key,
|
|
85
|
+
expiresAt: record.expiresAt
|
|
86
|
+
};
|
|
87
|
+
})();
|
|
88
|
+
pending = task;
|
|
89
|
+
try {
|
|
90
|
+
return await task;
|
|
91
|
+
} finally {
|
|
92
|
+
if (pending === task) pending = void 0;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
async openStream(name, input, onMessage, onClose) {
|
|
97
|
+
if (!/^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/.test(name) || name.length > 128 || typeof onMessage !== "function") throw new Error("Invalid stream request");
|
|
98
|
+
const body = JSON.stringify({
|
|
99
|
+
name,
|
|
100
|
+
input
|
|
101
|
+
});
|
|
102
|
+
if (new TextEncoder().encode(body).byteLength > 4096) throw new Error("Stream request too large");
|
|
103
|
+
const active = await connect();
|
|
104
|
+
const { nonce, timestamp, proof } = await signRequest(active, "framework-stream-attach-v1", body, now());
|
|
105
|
+
if (disposed || session !== active) throw new Error("Desktop channel is no longer active");
|
|
106
|
+
const socket = (options.createWebSocket ?? ((url) => new WebSocket(url)))(origin.replace(/^http:/, "ws:") + "/__framework/stream");
|
|
107
|
+
socket.binaryType = "arraybuffer";
|
|
108
|
+
sockets.add(socket);
|
|
109
|
+
return await new Promise((resolve, reject) => {
|
|
110
|
+
let ready = false, settled = false;
|
|
111
|
+
const timer = setTimeout(() => {
|
|
112
|
+
socket.close();
|
|
113
|
+
fail();
|
|
114
|
+
}, 5e3);
|
|
115
|
+
const fail = () => {
|
|
116
|
+
if (!settled) {
|
|
117
|
+
settled = true;
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
reject(/* @__PURE__ */ new Error("Desktop stream unavailable"));
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
socket.addEventListener("open", () => {
|
|
123
|
+
if (disposed || session !== active) {
|
|
124
|
+
socket.close();
|
|
125
|
+
fail();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
socket.send(JSON.stringify({
|
|
129
|
+
channelId: active.channelId,
|
|
130
|
+
credentialId: active.credentialId,
|
|
131
|
+
nonce,
|
|
132
|
+
timestamp,
|
|
133
|
+
body,
|
|
134
|
+
proof
|
|
135
|
+
}));
|
|
136
|
+
});
|
|
137
|
+
socket.addEventListener("message", (event) => {
|
|
138
|
+
if (!ready) {
|
|
139
|
+
if (event.data !== "{\"ok\":true}" || disposed || session !== active) {
|
|
140
|
+
socket.close();
|
|
141
|
+
fail();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
ready = true;
|
|
145
|
+
settled = true;
|
|
146
|
+
clearTimeout(timer);
|
|
147
|
+
resolve({
|
|
148
|
+
send(payload) {
|
|
149
|
+
const length = typeof payload === "string" ? new TextEncoder().encode(payload).byteLength : payload instanceof ArrayBuffer ? payload.byteLength : Infinity;
|
|
150
|
+
if (disposed || session !== active || socket.readyState !== WebSocket.OPEN || length > 262144 || socket.bufferedAmount > 1048576) {
|
|
151
|
+
socket.close();
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
socket.send(payload);
|
|
156
|
+
return true;
|
|
157
|
+
} catch {
|
|
158
|
+
socket.close();
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
close: () => socket.close()
|
|
163
|
+
});
|
|
164
|
+
} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) try {
|
|
165
|
+
onMessage(event.data);
|
|
166
|
+
} catch {
|
|
167
|
+
socket.close();
|
|
168
|
+
}
|
|
169
|
+
else socket.close();
|
|
170
|
+
});
|
|
171
|
+
socket.addEventListener("error", () => {
|
|
172
|
+
socket.close();
|
|
173
|
+
fail();
|
|
174
|
+
});
|
|
175
|
+
socket.addEventListener("close", () => {
|
|
176
|
+
sockets.delete(socket);
|
|
177
|
+
if (ready) try {
|
|
178
|
+
onClose?.();
|
|
179
|
+
} catch {}
|
|
180
|
+
fail();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
async invoke(name, input) {
|
|
185
|
+
if (!/^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/.test(name) || name.length > 128) throw new Error("Invalid service name");
|
|
186
|
+
const body = JSON.stringify({
|
|
187
|
+
name,
|
|
188
|
+
input
|
|
189
|
+
});
|
|
190
|
+
const active = await connect();
|
|
191
|
+
const { nonce, timestamp, proof } = await signRequest(active, "adea-invoke-v1", body, now());
|
|
192
|
+
if (disposed || session !== active) throw new Error("Desktop channel is no longer active");
|
|
193
|
+
const result = await request(origin + "/__framework/invoke", {
|
|
194
|
+
method: "POST",
|
|
195
|
+
headers: {
|
|
196
|
+
"content-type": "application/json",
|
|
197
|
+
"x-adea-channel": active.channelId,
|
|
198
|
+
"x-adea-credential": active.credentialId,
|
|
199
|
+
"x-adea-nonce": nonce,
|
|
200
|
+
"x-adea-timestamp": timestamp,
|
|
201
|
+
"x-adea-proof": proof
|
|
202
|
+
},
|
|
203
|
+
body
|
|
204
|
+
});
|
|
205
|
+
if (result.status === 401) {
|
|
206
|
+
if (session === active) session = void 0;
|
|
207
|
+
throw new Error("Desktop channel is no longer active");
|
|
208
|
+
}
|
|
209
|
+
if (!result.ok) throw new Error("Desktop service request failed");
|
|
210
|
+
const value = await result.json();
|
|
211
|
+
if (typeof value !== "object" || value === null || typeof value.ok !== "boolean") throw new Error("Invalid desktop service reply");
|
|
212
|
+
return value;
|
|
213
|
+
},
|
|
214
|
+
async reconnect() {
|
|
215
|
+
if (disposed) throw new Error("Desktop client is closed");
|
|
216
|
+
generation++;
|
|
217
|
+
session = void 0;
|
|
218
|
+
pending = void 0;
|
|
219
|
+
closeSockets();
|
|
220
|
+
await connect();
|
|
221
|
+
},
|
|
222
|
+
dispose() {
|
|
223
|
+
disposed = true;
|
|
224
|
+
generation++;
|
|
225
|
+
session = void 0;
|
|
226
|
+
pending = void 0;
|
|
227
|
+
closeSockets();
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
export { createDesktopClient };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { createChannelAuthority } from './channel-handshake';
|
|
2
|
+
import { type createServiceRegistry } from '../app/service-registry';
|
|
3
|
+
import type { createNativeWindowGrant } from '../native/window-bootstrap';
|
|
4
|
+
/**
|
|
5
|
+
* Authenticated invoke gateway. Bootstrap must be delivered separately by a
|
|
6
|
+
* trusted native window; this gateway never exposes it in a document or route.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createDesktopGateway<Principal>(options: {
|
|
9
|
+
origin: string;
|
|
10
|
+
authority: ReturnType<typeof createChannelAuthority<Principal>>;
|
|
11
|
+
services: ReturnType<typeof createServiceRegistry<Principal>>;
|
|
12
|
+
now?: () => number;
|
|
13
|
+
maxConcurrent?: number;
|
|
14
|
+
readTimeoutMs?: number;
|
|
15
|
+
windowGrant?: ReturnType<typeof createNativeWindowGrant<Principal>>;
|
|
16
|
+
}): {
|
|
17
|
+
handle(request: Request): Promise<Response>;
|
|
18
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { MAX_SERVICE_BODY_BYTES } from "../app/service-registry.js";
|
|
2
|
+
import { createChannelProofVerifier } from "./channel-proof.js";
|
|
3
|
+
import { createLoopbackPolicy, matchesLoopbackOrigin } from "./loopback-policy.js";
|
|
4
|
+
// src/desktop/desktop-gateway.ts
|
|
5
|
+
var response = (status) => new Response(null, {
|
|
6
|
+
status,
|
|
7
|
+
headers: {
|
|
8
|
+
"cache-control": "no-store",
|
|
9
|
+
"x-content-type-options": "nosniff"
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
var json = (value) => Response.json(value, { headers: {
|
|
13
|
+
"cache-control": "no-store",
|
|
14
|
+
"x-content-type-options": "nosniff"
|
|
15
|
+
} });
|
|
16
|
+
/** Read incrementally so the limit applies before the entire body is buffered. */
|
|
17
|
+
async function readBody(request, limit, timeoutMs) {
|
|
18
|
+
const declared = request.headers.get("content-length");
|
|
19
|
+
if (declared !== null && (!/^\d+$/.test(declared) || Number(declared) > limit)) return { status: 413 };
|
|
20
|
+
const reader = request.body?.getReader();
|
|
21
|
+
if (!reader) return {
|
|
22
|
+
body: "",
|
|
23
|
+
length: 0
|
|
24
|
+
};
|
|
25
|
+
let timedOut = false;
|
|
26
|
+
const timer = setTimeout(() => {
|
|
27
|
+
timedOut = true;
|
|
28
|
+
reader.cancel().catch(() => {});
|
|
29
|
+
}, timeoutMs);
|
|
30
|
+
const chunks = [];
|
|
31
|
+
let length = 0;
|
|
32
|
+
try {
|
|
33
|
+
while (true) {
|
|
34
|
+
const next = await reader.read();
|
|
35
|
+
if (timedOut) return { status: 408 };
|
|
36
|
+
if (next.done) break;
|
|
37
|
+
length += next.value.byteLength;
|
|
38
|
+
if (length > limit) {
|
|
39
|
+
reader.cancel().catch(() => {});
|
|
40
|
+
return { status: 413 };
|
|
41
|
+
}
|
|
42
|
+
chunks.push(next.value);
|
|
43
|
+
}
|
|
44
|
+
const bytes = new Uint8Array(length);
|
|
45
|
+
let offset = 0;
|
|
46
|
+
for (const chunk of chunks) {
|
|
47
|
+
bytes.set(chunk, offset);
|
|
48
|
+
offset += chunk.byteLength;
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
body: new TextDecoder("utf-8", { fatal: true }).decode(bytes),
|
|
52
|
+
length
|
|
53
|
+
};
|
|
54
|
+
} catch {
|
|
55
|
+
return { status: timedOut ? 408 : 413 };
|
|
56
|
+
} finally {
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
reader.releaseLock();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function parse(body) {
|
|
62
|
+
try {
|
|
63
|
+
const value = JSON.parse(body);
|
|
64
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
65
|
+
} catch {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Authenticated invoke gateway. Bootstrap must be delivered separately by a
|
|
71
|
+
* trusted native window; this gateway never exposes it in a document or route.
|
|
72
|
+
*/
|
|
73
|
+
function createDesktopGateway(options) {
|
|
74
|
+
const maxConcurrent = options.maxConcurrent ?? 64;
|
|
75
|
+
const readTimeoutMs = options.readTimeoutMs ?? 15e3;
|
|
76
|
+
if (!Number.isSafeInteger(maxConcurrent) || maxConcurrent < 1 || !Number.isSafeInteger(readTimeoutMs) || readTimeoutMs < 1) throw new Error("Invalid gateway limits");
|
|
77
|
+
let inFlight = 0;
|
|
78
|
+
const policy = createLoopbackPolicy(options.origin);
|
|
79
|
+
if (options.windowGrant && options.windowGrant.origin !== policy.origin) throw new Error("Window grant origin mismatch");
|
|
80
|
+
const verifier = createChannelProofVerifier({
|
|
81
|
+
resolve: options.authority.resolve,
|
|
82
|
+
now: options.now,
|
|
83
|
+
maxBodyBytes: MAX_SERVICE_BODY_BYTES
|
|
84
|
+
});
|
|
85
|
+
return { async handle(request) {
|
|
86
|
+
try {
|
|
87
|
+
const url = new URL(request.url);
|
|
88
|
+
if (url.pathname !== "/__framework/handshake" && url.pathname !== "/__framework/invoke" && url.pathname !== "/__framework/bootstrap") return response(404);
|
|
89
|
+
if (url.origin !== policy.origin || !matchesLoopbackOrigin({
|
|
90
|
+
host: request.headers.get("host"),
|
|
91
|
+
origin: request.headers.get("origin"),
|
|
92
|
+
secFetchSite: request.headers.get("sec-fetch-site")
|
|
93
|
+
}, policy)) return response(403);
|
|
94
|
+
if (request.method !== "POST") return response(405);
|
|
95
|
+
if (!/^application\/json(?:\s*;|\s*$)/i.test(request.headers.get("content-type") ?? "")) return response(415);
|
|
96
|
+
if (inFlight >= maxConcurrent) return response(429);
|
|
97
|
+
inFlight++;
|
|
98
|
+
try {
|
|
99
|
+
const received = await readBody(request, url.pathname.endsWith("/invoke") ? options.services.maxBodyBytes() : 4096, readTimeoutMs);
|
|
100
|
+
if ("status" in received) return response(received.status);
|
|
101
|
+
const body = received.body;
|
|
102
|
+
const input = parse(body);
|
|
103
|
+
if (!input) return response(400);
|
|
104
|
+
if (url.pathname.endsWith("/bootstrap")) {
|
|
105
|
+
const result = options.windowGrant?.acquire({
|
|
106
|
+
channelId: request.headers.get("x-adea-channel") ?? "",
|
|
107
|
+
credentialId: request.headers.get("x-adea-credential") ?? "",
|
|
108
|
+
nonce: request.headers.get("x-adea-nonce") ?? "",
|
|
109
|
+
timestamp: request.headers.get("x-adea-timestamp") ?? "",
|
|
110
|
+
proof: request.headers.get("x-adea-proof") ?? "",
|
|
111
|
+
body
|
|
112
|
+
});
|
|
113
|
+
return result ? json(result) : response(401);
|
|
114
|
+
}
|
|
115
|
+
if (url.pathname.endsWith("/handshake")) {
|
|
116
|
+
if (typeof input.bootstrap !== "string") return response(401);
|
|
117
|
+
const result = options.authority.handshake(input.bootstrap);
|
|
118
|
+
return result.ok ? json(result) : response(401);
|
|
119
|
+
}
|
|
120
|
+
const identity = {
|
|
121
|
+
channelId: request.headers.get("x-adea-channel") ?? "",
|
|
122
|
+
credentialId: request.headers.get("x-adea-credential") ?? ""
|
|
123
|
+
};
|
|
124
|
+
const result = verifier.verify({
|
|
125
|
+
...identity,
|
|
126
|
+
nonce: request.headers.get("x-adea-nonce") ?? "",
|
|
127
|
+
timestamp: request.headers.get("x-adea-timestamp") ?? "",
|
|
128
|
+
proof: request.headers.get("x-adea-proof") ?? "",
|
|
129
|
+
body
|
|
130
|
+
});
|
|
131
|
+
if (!result.ok) return response(401);
|
|
132
|
+
if (typeof input.name !== "string") return response(400);
|
|
133
|
+
if (received.length > options.services.maxBodyBytes(input.name)) return response(413);
|
|
134
|
+
return json(await options.services.dispatch(input.name, input.input, result.principal, () => {
|
|
135
|
+
const channel = options.authority.resolve(identity);
|
|
136
|
+
return channel !== void 0 && Object.is(channel.principal, result.principal);
|
|
137
|
+
}));
|
|
138
|
+
} finally {
|
|
139
|
+
inFlight--;
|
|
140
|
+
}
|
|
141
|
+
} catch {
|
|
142
|
+
return response(500);
|
|
143
|
+
}
|
|
144
|
+
} };
|
|
145
|
+
}
|
|
146
|
+
export { createDesktopGateway };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type NativeWindow, type SecureWindowOptions } from '../native/secure-window';
|
|
2
|
+
import type { DesktopRenderer } from '../native/renderer-profile';
|
|
3
|
+
import type { WindowFrame } from '../native/window-options';
|
|
4
|
+
import type { createServiceRegistry } from '../app/service-registry';
|
|
5
|
+
import { createDesktopStreams } from './desktop-stream';
|
|
6
|
+
/** Bind an isolated client server and open a window with a per-window channel. */
|
|
7
|
+
export declare function startDesktopHost<Principal, Window extends NativeWindow>(options: {
|
|
8
|
+
clientRoot: string;
|
|
9
|
+
/** Existing app-owned file or new path for a stable loopback origin. */
|
|
10
|
+
portFile?: string;
|
|
11
|
+
/** Preserve an existing loopback origin when creating the port file. */
|
|
12
|
+
initialPort?: number;
|
|
13
|
+
title: string;
|
|
14
|
+
renderer: DesktopRenderer;
|
|
15
|
+
platform: string;
|
|
16
|
+
principal: Principal;
|
|
17
|
+
services: ReturnType<typeof createServiceRegistry<Principal>>;
|
|
18
|
+
configureStreams?: (streams: ReturnType<typeof createDesktopStreams<Principal>>) => void;
|
|
19
|
+
/** App-owned same-origin HTTP routes. Handlers must apply their own authorization. */
|
|
20
|
+
routes?: readonly {
|
|
21
|
+
prefix: string;
|
|
22
|
+
handle(request: Request): Response | Promise<Response>;
|
|
23
|
+
}[];
|
|
24
|
+
construct(options: SecureWindowOptions): Window;
|
|
25
|
+
frame?: WindowFrame;
|
|
26
|
+
transparent?: boolean;
|
|
27
|
+
hidden?: boolean;
|
|
28
|
+
onRequest?: (kind: 'client' | 'bootstrap' | 'handshake' | 'invoke' | 'route' | 'rejected', result?: {
|
|
29
|
+
status: number;
|
|
30
|
+
origin: 'matching' | 'missing' | 'opaque' | 'same-host' | 'other';
|
|
31
|
+
fetchSite: string | null;
|
|
32
|
+
}) => void;
|
|
33
|
+
}): {
|
|
34
|
+
origin: string;
|
|
35
|
+
window: Window;
|
|
36
|
+
stop: () => void;
|
|
37
|
+
revokeChannels: () => void;
|
|
38
|
+
};
|
|
39
|
+
/** Retry only a failed loopback bind while the previous process exits. */
|
|
40
|
+
export declare function startDesktopHostWithRetry<Principal, Window extends NativeWindow>(options: Parameters<typeof startDesktopHost<Principal, Window>>[0], retry?: {
|
|
41
|
+
timeoutMs?: number;
|
|
42
|
+
intervalMs?: number;
|
|
43
|
+
}): Promise<{
|
|
44
|
+
origin: string;
|
|
45
|
+
window: Window;
|
|
46
|
+
stop: () => void;
|
|
47
|
+
revokeChannels: () => void;
|
|
48
|
+
}>;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { createChannelAuthority } from "./channel-handshake.js";
|
|
2
|
+
import { serveClient } from "./client-assets.js";
|
|
3
|
+
import { createLoopbackPolicy, matchesLoopbackOrigin } from "./loopback-policy.js";
|
|
4
|
+
import { createDesktopGateway } from "./desktop-gateway.js";
|
|
5
|
+
import { createNativeWindowGrant } from "../native/window-bootstrap.js";
|
|
6
|
+
import { createSecureWindow } from "../native/secure-window.js";
|
|
7
|
+
import { createDesktopStreams } from "./desktop-stream.js";
|
|
8
|
+
import { lstatSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
// src/desktop/desktop-host.ts
|
|
10
|
+
var DesktopPortInUseError = class extends Error {
|
|
11
|
+
code = "EADDRINUSE";
|
|
12
|
+
constructor(cause) {
|
|
13
|
+
super("Desktop origin port is in use", { cause });
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/** Bind an isolated client server and open a window with a per-window channel. */
|
|
17
|
+
function startDesktopHost(options) {
|
|
18
|
+
const configuredRoutes = options.routes ?? [];
|
|
19
|
+
if (!Array.isArray(configuredRoutes) || configuredRoutes.length > 32 || configuredRoutes.some((route) => !route || typeof route.prefix !== "string" || !/^\/[a-zA-Z0-9][a-zA-Z0-9._-]*(?:\/[a-zA-Z0-9][a-zA-Z0-9._-]*)*$/.test(route.prefix) || route.prefix.length > 200 || route.prefix === "/__framework" || route.prefix.startsWith("/__framework/") || typeof route.handle !== "function") || configuredRoutes.some((route, index) => configuredRoutes.slice(index + 1).some((other) => route.prefix === other.prefix || route.prefix.startsWith(other.prefix + "/") || other.prefix.startsWith(route.prefix + "/")))) throw new Error("Invalid desktop route policy");
|
|
20
|
+
const routes = configuredRoutes.map(({ prefix, handle }) => ({
|
|
21
|
+
prefix,
|
|
22
|
+
handle
|
|
23
|
+
}));
|
|
24
|
+
if (options.portFile !== void 0 && (typeof options.portFile !== "string" || !options.portFile)) throw new Error("Invalid desktop port file");
|
|
25
|
+
if (options.initialPort !== void 0 && (!Number.isInteger(options.initialPort) || options.initialPort < 1024 || options.initialPort > 65535)) throw new Error("Invalid initial desktop port");
|
|
26
|
+
let port = options.initialPort ?? 0;
|
|
27
|
+
let savePort = false;
|
|
28
|
+
if (options.portFile !== void 0) try {
|
|
29
|
+
const info = lstatSync(options.portFile);
|
|
30
|
+
if (!info.isFile() || info.isSymbolicLink() || info.size > 6) throw new Error("Invalid desktop port file");
|
|
31
|
+
const value = readFileSync(options.portFile, "utf8");
|
|
32
|
+
if (!/^[1-9]\d{3,4}\n$/.test(value) || Number(value) < 1024 || Number(value) > 65535) throw new Error("Invalid desktop port file");
|
|
33
|
+
const savedPort = Number(value);
|
|
34
|
+
if (options.initialPort !== void 0 && options.initialPort !== savedPort) throw new Error("Saved desktop port does not match initial port");
|
|
35
|
+
port = savedPort;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error.code !== "ENOENT") throw error;
|
|
38
|
+
savePort = true;
|
|
39
|
+
}
|
|
40
|
+
let origin = "";
|
|
41
|
+
let gateway;
|
|
42
|
+
let streams;
|
|
43
|
+
let server;
|
|
44
|
+
try {
|
|
45
|
+
server = Bun.serve({
|
|
46
|
+
hostname: "127.0.0.1",
|
|
47
|
+
port,
|
|
48
|
+
async fetch(request, connection) {
|
|
49
|
+
if (!origin || !gateway) return new Response(null, { status: 503 });
|
|
50
|
+
const url = new URL(request.url);
|
|
51
|
+
if (url.origin !== origin || request.headers.get("host") !== new URL(origin).host) {
|
|
52
|
+
try {
|
|
53
|
+
options.onRequest?.("rejected");
|
|
54
|
+
} catch {}
|
|
55
|
+
return new Response(null, { status: 403 });
|
|
56
|
+
}
|
|
57
|
+
if (url.pathname === "/__framework/stream") {
|
|
58
|
+
if (!streams || request.method !== "GET" || url.search || !matchesLoopbackOrigin({
|
|
59
|
+
host: request.headers.get("host"),
|
|
60
|
+
origin: request.headers.get("origin"),
|
|
61
|
+
secFetchSite: request.headers.get("sec-fetch-site")
|
|
62
|
+
}, createLoopbackPolicy(origin))) return new Response(null, { status: 403 });
|
|
63
|
+
if (connection.upgrade(request, { data: {} })) return void 0;
|
|
64
|
+
return new Response(null, { status: 400 });
|
|
65
|
+
}
|
|
66
|
+
const kind = url.pathname === "/__framework/bootstrap" ? "bootstrap" : url.pathname === "/__framework/handshake" ? "handshake" : url.pathname === "/__framework/invoke" ? "invoke" : "client";
|
|
67
|
+
if (url.pathname.startsWith("/__framework/")) {
|
|
68
|
+
if (kind === "invoke") connection.timeout(request, 0);
|
|
69
|
+
const result = await gateway.handle(request);
|
|
70
|
+
const header = request.headers.get("origin");
|
|
71
|
+
try {
|
|
72
|
+
options.onRequest?.(kind, {
|
|
73
|
+
status: result.status,
|
|
74
|
+
origin: header === origin ? "matching" : header === null ? "missing" : header === "null" ? "opaque" : /^http:\/\/127\.0\.0\.1(?::[0-9]{1,5})?$/.test(header) ? "same-host" : "other",
|
|
75
|
+
fetchSite: request.headers.get("sec-fetch-site")
|
|
76
|
+
});
|
|
77
|
+
} catch {}
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
const route = routes.find((value) => url.pathname === value.prefix || url.pathname.startsWith(value.prefix + "/"));
|
|
81
|
+
if (route) {
|
|
82
|
+
if (!matchesLoopbackOrigin({
|
|
83
|
+
host: request.headers.get("host"),
|
|
84
|
+
origin: request.headers.get("origin"),
|
|
85
|
+
secFetchSite: request.headers.get("sec-fetch-site")
|
|
86
|
+
}, createLoopbackPolicy(origin))) return new Response(null, { status: 403 });
|
|
87
|
+
try {
|
|
88
|
+
options.onRequest?.("route");
|
|
89
|
+
} catch {}
|
|
90
|
+
try {
|
|
91
|
+
const result = await route.handle(request);
|
|
92
|
+
return result instanceof Response ? result : new Response(null, { status: 500 });
|
|
93
|
+
} catch {
|
|
94
|
+
return new Response(null, { status: 500 });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
options.onRequest?.(kind);
|
|
99
|
+
} catch {}
|
|
100
|
+
if (request.method !== "GET" && request.method !== "HEAD") return new Response(null, { status: 405 });
|
|
101
|
+
const result = await serveClient(request, { root: options.clientRoot });
|
|
102
|
+
result.headers.set("content-security-policy", "frame-src 'none'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'");
|
|
103
|
+
result.headers.set("x-content-type-options", "nosniff");
|
|
104
|
+
result.headers.set("referrer-policy", "no-referrer");
|
|
105
|
+
return result;
|
|
106
|
+
},
|
|
107
|
+
websocket: {
|
|
108
|
+
maxPayloadLength: 262144,
|
|
109
|
+
backpressureLimit: 1048576,
|
|
110
|
+
closeOnBackpressureLimit: true,
|
|
111
|
+
open(socket) {
|
|
112
|
+
socket.data.connection = streams?.accept({
|
|
113
|
+
send: (payload) => socket.send(payload) > 0,
|
|
114
|
+
close: () => socket.close()
|
|
115
|
+
});
|
|
116
|
+
if (!socket.data.connection) socket.close();
|
|
117
|
+
},
|
|
118
|
+
message(socket, message) {
|
|
119
|
+
socket.data.connection?.receive(message);
|
|
120
|
+
},
|
|
121
|
+
close(socket) {
|
|
122
|
+
socket.data.connection?.close();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error?.code === "EADDRINUSE") throw new DesktopPortInUseError(error);
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
if (options.portFile !== void 0 && savePort) try {
|
|
131
|
+
writeFileSync(options.portFile, `${server.port}\n`, {
|
|
132
|
+
flag: "wx",
|
|
133
|
+
mode: 384
|
|
134
|
+
});
|
|
135
|
+
} catch (error) {
|
|
136
|
+
server.stop(true);
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
origin = `http://127.0.0.1:${server.port}`;
|
|
140
|
+
const authority = createChannelAuthority();
|
|
141
|
+
if (options.configureStreams) {
|
|
142
|
+
streams = createDesktopStreams({ authority });
|
|
143
|
+
try {
|
|
144
|
+
options.configureStreams(streams);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
streams.stop();
|
|
147
|
+
server.stop(true);
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const grant = createNativeWindowGrant({
|
|
152
|
+
origin,
|
|
153
|
+
authority,
|
|
154
|
+
principal: options.principal
|
|
155
|
+
});
|
|
156
|
+
gateway = createDesktopGateway({
|
|
157
|
+
origin,
|
|
158
|
+
authority,
|
|
159
|
+
services: options.services,
|
|
160
|
+
windowGrant: grant
|
|
161
|
+
});
|
|
162
|
+
let window;
|
|
163
|
+
let stopped = false;
|
|
164
|
+
const stop = () => {
|
|
165
|
+
if (stopped) return;
|
|
166
|
+
stopped = true;
|
|
167
|
+
streams?.stop();
|
|
168
|
+
grant.revoke();
|
|
169
|
+
server.stop(true);
|
|
170
|
+
window?.close();
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
window = createSecureWindow({
|
|
174
|
+
origin,
|
|
175
|
+
grant,
|
|
176
|
+
renderer: options.renderer,
|
|
177
|
+
platform: options.platform,
|
|
178
|
+
title: options.title,
|
|
179
|
+
frame: options.frame,
|
|
180
|
+
transparent: options.transparent,
|
|
181
|
+
hidden: options.hidden,
|
|
182
|
+
construct: options.construct
|
|
183
|
+
});
|
|
184
|
+
window.on("close", stop);
|
|
185
|
+
return {
|
|
186
|
+
origin,
|
|
187
|
+
window,
|
|
188
|
+
stop,
|
|
189
|
+
revokeChannels: () => authority.revokeAll()
|
|
190
|
+
};
|
|
191
|
+
} catch (error) {
|
|
192
|
+
stop();
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/** Retry only a failed loopback bind while the previous process exits. */
|
|
197
|
+
async function startDesktopHostWithRetry(options, retry = {}) {
|
|
198
|
+
const timeoutMs = retry.timeoutMs ?? 3e4, intervalMs = retry.intervalMs ?? 250;
|
|
199
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 12e4 || !Number.isSafeInteger(intervalMs) || intervalMs < 1 || intervalMs > 5e3) throw new Error("Invalid desktop bind retry policy");
|
|
200
|
+
const deadline = performance.now() + timeoutMs;
|
|
201
|
+
for (;;) try {
|
|
202
|
+
return startDesktopHost(options);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
if (!(error instanceof DesktopPortInUseError) || performance.now() >= deadline) throw error;
|
|
205
|
+
await Bun.sleep(Math.min(intervalMs, Math.max(1, deadline - performance.now())));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
export { startDesktopHost, startDesktopHostWithRetry };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { createChannelAuthority } from './channel-handshake';
|
|
2
|
+
type Payload = string | Uint8Array;
|
|
3
|
+
type StreamHandler = {
|
|
4
|
+
receive(payload: Payload): void | Promise<void>;
|
|
5
|
+
close(): void;
|
|
6
|
+
};
|
|
7
|
+
type StreamProvider<Principal> = {
|
|
8
|
+
parse(value: unknown): unknown;
|
|
9
|
+
authorize(principal: Principal, input: unknown): boolean | Promise<boolean>;
|
|
10
|
+
open(principal: Principal, input: unknown, context: {
|
|
11
|
+
signal: AbortSignal;
|
|
12
|
+
send(payload: Payload): boolean;
|
|
13
|
+
current(): boolean;
|
|
14
|
+
close(): void;
|
|
15
|
+
}): StreamHandler | Promise<StreamHandler>;
|
|
16
|
+
};
|
|
17
|
+
type Socket = {
|
|
18
|
+
send(payload: Payload): boolean;
|
|
19
|
+
close(): void;
|
|
20
|
+
};
|
|
21
|
+
/** Application-owned stream authorization and lifecycle, independent of the socket implementation. */
|
|
22
|
+
export declare function createDesktopStreams<Principal>(options: {
|
|
23
|
+
authority: ReturnType<typeof createChannelAuthority<Principal>>;
|
|
24
|
+
now?: () => number;
|
|
25
|
+
handshakeTimeoutMs?: number;
|
|
26
|
+
maxConnections?: number;
|
|
27
|
+
maxFrameBytes?: number;
|
|
28
|
+
}): {
|
|
29
|
+
register(name: string, provider: StreamProvider<Principal>): () => void;
|
|
30
|
+
accept(socket: Socket): {
|
|
31
|
+
close: () => void;
|
|
32
|
+
receive(payload: Payload): Promise<void>;
|
|
33
|
+
} | undefined;
|
|
34
|
+
stop(): void;
|
|
35
|
+
};
|
|
36
|
+
export {};
|